I try to define a new character (a capital german umlaut "Ä") on my 2004 lcd on a raspberry pi using wiringPi's lcdCharDef()
This is my code
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)
When I run this code I get the following error:
TypeError: in method 'lcdCharDef', argument 3 of type 'unsigned char [8]'
I expected these ints to be the same as unsigned chars
[edit]
In a different part of the code I use ord(char) to convert only one character to an unsigned int. Can this lead to the correct anser?
How can I cast/convert the array to a type that can be accepted?
P.S. (Note that (as far as I understand it) the python wiringPi library simply wraps the C functions of wiringPi)
[edit]
I opened an issue on github: https://github.com/WiringPi/WiringPi2-Python/issues/20
1 Answer 1
I did a bit of research and found the source of the relevant python binding at this github repo.
The line in question is
res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_unsigned_char, 0 | 0 );
as you can see, you have to pass in the python equivalent of a pointer to unsigned char. According to this thread, the equivalent is a byte-string. This means that the correct call would be
import struct
wiringpi.lcdCharDef(lcd_handle, 0, struct.pack('8B', *cap_umlaut_a))
which should be equivalent to
wiringpi.lcdCharDef(lcd_handle, 0, b'\x0A\x04\x0A\x11\x1F\x11\x11\x00')
7 Comments
wiringpi.lcdCharDef(self.lcd_handle, 0, b'\x0A\x04\x0A\x11\x1F\x11\x11\x0') gives an error "ValueError: invalid \x escape" and wiringpi.lcdCharDef(lcd_handle, 0, struct.pack('8B', *cap_umlaut_a)) gives "struct.error: pack requires exactly 8 arguments"Explore related questions
See similar questions with these tags.
wiringpi.lcdCharDef(self.lcd_handle, 0, array.array('B',cap_umlaut_a))? Sorry, that gives me the same error...chr()did not work andbyte()is not a built-in type in pythonwiringpi.lcdCharDef(self.lcd_handle, 0, struct.pack('8B', *cap_umlaut_a))