Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Tuesday, 15 April 2014

2500 LEDS!


Some time ago I picked up a couple of LPD-8008 displays from Embedded Adventures. Each display is made up of ten of the 8x8 LED matrices  that are often hooked up to Arduino and Pi's. That's 64 pixels per matrix,or 128 LEDs (as they're tricolour),making 1280 LED's per panel. Hooking up two panels makes a crazy 2560 LED's.

Half the time, when you buy stuff like this, half the problem of making it work is figuring out exactly what you have - but in this case the documentation is excellent. One thing it warns about when hooking up these panels is that this many LED's can use a lot of power. If you try turning on all of the LED's at the same time, then you need 2.5A (@5V=12.5W) per panel. What this means is that you shouldn't be running it of the internal power regulator of your Pi or Arduino... In practise you'll just about get away with it for displaying text as most of the pixels are off most of the time, so power consumption is a lot lower . However I did overload the Arduino power supply several times during development so I wouldn't recommend it - particularly on a Pi where reseting the PSU is far more serious than an Arduino.

To get these working in Sniff, I wrote a new lpd8008 device extension. While it would be possible to drive the whole thing from Sniff, writing an extension hides the internals, and makes it easy to use complex devices with only a few lines of code. The heavy lifting is done by the extension which consists of the two files Xlpd8008.h and Xlpd8008.c. Placing these in the Sniff lib directory automatically adds the device into your Sniff system.

For Arduino I chose the fairly arbitrary pinout:

  • Arduino 4 -> A
  • Arduino 5 -> B
  • Arduino 6 -> C
  • Arduino 7 -> D (Unused in the 8008)
  • Arduino 8 -> Enable
  • Arduini 9 ->  Green
  • Arduino 10 -> Red
  • Arduino 11 -> Latch
  • Arduino 12 -> Shift
You can check this in C file, as it might change in future (there are also pins declared for Raspberry Pi). Hooking up multiple displays is simply a matter of daisy changing them together with a ribbon cable!

One of the tricky parts of using these kinds of displays is that they only display one row of pixels at a time - you need to keep displaying each line in turn. Driving this from most programming languages generally means some tricky programming: you either need interrupts, or to integrate the screen refresh into your main code - you can't just display a message and forget about it. However Sniff repeatedly demonstrates its power here. Multiple scripts can run in parallel, so to get the display up and running we just need to use the following lines of code:

make lpd8008 device 160
make message string
make startColumn number
make endColumn number
make ledColor number

when start
.forever
..tell lpd8008 to "tick"

when start
.set message to "Hello World!"

We start by making an lpd8008 device, but as I'm using two hooked together I tell it to use 160 columns (rather than the default 80). The device supports a single command "tick" - which updates one row of the display. The first script is set up to constantly call this function, so that the whole display is active. In the second script we simply set the variable message, which contains the string to be displayed.

StartColumn allows you to shift the message right or left, so we can scroll the message to the right by simply using:

when start
.set message to "Hello World!"
.set startColumn to 0
.repeat until startColumn=160
..change startColumn by 1
..wait 0.1 secs

However this scrolls the message off the screen completely. If we just want to bounce a short message from left to right, so that it all stays on screen, we need to know the length of the text. This is a little tricky: the extension uses the standard 5x8 pixel font every device, but to make the most of the limited screen space, and to simply make it look better it proportionally spaces the letters. To make things easier, the "tick" command calculates the variable endColumn, which we can use. To scroll to the right, stopping while the whole message is still visible we an use:

..repeat until endColumn=160
...change startColumn by 1
...wait 0.1 secs

To bounce a message back and forth we can use:

.repeat 2
..repeat until endColumn=160
...change startColumn by 1
...wait 0.1 secs
..
..repeat until startColumn=0
...change startColumn by -1
...wait 0.1 secs

To Scroll it from offscreen right to offscreen left we can use:

.set ledColor to 2
.forever
..set startColumn to 160
..repeat until endColumn<0
...change startColumn by -1
...wait 0.1 secs

This also sets the colour to green (1=red, 2=green, 3=orange).

While its certainly possible to do more with these displays than just text, a large easily programmable text display that can easily be added to your own projects is a lot of fun. Here's a simple count down, which looks really cool:

make counter number
when start
.set startColumn to 15
.set ledColor to 3
.
.repeat until counter <0
..set counter to 10-timer
..set message to [ counter ]


The files will be included in the next Sniff release, but in the mean time you can download and install them into your current Sniff system: Download. They've been developed and tested on Arduino, but should work fine on Pi too.

Personally I'm looking forward to hooking this up to a Punchthough Bean, and displaying my phone notifications on it!

No comments:

Post a Comment