|
| 1 | +"""Implements a HD44780 character LCD connected via PCF8574 on I2C. |
| 2 | + This was tested with: https://www.wemos.cc/product/d1-mini.html""" |
| 3 | + |
| 4 | +from lcd_api import LcdApi |
| 5 | +import busio |
| 6 | +from time import sleep |
| 7 | + |
| 8 | +# The PCF8574 has a jumper selectable address: 0x20 - 0x27 |
| 9 | +DEFAULT_I2C_ADDR = 0x27 |
| 10 | + |
| 11 | +# Defines shifts or masks for the various LCD line attached to the PCF8574 |
| 12 | + |
| 13 | +MASK_RS = 0x01 |
| 14 | +MASK_RW = 0x02 |
| 15 | +MASK_E = 0x04 |
| 16 | +SHIFT_BACKLIGHT = 3 |
| 17 | +SHIFT_DATA = 4 |
| 18 | + |
| 19 | + |
| 20 | +class 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)) |
| 78 | + self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) |
| 79 | + self.i2c.writeto(self.i2c_addr, bytearray([byte])) |
| 80 | + byte = (MASK_RS | (self.backlight << SHIFT_BACKLIGHT) | ((data & 0x0f) << SHIFT_DATA)) |
| 81 | + self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) |
| 82 | + self.i2c.writeto(self.i2c_addr, bytearray([byte])) |
| 83 | + |
| 84 | + def hal_sleep_us(self, usecs): |
| 85 | + """Sleep for some time (given in microseconds).""" |
| 86 | + sleep(float(usecs)/1e6) |
0 commit comments