|
| 1 | +""" |
| 2 | +This MicroPython library is designed for any hardware plaform that supports MicroPython such as Raspberry Pi Pico, ESP32, Micro:bit... to work with the LCD I2C. It is created by DIYables to work with DIYables LCD I2C, but also work with other brand LCD I2C. Please consider purchasing products from DIYables to support our work. |
| 3 | +
|
| 4 | +Product Link: |
| 5 | +- [LCD I2C 16x2](https://diyables.io/products/lcd-i2c-16x2-blue-background) |
| 6 | +- [LCD I2C 20x4](https://diyables.io/products/lcd-20x4-display-i2c-interface) |
| 7 | +
|
| 8 | +
|
| 9 | +Copyright (c) 2024, DIYables.io. All rights reserved. |
| 10 | +
|
| 11 | +Redistribution and use in source and binary forms, with or without |
| 12 | +modification, are permitted provided that the following conditions |
| 13 | +are met: |
| 14 | +
|
| 15 | +- Redistributions of source code must retain the above copyright |
| 16 | + notice, this list of conditions and the following disclaimer. |
| 17 | +
|
| 18 | +- Redistributions in binary form must reproduce the above copyright |
| 19 | + notice, this list of conditions and the following disclaimer in the |
| 20 | + documentation and/or other materials provided with the distribution. |
| 21 | +
|
| 22 | +- Neither the name of the DIYables.io nor the names of its |
| 23 | + contributors may be used to endorse or promote products derived from |
| 24 | + this software without specific prior written permission. |
| 25 | +
|
| 26 | +THIS SOFTWARE IS PROVIDED BY DIYABLES.IO "AS IS" AND ANY EXPRESS OR |
| 27 | +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 28 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 | +DISCLAIMED. IN NO EVENT SHALL DIYABLES.IO BE LIABLE FOR ANY DIRECT, |
| 30 | +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 31 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 32 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 33 | +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 34 | +STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 35 | +IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 36 | +POSSIBILITY OF SUCH DAMAGE. |
| 37 | +""" |
| 38 | + |
| 39 | +from machine import I2C, Pin |
| 40 | +from time import sleep_ms |
| 41 | + |
| 42 | +class LCD_I2C: |
| 43 | + # LCD Commands |
| 44 | + LCD_CLR = 0x01 |
| 45 | + LCD_HOME = 0x02 |
| 46 | + |
| 47 | + LCD_ENTRY_MODE = 0x04 |
| 48 | + LCD_ENTRY_INC = 0x02 |
| 49 | + LCD_ENTRY_SHIFT = 0x01 |
| 50 | + |
| 51 | + LCD_ON_CTRL = 0x08 |
| 52 | + LCD_ON_DISPLAY = 0x04 |
| 53 | + LCD_ON_CURSOR = 0x02 |
| 54 | + LCD_ON_BLINK = 0x01 |
| 55 | + |
| 56 | + LCD_MOVE = 0x10 |
| 57 | + LCD_MOVE_DISP = 0x08 |
| 58 | + LCD_MOVE_RIGHT = 0x04 |
| 59 | + |
| 60 | + LCD_FUNCTION = 0x20 |
| 61 | + LCD_FUNCTION_8BIT = 0x10 |
| 62 | + LCD_FUNCTION_2LINES = 0x08 |
| 63 | + LCD_FUNCTION_10DOTS = 0x04 |
| 64 | + LCD_FUNCTION_RESET = 0x30 |
| 65 | + |
| 66 | + LCD_CGRAM = 0x40 |
| 67 | + LCD_DDRAM = 0x80 |
| 68 | + |
| 69 | + LCD_RS_CMD = 0 |
| 70 | + LCD_RS_DATA = 1 |
| 71 | + |
| 72 | + LCD_RW_WRITE = 0 |
| 73 | + LCD_RW_READ = 1 |
| 74 | + |
| 75 | + def __init__(self, i2c, i2c_addr, num_lines, num_columns): |
| 76 | + self.i2c = i2c |
| 77 | + self.i2c_addr = i2c_addr |
| 78 | + self.num_lines = num_lines |
| 79 | + self.num_columns = num_columns |
| 80 | + self._current_state = 0x00 |
| 81 | + self._backlight = 0x08 |
| 82 | + |
| 83 | + sleep_ms(20) |
| 84 | + self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) |
| 85 | + sleep_ms(5) |
| 86 | + self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) |
| 87 | + sleep_ms(1) |
| 88 | + self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) |
| 89 | + sleep_ms(1) |
| 90 | + self.hal_write_init_nibble(self.LCD_FUNCTION) |
| 91 | + sleep_ms(1) |
| 92 | + |
| 93 | + self.init_lcd() |
| 94 | + |
| 95 | + def hal_write_init_nibble(self, nibble): |
| 96 | + byte = ((nibble >> 4) & 0x0F) << 4 |
| 97 | + self.i2c.writeto(self.i2c_addr, bytes([byte | self.LCD_RS_CMD | self._backlight])) |
| 98 | + self.hal_pulse_enable(byte) |
| 99 | + |
| 100 | + def hal_backlight_on(self): |
| 101 | + self._backlight = 0x08 |
| 102 | + self.i2c.writeto(self.i2c_addr, bytes([self._current_state | self._backlight])) |
| 103 | + |
| 104 | + def hal_backlight_off(self): |
| 105 | + self._backlight = 0x00 |
| 106 | + self.i2c.writeto(self.i2c_addr, bytes([self._current_state | self._backlight])) |
| 107 | + |
| 108 | + def hal_write_command(self, cmd): |
| 109 | + self._write_byte((cmd & 0xF0) | self.LCD_RS_CMD) |
| 110 | + self._write_byte(((cmd << 4) & 0xF0) | self.LCD_RS_CMD) |
| 111 | + if cmd <= 3: |
| 112 | + sleep_ms(5) |
| 113 | + |
| 114 | + def hal_write_data(self, data): |
| 115 | + self._write_byte((data & 0xF0) | self.LCD_RS_DATA) |
| 116 | + self._write_byte(((data << 4) & 0xF0) | self.LCD_RS_DATA) |
| 117 | + |
| 118 | + def _write_byte(self, byte): |
| 119 | + self.i2c.writeto(self.i2c_addr, bytes([byte | self._backlight])) |
| 120 | + self.hal_pulse_enable(byte) |
| 121 | + |
| 122 | + def hal_pulse_enable(self, byte): |
| 123 | + self.i2c.writeto(self.i2c_addr, bytes([byte | 0x04 | self._backlight])) |
| 124 | + self.i2c.writeto(self.i2c_addr, bytes([byte & ~0x04 | self._backlight])) |
| 125 | + |
| 126 | + def init_lcd(self): |
| 127 | + self.display_on() |
| 128 | + self.clear() |
| 129 | + self.set_entry_mode() |
| 130 | + |
| 131 | + def clear(self): |
| 132 | + self.hal_write_command(self.LCD_CLR) |
| 133 | + self.hal_sleep(3) |
| 134 | + |
| 135 | + def show_cursor(self): |
| 136 | + self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | self.LCD_ON_CURSOR) |
| 137 | + |
| 138 | + def hide_cursor(self): |
| 139 | + self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY) |
| 140 | + |
| 141 | + def blink_cursor_on(self): |
| 142 | + self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | self.LCD_ON_CURSOR | self.LCD_ON_BLINK) |
| 143 | + |
| 144 | + def blink_cursor_off(self): |
| 145 | + self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | self.LCD_ON_CURSOR) |
| 146 | + |
| 147 | + def display_on(self): |
| 148 | + self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY) |
| 149 | + |
| 150 | + def display_off(self): |
| 151 | + self.hal_write_command(self.LCD_ON_CTRL) |
| 152 | + |
| 153 | + def set_entry_mode(self, inc=True, shift=False): |
| 154 | + entry_mode = self.LCD_ENTRY_MODE |
| 155 | + if inc: |
| 156 | + entry_mode |= self.LCD_ENTRY_INC |
| 157 | + if shift: |
| 158 | + entry_mode |= self.LCD_ENTRY_SHIFT |
| 159 | + self.hal_write_command(entry_mode) |
| 160 | + |
| 161 | + def backlight_off(self): |
| 162 | + self.hal_backlight_off() |
| 163 | + |
| 164 | + def backlight_on(self): |
| 165 | + self.hal_backlight_on() |
| 166 | + |
| 167 | + def home(self): |
| 168 | + self.hal_write_command(self.LCD_HOME) |
| 169 | + self.hal_sleep(3) |
| 170 | + |
| 171 | + def set_cursor(self, col, line): |
| 172 | + addr = col & 0x3F |
| 173 | + if line & 1: |
| 174 | + addr += 0x40 |
| 175 | + if line & 2: |
| 176 | + addr += 0x14 |
| 177 | + self.hal_write_command(self.LCD_DDRAM | addr) |
| 178 | + |
| 179 | + def print(self, string): |
| 180 | + for char in string: |
| 181 | + self.hal_write_data(ord(char)) |
| 182 | + |
| 183 | + def custom_char(self, location, charmap): |
| 184 | + self.hal_write_command(self.LCD_CGRAM | ((location & 7) << 3)) |
| 185 | + self.hal_sleep(5) |
| 186 | + for i in range(8): |
| 187 | + self.hal_write_data(charmap[i]) |
| 188 | + self.home() |
| 189 | + |
| 190 | + def print_custom_char(self, location): |
| 191 | + self.hal_write_data(location) |
| 192 | + |
| 193 | + def hal_sleep(self, time_ms): |
| 194 | + sleep_ms(time_ms) |
0 commit comments