/ adafruit_character_lcd / character_lcd_rgb_i2c.py
character_lcd_rgb_i2c.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2018 Kattni Rembor for Adafruit Industries 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 """ 23 `adafruit_character_lcd.character_lcd_i2c` 24 ==================================================== 25 26 Module for using I2C with I2C RGB LCD Shield or I2C RGB LCD Pi Plate 27 28 * Author(s): Kattni Rembor 29 30 Implementation Notes 31 -------------------- 32 33 **Hardware:** 34 35 "* `RGB LCD Shield Kit w/ 16x2 Character Display - Negative Display 36 <https://www.adafruit.com/product/714>`_" 37 38 "* `RGB LCD Shield Kit w/ 16x2 Character Display - Positive Display 39 <https://www.adafruit.com/product/716>`_" 40 41 "* `Adafruit RGB Negative 16x2 LCD+Keypad Kit for Raspberry Pi 42 <https://www.adafruit.com/product/1110>`_" 43 44 "* `Adafruit RGB Positive 16x2 LCD+Keypad Kit for Raspberry Pi 45 <https://www.adafruit.com/product/1109>`_" 46 47 **Software and Dependencies:** 48 49 * Adafruit CircuitPython firmware: 50 https://github.com/adafruit/circuitpython/releases 51 * Adafruit's Bus Device library (when using I2C/SPI): 52 https://github.com/adafruit/Adafruit_CircuitPython_BusDevice 53 54 """ 55 56 import digitalio 57 from adafruit_mcp230xx.mcp23017 import MCP23017 58 from adafruit_character_lcd.character_lcd import Character_LCD_RGB 59 60 __version__ = "0.0.0-auto.0" 61 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CharLCD.git" 62 63 64 class Character_LCD_RGB_I2C(Character_LCD_RGB): 65 """RGB Character LCD connected to I2C shield or Pi plate using I2C connection. 66 This is a subclass of Character_LCD_RGB and implements all of the same 67 functions and functionality. 68 69 To use, import and initialise as follows: 70 71 .. code-block:: python 72 73 import board 74 import busio 75 from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C 76 77 i2c = busio.I2C(board.SCL, board.SDA) 78 lcd = Character_LCD_RGB_I2C(i2c, 16, 2) 79 80 """ 81 82 def __init__(self, i2c, columns, lines, address=None): 83 # pylint: disable=too-many-locals 84 """Initialize RGB character LCD connected to shield using I2C connection 85 on the specified I2C bus with the specified number of columns and lines 86 on the display. 87 """ 88 89 if address: 90 mcp = MCP23017(i2c, address=address) 91 else: 92 mcp = MCP23017(i2c) 93 94 self._left_button = mcp.get_pin(4) 95 self._up_button = mcp.get_pin(3) 96 self._down_button = mcp.get_pin(2) 97 self._right_button = mcp.get_pin(1) 98 self._select_button = mcp.get_pin(0) 99 100 self._buttons = [ 101 self._left_button, 102 self._up_button, 103 self._down_button, 104 self._right_button, 105 self._select_button, 106 ] 107 108 for pin in self._buttons: 109 pin.switch_to_input(pull=digitalio.Pull.UP) 110 111 super().__init__( 112 mcp.get_pin(15), 113 mcp.get_pin(13), 114 mcp.get_pin(12), 115 mcp.get_pin(11), 116 mcp.get_pin(10), 117 mcp.get_pin(9), 118 columns, 119 lines, 120 mcp.get_pin(6), 121 mcp.get_pin(7), 122 mcp.get_pin(8), 123 mcp.get_pin(14), 124 ) 125 126 @property 127 def left_button(self): 128 """The left button on the RGB Character LCD I2C Shield or Pi plate. 129 130 The following example prints "Left!" to the LCD when the left button is pressed: 131 132 .. code-block:: python 133 134 import board 135 import busio 136 from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C 137 138 i2c = busio.I2C(board.SCL, board.SDA) 139 lcd = Character_LCD_RGB_I2C(i2c, 16, 2) 140 141 while True: 142 if lcd.left_button: 143 lcd.message = "Left!" 144 145 """ 146 return not self._left_button.value 147 148 @property 149 def up_button(self): 150 """The up button on the RGB Character LCD I2C Shield or Pi plate. 151 152 The following example prints "Up!" to the LCD when the up button is pressed: 153 154 .. code-block:: python 155 156 import board 157 import busio 158 from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C 159 160 i2c = busio.I2C(board.SCL, board.SDA) 161 lcd = Character_LCD_RGB_I2C(i2c, 16, 2) 162 163 while True: 164 if lcd.up_button: 165 lcd.message = "Up!" 166 167 """ 168 return not self._up_button.value 169 170 @property 171 def down_button(self): 172 """The down button on the RGB Character LCD I2C Shield or Pi plate. 173 174 The following example prints "Down!" to the LCD when the down button is pressed: 175 176 .. code-block:: python 177 178 import board 179 import busio 180 from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C 181 182 i2c = busio.I2C(board.SCL, board.SDA) 183 lcd = Character_LCD_RGB_I2C(i2c, 16, 2) 184 185 while True: 186 if lcd.down_button: 187 lcd.message = "Down!" 188 189 """ 190 return not self._down_button.value 191 192 @property 193 def right_button(self): 194 """The right button on the RGB Character LCD I2C Shield or Pi plate. 195 196 The following example prints "Right!" to the LCD when the right button is pressed: 197 198 .. code-block:: python 199 200 import board 201 import busio 202 from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C 203 204 i2c = busio.I2C(board.SCL, board.SDA) 205 lcd = Character_LCD_RGB_I2C(i2c, 16, 2) 206 207 while True: 208 if lcd.right_button: 209 lcd.message = "Right!" 210 211 """ 212 return not self._right_button.value 213 214 @property 215 def select_button(self): 216 """The select button on the RGB Character LCD I2C Shield or Pi plate. 217 218 The following example prints "Select!" to the LCD when the select button is pressed: 219 220 .. code-block:: python 221 222 import board 223 import busio 224 from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C 225 226 i2c = busio.I2C(board.SCL, board.SDA) 227 lcd = Character_LCD_RGB_I2C(i2c, 16, 2) 228 229 while True: 230 if lcd.select_button: 231 lcd.message = "Select!" 232 233 """ 234 return not self._select_button.value