Rediscovering the Joy of Hardware Hacking with Raspberry Pi and Lisp
HardwareHacking
I first learned about microcomputers around 1980. Back then, they were sold as training kits with exposed circuit boards, and programming involved directly entering machine code. Subsequently, personal computers experienced rapid development and widespread adoption. However, the ability to tinker with hardware directly became less accessible, especially with Windows OS, where users couldn’t manipulate hardware directly. I drifted away from hardware tinkering altogether.
Rise of RaspberryPi
Although I had drifted away from hardware tinkering, in recent years, a single-board computer called Raspberry Pi emerged. It harkened back to the days of microcomputers with exposed hardware, and since it runs on a Linux-based OS, it allows access to hardware. I felt a surge of excitement and stocked up on soldering irons and testers. It reminded me of the joy I had in my youth, building electrical circuits for fun.
GPIO with Lisp
Raspberry Pi seems to favor Python as its standard programming language. Python is well-crafted and straightforward, even for middle school students to grasp. However, as a Lisp enthusiast, I wanted to try it in Lisp. My creation, Easy-ISLisp, is set up to automatically control GPIO when compiled on Raspberry Pi.
Here’s a sample function:
EISL <==================================> C
(wiringpi-spi-setup ch speed) <===> wiringPiSPISetup (SPI_CH, SPI_SPEED)
(wiringpi-setup-gpio ) <===> wiringPiSetupGpio()
(pin-mode n 'output) <====> pinMode(n, OUTPUT) or 'input -> INPUT 'pwm-output -> PWM-OUTPUT
(digital-write n v) <===> digitalWrite(n, v)
(digital-write-byte v) <===> digitalWriteByte(value)
(digital-read pin) <===> digitalRead(pin)
(delay-seconds howlong) <===> void delay(unsigned int howLong)
(delay-microseconds howlong) <===> void delayMicroseconds(unsigned int howLong)
(pull-up-dn-control pin pud) <===> pullUpDnControl(pin,pud)
(pwm-set-mode 'pwm-mode-ms) <===> pwmSetMode(PWM_MODE_MS); or 'pwm-mode-bal -> PWM_MODE_BAL
(pwm-set-clock n) <===> pwmSetClock(n)
(pwm-set-range n) <===> pwmSetRange(n)
(pwm-write pin value) <===> pwmWrite(pin , value)
Blinking LEDs
Creating hardware to blink LEDs is relatively simple. Let’s try it with Easy-ISLisp. The code looks like this:
;;LED on/off
(defglobal pin 5)
(defglobal flag nil)
(defun test (n)
(cond ((null flag) (wiringpi-setup-gpio)(setq flag t)))
(pin-mode pin 'output)
(for ((i 0 (+ i 1)))
((> i n) t)
(digital-write pin 1)
(delay-seconds 1)
(digital-write pin 0)
(delay-seconds 1)))
Controlling Servo Motors
Similarly, you can control servo motors with Easy-ISLisp. The code for that is as follows:
;;control servo moter.
;;SG90 Micro servo Digital 9g
(defun setup ()
(cond ((null flag) (wiringpi-setup-gpio ) (setq flag t)))
(pin-mode 18 'pwm-output)
(pwm-set-mode 'pwm-mode-ms)
(pwm-set-clock 400)
(pwm-set-range 1024))
(defun test (n)
(pwm-write 18 n))
An Affordable Lisp Machine
Recently, a keyboard-integrated PI400 has become available for around $100. Even kids can afford it with their allowances. Easy-ISLisp is compact, simple, and runs smoothly on the PI400. Enjoy hardware control with Lisp.
Easy-ISLisp is an ISLisp-compliant Lisp interpreter and compiler, available under the BSD license. Feel free to use it. sasagawa888/eisl: ISLisp interpreter/compiler (github.com)