我尝试使用 wiringPi's lcdCharDef()
在树莓派上的 2004 液晶显示器上定义一个新字符(大写德语变音符号“Ä”)
这是我的代码
import wiringpi2 as wiringpi
# Ä
cap_umlaut_a = [
int('0b01010', 2),
int('0b00100', 2),
int('0b01010', 2),
int('0b10001', 2),
int('0b11111', 2),
int('0b10001', 2),
int('0b10001', 2),
int('0b00000', 2)
]
print(cap_umlaut_a) # [10, 4, 10, 17, 31, 17, 17, 0]
wiringpi.lcdCharDef(lcd_handle, 0, cap_umlaut_a)
当我运行这段代码时,出现以下错误:
TypeError: in method 'lcdCharDef', argument 3 of type 'unsigned char [8]'
我希望这些 ints
与 unsigned chars
相同
[编辑]
在代码的不同部分,我使用 ord(char)
仅将一个字符转换为 unsigned int。这能得出正确的答案吗?
如何将数组转换/转换为可以接受的类型?
附言(请注意(据我所知)python wiringPi 库只是包装了 wiringPi 的 C 函数)
[编辑]
我在 github 上开了一个问题:https://github.com/WiringPi/WiringPi2-Python/issues/20
最佳答案
我做了一些研究,找到了相关 python 绑定(bind)的来源 at this github repo .
有问题的行是
res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_unsigned_char, 0 | 0 );
如您所见,您必须传入相当于指向 unsigned char 的指针的 python。根据this thread ,相当于一个字节串。这意味着正确的调用是
import struct
wiringpi.lcdCharDef(lcd_handle, 0, struct.pack('8B', *cap_umlaut_a))
应该等同于
wiringpi.lcdCharDef(lcd_handle, 0, b'\x0A\x04\x0A\x11\x1F\x11\x11\x00')
关于python - python中int数组中的unsigned char数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31711978/