├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── Adafruit_CharLCD ├── Adafruit_CharLCD.py └── __init__.py ├── LICENSE ├── README.md ├── examples ├── char_lcd.py ├── char_lcd_backpack.py ├── char_lcd_mcp.py ├── char_lcd_plate.py ├── char_lcd_rgb.py └── char_lcd_rgb_pwm.py ├── ez_setup.py └── setup.py /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Python library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Python/Raspberry Pi projects check these very common issues to ensure they don't apply**: 17 | 18 | - If you are receiving an **ImportError: No module named...** error then a 19 | library the code depends on is not installed. Check the tutorial/guide or 20 | README to ensure you have installed the necessary libraries. Usually the 21 | missing library can be installed with the `pip` tool, but check the tutorial/guide 22 | for the exact command. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and power in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | If you're sure this issue is a defect in the code and checked the steps above 33 | please fill in the following fields to provide enough troubleshooting information. 34 | You may delete the guideline and text above to just leave the following details: 35 | 36 | - Platform/operating system (i.e. Raspberry Pi with Raspbian operating system, 37 | Windows 32-bit, Windows 64-bit, Mac OSX 64-bit, etc.): **INSERT PLATFORM/OPERATING 38 | SYSTEM HERE** 39 | 40 | - Python version (run `python -version` or `python3 -version`): **INSERT PYTHON 41 | VERSION HERE** 42 | 43 | - Error message you are receiving, including any Python exception traces: **INSERT 44 | ERROR MESAGE/EXCEPTION TRACES HERE*** 45 | 46 | - List the steps to reproduce the problem below (if possible attach code or commands 47 | to run): **LIST REPRO STEPS BELOW** 48 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | build 3 | *.egg-info 4 | *.pyc 5 | setuptools-* 6 | -------------------------------------------------------------------------------- /Adafruit_CharLCD/Adafruit_CharLCD.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014 Adafruit Industries 2 | # Author: Tony DiCola 3 | # 4 | # Permission is hereby granted, free of charge, to any person obtaining a copy 5 | # of this software and associated documentation files (the "Software"), to deal 6 | # in the Software without restriction, including without limitation the rights 7 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | # copies of the Software, and to permit persons to whom the Software is 9 | # furnished to do so, subject to the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included in 12 | # all copies or substantial portions of the Software. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | # THE SOFTWARE. 21 | import time 22 | 23 | import Adafruit_GPIO as GPIO 24 | import Adafruit_GPIO.I2C as I2C 25 | import Adafruit_GPIO.MCP230xx as MCP 26 | import Adafruit_GPIO.PWM as PWM 27 | 28 | 29 | # Commands 30 | LCD_CLEARDISPLAY = 0x01 31 | LCD_RETURNHOME = 0x02 32 | LCD_ENTRYMODESET = 0x04 33 | LCD_DISPLAYCONTROL = 0x08 34 | LCD_CURSORSHIFT = 0x10 35 | LCD_FUNCTIONSET = 0x20 36 | LCD_SETCGRAMADDR = 0x40 37 | LCD_SETDDRAMADDR = 0x80 38 | 39 | # Entry flags 40 | LCD_ENTRYRIGHT = 0x00 41 | LCD_ENTRYLEFT = 0x02 42 | LCD_ENTRYSHIFTINCREMENT = 0x01 43 | LCD_ENTRYSHIFTDECREMENT = 0x00 44 | 45 | # Control flags 46 | LCD_DISPLAYON = 0x04 47 | LCD_DISPLAYOFF = 0x00 48 | LCD_CURSORON = 0x02 49 | LCD_CURSOROFF = 0x00 50 | LCD_BLINKON = 0x01 51 | LCD_BLINKOFF = 0x00 52 | 53 | # Move flags 54 | LCD_DISPLAYMOVE = 0x08 55 | LCD_CURSORMOVE = 0x00 56 | LCD_MOVERIGHT = 0x04 57 | LCD_MOVELEFT = 0x00 58 | 59 | # Function set flags 60 | LCD_8BITMODE = 0x10 61 | LCD_4BITMODE = 0x00 62 | LCD_2LINE = 0x08 63 | LCD_1LINE = 0x00 64 | LCD_5x10DOTS = 0x04 65 | LCD_5x8DOTS = 0x00 66 | 67 | # Offset for up to 4 rows. 68 | LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54) 69 | 70 | # Char LCD plate GPIO numbers. 71 | LCD_PLATE_RS = 15 72 | LCD_PLATE_RW = 14 73 | LCD_PLATE_EN = 13 74 | LCD_PLATE_D4 = 12 75 | LCD_PLATE_D5 = 11 76 | LCD_PLATE_D6 = 10 77 | LCD_PLATE_D7 = 9 78 | LCD_PLATE_RED = 6 79 | LCD_PLATE_GREEN = 7 80 | LCD_PLATE_BLUE = 8 81 | 82 | # Char LCD plate button names. 83 | SELECT = 0 84 | RIGHT = 1 85 | DOWN = 2 86 | UP = 3 87 | LEFT = 4 88 | 89 | # Char LCD backpack GPIO numbers. 90 | LCD_BACKPACK_RS = 1 91 | LCD_BACKPACK_EN = 2 92 | LCD_BACKPACK_D4 = 3 93 | LCD_BACKPACK_D5 = 4 94 | LCD_BACKPACK_D6 = 5 95 | LCD_BACKPACK_D7 = 6 96 | LCD_BACKPACK_LITE = 7 97 | 98 | class Adafruit_CharLCD(object): 99 | """Class to represent and interact with an HD44780 character LCD display.""" 100 | 101 | def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, backlight=None, 102 | invert_polarity=True, 103 | enable_pwm=False, 104 | gpio=GPIO.get_platform_gpio(), 105 | pwm=PWM.get_platform_pwm(), 106 | initial_backlight=1.0): 107 | """Initialize the LCD. RS, EN, and D4...D7 parameters should be the pins 108 | connected to the LCD RS, clock enable, and data line 4 through 7 connections. 109 | The LCD will be used in its 4-bit mode so these 6 lines are the only ones 110 | required to use the LCD. You must also pass in the number of columns and 111 | lines on the LCD. 112 | 113 | If you would like to control the backlight, pass in the pin connected to 114 | the backlight with the backlight parameter. The invert_polarity boolean 115 | controls if the backlight is one with a LOW signal or HIGH signal. The 116 | default invert_polarity value is True, i.e. the backlight is on with a 117 | LOW signal. 118 | 119 | You can enable PWM of the backlight pin to have finer control on the 120 | brightness. To enable PWM make sure your hardware supports PWM on the 121 | provided backlight pin and set enable_pwm to True (the default is False). 122 | The appropriate PWM library will be used depending on the platform, but 123 | you can provide an explicit one with the pwm parameter. 124 | 125 | The initial state of the backlight is ON, but you can set it to an 126 | explicit initial state with the initial_backlight parameter (0 is off, 127 | 1 is on/full bright). 128 | 129 | You can optionally pass in an explicit GPIO class, 130 | for example if you want to use an MCP230xx GPIO extender. If you don't 131 | pass in an GPIO instance, the default GPIO for the running platform will 132 | be used. 133 | """ 134 | # Save column and line state. 135 | self._cols = cols 136 | self._lines = lines 137 | # Save GPIO state and pin numbers. 138 | self._gpio = gpio 139 | self._rs = rs 140 | self._en = en 141 | self._d4 = d4 142 | self._d5 = d5 143 | self._d6 = d6 144 | self._d7 = d7 145 | # Save backlight state. 146 | self._backlight = backlight 147 | self._pwm_enabled = enable_pwm 148 | self._pwm = pwm 149 | self._blpol = not invert_polarity 150 | # Setup all pins as outputs. 151 | for pin in (rs, en, d4, d5, d6, d7): 152 | gpio.setup(pin, GPIO.OUT) 153 | # Setup backlight. 154 | if backlight is not None: 155 | if enable_pwm: 156 | pwm.start(backlight, self._pwm_duty_cycle(initial_backlight)) 157 | else: 158 | gpio.setup(backlight, GPIO.OUT) 159 | gpio.output(backlight, self._blpol if initial_backlight else not self._blpol) 160 | # Initialize the display. 161 | self.write8(0x33) 162 | self.write8(0x32) 163 | # Initialize display control, function, and mode registers. 164 | self.displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF 165 | self.displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_2LINE | LCD_5x8DOTS 166 | self.displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT 167 | # Write registers. 168 | self.write8(LCD_DISPLAYCONTROL | self.displaycontrol) 169 | self.write8(LCD_FUNCTIONSET | self.displayfunction) 170 | self.write8(LCD_ENTRYMODESET | self.displaymode) # set the entry mode 171 | self.clear() 172 | 173 | def home(self): 174 | """Move the cursor back to its home (first line and first column).""" 175 | self.write8(LCD_RETURNHOME) # set cursor position to zero 176 | self._delay_microseconds(3000) # this command takes a long time! 177 | 178 | def clear(self): 179 | """Clear the LCD.""" 180 | self.write8(LCD_CLEARDISPLAY) # command to clear display 181 | self._delay_microseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time 182 | 183 | def set_cursor(self, col, row): 184 | """Move the cursor to an explicit column and row position.""" 185 | # Clamp row to the last row of the display. 186 | if row > self._lines: 187 | row = self._lines - 1 188 | # Set location. 189 | self.write8(LCD_SETDDRAMADDR | (col + LCD_ROW_OFFSETS[row])) 190 | 191 | def enable_display(self, enable): 192 | """Enable or disable the display. Set enable to True to enable.""" 193 | if enable: 194 | self.displaycontrol |= LCD_DISPLAYON 195 | else: 196 | self.displaycontrol &= ~LCD_DISPLAYON 197 | self.write8(LCD_DISPLAYCONTROL | self.displaycontrol) 198 | 199 | def show_cursor(self, show): 200 | """Show or hide the cursor. Cursor is shown if show is True.""" 201 | if show: 202 | self.displaycontrol |= LCD_CURSORON 203 | else: 204 | self.displaycontrol &= ~LCD_CURSORON 205 | self.write8(LCD_DISPLAYCONTROL | self.displaycontrol) 206 | 207 | def blink(self, blink): 208 | """Turn on or off cursor blinking. Set blink to True to enable blinking.""" 209 | if blink: 210 | self.displaycontrol |= LCD_BLINKON 211 | else: 212 | self.displaycontrol &= ~LCD_BLINKON 213 | self.write8(LCD_DISPLAYCONTROL | self.displaycontrol) 214 | 215 | def move_left(self): 216 | """Move display left one position.""" 217 | self.write8(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT) 218 | 219 | def move_right(self): 220 | """Move display right one position.""" 221 | self.write8(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT) 222 | 223 | def set_left_to_right(self): 224 | """Set text direction left to right.""" 225 | self.displaymode |= LCD_ENTRYLEFT 226 | self.write8(LCD_ENTRYMODESET | self.displaymode) 227 | 228 | def set_right_to_left(self): 229 | """Set text direction right to left.""" 230 | self.displaymode &= ~LCD_ENTRYLEFT 231 | self.write8(LCD_ENTRYMODESET | self.displaymode) 232 | 233 | def autoscroll(self, autoscroll): 234 | """Autoscroll will 'right justify' text from the cursor if set True, 235 | otherwise it will 'left justify' the text. 236 | """ 237 | if autoscroll: 238 | self.displaymode |= LCD_ENTRYSHIFTINCREMENT 239 | else: 240 | self.displaymode &= ~LCD_ENTRYSHIFTINCREMENT 241 | self.write8(LCD_ENTRYMODESET | self.displaymode) 242 | 243 | def message(self, text): 244 | """Write text to display. Note that text can include newlines.""" 245 | line = 0 246 | # Iterate through each character. 247 | for char in text: 248 | # Advance to next line if character is a new line. 249 | if char == '\n': 250 | line += 1 251 | # Move to left or right side depending on text direction. 252 | col = 0 if self.displaymode & LCD_ENTRYLEFT > 0 else self._cols-1 253 | self.set_cursor(col, line) 254 | # Write the character to the display. 255 | else: 256 | self.write8(ord(char), True) 257 | 258 | def set_backlight(self, backlight): 259 | """Enable or disable the backlight. If PWM is not enabled (default), a 260 | non-zero backlight value will turn on the backlight and a zero value will 261 | turn it off. If PWM is enabled, backlight can be any value from 0.0 to 262 | 1.0, with 1.0 being full intensity backlight. 263 | """ 264 | if self._backlight is not None: 265 | if self._pwm_enabled: 266 | self._pwm.set_duty_cycle(self._backlight, self._pwm_duty_cycle(backlight)) 267 | else: 268 | self._gpio.output(self._backlight, self._blpol if backlight else not self._blpol) 269 | 270 | def write8(self, value, char_mode=False): 271 | """Write 8-bit value in character or data mode. Value should be an int 272 | value from 0-255, and char_mode is True if character data or False if 273 | non-character data (default). 274 | """ 275 | # One millisecond delay to prevent writing too quickly. 276 | self._delay_microseconds(1000) 277 | # Set character / data bit. 278 | self._gpio.output(self._rs, char_mode) 279 | # Write upper 4 bits. 280 | self._gpio.output_pins({ self._d4: ((value >> 4) & 1) > 0, 281 | self._d5: ((value >> 5) & 1) > 0, 282 | self._d6: ((value >> 6) & 1) > 0, 283 | self._d7: ((value >> 7) & 1) > 0 }) 284 | self._pulse_enable() 285 | # Write lower 4 bits. 286 | self._gpio.output_pins({ self._d4: (value & 1) > 0, 287 | self._d5: ((value >> 1) & 1) > 0, 288 | self._d6: ((value >> 2) & 1) > 0, 289 | self._d7: ((value >> 3) & 1) > 0 }) 290 | self._pulse_enable() 291 | 292 | def create_char(self, location, pattern): 293 | """Fill one of the first 8 CGRAM locations with custom characters. 294 | The location parameter should be between 0 and 7 and pattern should 295 | provide an array of 8 bytes containing the pattern. E.g. you can easyly 296 | design your custom character at http://www.quinapalus.com/hd44780udg.html 297 | To show your custom character use eg. lcd.message('\x01') 298 | """ 299 | # only position 0..7 are allowed 300 | location &= 0x7 301 | self.write8(LCD_SETCGRAMADDR | (location << 3)) 302 | for i in range(8): 303 | self.write8(pattern[i], char_mode=True) 304 | 305 | def _delay_microseconds(self, microseconds): 306 | # Busy wait in loop because delays are generally very short (few microseconds). 307 | end = time.time() + (microseconds/1000000.0) 308 | while time.time() < end: 309 | pass 310 | 311 | def _pulse_enable(self): 312 | # Pulse the clock enable line off, on, off to send command. 313 | self._gpio.output(self._en, False) 314 | self._delay_microseconds(1) # 1 microsecond pause - enable pulse must be > 450ns 315 | self._gpio.output(self._en, True) 316 | self._delay_microseconds(1) # 1 microsecond pause - enable pulse must be > 450ns 317 | self._gpio.output(self._en, False) 318 | self._delay_microseconds(1) # commands need > 37us to settle 319 | 320 | def _pwm_duty_cycle(self, intensity): 321 | # Convert intensity value of 0.0 to 1.0 to a duty cycle of 0.0 to 100.0 322 | intensity = 100.0*intensity 323 | # Invert polarity if required. 324 | if not self._blpol: 325 | intensity = 100.0-intensity 326 | return intensity 327 | 328 | 329 | class Adafruit_RGBCharLCD(Adafruit_CharLCD): 330 | """Class to represent and interact with an HD44780 character LCD display with 331 | an RGB backlight.""" 332 | 333 | def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, red, green, blue, 334 | gpio=GPIO.get_platform_gpio(), 335 | invert_polarity=True, 336 | enable_pwm=False, 337 | pwm=PWM.get_platform_pwm(), 338 | initial_color=(1.0, 1.0, 1.0)): 339 | """Initialize the LCD with RGB backlight. RS, EN, and D4...D7 parameters 340 | should be the pins connected to the LCD RS, clock enable, and data line 341 | 4 through 7 connections. The LCD will be used in its 4-bit mode so these 342 | 6 lines are the only ones required to use the LCD. You must also pass in 343 | the number of columns and lines on the LCD. 344 | 345 | The red, green, and blue parameters define the pins which are connected 346 | to the appropriate backlight LEDs. The invert_polarity parameter is a 347 | boolean that controls if the LEDs are on with a LOW or HIGH signal. By 348 | default invert_polarity is True, i.e. the backlight LEDs are on with a 349 | low signal. If you want to enable PWM on the backlight LEDs (for finer 350 | control of colors) and the hardware supports PWM on the provided pins, 351 | set enable_pwm to True. Finally you can set an explicit initial backlight 352 | color with the initial_color parameter. The default initial color is 353 | white (all LEDs lit). 354 | 355 | You can optionally pass in an explicit GPIO class, 356 | for example if you want to use an MCP230xx GPIO extender. If you don't 357 | pass in an GPIO instance, the default GPIO for the running platform will 358 | be used. 359 | """ 360 | super(Adafruit_RGBCharLCD, self).__init__(rs, en, d4, d5, d6, d7, 361 | cols, 362 | lines, 363 | enable_pwm=enable_pwm, 364 | backlight=None, 365 | invert_polarity=invert_polarity, 366 | gpio=gpio, 367 | pwm=pwm) 368 | self._red = red 369 | self._green = green 370 | self._blue = blue 371 | # Setup backlight pins. 372 | if enable_pwm: 373 | # Determine initial backlight duty cycles. 374 | rdc, gdc, bdc = self._rgb_to_duty_cycle(initial_color) 375 | pwm.start(red, rdc) 376 | pwm.start(green, gdc) 377 | pwm.start(blue, bdc) 378 | else: 379 | gpio.setup(red, GPIO.OUT) 380 | gpio.setup(green, GPIO.OUT) 381 | gpio.setup(blue, GPIO.OUT) 382 | self._gpio.output_pins(self._rgb_to_pins(initial_color)) 383 | 384 | def _rgb_to_duty_cycle(self, rgb): 385 | # Convert tuple of RGB 0-1 values to tuple of duty cycles (0-100). 386 | red, green, blue = rgb 387 | # Clamp colors between 0.0 and 1.0 388 | red = max(0.0, min(1.0, red)) 389 | green = max(0.0, min(1.0, green)) 390 | blue = max(0.0, min(1.0, blue)) 391 | return (self._pwm_duty_cycle(red), 392 | self._pwm_duty_cycle(green), 393 | self._pwm_duty_cycle(blue)) 394 | 395 | def _rgb_to_pins(self, rgb): 396 | # Convert tuple of RGB 0-1 values to dict of pin values. 397 | red, green, blue = rgb 398 | return { self._red: self._blpol if red else not self._blpol, 399 | self._green: self._blpol if green else not self._blpol, 400 | self._blue: self._blpol if blue else not self._blpol } 401 | 402 | def set_color(self, red, green, blue): 403 | """Set backlight color to provided red, green, and blue values. If PWM 404 | is enabled then color components can be values from 0.0 to 1.0, otherwise 405 | components should be zero for off and non-zero for on. 406 | """ 407 | if self._pwm_enabled: 408 | # Set duty cycle of PWM pins. 409 | rdc, gdc, bdc = self._rgb_to_duty_cycle((red, green, blue)) 410 | self._pwm.set_duty_cycle(self._red, rdc) 411 | self._pwm.set_duty_cycle(self._green, gdc) 412 | self._pwm.set_duty_cycle(self._blue, bdc) 413 | else: 414 | # Set appropriate backlight pins based on polarity and enabled colors. 415 | self._gpio.output_pins({self._red: self._blpol if red else not self._blpol, 416 | self._green: self._blpol if green else not self._blpol, 417 | self._blue: self._blpol if blue else not self._blpol }) 418 | 419 | def set_backlight(self, backlight): 420 | """Enable or disable the backlight. If PWM is not enabled (default), a 421 | non-zero backlight value will turn on the backlight and a zero value will 422 | turn it off. If PWM is enabled, backlight can be any value from 0.0 to 423 | 1.0, with 1.0 being full intensity backlight. On an RGB display this 424 | function will set the backlight to all white. 425 | """ 426 | self.set_color(backlight, backlight, backlight) 427 | 428 | 429 | 430 | class Adafruit_CharLCDPlate(Adafruit_RGBCharLCD): 431 | """Class to represent and interact with an Adafruit Raspberry Pi character 432 | LCD plate.""" 433 | 434 | def __init__(self, address=0x20, busnum=I2C.get_default_bus(), cols=16, lines=2): 435 | """Initialize the character LCD plate. Can optionally specify a separate 436 | I2C address or bus number, but the defaults should suffice for most needs. 437 | Can also optionally specify the number of columns and lines on the LCD 438 | (default is 16x2). 439 | """ 440 | # Configure MCP23017 device. 441 | self._mcp = MCP.MCP23017(address=address, busnum=busnum) 442 | # Set LCD R/W pin to low for writing only. 443 | self._mcp.setup(LCD_PLATE_RW, GPIO.OUT) 444 | self._mcp.output(LCD_PLATE_RW, GPIO.LOW) 445 | # Set buttons as inputs with pull-ups enabled. 446 | for button in (SELECT, RIGHT, DOWN, UP, LEFT): 447 | self._mcp.setup(button, GPIO.IN) 448 | self._mcp.pullup(button, True) 449 | # Initialize LCD (with no PWM support). 450 | super(Adafruit_CharLCDPlate, self).__init__(LCD_PLATE_RS, LCD_PLATE_EN, 451 | LCD_PLATE_D4, LCD_PLATE_D5, LCD_PLATE_D6, LCD_PLATE_D7, cols, lines, 452 | LCD_PLATE_RED, LCD_PLATE_GREEN, LCD_PLATE_BLUE, enable_pwm=False, 453 | gpio=self._mcp) 454 | 455 | def is_pressed(self, button): 456 | """Return True if the provided button is pressed, False otherwise.""" 457 | if button not in set((SELECT, RIGHT, DOWN, UP, LEFT)): 458 | raise ValueError('Unknown button, must be SELECT, RIGHT, DOWN, UP, or LEFT.') 459 | return self._mcp.input(button) == GPIO.LOW 460 | 461 | 462 | class Adafruit_CharLCDBackpack(Adafruit_CharLCD): 463 | """Class to represent and interact with an Adafruit I2C / SPI 464 | LCD backpack using I2C.""" 465 | 466 | def __init__(self, address=0x20, busnum=I2C.get_default_bus(), cols=16, lines=2): 467 | """Initialize the character LCD plate. Can optionally specify a separate 468 | I2C address or bus number, but the defaults should suffice for most needs. 469 | Can also optionally specify the number of columns and lines on the LCD 470 | (default is 16x2). 471 | """ 472 | # Configure the MCP23008 device. 473 | self._mcp = MCP.MCP23008(address=address, busnum=busnum) 474 | # Initialize LCD (with no PWM support). 475 | super(Adafruit_CharLCDBackpack, self).__init__(LCD_BACKPACK_RS, LCD_BACKPACK_EN, 476 | LCD_BACKPACK_D4, LCD_BACKPACK_D5, LCD_BACKPACK_D6, LCD_BACKPACK_D7, 477 | cols, lines, LCD_BACKPACK_LITE, enable_pwm=False, gpio=self._mcp) -------------------------------------------------------------------------------- /Adafruit_CharLCD/__init__.py: -------------------------------------------------------------------------------- 1 | from .Adafruit_CharLCD import * 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 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 all 13 | 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 THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | DEPRECATED LIBRARY. Adafruit Python CharLCD 2 | ======================= 3 | 4 | This library has been deprecated! We are leaving this up for historical and research purposes but archiving the repository. 5 | 6 | We are now only supporting the use of our CircuitPython libraries for use with Python. 7 | 8 | Check out this guide for info on using character LCDs with the CircuitPython library: https://learn.adafruit.com/character-lcds/python-circuitpython 9 | 10 | 11 | **Adafruit_Python_CharLCD** 12 | 13 | Python library for accessing Adafruit character LCDs from a Raspberry Pi or BeagleBone Black. 14 | 15 | Designed specifically to work with the Adafruit character LCDs ----> https://learn.adafruit.com/character-lcds/overview 16 | 17 | For all platforms (Raspberry Pi and Beaglebone Black) make sure you have the following dependencies: 18 | 19 | ```` 20 | sudo apt-get update 21 | sudo apt-get install build-essential python-dev python-smbus python-pip 22 | ```` 23 | 24 | For a Raspberry Pi make sure you have the RPi.GPIO library by executing: 25 | 26 | ```` 27 | sudo pip install RPi.GPIO 28 | ```` 29 | 30 | For a BeagleBone Black make sure you have the Adafruit_BBIO library by executing: 31 | 32 | ```` 33 | sudo pip install Adafruit_BBIO 34 | ```` 35 | 36 | Install the library by downloading with the download link on the right, unzipping the archive, navigating inside the library's directory and executing: 37 | 38 | ```` 39 | sudo python setup.py install 40 | ```` 41 | 42 | See example of usage in the examples folder. 43 | 44 | Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! 45 | 46 | Written by Tony DiCola for Adafruit Industries. 47 | 48 | MIT license, all text above must be included in any redistribution 49 | -------------------------------------------------------------------------------- /examples/char_lcd.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # Example using a character LCD connected to a Raspberry Pi or BeagleBone Black. 3 | import time 4 | 5 | import Adafruit_CharLCD as LCD 6 | 7 | 8 | # Raspberry Pi pin configuration: 9 | lcd_rs = 27 # Note this might need to be changed to 21 for older revision Pi's. 10 | lcd_en = 22 11 | lcd_d4 = 25 12 | lcd_d5 = 24 13 | lcd_d6 = 23 14 | lcd_d7 = 18 15 | lcd_backlight = 4 16 | 17 | # BeagleBone Black configuration: 18 | # lcd_rs = 'P8_8' 19 | # lcd_en = 'P8_10' 20 | # lcd_d4 = 'P8_18' 21 | # lcd_d5 = 'P8_16' 22 | # lcd_d6 = 'P8_14' 23 | # lcd_d7 = 'P8_12' 24 | # lcd_backlight = 'P8_7' 25 | 26 | # Define LCD column and row size for 16x2 LCD. 27 | lcd_columns = 16 28 | lcd_rows = 2 29 | 30 | # Alternatively specify a 20x4 LCD. 31 | # lcd_columns = 20 32 | # lcd_rows = 4 33 | 34 | # Initialize the LCD using the pins above. 35 | lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, 36 | lcd_columns, lcd_rows, lcd_backlight) 37 | 38 | # Print a two line message 39 | lcd.message('Hello\nworld!') 40 | 41 | # Wait 5 seconds 42 | time.sleep(5.0) 43 | 44 | # Demo showing the cursor. 45 | lcd.clear() 46 | lcd.show_cursor(True) 47 | lcd.message('Show cursor') 48 | 49 | time.sleep(5.0) 50 | 51 | # Demo showing the blinking cursor. 52 | lcd.clear() 53 | lcd.blink(True) 54 | lcd.message('Blink cursor') 55 | 56 | time.sleep(5.0) 57 | 58 | # Stop blinking and showing cursor. 59 | lcd.show_cursor(False) 60 | lcd.blink(False) 61 | 62 | # Demo scrolling message right/left. 63 | lcd.clear() 64 | message = 'Scroll' 65 | lcd.message(message) 66 | for i in range(lcd_columns-len(message)): 67 | time.sleep(0.5) 68 | lcd.move_right() 69 | for i in range(lcd_columns-len(message)): 70 | time.sleep(0.5) 71 | lcd.move_left() 72 | 73 | # Demo turning backlight off and on. 74 | lcd.clear() 75 | lcd.message('Flash backlight\nin 5 seconds...') 76 | time.sleep(5.0) 77 | # Turn backlight off. 78 | lcd.set_backlight(0) 79 | time.sleep(2.0) 80 | # Change message. 81 | lcd.clear() 82 | lcd.message('Goodbye!') 83 | # Turn backlight on. 84 | lcd.set_backlight(1) 85 | -------------------------------------------------------------------------------- /examples/char_lcd_backpack.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # Example using a character LCD backpack. 3 | import time 4 | 5 | import Adafruit_CharLCD as LCD 6 | 7 | # Define LCD column and row size for 16x2 LCD. 8 | lcd_columns = 16 9 | lcd_rows = 2 10 | 11 | # Initialize the LCD using the pins 12 | lcd = LCD.Adafruit_CharLCDBackpack() 13 | 14 | # Turn backlight on 15 | lcd.set_backlight(0) 16 | 17 | # Print a two line message 18 | lcd.message('Hello\nworld!') 19 | 20 | # Wait 5 seconds 21 | time.sleep(5.0) 22 | 23 | # Demo showing the cursor. 24 | lcd.clear() 25 | lcd.show_cursor(True) 26 | lcd.message('Show cursor') 27 | 28 | time.sleep(5.0) 29 | 30 | # Demo showing the blinking cursor. 31 | lcd.clear() 32 | lcd.blink(True) 33 | lcd.message('Blink cursor') 34 | 35 | time.sleep(5.0) 36 | 37 | # Stop blinking and showing cursor. 38 | lcd.show_cursor(False) 39 | lcd.blink(False) 40 | 41 | # Demo scrolling message right/left. 42 | lcd.clear() 43 | message = 'Scroll' 44 | lcd.message(message) 45 | for i in range(lcd_columns-len(message)): 46 | time.sleep(0.5) 47 | lcd.move_right() 48 | for i in range(lcd_columns-len(message)): 49 | time.sleep(0.5) 50 | lcd.move_left() 51 | 52 | # Demo turning backlight off and on. 53 | lcd.clear() 54 | lcd.message('Flash backlight\nin 5 seconds...') 55 | time.sleep(5.0) 56 | # Turn backlight off. 57 | lcd.set_backlight(1) 58 | time.sleep(2.0) 59 | # Change message. 60 | lcd.clear() 61 | lcd.message('Goodbye!') 62 | # Turn backlight on. 63 | lcd.set_backlight(0) -------------------------------------------------------------------------------- /examples/char_lcd_mcp.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # Example using an RGB character LCD connected to an MCP23017 GPIO extender. 3 | import time 4 | 5 | import Adafruit_CharLCD as LCD 6 | import Adafruit_GPIO.MCP230xx as MCP 7 | 8 | 9 | # Define MCP pins connected to the LCD. 10 | lcd_rs = 0 11 | lcd_en = 1 12 | lcd_d4 = 2 13 | lcd_d5 = 3 14 | lcd_d6 = 4 15 | lcd_d7 = 5 16 | lcd_red = 6 17 | lcd_green = 7 18 | lcd_blue = 8 19 | 20 | # Define LCD column and row size for 16x2 LCD. 21 | lcd_columns = 16 22 | lcd_rows = 2 23 | 24 | # Alternatively specify a 20x4 LCD. 25 | # lcd_columns = 20 26 | # lcd_rows = 4 27 | 28 | # Initialize MCP23017 device using its default 0x20 I2C address. 29 | gpio = MCP.MCP23017() 30 | 31 | # Alternatively you can initialize the MCP device on another I2C address or bus. 32 | # gpio = MCP.MCP23017(0x24, busnum=1) 33 | 34 | # Initialize the LCD using the pins 35 | lcd = LCD.Adafruit_RGBCharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, 36 | lcd_columns, lcd_rows, lcd_red, lcd_green, lcd_blue, 37 | gpio=gpio) 38 | 39 | # Print a two line message 40 | lcd.message('Hello\nworld!') 41 | 42 | # Wait 5 seconds 43 | time.sleep(5.0) 44 | 45 | # Demo showing the cursor. 46 | lcd.clear() 47 | lcd.show_cursor(True) 48 | lcd.message('Show cursor') 49 | 50 | time.sleep(5.0) 51 | 52 | # Demo showing the blinking cursor. 53 | lcd.clear() 54 | lcd.blink(True) 55 | lcd.message('Blink cursor') 56 | 57 | time.sleep(5.0) 58 | 59 | # Stop blinking and showing cursor. 60 | lcd.show_cursor(False) 61 | lcd.blink(False) 62 | 63 | # Demo scrolling message right/left. 64 | lcd.clear() 65 | message = 'Scroll' 66 | lcd.message(message) 67 | for i in range(lcd_columns-len(message)): 68 | time.sleep(0.5) 69 | lcd.move_right() 70 | for i in range(lcd_columns-len(message)): 71 | time.sleep(0.5) 72 | lcd.move_left() 73 | 74 | # Demo turning backlight off and on. 75 | lcd.clear() 76 | lcd.message('Flash backlight\nin 5 seconds...') 77 | time.sleep(5.0) 78 | # Turn backlight off. 79 | lcd.set_backlight(0) 80 | time.sleep(2.0) 81 | # Change message. 82 | lcd.clear() 83 | lcd.message('Goodbye!') 84 | # Turn backlight on. 85 | lcd.set_backlight(1) 86 | -------------------------------------------------------------------------------- /examples/char_lcd_plate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # Example using a character LCD plate. 3 | import time 4 | 5 | import Adafruit_CharLCD as LCD 6 | 7 | 8 | # Initialize the LCD using the pins 9 | lcd = LCD.Adafruit_CharLCDPlate() 10 | 11 | # create some custom characters 12 | lcd.create_char(1, [2, 3, 2, 2, 14, 30, 12, 0]) 13 | lcd.create_char(2, [0, 1, 3, 22, 28, 8, 0, 0]) 14 | lcd.create_char(3, [0, 14, 21, 23, 17, 14, 0, 0]) 15 | lcd.create_char(4, [31, 17, 10, 4, 10, 17, 31, 0]) 16 | lcd.create_char(5, [8, 12, 10, 9, 10, 12, 8, 0]) 17 | lcd.create_char(6, [2, 6, 10, 18, 10, 6, 2, 0]) 18 | lcd.create_char(7, [31, 17, 21, 21, 21, 21, 17, 31]) 19 | 20 | # Show some basic colors. 21 | lcd.set_color(1.0, 0.0, 0.0) 22 | lcd.clear() 23 | lcd.message('RED \x01') 24 | time.sleep(3.0) 25 | 26 | lcd.set_color(0.0, 1.0, 0.0) 27 | lcd.clear() 28 | lcd.message('GREEN \x02') 29 | time.sleep(3.0) 30 | 31 | lcd.set_color(0.0, 0.0, 1.0) 32 | lcd.clear() 33 | lcd.message('BLUE \x03') 34 | time.sleep(3.0) 35 | 36 | lcd.set_color(1.0, 1.0, 0.0) 37 | lcd.clear() 38 | lcd.message('YELLOW \x04') 39 | time.sleep(3.0) 40 | 41 | lcd.set_color(0.0, 1.0, 1.0) 42 | lcd.clear() 43 | lcd.message('CYAN \x05') 44 | time.sleep(3.0) 45 | 46 | lcd.set_color(1.0, 0.0, 1.0) 47 | lcd.clear() 48 | lcd.message('MAGENTA \x06') 49 | time.sleep(3.0) 50 | 51 | lcd.set_color(1.0, 1.0, 1.0) 52 | lcd.clear() 53 | lcd.message('WHITE \x07') 54 | time.sleep(3.0) 55 | 56 | # Show button state. 57 | lcd.clear() 58 | lcd.message('Press buttons...') 59 | 60 | # Make list of button value, text, and backlight color. 61 | buttons = ( (LCD.SELECT, 'Select', (1,1,1)), 62 | (LCD.LEFT, 'Left' , (1,0,0)), 63 | (LCD.UP, 'Up' , (0,0,1)), 64 | (LCD.DOWN, 'Down' , (0,1,0)), 65 | (LCD.RIGHT, 'Right' , (1,0,1)) ) 66 | 67 | print('Press Ctrl-C to quit.') 68 | while True: 69 | # Loop through each button and check if it is pressed. 70 | for button in buttons: 71 | if lcd.is_pressed(button[0]): 72 | # Button is pressed, change the message and backlight. 73 | lcd.clear() 74 | lcd.message(button[1]) 75 | lcd.set_color(button[2][0], button[2][1], button[2][2]) 76 | -------------------------------------------------------------------------------- /examples/char_lcd_rgb.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # Example using an RGB character LCD wired directly to Raspberry Pi or BeagleBone Black. 3 | import time 4 | 5 | import Adafruit_CharLCD as LCD 6 | 7 | 8 | # Raspberry Pi configuration: 9 | lcd_rs = 27 # Change this to pin 21 on older revision Raspberry Pi's 10 | lcd_en = 22 11 | lcd_d4 = 25 12 | lcd_d5 = 24 13 | lcd_d6 = 23 14 | lcd_d7 = 18 15 | lcd_red = 4 16 | lcd_green = 17 17 | lcd_blue = 7 # Pin 7 is CE1 18 | 19 | # BeagleBone Black configuration: 20 | # lcd_rs = 'P8_8' 21 | # lcd_en = 'P8_10' 22 | # lcd_d4 = 'P8_18' 23 | # lcd_d5 = 'P8_16' 24 | # lcd_d6 = 'P8_14' 25 | # lcd_d7 = 'P8_12' 26 | # lcd_red = 'P8_7' 27 | # lcd_green = 'P8_9' 28 | # lcd_blue = 'P8_11' 29 | 30 | # Define LCD column and row size for 16x2 LCD. 31 | lcd_columns = 16 32 | lcd_rows = 2 33 | 34 | # Alternatively specify a 20x4 LCD. 35 | # lcd_columns = 20 36 | # lcd_rows = 4 37 | 38 | # Initialize the LCD using the pins above. 39 | lcd = LCD.Adafruit_RGBCharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, 40 | lcd_columns, lcd_rows, lcd_red, lcd_green, lcd_blue) 41 | 42 | # Show some basic colors. 43 | lcd.set_color(1.0, 0.0, 0.0) 44 | lcd.clear() 45 | lcd.message('RED') 46 | time.sleep(3.0) 47 | 48 | lcd.set_color(0.0, 1.0, 0.0) 49 | lcd.clear() 50 | lcd.message('GREEN') 51 | time.sleep(3.0) 52 | 53 | lcd.set_color(0.0, 0.0, 1.0) 54 | lcd.clear() 55 | lcd.message('BLUE') 56 | time.sleep(3.0) 57 | 58 | lcd.set_color(1.0, 1.0, 0.0) 59 | lcd.clear() 60 | lcd.message('YELLOW') 61 | time.sleep(3.0) 62 | 63 | lcd.set_color(0.0, 1.0, 1.0) 64 | lcd.clear() 65 | lcd.message('CYAN') 66 | time.sleep(3.0) 67 | 68 | lcd.set_color(1.0, 0.0, 1.0) 69 | lcd.clear() 70 | lcd.message('MAGENTA') 71 | time.sleep(3.0) 72 | 73 | lcd.set_color(1.0, 1.0, 1.0) 74 | lcd.clear() 75 | lcd.message('WHITE') 76 | time.sleep(3.0) 77 | -------------------------------------------------------------------------------- /examples/char_lcd_rgb_pwm.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # Example using an RGB character LCD with PWM control of the backlight. 3 | import math 4 | import time 5 | 6 | import Adafruit_CharLCD as LCD 7 | 8 | 9 | def hsv_to_rgb(hsv): 10 | """Converts a tuple of hue, saturation, value to a tuple of red, green blue. 11 | Hue should be an angle from 0.0 to 359.0. Saturation and value should be a 12 | value from 0.0 to 1.0, where saturation controls the intensity of the hue and 13 | value controls the brightness. 14 | """ 15 | # Algorithm adapted from http://www.cs.rit.edu/~ncs/color/t_convert.html 16 | h, s, v = hsv 17 | if s == 0: 18 | return (v, v, v) 19 | h /= 60.0 20 | i = math.floor(h) 21 | f = h-i 22 | p = v*(1.0-s) 23 | q = v*(1.0-s*f) 24 | t = v*(1.0-s*(1.0-f)) 25 | if i == 0: 26 | return (v, t, p) 27 | elif i == 1: 28 | return (q, v, p) 29 | elif i == 2: 30 | return (p, v, t) 31 | elif i == 3: 32 | return (p, q, v) 33 | elif i == 4: 34 | return (t, p, v) 35 | else: 36 | return (v, p, q) 37 | 38 | # Raspberry Pi configuration: 39 | lcd_rs = 27 # Change this to pin 21 on older revision Raspberry Pi's 40 | lcd_en = 22 41 | lcd_d4 = 25 42 | lcd_d5 = 24 43 | lcd_d6 = 23 44 | lcd_d7 = 18 45 | lcd_red = 4 46 | lcd_green = 17 47 | lcd_blue = 7 # Pin 7 is CE1 48 | 49 | # BeagleBone Black configuration: 50 | # lcd_rs = 'P8_8' 51 | # lcd_en = 'P8_10' 52 | # lcd_d4 = 'P8_18' 53 | # lcd_d5 = 'P8_16' 54 | # lcd_d6 = 'P8_14' 55 | # lcd_d7 = 'P8_12' 56 | # lcd_red = 'P9_16' 57 | # lcd_green = 'P9_14' 58 | # lcd_blue = 'P8_13' 59 | 60 | # Define LCD column and row size for 16x2 LCD. 61 | lcd_columns = 16 62 | lcd_rows = 2 63 | 64 | # Alternatively specify a 20x4 LCD. 65 | # lcd_columns = 20 66 | # lcd_rows = 4 67 | 68 | # Initialize the LCD using the pins 69 | lcd = LCD.Adafruit_RGBCharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, 70 | lcd_columns, lcd_rows, lcd_red, lcd_green, lcd_blue, 71 | enable_pwm=True) 72 | 73 | # Show some basic colors. 74 | lcd.set_color(1.0, 0.0, 0.0) 75 | lcd.clear() 76 | lcd.message('RED') 77 | time.sleep(3.0) 78 | 79 | lcd.set_color(0.0, 1.0, 0.0) 80 | lcd.clear() 81 | lcd.message('GREEN') 82 | time.sleep(3.0) 83 | 84 | lcd.set_color(0.0, 0.0, 1.0) 85 | lcd.clear() 86 | lcd.message('BLUE') 87 | time.sleep(3.0) 88 | 89 | lcd.set_color(1.0, 1.0, 0.0) 90 | lcd.clear() 91 | lcd.message('YELLOW') 92 | time.sleep(3.0) 93 | 94 | lcd.set_color(0.0, 1.0, 1.0) 95 | lcd.clear() 96 | lcd.message('CYAN') 97 | time.sleep(3.0) 98 | 99 | lcd.set_color(1.0, 0.0, 1.0) 100 | lcd.clear() 101 | lcd.message('MAGENTA') 102 | time.sleep(3.0) 103 | 104 | lcd.set_color(1.0, 1.0, 1.0) 105 | lcd.clear() 106 | lcd.message('WHITE') 107 | time.sleep(3.0) 108 | 109 | # Use HSV color space so the hue can be adjusted to see a nice gradient of colors. 110 | # Hue ranges from 0.0 to 359.0, saturation from 0.0 to 1.0, and value from 0.0 to 1.0. 111 | hue = 0.0 112 | saturation = 1.0 113 | value = 1.0 114 | 115 | # Loop through all RGB colors. 116 | lcd.clear() 117 | print('Press Ctrl-C to quit.') 118 | while True: 119 | # Convert HSV to RGB colors. 120 | red, green, blue = hsv_to_rgb((hue, saturation, value)) 121 | # Set backlight color. 122 | lcd.set_color(red, green, blue) 123 | # Print message with RGB values to display. 124 | lcd.set_cursor(0, 0) 125 | lcd.message('RED GREEN BLUE\n{0:0.2f} {1:0.2f} {2:0.2f}'.format(red, green, blue)) 126 | # Increment hue (wrapping around at 360 degrees). 127 | hue += 1.0 128 | if hue > 359.0: 129 | hue = 0.0 130 | -------------------------------------------------------------------------------- /ez_setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Bootstrap setuptools installation 3 | 4 | To use setuptools in your package's setup.py, include this 5 | file in the same directory and add this to the top of your setup.py:: 6 | 7 | from ez_setup import use_setuptools 8 | use_setuptools() 9 | 10 | To require a specific version of setuptools, set a download 11 | mirror, or use an alternate download directory, simply supply 12 | the appropriate options to ``use_setuptools()``. 13 | 14 | This file can also be run as a script to install or upgrade setuptools. 15 | """ 16 | import os 17 | import shutil 18 | import sys 19 | import tempfile 20 | import zipfile 21 | import optparse 22 | import subprocess 23 | import platform 24 | import textwrap 25 | import contextlib 26 | 27 | from distutils import log 28 | 29 | try: 30 | from site import USER_SITE 31 | except ImportError: 32 | USER_SITE = None 33 | 34 | DEFAULT_VERSION = "3.5.1" 35 | DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/" 36 | 37 | def _python_cmd(*args): 38 | """ 39 | Return True if the command succeeded. 40 | """ 41 | args = (sys.executable,) + args 42 | return subprocess.call(args) == 0 43 | 44 | 45 | def _install(archive_filename, install_args=()): 46 | with archive_context(archive_filename): 47 | # installing 48 | log.warn('Installing Setuptools') 49 | if not _python_cmd('setup.py', 'install', *install_args): 50 | log.warn('Something went wrong during the installation.') 51 | log.warn('See the error message above.') 52 | # exitcode will be 2 53 | return 2 54 | 55 | 56 | def _build_egg(egg, archive_filename, to_dir): 57 | with archive_context(archive_filename): 58 | # building an egg 59 | log.warn('Building a Setuptools egg in %s', to_dir) 60 | _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir) 61 | # returning the result 62 | log.warn(egg) 63 | if not os.path.exists(egg): 64 | raise IOError('Could not build the egg.') 65 | 66 | 67 | def get_zip_class(): 68 | """ 69 | Supplement ZipFile class to support context manager for Python 2.6 70 | """ 71 | class ContextualZipFile(zipfile.ZipFile): 72 | def __enter__(self): 73 | return self 74 | def __exit__(self, type, value, traceback): 75 | self.close 76 | return zipfile.ZipFile if hasattr(zipfile.ZipFile, '__exit__') else \ 77 | ContextualZipFile 78 | 79 | 80 | @contextlib.contextmanager 81 | def archive_context(filename): 82 | # extracting the archive 83 | tmpdir = tempfile.mkdtemp() 84 | log.warn('Extracting in %s', tmpdir) 85 | old_wd = os.getcwd() 86 | try: 87 | os.chdir(tmpdir) 88 | with get_zip_class()(filename) as archive: 89 | archive.extractall() 90 | 91 | # going in the directory 92 | subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0]) 93 | os.chdir(subdir) 94 | log.warn('Now working in %s', subdir) 95 | yield 96 | 97 | finally: 98 | os.chdir(old_wd) 99 | shutil.rmtree(tmpdir) 100 | 101 | 102 | def _do_download(version, download_base, to_dir, download_delay): 103 | egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg' 104 | % (version, sys.version_info[0], sys.version_info[1])) 105 | if not os.path.exists(egg): 106 | archive = download_setuptools(version, download_base, 107 | to_dir, download_delay) 108 | _build_egg(egg, archive, to_dir) 109 | sys.path.insert(0, egg) 110 | 111 | # Remove previously-imported pkg_resources if present (see 112 | # https://bitbucket.org/pypa/setuptools/pull-request/7/ for details). 113 | if 'pkg_resources' in sys.modules: 114 | del sys.modules['pkg_resources'] 115 | 116 | import setuptools 117 | setuptools.bootstrap_install_from = egg 118 | 119 | 120 | def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 121 | to_dir=os.curdir, download_delay=15): 122 | to_dir = os.path.abspath(to_dir) 123 | rep_modules = 'pkg_resources', 'setuptools' 124 | imported = set(sys.modules).intersection(rep_modules) 125 | try: 126 | import pkg_resources 127 | except ImportError: 128 | return _do_download(version, download_base, to_dir, download_delay) 129 | try: 130 | pkg_resources.require("setuptools>=" + version) 131 | return 132 | except pkg_resources.DistributionNotFound: 133 | return _do_download(version, download_base, to_dir, download_delay) 134 | except pkg_resources.VersionConflict as VC_err: 135 | if imported: 136 | msg = textwrap.dedent(""" 137 | The required version of setuptools (>={version}) is not available, 138 | and can't be installed while this script is running. Please 139 | install a more recent version first, using 140 | 'easy_install -U setuptools'. 141 | 142 | (Currently using {VC_err.args[0]!r}) 143 | """).format(VC_err=VC_err, version=version) 144 | sys.stderr.write(msg) 145 | sys.exit(2) 146 | 147 | # otherwise, reload ok 148 | del pkg_resources, sys.modules['pkg_resources'] 149 | return _do_download(version, download_base, to_dir, download_delay) 150 | 151 | def _clean_check(cmd, target): 152 | """ 153 | Run the command to download target. If the command fails, clean up before 154 | re-raising the error. 155 | """ 156 | try: 157 | subprocess.check_call(cmd) 158 | except subprocess.CalledProcessError: 159 | if os.access(target, os.F_OK): 160 | os.unlink(target) 161 | raise 162 | 163 | def download_file_powershell(url, target): 164 | """ 165 | Download the file at url to target using Powershell (which will validate 166 | trust). Raise an exception if the command cannot complete. 167 | """ 168 | target = os.path.abspath(target) 169 | cmd = [ 170 | 'powershell', 171 | '-Command', 172 | "(new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)" % vars(), 173 | ] 174 | _clean_check(cmd, target) 175 | 176 | def has_powershell(): 177 | if platform.system() != 'Windows': 178 | return False 179 | cmd = ['powershell', '-Command', 'echo test'] 180 | devnull = open(os.path.devnull, 'wb') 181 | try: 182 | try: 183 | subprocess.check_call(cmd, stdout=devnull, stderr=devnull) 184 | except Exception: 185 | return False 186 | finally: 187 | devnull.close() 188 | return True 189 | 190 | download_file_powershell.viable = has_powershell 191 | 192 | def download_file_curl(url, target): 193 | cmd = ['curl', url, '--silent', '--output', target] 194 | _clean_check(cmd, target) 195 | 196 | def has_curl(): 197 | cmd = ['curl', '--version'] 198 | devnull = open(os.path.devnull, 'wb') 199 | try: 200 | try: 201 | subprocess.check_call(cmd, stdout=devnull, stderr=devnull) 202 | except Exception: 203 | return False 204 | finally: 205 | devnull.close() 206 | return True 207 | 208 | download_file_curl.viable = has_curl 209 | 210 | def download_file_wget(url, target): 211 | cmd = ['wget', url, '--quiet', '--output-document', target] 212 | _clean_check(cmd, target) 213 | 214 | def has_wget(): 215 | cmd = ['wget', '--version'] 216 | devnull = open(os.path.devnull, 'wb') 217 | try: 218 | try: 219 | subprocess.check_call(cmd, stdout=devnull, stderr=devnull) 220 | except Exception: 221 | return False 222 | finally: 223 | devnull.close() 224 | return True 225 | 226 | download_file_wget.viable = has_wget 227 | 228 | def download_file_insecure(url, target): 229 | """ 230 | Use Python to download the file, even though it cannot authenticate the 231 | connection. 232 | """ 233 | try: 234 | from urllib.request import urlopen 235 | except ImportError: 236 | from urllib2 import urlopen 237 | src = dst = None 238 | try: 239 | src = urlopen(url) 240 | # Read/write all in one block, so we don't create a corrupt file 241 | # if the download is interrupted. 242 | data = src.read() 243 | dst = open(target, "wb") 244 | dst.write(data) 245 | finally: 246 | if src: 247 | src.close() 248 | if dst: 249 | dst.close() 250 | 251 | download_file_insecure.viable = lambda: True 252 | 253 | def get_best_downloader(): 254 | downloaders = [ 255 | download_file_powershell, 256 | download_file_curl, 257 | download_file_wget, 258 | download_file_insecure, 259 | ] 260 | 261 | for dl in downloaders: 262 | if dl.viable(): 263 | return dl 264 | 265 | def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 266 | to_dir=os.curdir, delay=15, downloader_factory=get_best_downloader): 267 | """ 268 | Download setuptools from a specified location and return its filename 269 | 270 | `version` should be a valid setuptools version number that is available 271 | as an egg for download under the `download_base` URL (which should end 272 | with a '/'). `to_dir` is the directory where the egg will be downloaded. 273 | `delay` is the number of seconds to pause before an actual download 274 | attempt. 275 | 276 | ``downloader_factory`` should be a function taking no arguments and 277 | returning a function for downloading a URL to a target. 278 | """ 279 | # making sure we use the absolute path 280 | to_dir = os.path.abspath(to_dir) 281 | zip_name = "setuptools-%s.zip" % version 282 | url = download_base + zip_name 283 | saveto = os.path.join(to_dir, zip_name) 284 | if not os.path.exists(saveto): # Avoid repeated downloads 285 | log.warn("Downloading %s", url) 286 | downloader = downloader_factory() 287 | downloader(url, saveto) 288 | return os.path.realpath(saveto) 289 | 290 | def _build_install_args(options): 291 | """ 292 | Build the arguments to 'python setup.py install' on the setuptools package 293 | """ 294 | return ['--user'] if options.user_install else [] 295 | 296 | def _parse_args(): 297 | """ 298 | Parse the command line for options 299 | """ 300 | parser = optparse.OptionParser() 301 | parser.add_option( 302 | '--user', dest='user_install', action='store_true', default=False, 303 | help='install in user site package (requires Python 2.6 or later)') 304 | parser.add_option( 305 | '--download-base', dest='download_base', metavar="URL", 306 | default=DEFAULT_URL, 307 | help='alternative URL from where to download the setuptools package') 308 | parser.add_option( 309 | '--insecure', dest='downloader_factory', action='store_const', 310 | const=lambda: download_file_insecure, default=get_best_downloader, 311 | help='Use internal, non-validating downloader' 312 | ) 313 | parser.add_option( 314 | '--version', help="Specify which version to download", 315 | default=DEFAULT_VERSION, 316 | ) 317 | options, args = parser.parse_args() 318 | # positional arguments are ignored 319 | return options 320 | 321 | def main(): 322 | """Install or upgrade setuptools and EasyInstall""" 323 | options = _parse_args() 324 | archive = download_setuptools( 325 | version=options.version, 326 | download_base=options.download_base, 327 | downloader_factory=options.downloader_factory, 328 | ) 329 | return _install(archive, _build_install_args(options)) 330 | 331 | if __name__ == '__main__': 332 | sys.exit(main()) 333 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | try: 2 | # Try using ez_setup to install setuptools if not already installed. 3 | from ez_setup import use_setuptools 4 | use_setuptools() 5 | except ImportError: 6 | # Ignore import error and assume Python 3 which already has setuptools. 7 | pass 8 | 9 | from setuptools import setup, find_packages 10 | 11 | classifiers = ['Development Status :: 4 - Beta', 12 | 'Operating System :: POSIX :: Linux', 13 | 'License :: OSI Approved :: MIT License', 14 | 'Intended Audience :: Developers', 15 | 'Programming Language :: Python :: 2.7', 16 | 'Programming Language :: Python :: 3', 17 | 'Topic :: Software Development', 18 | 'Topic :: System :: Hardware'] 19 | 20 | setup(name = 'Adafruit_CharLCD', 21 | version = '1.1.1', 22 | author = 'Tony DiCola', 23 | author_email = 'tdicola@adafruit.com', 24 | description = 'Library to drive character LCD display and plate.', 25 | license = 'MIT', 26 | classifiers = classifiers, 27 | url = 'https://github.com/adafruit/Adafruit_Python_CharLCD/', 28 | dependency_links = ['https://github.com/adafruit/Adafruit_Python_GPIO/tarball/master#egg=Adafruit-GPIO-0.4.0'], 29 | install_requires = ['Adafruit-GPIO>=0.4.0'], 30 | packages = find_packages()) 31 | --------------------------------------------------------------------------------