beanz Magazine

uLisp

California National Guard on Flickr

Meet lisp, one of the oldest and simplest families of programming languages out there!

If you’ve ever been interested in programming microcontroller computers like Arduinos, you’ve probably heard that you have to use the C programming language. This is mostly true. Today, however, we’re going to introduce a different language that lets you interactively program your Arduino over the serial connection: uLisp.

The very first thing I’ll say is that while the language is written as uLisp, the “u” is really supposed to be a ?, the greek letter “mu” which often means “micro” in science, so ?Lisp is “microlisp” because it’s for “microcontrollers” like Arduino.

What is uLisp? It’s an example of the ancient lineage of programming languages called “lisps”. The first lisp was actually one of the first programming languages ever made, so they’re almost as old as programmable computers and even older than the home computer by a couple of decades. The family includes Racket, Scheme, Common Lisp, and Clojure among many others.

All lisps have a similar design philosophy and very, very similar syntax: prefix notation with lots of parentheses. So in every lisp, instead of saying “3+4” you’ll say “(+ 3 4)” and instead of “print(10)” you’ll say something like “(print 10)” and this is how you’d write the “blink” function that’s the equivalent of “hello world” for microcontrollers

(defun b (d &optional (p 13))
  (pinmode p t)
  (loop
   (digitalwrite p t)
   (delay d)
   (digitalwrite p nil)
   (delay d)))

Compare it visually to the normal blink written in C:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);                       
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);                  
}

There’s no separate “setup” or “loop” here, just a single function that runs the program: b for blink. This function is defined with defun, which is a lot like saying def to define a function in Python. Instead of constants HIGH and LOW for whether a pin has power or not, we instead writing uLisp’s version of “true” and “false”: t and nil.

Let’s talk about how to get uLisp working. To load uLisp you need to get the uLisp code from the main website, paste it into your Arduino studio program, and then compile & load it onto the microcontroller as you would any other program. Once you’ve done that your microcontroller will be running the uLisp interpreter! The uLisp interpreter listens over the serial line for new programs, so as long as the microcontroller is plugged into your computer you can load new programs and change the behavior of the board without ever recompiling for it.

So if you’ve got uLisp running right now you can copy and paste the above code into the serial monitor and you should see the number next to the prompt go down, because you’ve provided a definition that uses some of the limited space on the microcontroller.

But! Now you can call “b” by typing (b 1000) into the serial monitor and hitting enter. When you want to stop the program enter a tilde (~) into the serial monitor and hit enter. Now you can go back to entering in other expressions and trying things out.

That’s pretty cool! There’s even an edit command you can use from the serial monitor in order to edit function definitions you already made.

It ends up being a full development environment with a repl and instead of having to run a single “loop” program on the board you can run all sorts of programs.

Now, there are limitations here: because the interpreter takes up a decent amount of the memory of the microcontroller, there’s not necessarily a ton of room for your programs or uLisp data. That doesn’t mean this is just a toy, though! The uLisp site has some amazing demos, including a ray tracer, small video games, and GPS mapping app that plots your walking path on a map. Because uLisp is, broadly, easier to code in than C it’s possible to make things like this even with the smaller overhead left by running the uLisp interpreter.

If you’re like me and you first see something like this and you’re not super sure what you’d use it for here are some ideas:

  • Writing a bare-bones gemini server that runs on a ESP32 chip (What’s gemini? See the article in this issue on the small internet!)
  • Making a video game on a PyGamer board from AdaFruit
  • Incrementally program an AI for an Arduino powered robot car
  • Live code music on a piezo speaker connected to a microcontroller

So all-in-all, uLisp is a really cool language and I highly recommend you check out the docs and the examples on the main site. The language isn’t too hard to learn once you get used to the syntax, and it lets you experiment actively a lot more with your microcontroller than the slow “compile-upload” loop of writing C code.

Happy hacking!

Learn More

uLisp site

http://www.ulisp.com/show?3J

Lisp programming language

https://en.wikipedia.org/wiki/Lisp_(programming_language)

History of Lisp

https://history-computer.com/ModernComputer/Software/LISP.html

Short history of Lisp Programming language

https://www.kidscodecs.com/lisp/

Timeline of programming languages

https://en.wikipedia.org/wiki/Timeline_of_programming_languages

uLisp supports adafruit

https://blog.adafruit.com/2018/09/17/lisp-for-microcontrollers-ulisp-interpreter-supports-adafruit-m4-boards-johnson_davies/

Also In The December 2020 Issue

Welcome to the far away land of the Ein Lifon where the number of appendages you have changes how you count!

You've looked at and used Susan Kare's work, or work inspired by hers, every time you use a computer.

Through the example of Theo Jansen and his Strandbeests we discuss the importance of fostering creativity and innovation in students.

From gopherspace to geminispace, the small internet is a lot bigger than you think!

Learn how asymmetric encryption and prime numbers help keep you safe online!

What could we accomplish if batteries lasted forever? These two companies want to find out!

Interesting stories about computer science, software programming, and technology for December 2020.