def test_init_4bitmode(): """ Test initialization of display using 4 bit mode """ hd44780(serial, gpio=gpio) to_8 = \ [call(0x3), call(0x3), call(0x3, 0x3)] * 3 to_4 = \ [call(0x3), call(0x3), call(0x3, 0x02)] fs = [CONST.FUNCTIONSET | CONST.DL4 | CONST.LINES2] calls = \ to_8 + \ to_4 + \ [call(*bytes_to_nibbles(fs))] + \ [call(*bytes_to_nibbles([CONST.DISPLAYOFF]))] + \ [call(*bytes_to_nibbles([CONST.ENTRY]))] + \ [call(*bytes_to_nibbles([CONST.DISPLAYON]))] + \ [call(*bytes_to_nibbles([CONST.DDRAMADDR]))] + \ [call(*bytes_to_nibbles([CONST.DDRAMADDR | CONST.LINES[1]]))] + \ [call(*bytes_to_nibbles([CONST.CLEAR]))] serial.command.assert_has_calls(calls) # Data to clear the screen calls = \ [call(bytes_to_nibbles([0x20] * 16))] + \ [call(bytes_to_nibbles([0x20] * 16))] serial.data.assert_has_calls(calls)
def test_bytes_to_nibbles(): """ Test the conversion from 8 bit values into 4 bit (nibbles) """ data = [0, 1, 2, 3, 0xFF, 0x7F] expect = [0, 0, 0, 1, 0, 2, 0, 3, 15, 15, 7, 15] result = util.bytes_to_nibbles(data) assert result == expect
def test_display(): interface._bitmode = 4 d = ws0010(interface) interface.reset_mock() # Use canvas to create an all white screen with canvas(d) as drw: drw.rectangle((0, 0, 99, 15), fill='white', outline='white') line1 = [ call.command(*bytes_to_nibbles([DDRAMADDR, CGRAMADDR])), call.data([0x0F] * 200) ] line2 = [ call.command(*bytes_to_nibbles([DDRAMADDR, CGRAMADDR + 1])), call.data([0x0F] * 200) ] interface.assert_has_calls(line1 + line2)
def data(self, data): """ Sends a sequence of bytes through to the serial interface. If operating in four bit mode, expands each byte from a single value (8 bits) to two nibble values (4 bits each) :param data: a sequence of bytes to send to the display :type data: list """ data = data if self._bitmode == 8 else \ bytes_to_nibbles(data) super(parallel_device, self).data(data)
def test_init_4bitmode(): ws0010(interface) to_4 = \ [call(0, 0, 0, 0, 0, 2, 2, 9)] calls = \ to_4 + \ [call(*bytes_to_nibbles([DISPLAYOFF]))] + \ [call(*bytes_to_nibbles([POWEROFF]))] + \ [call(*bytes_to_nibbles([ENTRY]))] + \ [call(*bytes_to_nibbles([POWERON | GRAPHIC]))] + \ [call(*bytes_to_nibbles([DISPLAYON]))] + \ [call(*bytes_to_nibbles([DDRAMADDR, CGRAMADDR]))] + \ [call(*bytes_to_nibbles([DDRAMADDR, CGRAMADDR + 1]))] interface.command.assert_has_calls(calls) # Data to clear the screen calls = \ [call(bytes_to_nibbles([0] * 100))] + \ [call(bytes_to_nibbles([0] * 100))] interface.data.assert_has_calls(calls)
def command(self, *cmd, exec_time=None, only_low_bits=False): """ Sends a command or sequence of commands through to the serial interface. If operating in four bit mode, expands each command from one byte values (8 bits) to two nibble values (4 bits each) :param cmd: A spread of commands. :type cmd: int :param exec_time: Amount of time to wait for the command to finish execution. If not provided, the device default will be used instead :type exec_time: float :param only_low_bits: If ``True``, only the lowest four bits of the command will be sent. This is necessary on some devices during initialization :type only_low_bits: bool """ cmd = cmd if (self._bitmode == 8 or only_low_bits) else \ bytes_to_nibbles(cmd) super(parallel_device, self).command(*cmd) sleep(exec_time or self._exec_time)