Scrolling text display
This example gives a scrolling text display on a four-character alphanumeric display, and demonstrates the use of the character functions introduced in uLisp 1.9:
The display shows a message scrolling slowly to the left, and the message can be as long as you like, subject to the amount of memory available on the processor board. I've tested it with the Arduino Mega 2560 and MSP430F5529 LaunchPad boards.
The display I used is a four-character alphanumeric display from Adafruit with an I2C interface [1], and the program communicates with the display using the I2C interface functions in uLisp.
The program
Each display has 14 segments, and you display a character by writing two bytes to the display to specify which segments are lit. First I defined a list of codes for the 64 characters from space (ASCII code 32) to '_' (ASCII code 95):
(defvar *codes* '(#x0 #x6 #x220 #x12CE #x12ED #xC24 #x235D #x400 #x2400 #x900 #x3FC0 #x12C0
#x800 #xC0 #x0 #xC00 #xC3F #x6 #xDB #x8F #xE6 #x2069 #xFD #x7 #xFF #xEF #x1200 #xA00 #x2400
#xC8 #x900 #x1083 #x2BB #xF7 #x128F #x39 #x120F #xF9 #x71 #xBD #xF6 #x1200 #x1E #x2470 #x38
#x536 #x2136 #x3F #xF3 #x203F #x20F3 #xED #x1201 #x3E #xC30 #x2836 #x2D00 #x1500 #xC09 #x39
#x2100 #xF #xC03 #x8))
Although the display can in theory show lower-case letters, they are not very readable, so I decided to display text in all capitals, and these codes don't include the lower-case letters.
The routine on turns on the display and sets the brightness to a value of between 0 and 15:
(defun on (bri) (with-i2c (str #x70) (write-byte #x21 str) (restart-i2c str) (write-byte (+ bri #xe0) str) (restart-i2c str) (write-byte #x81 str)))
The routine display writes the segment codes for a character x to the stream str as two bytes:
(defun display (x str) (if (>= x #x60) (setq x (- x #x20))) (write-byte (nth (- x 32) *codes*) str) (write-byte (ash (nth (- x 32) *codes*) -8) str))
Finally, scroll displays a text string as a scrolling message:
(defun scroll (string) (let ((len (length string))) (loop (dotimes (x (+ len 4)) (with-i2c (str #x70) (write-byte #x00 str) (dotimes (j 4) (let ((i (+ x j -4))) (display (if (<= 0 i (1- len)) (char-code (char string i)) 32) str)))) (delay 250)))))
The delay on the last line determines the scrolling speed.
To display a message first turn the display on:
(on 8)
Then give the command scroll followed by the string; for example:
(scroll "A scrolling message display written in Lisp")
Here's the whole listing: Scrolling text display program.