简体   繁体   English

在Raspberry PI B +上将HEX值发送到SPI

[英]Send HEX values to SPI on a Raspberry PI B+

I have a LED strip that I want to control with my Raspberry PI. 我有一个我想用Raspberry PI控制的LED灯条。 I have connected it to the GPIO10 (MOSI) and GPIO11 (CLK). 我已将其连接到GPIO10(MOSI)和GPIO11(CLK)。 The SPI module is loaded in Raspbian. SPI模块在Raspbian中加载。

I have created a file that I send to /dev/spidev-0.0 , when i do that i can control the LEDs. 我创建了一个发送到/dev/spidev-0.0的文件,当我这样做时,我可以控制LED。

If i send a file that looks like the one below i turn the LED off. 如果我发送的文件看起来像下面那个我关闭LED。

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. 如果我发送的文件看起来像下面的那个,我打开LED。

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? 我的问题是如何在Python中执行此操作? I want to create this strings on the fly and send them to the SPI interface. 我想动态创建这些字符串并将它们发送到SPI接口。

Constructing those byte strings is easy: just use \\x escape codes. 构造那些字节字符串很简单:只需使用\\x转义码即可。

Here's a simple example, which I tested on Python 2.6, but it should work ok on Python 3, too. 这是一个简单的例子,我在Python 2.6上测试过,但它也适用于Python 3。

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; 该代码创建文件以关闭LED; to turn it on simply do f.write(leds[1]) . 打开它只需要f.write(leds[1])

The b prefix on the strings indicate that the strings are byte strings. 字符串上的b前缀表示字符串是字节字符串。 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. Python 2上不需要该前缀,因为Python 2字符串是字节字符串对象,但它应该在Python 3中使用,因为Python 3字符串是Unicode字符串对象。

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 . 我的代码写入/dev/stdout以简化测试,因为我没有Raspberry Pi,但您可以轻松地将文件名更改为/dev/spidev-0.0

Here's a hexdump of its output: 这是输出的hexdump:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM