MCPcopy Create free account
hub / github.com/RetiredWizard/PyDOS / I2cLcd

Class I2cLcd

cpython/lib/circuitpython_i2c_lcd.py:20–86  ·  view source on GitHub ↗

Implements a HD44780 character LCD connected via PCF8574 on I2C.

Source from the content-addressed store, hash-verified

18
19
20class I2cLcd(LcdApi):
21 """Implements a HD44780 character LCD connected via PCF8574 on I2C."""
22 def __init__(self, i2c, i2c_addr, num_lines, num_columns):
23 self.i2c = i2c
24 self.i2c_addr = i2c_addr
25 self.i2c.writeto(self.i2c_addr, bytearray([0]))
26 sleep(0.02) # Allow LCD time to powerup
27 # Send reset 3 times
28 self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
29 sleep(0.005) # need to delay at least 4.1 msec
30 self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
31 sleep(0.001)
32 self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
33 sleep(0.001)
34 # Put LCD into 4 bit mode
35 self.hal_write_init_nibble(self.LCD_FUNCTION)
36 sleep(0.001)
37 LcdApi.__init__(self, num_lines, num_columns)
38 cmd = self.LCD_FUNCTION
39 if num_lines > 1:
40 cmd |= self.LCD_FUNCTION_2LINES
41 self.hal_write_command(cmd)
42
43 def hal_write_init_nibble(self, nibble):
44 """Writes an initialization nibble to the LCD.
45
46 This particular function is only used during initialization.
47 """
48 byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA
49 self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E]))
50 self.i2c.writeto(self.i2c_addr, bytearray([byte]))
51
52 def hal_backlight_on(self):
53 """Allows the hal layer to turn the backlight on."""
54 self.i2c.writeto(self.i2c_addr, bytearray([1 << SHIFT_BACKLIGHT]))
55
56 def hal_backlight_off(self):
57 """Allows the hal layer to turn the backlight off."""
58 self.i2c.writeto(self.i2c_addr, bytearray([0]))
59
60 def hal_write_command(self, cmd):
61 """Writes a command to the LCD.
62
63 Data is latched on the falling edge of E.
64 """
65 byte = ((self.backlight << SHIFT_BACKLIGHT) | (((cmd >> 4) & 0x0f) << SHIFT_DATA))
66 self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E]))
67 self.i2c.writeto(self.i2c_addr, bytearray([byte]))
68 byte = ((self.backlight << SHIFT_BACKLIGHT) | ((cmd & 0x0f) << SHIFT_DATA))
69 self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E]))
70 self.i2c.writeto(self.i2c_addr, bytearray([byte]))
71 if cmd <= 3:
72 # The home and clear commands require a worst case delay of 4.1 msec
73 sleep(0.005)
74
75 def hal_write_data(self, data):
76 """Write data to the LCD."""
77 byte = (MASK_RS | (self.backlight << SHIFT_BACKLIGHT) | (((data >> 4) & 0x0f) << SHIFT_DATA))

Callers 3

lcdPrintFunction · 0.90
lcdScrollFunction · 0.90
displayTempFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected