4

I have a LED strip that I want to control with my Raspberry PI. I have connected it to the GPIO10 (MOSI) and GPIO11 (CLK). The SPI module is loaded in Raspbian.

I have created a file that I send to /dev/spidev-0.0, when i do that i can control the LEDs.

If i send a file that looks like the one below i turn the LED off.

00000000   00 00 00 00  80 00 80 00  80 00 80 00  80 00 80 00  ................
00000010   80 00 80 00  80 00 80 00  80 00 80 00  80 00 80 00  ................
00000020   80 00 80 00  80 00 80 00  80 00 80 00  80 00 80 00  ................
00000030   80 00 80 00  80 00 80 00  80 00 80 00  80 00 80 00  ................
00000040   80 00 80 00  80 00 80 00  80 00 80 00  80 00 80 00  ................
00000050   80 00 80 00  80 00 80 00  80 00 80 00  80 00 80 00  ................
00000060   80 00 80 00  80 00 80 00  80 00                     ..........

If i send a file that looks like the one below i turn the LED on.

00000000   00 00 00 00  FF FF FF FF  FF FF FF FF  FF FF FF FF  ................
00000010   FF FF FF FF  FF FF FF FF  FF FF FF FF  FF FF FF FF  ................
00000020   FF FF FF FF  FF FF FF FF  FF FF FF FF  FF FF FF FF  ................
00000030   FF FF FF FF  FF FF FF FF  FF FF FF FF  FF FF FF FF  ................
00000040   FF FF FF FF  FF FF FF FF  FF FF FF FF  FF FF FF FF  ................
00000050   FF FF FF FF  FF FF FF FF  FF FF FF FF  FF FF FF FF  ................
00000060   FF FF FF FF  FF FF FF FF  FF FF                     ..........

My problem is how do i do this in Python? I want to create this strings on the fly and send them to the SPI interface.

Rickard
  • 620
  • 2
  • 7
  • 23

1 Answers1

4

Constructing those byte strings is easy: just use \x escape codes.

Here's a simple example, which I tested on Python 2.6, but it should work ok on Python 3, too.

hdr = b'\x00' * 4
blocksize = 51
leds = (
    #LED off
    hdr + b'\x80\x00' * blocksize,
    #LED on
    hdr + b'\xff\xff' * blocksize,
)

fname = '/dev/stdout'
with open(fname, 'wb') as f:
    f.write(leds[0])

That code creates the file to turn the LED off; to turn it on simply do f.write(leds[1]).

The b prefix on the strings indicate that the strings are byte strings. That prefix isn't required on Python 2, since Python 2 strings are byte string objects, but it should be used in Python 3, since Python 3 strings are Unicode string objects.

My code writes to /dev/stdout to simplify testing, since I don't have a Raspberry Pi, but you can easily change the filename to /dev/spidev-0.0.

Here's a hexdump of its output:

00000000  00 00 00 00 80 00 80 00  80 00 80 00 80 00 80 00  |................|
00000010  80 00 80 00 80 00 80 00  80 00 80 00 80 00 80 00  |................|
00000020  80 00 80 00 80 00 80 00  80 00 80 00 80 00 80 00  |................|
00000030  80 00 80 00 80 00 80 00  80 00 80 00 80 00 80 00  |................|
00000040  80 00 80 00 80 00 80 00  80 00 80 00 80 00 80 00  |................|
00000050  80 00 80 00 80 00 80 00  80 00 80 00 80 00 80 00  |................|
00000060  80 00 80 00 80 00 80 00  80 00                    |..........|
0000006a
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182