From 3e6a1a64d1ff386cc176598ea6b82d9824c82afd Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 13 Nov 2018 16:01:18 -0500 Subject: [PATCH 01/17] I2C working, SPI needs testing --- adafruit_character_lcd/character_lcd.py | 111 ++-------- adafruit_character_lcd/shift_reg_74hc595.py | 228 ++++++++++---------- 2 files changed, 137 insertions(+), 202 deletions(-) mode change 100644 => 100755 adafruit_character_lcd/character_lcd.py mode change 100644 => 100755 adafruit_character_lcd/shift_reg_74hc595.py diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py old mode 100644 new mode 100755 index 4b5abd9..789b563 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -350,65 +350,21 @@ def create_char(self, location, pattern): #pylint: enable-msg=too-many-instance-attributes -class Character_LCD_I2C(Character_LCD): - """Character LCD connected to I2C/SPI backpack using its I2C connection. - This is a subclass of Character_LCD and implements all of the same - functions and functionality. - """ +class Character_LCD_I2C(Character_LCD): def __init__(self, i2c, cols, lines): - """Initialize character LCD connectedto backpack using I2C connection - on the specified I2C bus and of the specified number of columns and - lines on the display. - """ - # Import the MCP23008 module here when the class is used - # to keep memory usage low. If you attempt to import globally at the - # top of this file you WILL run out of memory on the M0, even with - # MPY files. The amount of code and classes implicitly imported - # by all the SPI and I2C code is too high. Thus import on demand. - import adafruit_character_lcd.mcp23008 as mcp23008 - self._mcp = mcp23008.MCP23008(i2c) - # Setup pins for I2C backpack, see diagram: - # https://learn.adafruit.com/assets/35681 - reset = self._mcp.DigitalInOut(_MCP23008_LCD_RS, self._mcp) - enable = self._mcp.DigitalInOut(_MCP23008_LCD_EN, self._mcp) - dl4 = self._mcp.DigitalInOut(_MCP23008_LCD_D4, self._mcp) - dl5 = self._mcp.DigitalInOut(_MCP23008_LCD_D5, self._mcp) - dl6 = self._mcp.DigitalInOut(_MCP23008_LCD_D6, self._mcp) - dl7 = self._mcp.DigitalInOut(_MCP23008_LCD_D7, self._mcp) - backlight = self._mcp.DigitalInOut(_MCP23008_LCD_BACKLIGHT, self._mcp) - # Call superclass initializer with MCP23008 pins. - super().__init__(reset, enable, dl4, dl5, dl6, dl7, cols, lines, + import adafruit_mcp230xx + self._mcp = adafruit_mcp230xx.MCP23008(i2c) + reset = self._mcp.get_pin(1) + enable = self._mcp.get_pin(2) + d4 = self._mcp.get_pin(3) + d5 = self._mcp.get_pin(4) + d6 = self._mcp.get_pin(5) + d7 = self._mcp.get_pin(6) + backlight = self._mcp.get_pin(7) + super().__init__(reset, enable, d4, d5, d6, d7, cols, lines, backlight=backlight) - def _write8(self, value, char_mode=False): - # Optimize a command write by changing all GPIO pins at once instead - # of letting the super class try to set each one invidually (far too - # slow with overhead of I2C communication). - gpio = self._mcp.gpio - # Make sure enable is low. - gpio = _set_bit(gpio, _MCP23008_LCD_EN, False) - # Set character/data bit. (charmode = False). - gpio = _set_bit(gpio, _MCP23008_LCD_RS, char_mode) - # Set upper 4 bits. - gpio = _set_bit(gpio, _MCP23008_LCD_D4, ((value >> 4) & 1) > 0) - gpio = _set_bit(gpio, _MCP23008_LCD_D5, ((value >> 5) & 1) > 0) - gpio = _set_bit(gpio, _MCP23008_LCD_D6, ((value >> 6) & 1) > 0) - gpio = _set_bit(gpio, _MCP23008_LCD_D7, ((value >> 7) & 1) > 0) - self._mcp.gpio = gpio - # Send command. - self._pulse_enable() - # Now repeat for lower 4 bits. - gpio = self._mcp.gpio - gpio = _set_bit(gpio, _MCP23008_LCD_EN, False) - gpio = _set_bit(gpio, _MCP23008_LCD_RS, char_mode) - gpio = _set_bit(gpio, _MCP23008_LCD_D4, (value & 1) > 0) - gpio = _set_bit(gpio, _MCP23008_LCD_D5, ((value >> 1) & 1) > 0) - gpio = _set_bit(gpio, _MCP23008_LCD_D6, ((value >> 2) & 1) > 0) - gpio = _set_bit(gpio, _MCP23008_LCD_D7, ((value >> 3) & 1) > 0) - self._mcp.gpio = gpio - self._pulse_enable() - class Character_LCD_SPI(Character_LCD): """Character LCD connected to I2C/SPI backpack using its SPI connection. @@ -426,41 +382,12 @@ def __init__(self, spi, latch, cols, lines): self._sr = shift_reg_74hc595.ShiftReg74HC595(spi, latch) # Setup pins for SPI backpack, see diagram: # https://learn.adafruit.com/assets/35681 - reset = self._sr.DigitalInOut(_74HC595_LCD_RS, self._sr) - enable = self._sr.DigitalInOut(_74HC595_LCD_EN, self._sr) - dl4 = self._sr.DigitalInOut(_74HC595_LCD_D4, self._sr) - dl5 = self._sr.DigitalInOut(_74HC595_LCD_D5, self._sr) - dl6 = self._sr.DigitalInOut(_74HC595_LCD_D6, self._sr) - dl7 = self._sr.DigitalInOut(_74HC595_LCD_D7, self._sr) - backlight = self._sr.DigitalInOut(_74HC595_LCD_BACKLIGHT, self._sr) - # Call superclass initializer with shift register pins. - super().__init__(reset, enable, dl4, dl5, dl6, dl7, cols, lines, + reset = self._sr.get_pin(1) + enable = self._sr.get_pin(2) + d4 = self._sr.get_pin(3) + d5 = self._sr.get_pin(4) + d6 = self._sr.get_pin(5) + d7 = self._sr.get_pin(6) + backlight = self._sr.get_pin(7) + super().__init__(reset, enable, d4, d5, d6, d7, cols, lines, backlight=backlight) - - def _write8(self, value, char_mode=False): - # Optimize a command write by changing all GPIO pins at once instead - # of letting the super class try to set each one invidually (far too - # slow with overhead of SPI communication). - gpio = self._sr.gpio - # Make sure enable is low. - gpio = _set_bit(gpio, _74HC595_LCD_EN, False) - # Set character/data bit. (charmode = False). - gpio = _set_bit(gpio, _74HC595_LCD_RS, char_mode) - # Set upper 4 bits. - gpio = _set_bit(gpio, _74HC595_LCD_D4, ((value >> 4) & 1) > 0) - gpio = _set_bit(gpio, _74HC595_LCD_D5, ((value >> 5) & 1) > 0) - gpio = _set_bit(gpio, _74HC595_LCD_D6, ((value >> 6) & 1) > 0) - gpio = _set_bit(gpio, _74HC595_LCD_D7, ((value >> 7) & 1) > 0) - self._sr.gpio = gpio - # Send command. - self._pulse_enable() - # Now repeat for lower 4 bits. - gpio = self._sr.gpio - gpio = _set_bit(gpio, _74HC595_LCD_EN, False) - gpio = _set_bit(gpio, _74HC595_LCD_RS, char_mode) - gpio = _set_bit(gpio, _74HC595_LCD_D4, (value & 1) > 0) - gpio = _set_bit(gpio, _74HC595_LCD_D5, ((value >> 1) & 1) > 0) - gpio = _set_bit(gpio, _74HC595_LCD_D6, ((value >> 2) & 1) > 0) - gpio = _set_bit(gpio, _74HC595_LCD_D7, ((value >> 3) & 1) > 0) - self._sr.gpio = gpio - self._pulse_enable() diff --git a/adafruit_character_lcd/shift_reg_74hc595.py b/adafruit_character_lcd/shift_reg_74hc595.py old mode 100644 new mode 100755 index 292edae..79e7709 --- a/adafruit_character_lcd/shift_reg_74hc595.py +++ b/adafruit_character_lcd/shift_reg_74hc595.py @@ -1,110 +1,118 @@ -""" -`adafruit_character_led.shift_reg_74hc595` -=============================================== - -74HC595 Serial to Paralllel Shift Register Driver -Bare-bones driver for the 74HC595, as used by the character LCD -backpack. This exposes the 74HC595 and its pins as standard CircuitPython -digitalio pins. Currently this is integrated in the character LCD class for -simplicity and reduction in dependent imports, but it could be broken out -into a standalone library later. - -* Author: Tony DiCola -""" -import digitalio - -import adafruit_bus_device.spi_device as spi_device - - -#pylint: disable-msg=too-few-public-methods -#pylint: disable-msg=no-self-use -class ShiftReg74HC595: - """Shift Register 74LS95 driver class""" - class DigitalInOut: - """Digital input/output of the 74HC595. The interface is exactly the - same as the digitalio.DigitalInOut class, however note that by design - this device is OUTPUT ONLY! Attempting to read inputs or set - direction as input will raise an exception. - """ - - def __init__(self, pin_number, shift_reg_74ls595): - """Specify the pin number of the shift register (0...7) and - ShiftReg74HC595 instance. - """ - self._pin = pin_number - self._sr = shift_reg_74ls595 - - # kwargs in switch functions below are _necessary_ for compatibility - # with DigitalInout class (which allows specifying pull, etc. which - # is unused by this class). Do not remove them, instead turn off pylint - # in this case. - #pylint: disable=unused-argument - def switch_to_output(self, value=False, **kwargs): - """DigitalInOut switch_to_output""" - self.direction = digitalio.Direction.OUTPUT - self.value = value - - def switch_to_input(self, **kwargs): - """do not call switch_to_input""" - raise RuntimeError('Unable to use 74HC595 as digital input!') - #pylint: enable=unused-argument - - @property - def value(self): - """do not call value""" - raise RuntimeError('Unable to use 74HC595 as digital input!') - - @value.setter - def value(self, val): - # Only supported operation, writing a digital output. - gpio = self._sr.gpio - if val: - gpio |= (1 << self._pin) - else: - gpio &= ~(1 << self._pin) - self._sr.gpio = gpio - - @property - def direction(self): - """ALWAYS an output!""" - return digitalio.Direction.OUTPUT - - @direction.setter - def direction(self, val): - """Can only be set as OUTPUT!""" - if val != digitalio.Direction.OUTPUT: - raise RuntimeError('Unable to use 74HC595 as digital input!') - - @property - def pull(self): - """Pull-up/down not supported, return NonLiberty e for no pull-up/down.""" - return None - - @pull.setter - def pull(self, val): - """Only supports null/no pull state.""" - if val is not None: - raise RuntimeError('Unable to set 74HC595 pull!') - - - def __init__(self, spi, latch): - self._device = spi_device.SPIDevice(spi, latch, baudrate=1000000) - self._gpio = bytearray(1) - self._gpio[0] = 0x00 - - @property - def gpio(self): - """Get and set the raw GPIO output register. Each bit represents the - output value of the associated pin (0 = low, 1 = high). - """ - return self._gpio[0] - - @gpio.setter - def gpio(self, val): - self._gpio[0] = val & 0xFF - with self._device as spi: - # pylint: disable=no-member - spi.write(self._gpio) - -#pylint: enable-msg=no-self-use -#pylint: enable-msg=too-few-public-methods +""" +`adafruit_character_led.shift_reg_74hc595` +=============================================== + +74HC595 Serial to Paralllel Shift Register Driver +Bare-bones driver for the 74HC595, as used by the character LCD +backpack. This exposes the 74HC595 and its pins as standard CircuitPython +digitalio pins. Currently this is integrated in the character LCD class for +simplicity and reduction in dependent imports, but it could be broken out +into a standalone library later. + +* Author: Tony DiCola +""" +import digitalio + +import adafruit_bus_device.spi_device as spi_device + + +#pylint: disable-msg=too-few-public-methods +#pylint: disable-msg=no-self-use +#class ShiftReg74HC595: +# """Shift Register 74LS95 driver class""" +class DigitalInOut: + """Digital input/output of the 74HC595. The interface is exactly the + same as the digitalio.DigitalInOut class, however note that by design + this device is OUTPUT ONLY! Attempting to read inputs or set + direction as input will raise an exception. + """ + + def __init__(self, pin_number, shift_reg_74ls595): + """Specify the pin number of the shift register (0...7) and + ShiftReg74HC595 instance. + """ + self._pin = pin_number + self._sr = shift_reg_74ls595 + + # kwargs in switch functions below are _necessary_ for compatibility + # with DigitalInout class (which allows specifying pull, etc. which + # is unused by this class). Do not remove them, instead turn off pylint + # in this case. + #pylint: disable=unused-argument + def switch_to_output(self, value=False, **kwargs): + """DigitalInOut switch_to_output""" + self.direction = digitalio.Direction.OUTPUT + self.value = value + + def switch_to_input(self, **kwargs): + """do not call switch_to_input""" + raise RuntimeError('Unable to use 74HC595 as digital input!') + #pylint: enable=unused-argument + + @property + def value(self): + """do not call value""" + raise RuntimeError('Unable to use 74HC595 as digital input!') + + @value.setter + def value(self, val): + # Only supported operation, writing a digital output. + gpio = self._sr.gpio + if val: + gpio |= (1 << self._pin) + else: + gpio &= ~(1 << self._pin) + self._sr.gpio = gpio + + @property + def direction(self): + """ALWAYS an output!""" + return digitalio.Direction.OUTPUT + + @direction.setter + def direction(self, val): + """Can only be set as OUTPUT!""" + if val != digitalio.Direction.OUTPUT: + raise RuntimeError('Unable to use 74HC595 as digital input!') + + @property + def pull(self): + """Pull-up/down not supported, return NonLiberty e for no pull-up/down.""" + return None + + @pull.setter + def pull(self, val): + """Only supports null/no pull state.""" + if val is not None: + raise RuntimeError('Unable to set 74HC595 pull!') + + +class ShiftReg74HC595: + def __init__(self, spi, latch): + self._device = spi_device.SPIDevice(spi, latch, baudrate=1000000) + self._gpio = bytearray(1) + self._gpio[0] = 0x00 + + @property + def gpio(self): + """Get and set the raw GPIO output register. Each bit represents the + output value of the associated pin (0 = low, 1 = high). + """ + return self._gpio[0] + + @gpio.setter + def gpio(self, val): + self._gpio[0] = val & 0xFF + with self._device as spi: + # pylint: disable=no-member + spi.write(self._gpio) + + def get_pin(self, pin): + """Convenience function to create an instance of the DigitalInOut class + pointing at the specified pin of this MCP23008 device. + """ + assert 0 <= pin <= 7 + return DigitalInOut(pin, self) + +#pylint: enable-msg=no-self-use +#pylint: enable-msg=too-few-public-methods From 9027f16d2de10a05d6cae88b41c2f4171fbc7223 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 13 Nov 2018 16:13:17 -0500 Subject: [PATCH 02/17] SPI pin change. --- adafruit_character_lcd/character_lcd.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index 789b563..9e3b51c 100755 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -384,10 +384,10 @@ def __init__(self, spi, latch, cols, lines): # https://learn.adafruit.com/assets/35681 reset = self._sr.get_pin(1) enable = self._sr.get_pin(2) - d4 = self._sr.get_pin(3) - d5 = self._sr.get_pin(4) - d6 = self._sr.get_pin(5) - d7 = self._sr.get_pin(6) + d4 = self._sr.get_pin(6) + d5 = self._sr.get_pin(5) + d6 = self._sr.get_pin(4) + d7 = self._sr.get_pin(3) backlight = self._sr.get_pin(7) super().__init__(reset, enable, d4, d5, d6, d7, cols, lines, backlight=backlight) From 7fa1519e704ed40654090d5419b4962d8f3a2e4e Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 14 Nov 2018 12:39:22 -0500 Subject: [PATCH 03/17] Add I2C to RGB, get_pin() to 74HC595 Added functionality from monochrome LCD to RGB. --- adafruit_character_lcd/character_lcd.py | 41 ++++-------- adafruit_character_lcd/character_lcd_rgb.py | 74 ++++++++++++++++++--- adafruit_character_lcd/shift_reg_74hc595.py | 6 +- 3 files changed, 80 insertions(+), 41 deletions(-) diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index 9e3b51c..c5e0d69 100755 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -51,7 +51,7 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CharLCD.git" -#pylint: disable-msg=bad-whitespace +# pylint: disable-msg=bad-whitespace # Commands LCD_CLEARDISPLAY = const(0x01) LCD_RETURNHOME = const(0x02) @@ -93,25 +93,8 @@ # Offset for up to 4 rows. LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54) -# MCP23008 I2C backpack pin mapping from LCD logical pin to MCP23008 pin. -_MCP23008_LCD_RS = const(1) -_MCP23008_LCD_EN = const(2) -_MCP23008_LCD_D4 = const(3) -_MCP23008_LCD_D5 = const(4) -_MCP23008_LCD_D6 = const(5) -_MCP23008_LCD_D7 = const(6) -_MCP23008_LCD_BACKLIGHT = const(7) - -# 74HC595 SPI backpack pin mapping from LCD logical pin to 74HC595 pin. -_74HC595_LCD_RS = const(1) -_74HC595_LCD_EN = const(2) -_74HC595_LCD_D4 = const(6) -_74HC595_LCD_D5 = const(5) -_74HC595_LCD_D6 = const(4) -_74HC595_LCD_D7 = const(3) -_74HC595_LCD_BACKLIGHT = const(7) - -#pylint: enable-msg=bad-whitespace +# pylint: enable-msg=bad-whitespace + def _set_bit(byte_value, position, val): # Given the specified byte_value set the bit at position to the provided @@ -124,7 +107,8 @@ def _set_bit(byte_value, position, val): return ret -#pylint: disable-msg=too-many-instance-attributes + +# pylint: disable-msg=too-many-instance-attributes class Character_LCD(object): """ Interfaces with a character LCD @@ -140,12 +124,12 @@ class Character_LCD(object): the last pin. Check with your datasheet """ - #pylint: disable-msg=too-many-arguments + # pylint: disable-msg=too-many-arguments def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, - backlight=None #, - #enable_pwm = False, - #initial_backlight = 1.0 - ): + backlight=None # , + # enable_pwm = False, + # initial_backlight = 1.0 + ): self.cols = cols self.lines = lines @@ -182,7 +166,7 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, # set the entry mode self._write8(LCD_ENTRYMODESET | self.displaymode) self.clear() - #pylint: enable-msg=too-many-arguments + # pylint: enable-msg=too-many-arguments def home(self): """Moves the cursor back home pos(1,1)""" @@ -310,7 +294,6 @@ def set_backlight(self, lighton): else: self.backlight.value = 1 - def message(self, text): """ Write text to display. Can include ``\\n`` for newline. @@ -348,7 +331,7 @@ def create_char(self, location, pattern): for i in range(8): self._write8(pattern[i], char_mode=True) -#pylint: enable-msg=too-many-instance-attributes +# pylint: enable-msg=too-many-instance-attributes class Character_LCD_I2C(Character_LCD): diff --git a/adafruit_character_lcd/character_lcd_rgb.py b/adafruit_character_lcd/character_lcd_rgb.py index d0e4fa6..992c1bc 100755 --- a/adafruit_character_lcd/character_lcd_rgb.py +++ b/adafruit_character_lcd/character_lcd_rgb.py @@ -26,9 +26,10 @@ Character_LCD - module for interfacing with RGB character LCDs * Author(s): - -Brent Rubell - -Asher Lieber - -Tony DiCola for the original python charLCD library + - Kattni Rembor + - Brent Rubell + - Asher Lieber + - Tony DiCola for the original python charLCD library Implementation Notes -------------------- @@ -70,7 +71,7 @@ # Control flags _LCD_DISPLAYON = const(0x04) _LCD_DISPLAYOFF = const(0x00) -LCD_CURSORON = const(0x02) +_LCD_CURSORON = const(0x02) _LCD_CURSOROFF = const(0x00) _LCD_BLINKON = const(0x01) _LCD_BLINKOFF = const(0x00) @@ -90,7 +91,7 @@ _LCD_5X8DOTS = const(0x00) # Offset for up to 4 rows. -LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54) +_LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54) #pylint: enable-msg=bad-whitespace @@ -129,6 +130,7 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, red, green, blue, + read_write=None, backlight=None ): self.cols = cols @@ -142,6 +144,9 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, self.dl6 = d6 self.dl7 = d7 + # Define read_write (rw) pin + self.read_write = read_write + # define backlight pin self.backlight = backlight @@ -149,10 +154,14 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, for pin in(rs, en, d4, d5, d6, d7): pin.direction = digitalio.Direction.OUTPUT + # Setup rw pin if used + if read_write is not None: + self.read_write.direction = digitalio.Direction.OUTPUT + # setup backlight if backlight is not None: self.backlight.direction = digitalio.Direction.OUTPUT - self.backlight.value = 0 # turn backlight on + self.backlight.value = 0 # turn backlight on # define color params self.red = red @@ -201,7 +210,7 @@ def clear(self): def show_cursor(self, show): """Show or hide the cursor""" if show: - self.displaycontrol |= LCD_CURSORON + self.displaycontrol |= _LCD_CURSORON else: self.displaycontrol &= ~_LCD_DISPLAYON self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) @@ -215,7 +224,38 @@ def set_cursor(self, col, row): if row > self.lines: row = self.lines - 1 # Set location - self._write8(_LCD_SETDDRAMADDR | (col + LCD_ROW_OFFSETS[row])) + self._write8(_LCD_SETDDRAMADDR | (col + _LCD_ROW_OFFSETS[row])) + + def blink(self, blink): + """ + Blinks the cursor if blink = true. + + :param blink: True to blink, False no blink + + """ + if blink is True: + self.displaycontrol |= _LCD_BLINKON + else: + self.displaycontrol &= ~_LCD_BLINKON + self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) + + def move_left(self): + """Moves display left one position""" + self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVELEFT) + + def move_right(self): + """Moves display right one position""" + self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVERIGHT) + + def set_left_to_right(self): + """Set direction of text to read from left to right""" + self.displaymode |= _LCD_ENTRYLEFT + self._write8(_LCD_ENTRYMODESET | self.displaymode) + + def set_right_to_left(self): + """Set direction of text to read from right to left""" + self.displaymode |= _LCD_ENTRYLEFT + self._write8(_LCD_ENTRYMODESET | self.displaymode) def enable_display(self, enable): """Enable or disable the display. @@ -303,3 +343,21 @@ def message(self, text): self._write8(ord(char), True) #pylint: enable-msg=too-many-instance-attributes + + +class Character_LCD_I2C_RGB(Character_LCD_RGB): + def __init__(self, i2c, cols, lines): + import adafruit_mcp230xx + self._mcp = adafruit_mcp230xx.MCP23017(i2c) + reset = self._mcp.get_pin(15) + read_write = self._mcp.get_pin(14) + enable = self._mcp.get_pin(13) + d4 = self._mcp.get_pin(12) + d5 = self._mcp.get_pin(11) + d6 = self._mcp.get_pin(10) + d7 = self._mcp.get_pin(9) + red = self._mcp.get_pin(6) + green = self._mcp.get_pin(7) + blue = self._mcp.get_pin(8) + super().__init__(reset, enable, d4, d5, d6, d7, cols, lines, red, green, blue, read_write, + backlight=None) \ No newline at end of file diff --git a/adafruit_character_lcd/shift_reg_74hc595.py b/adafruit_character_lcd/shift_reg_74hc595.py index 79e7709..02b69bd 100755 --- a/adafruit_character_lcd/shift_reg_74hc595.py +++ b/adafruit_character_lcd/shift_reg_74hc595.py @@ -12,14 +12,12 @@ * Author: Tony DiCola """ import digitalio - import adafruit_bus_device.spi_device as spi_device #pylint: disable-msg=too-few-public-methods #pylint: disable-msg=no-self-use -#class ShiftReg74HC595: -# """Shift Register 74LS95 driver class""" + class DigitalInOut: """Digital input/output of the 74HC595. The interface is exactly the same as the digitalio.DigitalInOut class, however note that by design @@ -109,7 +107,7 @@ def gpio(self, val): def get_pin(self, pin): """Convenience function to create an instance of the DigitalInOut class - pointing at the specified pin of this MCP23008 device. + pointing at the specified pin of this 74HC595 device. """ assert 0 <= pin <= 7 return DigitalInOut(pin, self) From 287864b9fa9dcbdce663be4b2288b0c86ca7bed3 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Fri, 16 Nov 2018 20:47:10 -0500 Subject: [PATCH 04/17] Current status: functional. --- adafruit_character_lcd/character_lcd.py | 349 +++++++++++--------- adafruit_character_lcd/character_lcd_rgb.py | 222 +++++++------ adafruit_character_lcd/mcp23008.py | 169 ---------- adafruit_character_lcd/shift_reg_74hc595.py | 116 ------- 4 files changed, 319 insertions(+), 537 deletions(-) delete mode 100644 adafruit_character_lcd/mcp23008.py delete mode 100755 adafruit_character_lcd/shift_reg_74hc595.py diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index c5e0d69..1d310b6 100755 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -1,6 +1,7 @@ # The MIT License (MIT) # # Copyright (c) 2017 Brent Rubell for Adafruit Industries +# Copyright (c) 2018 Kattni Rembor for Adafruit Industries # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -25,7 +26,8 @@ Module for interfacing with monochromatic character LCDs -* Author(s): Brent Rubell, Asher Lieber, Tony DiCola (original python charLCD library) +* Author(s): Kattni Rembor, Brent Rubell, Asher Lieber, + Tony DiCola (original python charLCD library) Implementation Notes -------------------- @@ -37,7 +39,7 @@ **Software and Dependencies:** -* Adafruit CircuitPython firmware (2.2.0+) for the ESP8622 and M0-based boards: +* Adafruit CircuitPython firmware: https://github.com/adafruit/circuitpython/releases * Adafruit's Bus Device library (when using I2C/SPI): https://github.com/adafruit/Adafruit_CircuitPython_BusDevice @@ -47,54 +49,55 @@ import time import digitalio from micropython import const +import adafruit_mcp230xx +import adafruit_74hc595 __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CharLCD.git" -# pylint: disable-msg=bad-whitespace +#pylint: disable-msg=bad-whitespace # Commands -LCD_CLEARDISPLAY = const(0x01) -LCD_RETURNHOME = const(0x02) -LCD_ENTRYMODESET = const(0x04) -LCD_DISPLAYCONTROL = const(0x08) -LCD_CURSORSHIFT = const(0x10) -LCD_FUNCTIONSET = const(0x20) -LCD_SETCGRAMADDR = const(0x40) -LCD_SETDDRAMADDR = const(0x80) +_LCD_CLEARDISPLAY = const(0x01) +_LCD_RETURNHOME = const(0x02) +_LCD_ENTRYMODESET = const(0x04) +_LCD_DISPLAYCONTROL = const(0x08) +_LCD_CURSORSHIFT = const(0x10) +_LCD_FUNCTIONSET = const(0x20) +_LCD_SETCGRAMADDR = const(0x40) +_LCD_SETDDRAMADDR = const(0x80) # Entry flags -LCD_ENTRYRIGHT = const(0x00) -LCD_ENTRYLEFT = const(0x02) -LCD_ENTRYSHIFTINCREMENT = const(0x01) -LCD_ENTRYSHIFTDECREMENT = const(0x00) +_LCD_ENTRYRIGHT = const(0x00) +_LCD_ENTRYLEFT = const(0x02) +_LCD_ENTRYSHIFTINCREMENT = const(0x01) +_LCD_ENTRYSHIFTDECREMENT = const(0x00) # Control flags -LCD_DISPLAYON = const(0x04) -LCD_DISPLAYOFF = const(0x00) -LCD_CURSORON = const(0x02) -LCD_CURSOROFF = const(0x00) -LCD_BLINKON = const(0x01) -LCD_BLINKOFF = const(0x00) +_LCD_DISPLAYON = const(0x04) +_LCD_DISPLAYOFF = const(0x00) +_LCD_CURSORON = const(0x02) +_LCD_CURSOROFF = const(0x00) +_LCD_BLINKON = const(0x01) +_LCD_BLINKOFF = const(0x00) # Move flags -LCD_DISPLAYMOVE = const(0x08) -LCD_CURSORMOVE = const(0x00) -LCD_MOVERIGHT = const(0x04) -LCD_MOVELEFT = const(0x00) +_LCD_DISPLAYMOVE = const(0x08) +_LCD_CURSORMOVE = const(0x00) +_LCD_MOVERIGHT = const(0x04) +_LCD_MOVELEFT = const(0x00) # Function set flags -LCD_8BITMODE = const(0x10) -LCD_4BITMODE = const(0x00) -LCD_2LINE = const(0x08) -LCD_1LINE = const(0x00) -LCD_5X10DOTS = const(0x04) -LCD_5X8DOTS = const(0x00) +_LCD_8BITMODE = const(0x10) +_LCD_4BITMODE = const(0x00) +_LCD_2LINE = const(0x08) +_LCD_1LINE = const(0x00) +_LCD_5X10DOTS = const(0x04) +_LCD_5X8DOTS = const(0x00) # Offset for up to 4 rows. -LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54) - -# pylint: enable-msg=bad-whitespace +_LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54) +#pylint: enable-msg=bad-whitespace def _set_bit(byte_value, position, val): # Given the specified byte_value set the bit at position to the provided @@ -107,8 +110,7 @@ def _set_bit(byte_value, position, val): return ret - -# pylint: disable-msg=too-many-instance-attributes +#pylint: disable-msg=too-many-instance-attributes class Character_LCD(object): """ Interfaces with a character LCD @@ -124,12 +126,11 @@ class Character_LCD(object): the last pin. Check with your datasheet """ - # pylint: disable-msg=too-many-arguments + #pylint: disable-msg=too-many-arguments def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, - backlight=None # , - # enable_pwm = False, - # initial_backlight = 1.0 - ): + backlight_pin=None, + backlight_inverted=False + ): self.cols = cols self.lines = lines @@ -141,70 +142,79 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, self.dl6 = d6 self.dl7 = d7 # backlight pin - self.backlight = backlight + self.backlight_pin = backlight_pin + self.backlight_inverted = backlight_inverted # self.pwn_enabled = enable_pwm # set all pins as outputs for pin in(rs, en, d4, d5, d6, d7): pin.direction = digitalio.Direction.OUTPUT # Setup backlight - if backlight is not None: - self.backlight.direction = digitalio.Direction.OUTPUT - self.backlight.value = 0 # turn backlight on + if backlight_pin is not None: + self.backlight_pin.direction = digitalio.Direction.OUTPUT + if backlight_inverted: + self.backlight_pin.value = 0 # turn backlight on + else: + self.backlight_pin.value = 1 # turn backlight on # initialize the display self._write8(0x33) self._write8(0x32) # init. display control - self.displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF + self.displaycontrol = _LCD_DISPLAYON | _LCD_CURSOROFF | _LCD_BLINKOFF # init display function - self.displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_2LINE | LCD_5X8DOTS + self.displayfunction = _LCD_4BITMODE | _LCD_1LINE | _LCD_2LINE | _LCD_5X8DOTS # init display mode - self.displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT + self.displaymode = _LCD_ENTRYLEFT | _LCD_ENTRYSHIFTDECREMENT # write to display control - self._write8(LCD_DISPLAYCONTROL | self.displaycontrol) + self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) # write displayfunction - self._write8(LCD_FUNCTIONSET | self.displayfunction) + self._write8(_LCD_FUNCTIONSET | self.displayfunction) # set the entry mode - self._write8(LCD_ENTRYMODESET | self.displaymode) + self._write8(_LCD_ENTRYMODESET | self.displaymode) self.clear() - # pylint: enable-msg=too-many-arguments + + self._message = None + #pylint: enable-msg=too-many-arguments def home(self): """Moves the cursor back home pos(1,1)""" - self._write8(LCD_RETURNHOME) + self._write8(_LCD_RETURNHOME) time.sleep(0.003) def clear(self): """Clears the LCD""" - self._write8(LCD_CLEARDISPLAY) + self._write8(_LCD_CLEARDISPLAY) time.sleep(0.003) - def show_cursor(self, show): - """ - Show or hide the cursor - - :param show: True to show cursor, False to hide + @property + def cursor(self): + """True if cursor is visible, otherwise False.""" + return self.displaycontrol & _LCD_CURSORON == _LCD_CURSORON - """ + @cursor.setter + def cursor(self, show): + """True if cursor is visible, otherwise False.""" if show: - self.displaycontrol |= LCD_CURSORON + self.displaycontrol |= _LCD_CURSORON else: - self.displaycontrol &= ~LCD_DISPLAYON - self._write8(LCD_DISPLAYCONTROL | self.displaycontrol) - - def set_cursor(self, col, row): - """ - Sets the cursor to ``row`` and ``col`` - - :param col: column location - :param row: row location + self.displaycontrol &= ~_LCD_CURSORON + self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) + def cursor_position(self, column, row): + """Move the cursor to position ``column`` and ``row`` + :param column: column location + :param row: row location """ # Clamp row to the last row of the display if row > self.lines: row = self.lines - 1 # Set location - self._write8(LCD_SETDDRAMADDR | (col + LCD_ROW_OFFSETS[row])) + self._write8(_LCD_SETDDRAMADDR | (column + _LCD_ROW_OFFSETS[row])) + + @property + def blink(self): + return self.displaycontrol & _LCD_BLINKON == _LCD_BLINKON + @blink.setter def blink(self, blink): """ Blinks the cursor if blink = true. @@ -212,42 +222,118 @@ def blink(self, blink): :param blink: True to blink, False no blink """ - if blink is True: - self.displaycontrol |= LCD_BLINKON + if blink: + self.displaycontrol |= _LCD_BLINKON + else: + self.displaycontrol &= ~_LCD_BLINKON + self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) + + @property + def display(self): + return self.displaycontrol & _LCD_DISPLAYON == _LCD_DISPLAYON + + @display.setter + def display(self, enable): + """ + Enable or disable the display. + + :param enable: True to enable display, False to disable + + """ + if enable: + self.displaycontrol |= _LCD_DISPLAYON else: - self.displaycontrol &= ~LCD_BLINKON - self._write8(LCD_DISPLAYCONTROL | self.displaycontrol) + self.displaycontrol &= ~_LCD_DISPLAYON + self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) + + @property + def message(self): + return self._message + + @message.setter + def message(self, message): + """Write text to display, can include \n for newline + :param message: string to display + """ + self._message = message + line = 0 + # Track times through iteration, to act on the initial character of the message + initial_character = 0 + # iterate through each character + for character in message: + if initial_character == 0: + col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols - 1 + self.cursor_position(col, line) + initial_character += 1 + # if character is \n, go to next line + if character == '\n': + line += 1 + # move to left/right depending on text direction + col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols - 1 + self.cursor_position(col, line) + # Write character to display + else: + self._write8(ord(character), True) def move_left(self): """Moves display left one position""" - self._write8(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT) + self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVELEFT) def move_right(self): """Moves display right one position""" - self._write8(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT) + self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVERIGHT) + + @property + def left_to_right(self): + return self.displaymode & _LCD_ENTRYLEFT == _LCD_ENTRYLEFT + + @left_to_right.setter + def left_to_right(self, left_to_right): + """Direction of text to read from left to right""" + if left_to_right: + self.displaymode |= _LCD_ENTRYLEFT + self._write8(_LCD_ENTRYMODESET | self.displaymode) + + @property + def right_to_left(self): + return self.displaymode & _LCD_ENTRYLEFT != _LCD_ENTRYLEFT + + @right_to_left.setter + def right_to_left(self, right_to_left): + """Direction of text to read from right to left""" + if right_to_left: + self.displaymode &= ~_LCD_ENTRYLEFT + self._write8(_LCD_ENTRYMODESET | self.displaymode) + + def backlight(self, enable): + """ + Set lighton to turn the charLCD backlight on. - def set_left_to_right(self): - """Set direction of text to read from left to right""" - self.displaymode |= LCD_ENTRYLEFT - self._write8(LCD_ENTRYMODESET | self.displaymode) + :param enable: True to turn backlight on, False to turn off - def set_right_to_left(self): - """Set direction of text to read from right to left""" - self.displaymode |= LCD_ENTRYLEFT - self._write8(LCD_ENTRYMODESET | self.displaymode) + """ + if enable and not self.backlight_inverted or not enable and self.backlight_inverted: + self.backlight_pin.value = 1 + if enable and self.backlight_inverted or not enable and not self.backlight_inverted: + self.backlight_pin.value = 0 - def enable_display(self, enable): + def create_char(self, location, pattern): """ - Enable or disable the display. + Fill one of the first 8 CGRAM locations with custom characters. + The location parameter should be between 0 and 7 and pattern should + provide an array of 8 bytes containing the pattern. E.g. you can easyly + design your custom character at http://www.quinapalus.com/hd44780udg.html + To show your custom character use eg. lcd.message('\x01') - :param enable: True to enable display, False to disable + :param location: integer in range(8) to store the created character + :param ~bytes pattern: len(8) describes created character """ - if enable: - self.displaycontrol |= LCD_DISPLAYON - else: - self.displaycontrol &= ~LCD_DISPLAYON - self._write8(LCD_DISPLAYCONTROL | self.displaycontrol) + # only position 0..7 are allowed + location &= 0x7 + self._write8(_LCD_SETCGRAMADDR | (location << 3)) + for i in range(8): + self._write8(pattern[i], char_mode=True) def _write8(self, value, char_mode=False): # Sends 8b ``value`` in ``char_mode``. @@ -281,62 +367,11 @@ def _pulse_enable(self): time.sleep(0.0000001) self.enable.value = False time.sleep(0.0000001) - - def set_backlight(self, lighton): - """ - Set lighton to turn the charLCD backlight on. - - :param lighton: True to turn backlight on, False to turn off - - """ - if lighton: - self.backlight.value = 0 - else: - self.backlight.value = 1 - - def message(self, text): - """ - Write text to display. Can include ``\\n`` for newline. - - :param text: text string to display - """ - line = 0 - # iterate thru each char - for char in text: - # if character is \n, go to next line - if char == '\n': - line += 1 - # move to left/right depending on text direction - col = 0 if self.displaymode & LCD_ENTRYLEFT > 0 else self.cols-1 - self.set_cursor(col, line) - # Write character to display - else: - self._write8(ord(char), True) - - def create_char(self, location, pattern): - """ - Fill one of the first 8 CGRAM locations with custom characters. - The location parameter should be between 0 and 7 and pattern should - provide an array of 8 bytes containing the pattern. E.g. you can easyly - design your custom character at http://www.quinapalus.com/hd44780udg.html - To show your custom character use eg. lcd.message('\x01') - - :param location: integer in range(8) to store the created character - :param ~bytes pattern: len(8) describes created character - - """ - # only position 0..7 are allowed - location &= 0x7 - self._write8(LCD_SETCGRAMADDR | (location << 3)) - for i in range(8): - self._write8(pattern[i], char_mode=True) - # pylint: enable-msg=too-many-instance-attributes class Character_LCD_I2C(Character_LCD): def __init__(self, i2c, cols, lines): - import adafruit_mcp230xx self._mcp = adafruit_mcp230xx.MCP23008(i2c) reset = self._mcp.get_pin(1) enable = self._mcp.get_pin(2) @@ -344,9 +379,9 @@ def __init__(self, i2c, cols, lines): d5 = self._mcp.get_pin(4) d6 = self._mcp.get_pin(5) d7 = self._mcp.get_pin(6) - backlight = self._mcp.get_pin(7) + backlight_pin = self._mcp.get_pin(7) super().__init__(reset, enable, d4, d5, d6, d7, cols, lines, - backlight=backlight) + backlight_pin=backlight_pin) class Character_LCD_SPI(Character_LCD): @@ -355,22 +390,22 @@ class Character_LCD_SPI(Character_LCD): functions and functionality. """ - def __init__(self, spi, latch, cols, lines): + def __init__(self, spi, latch, cols, lines, backlight_inverted=False): """Initialize character LCD connectedto backpack using SPI connection on the specified SPI bus and latch line with the specified number of columns and lines on the display. """ # See comment above on I2C class for why this is imported here: - import adafruit_character_lcd.shift_reg_74hc595 as shift_reg_74hc595 - self._sr = shift_reg_74hc595.ShiftReg74HC595(spi, latch) + self._shift_register = adafruit_74hc595.ShiftRegister74HC595(spi, latch) # Setup pins for SPI backpack, see diagram: # https://learn.adafruit.com/assets/35681 - reset = self._sr.get_pin(1) - enable = self._sr.get_pin(2) - d4 = self._sr.get_pin(6) - d5 = self._sr.get_pin(5) - d6 = self._sr.get_pin(4) - d7 = self._sr.get_pin(3) - backlight = self._sr.get_pin(7) + reset = self._shift_register.get_pin(1) + enable = self._shift_register.get_pin(2) + d4 = self._shift_register.get_pin(6) + d5 = self._shift_register.get_pin(5) + d6 = self._shift_register.get_pin(4) + d7 = self._shift_register.get_pin(3) + backlight_pin = self._shift_register.get_pin(7) + self.backlight_inverted = backlight_inverted super().__init__(reset, enable, d4, d5, d6, d7, cols, lines, - backlight=backlight) + backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) diff --git a/adafruit_character_lcd/character_lcd_rgb.py b/adafruit_character_lcd/character_lcd_rgb.py index 992c1bc..6e78067 100755 --- a/adafruit_character_lcd/character_lcd_rgb.py +++ b/adafruit_character_lcd/character_lcd_rgb.py @@ -1,6 +1,7 @@ # The MIT License (MIT) # # Copyright (c) 2017 Brent Rubell for Adafruit Industries +# Copyright (c) 2018 Kattni Rembor for Adafruit Industries # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -25,11 +26,8 @@ Character_LCD - module for interfacing with RGB character LCDs -* Author(s): - - Kattni Rembor - - Brent Rubell - - Asher Lieber - - Tony DiCola for the original python charLCD library +* Author(s): Kattni Rembor, Brent Rubell, Asher Lieber + Tony DiCola for the original python charLCD library Implementation Notes -------------------- @@ -41,7 +39,7 @@ **Software and Dependencies:** -* Adafruit CircuitPython firmware (2.2.0+) for the ESP8622 and M0-based boards: +* Adafruit CircuitPython firmware: https://github.com/adafruit/circuitpython/releases * Adafruit's Bus Device library (when using I2C/SPI): https://github.com/adafruit/Adafruit_CircuitPython_BusDevice @@ -50,8 +48,9 @@ import time import digitalio from micropython import const +import adafruit_mcp230xx -#pylint: disable-msg=bad-whitespace +# pylint: disable-msg=bad-whitespace # Commands _LCD_CLEARDISPLAY = const(0x01) _LCD_RETURNHOME = const(0x02) @@ -92,8 +91,8 @@ # Offset for up to 4 rows. _LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54) +# pylint: enable-msg=bad-whitespace -#pylint: enable-msg=bad-whitespace def _map(xval, in_min, in_max, out_min, out_max): # Affine transfer/map with constrained output. @@ -107,7 +106,7 @@ def _map(xval, in_min, in_max, out_min, out_max): return ret -#pylint: disable-msg=too-many-instance-attributes +# pylint: disable-msg=too-many-instance-attributes class Character_LCD_RGB: """ Interfaces with a character LCD :param ~digitalio.DigitalInOut rs: The reset data line @@ -121,17 +120,16 @@ class Character_LCD_RGB: :param ~pulseio.PWMOut, ~digitalio.DigitalInOut red: Red RGB Anode :param ~pulseio.PWMOut, ~digitalio.DigitalInOut green: Green RGB Anode :param ~pulseio.PWMOut, ~digitalio.DigitalInOut blue: Blue RGB Anode - :param ~digitalio.DigitalInOut backlight: The backlight pin, usually the last pin. - Consult the datasheet. Note that Pin value 0 means backlight is lit. + :param ~digitalio.DigitalInOut read_write: The rw pin. Determines whether to read to or + write from the display. Not necessary if only writing to the display. Used on shield. """ - #pylint: disable-msg=too-many-arguments + # pylint: disable-msg=too-many-arguments def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, red, green, blue, - read_write=None, - backlight=None + read_write=None ): self.cols = cols self.lines = lines @@ -147,9 +145,6 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, # Define read_write (rw) pin self.read_write = read_write - # define backlight pin - self.backlight = backlight - # set all pins as outputs for pin in(rs, en, d4, d5, d6, d7): pin.direction = digitalio.Direction.OUTPUT @@ -158,11 +153,6 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, if read_write is not None: self.read_write.direction = digitalio.Direction.OUTPUT - # setup backlight - if backlight is not None: - self.backlight.direction = digitalio.Direction.OUTPUT - self.backlight.value = 0 # turn backlight on - # define color params self.red = red self.green = green @@ -195,7 +185,10 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, # set the entry mode self._write8(_LCD_ENTRYMODESET | self.displaymode) self.clear() - #pylint: enable-msg=too-many-arguments + + self._color = [0, 0, 0] + self._message = None + # pylint: enable-msg=too-many-arguments def home(self): """Moves the cursor back home pos(1,1)""" @@ -207,25 +200,37 @@ def clear(self): self._write8(_LCD_CLEARDISPLAY) time.sleep(0.003) - def show_cursor(self, show): - """Show or hide the cursor""" + @property + def cursor(self): + """True if cursor is visible, otherwise False.""" + return self.displaycontrol & _LCD_CURSORON == _LCD_CURSORON + + @cursor.setter + def cursor(self, show): + """True if cursor is visible, otherwise False.""" if show: self.displaycontrol |= _LCD_CURSORON else: - self.displaycontrol &= ~_LCD_DISPLAYON + self.displaycontrol &= ~_LCD_CURSORON self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) - def set_cursor(self, col, row): - """Sets the cursor to ``row`` and ``col`` - :param col: column location + def cursor_position(self, column, row): + """Move the cursor to position ``column`` and ``row`` + :param column: column location :param row: row location """ # Clamp row to the last row of the display if row > self.lines: row = self.lines - 1 # Set location - self._write8(_LCD_SETDDRAMADDR | (col + _LCD_ROW_OFFSETS[row])) + self._write8(_LCD_SETDDRAMADDR | (column + _LCD_ROW_OFFSETS[row])) + @property + def blink(self): + """Blinks the cursor""" + return self.displaycontrol & _LCD_BLINKON == _LCD_BLINKON + + @blink.setter def blink(self, blink): """ Blinks the cursor if blink = true. @@ -233,33 +238,45 @@ def blink(self, blink): :param blink: True to blink, False no blink """ - if blink is True: + if blink: self.displaycontrol |= _LCD_BLINKON else: self.displaycontrol &= ~_LCD_BLINKON self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) - def move_left(self): - """Moves display left one position""" - self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVELEFT) + @property + def color(self): + return self._color - def move_right(self): - """Moves display right one position""" - self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVERIGHT) + @color.setter + def color(self, color): + self._color = color + """Method to set the duty cycle or the on/off value of the RGB LED + :param color: list of 3 integers in range(100). ``[R,G,B]`` 0 is no + color, 100 is maximum color. If PWM is unavailable, 0 is off and + non-zero is on. + """ + for number, pin in enumerate(self.rgb_led): + if hasattr(pin, 'duty_cycle'): + # Assume a pulseio.PWMOut or compatible interface and set duty cycle: + pin.duty_cycle = int(_map(color[number], 0, 100, 65535, 0)) + elif hasattr(pin, 'value'): + # If we don't have a PWM interface, all we can do is turn each color + # on / off. Assume a DigitalInOut (or compatible interface) and write + # 0 (on) to pin for any value greater than 0, or 1 (off) for 0: + pin.value = 0 if color[number] > 0 else 1 - def set_left_to_right(self): - """Set direction of text to read from left to right""" - self.displaymode |= _LCD_ENTRYLEFT - self._write8(_LCD_ENTRYMODESET | self.displaymode) + @property + def display(self): + return self.displaycontrol & _LCD_DISPLAYON == _LCD_DISPLAYON - def set_right_to_left(self): - """Set direction of text to read from right to left""" - self.displaymode |= _LCD_ENTRYLEFT - self._write8(_LCD_ENTRYMODESET | self.displaymode) + @display.setter + def display(self, enable): + """ + Enable or disable the display. + + :param enable: True to enable display, False to disable - def enable_display(self, enable): - """Enable or disable the display. - :param enable: True to enable display, False to disable """ if enable: self.displaycontrol |= _LCD_DISPLAYON @@ -267,12 +284,71 @@ def enable_display(self, enable): self.displaycontrol &= ~_LCD_DISPLAYON self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) + @property + def message(self): + return self._message + + @message.setter + def message(self, message): + """Write text to display, can include \n for newline + :param message: string to display + """ + self._message = message + line = 0 + # Track times through iteration, to act on the initial character of the message + initial_character = 0 + # iterate through each character + for character in message: + if initial_character == 0: + col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols - 1 + self.cursor_position(col, line) + initial_character += 1 + # if character is \n, go to next line + if character == '\n': + line += 1 + # move to left/right depending on text direction + col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols - 1 + self.cursor_position(col, line) + # Write character to display + else: + self._write8(ord(character), True) + + def move_left(self): + """Moves display left one position""" + self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVELEFT) + + def move_right(self): + """Moves display right one position""" + return self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVERIGHT) + + @property + def left_to_right(self): + return self.displaymode & _LCD_ENTRYLEFT == _LCD_ENTRYLEFT + + @left_to_right.setter + def left_to_right(self, left_to_right): + """Direction of text to read from left to right""" + if left_to_right: + self.displaymode |= _LCD_ENTRYLEFT + self._write8(_LCD_ENTRYMODESET | self.displaymode) + + @property + def right_to_left(self): + return self.displaymode & _LCD_ENTRYLEFT != _LCD_ENTRYLEFT + + @right_to_left.setter + def right_to_left(self, right_to_left): + """Direction of text to read from right to left""" + if right_to_left: + self.displaymode &= ~_LCD_ENTRYLEFT + self._write8(_LCD_ENTRYMODESET | self.displaymode) + def _write8(self, value, char_mode=False): # Sends 8b ``value`` in ``char_mode``. # :param value: bytes # :param char_mode: character/data mode selector. False (default) for # data only, True for character bits. - # one ms delay to prevent writing too quickly. + # one ms delay to prevent writing too quickly. time.sleep(0.001) # set character/data bit. (charmode = False) self.reset.value = char_mode @@ -300,54 +376,11 @@ def _pulse_enable(self): self.enable.value = False time.sleep(0.0000001) - def set_backlight(self, lighton): - """ Set lighton to turn the charLCD backlight on. - :param lighton: True to turn backlight on, False to turn off - """ - if lighton: - self.backlight.value = 0 - else: - self.backlight.value = 1 - - def set_color(self, color): - """Method to set the duty cycle or the on/off value of the RGB LED - :param color: list of 3 integers in range(100). ``[R,G,B]`` 0 is no - color, 100 is maximum color. If PWM is unavailable, 0 is off and - non-zero is on. - """ - for number, pin in enumerate(self.rgb_led): - if hasattr(pin, 'duty_cycle'): - # Assume a pulseio.PWMOut or compatible interface and set duty cycle: - pin.duty_cycle = int(_map(color[number], 0, 100, 65535, 0)) - elif hasattr(pin, 'value'): - # If we don't have a PWM interface, all we can do is turn each color - # on / off. Assume a DigitalInOut (or compatible interface) and write - # 0 (on) to pin for any value greater than 0, or 1 (off) for 0: - pin.value = 0 if color[number] > 0 else 1 - - def message(self, text): - """Write text to display, can include \n for newline - :param text: string to display - """ - line = 0 - # iterate thru each char - for char in text: - # if character is \n, go to next line - if char == '\n': - line += 1 - # move to left/right depending on text direction - col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols-1 - self.set_cursor(col, line) - # Write character to display - else: - self._write8(ord(char), True) - -#pylint: enable-msg=too-many-instance-attributes +# pylint: enable-msg=too-many-instance-attributes class Character_LCD_I2C_RGB(Character_LCD_RGB): def __init__(self, i2c, cols, lines): - import adafruit_mcp230xx self._mcp = adafruit_mcp230xx.MCP23017(i2c) reset = self._mcp.get_pin(15) read_write = self._mcp.get_pin(14) @@ -359,5 +392,4 @@ def __init__(self, i2c, cols, lines): red = self._mcp.get_pin(6) green = self._mcp.get_pin(7) blue = self._mcp.get_pin(8) - super().__init__(reset, enable, d4, d5, d6, d7, cols, lines, red, green, blue, read_write, - backlight=None) \ No newline at end of file + super().__init__(reset, enable, d4, d5, d6, d7, cols, lines, red, green, blue, read_write) \ No newline at end of file diff --git a/adafruit_character_lcd/mcp23008.py b/adafruit_character_lcd/mcp23008.py deleted file mode 100644 index e777121..0000000 --- a/adafruit_character_lcd/mcp23008.py +++ /dev/null @@ -1,169 +0,0 @@ -""" -`adafruit_character_lcd.mcp23008` -========================================= - -MCP23008 I2C GPIO Extender Driver -Bare-bones driver for the MCP23008 driver, as used by the character LCD -backpack. This exposes the MCP2308 and its pins as standard CircuitPython -digitalio pins. Currently this is integrated in the character LCD class for -simplicity and reduction in dependent imports, but it could be broken out -into a standalone library later. - -* Author: Tony DiCola - -""" -import digitalio - -import adafruit_bus_device.i2c_device as i2c_device - -from micropython import const - -#pylint: disable-msg=bad-whitespace -# Registers and other constants: -_MCP23008_ADDRESS = const(0x20) -_MCP23008_IODIR = const(0x00) -_MCP23008_IPOL = const(0x01) -_MCP23008_GPINTEN = const(0x02) -_MCP23008_DEFVAL = const(0x03) -_MCP23008_INTCON = const(0x04) -_MCP23008_IOCON = const(0x05) -_MCP23008_GPPU = const(0x06) -_MCP23008_INTF = const(0x07) -_MCP23008_INTCAP = const(0x08) -_MCP23008_GPIO = const(0x09) -_MCP23008_OLAT = const(0x0A) - -#pylint: enable-msg=bad-whitespace - - -class MCP23008: - - """Class-level buffer for reading and writing registers with the device. - This reduces memory allocations but makes the code non-reentrant/thread- - safe!""" - _BUFFER = bytearray(2) - - class DigitalInOut: - """Digital input/output of the MCP23008. The interface is exactly the - same as the digitalio.DigitalInOut class (however the MCP23008 does not - support pull-down resistors and an exception will be thrown - attempting to set one). - """ - - def __init__(self, pin_number, mcp23008): - """Specify the pin number of the MCP23008 (0...7) and MCP23008 - instance. - """ - self._pin = pin_number - self._mcp = mcp23008 - - # kwargs in switch functions below are _necessary_ for compatibility - # with DigitalInout class (which allows specifying pull, etc. which - # is unused by this class). Do not remove them, instead turn off pylint - # in this case. - #pylint: disable=unused-argument - def switch_to_output(self, value=False, **kwargs): - """DigitalInOut switch_to_output""" - self.direction = digitalio.Direction.OUTPUT - self.value = value - - def switch_to_input(self, pull=None, **kwargs): - """DigitalInOut switch_to_input""" - self.direction = digitalio.Direction.INPUT - self.pull = pull - #pylint: enable=unused-argument - - @property - def value(self): - """Get ot Set pin value: True or False""" - gpio = self._mcp.gpio - return bool(gpio & (1 << self._pin)) - - @value.setter - def value(self, val): - gpio = self._mcp.gpio - if val: - gpio |= (1 << self._pin) - else: - gpio &= ~(1 << self._pin) - self._mcp.gpio = gpio - - @property - def direction(self): - """Set or Get pin Directtion INPUT or OUTPUT""" - iodir = self._mcp.read_u8(_MCP23008_IODIR) - if iodir & (1 << self._pin) > 0: - return digitalio.Direction.INPUT - - return digitalio.Direction.OUTPUT - - @direction.setter - def direction(self, val): - iodir = self._mcp.read_u8(_MCP23008_IODIR) - if val == digitalio.Direction.INPUT: - iodir |= (1 << self._pin) - elif val == digitalio.Direction.OUTPUT: - iodir &= ~(1 << self._pin) - else: - raise ValueError('Expected INPUT or OUTPUT direction!') - self._mcp.write_u8(_MCP23008_IODIR, iodir) - - @property - def pull(self): - """Set or Get Pull UP state: only digitalio.Pull.UP is supported""" - gppu = self._mcp.read_u8(_MCP23008_GPPU) - if gppu & (1 << self._pin) > 0: - return digitalio.Pull.UP - - return None - - @pull.setter - def pull(self, val): - gppu = self._mcp.read_u8(_MCP23008_GPPU) - if val is None: - gppu &= ~(1 << self._pin) # Disable pull-up - elif val == digitalio.Pull.UP: - gppu |= (1 << self._pin) - elif val == digitalio.Pull.DOWN: - raise ValueError('Pull-down resistors are not supported!') - else: - raise ValueError('Expected UP, DOWN, or None for pull state!') - self._mcp.write_u8(_MCP23008_GPPU, gppu) - - def __init__(self, i2c, address=_MCP23008_ADDRESS): - """Initialize MCP23008 instance on specified I2C bus and optionally - at the specified I2C address. - """ - self._device = i2c_device.I2CDevice(i2c, address) - # Reset device state to all pins as inputs (safest option). - with self._device as device: - # Write to MCP23008_IODIR register 0xFF followed by 9 zeros - # for defaults of other registers. - device.write('\x00\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00') - - def read_u8(self, register): - """Read an unsigned 8 bit value from the specified 8-bit register.""" - with self._device as i2c: - self._BUFFER[0] = register & 0xFF - i2c.write(self._BUFFER, end=1, stop=False) - i2c.readinto(self._BUFFER, end=1) - return self._BUFFER[0] - - def write_u8(self, register, val): - """Write an 8 bit value to the specified 8-bit register.""" - with self._device as i2c: - self._BUFFER[0] = register & 0xFF - self._BUFFER[1] = val & 0xFF - i2c.write(self._BUFFER) - - @property - def gpio(self): - """Get and set the raw GPIO output register. Each bit represents the - output value of the associated pin (0 = low, 1 = high), assuming that - pin has been configured as an output previously. - """ - return self.read_u8(_MCP23008_GPIO) - - @gpio.setter - def gpio(self, val): - self.write_u8(_MCP23008_GPIO, val) diff --git a/adafruit_character_lcd/shift_reg_74hc595.py b/adafruit_character_lcd/shift_reg_74hc595.py deleted file mode 100755 index 02b69bd..0000000 --- a/adafruit_character_lcd/shift_reg_74hc595.py +++ /dev/null @@ -1,116 +0,0 @@ -""" -`adafruit_character_led.shift_reg_74hc595` -=============================================== - -74HC595 Serial to Paralllel Shift Register Driver -Bare-bones driver for the 74HC595, as used by the character LCD -backpack. This exposes the 74HC595 and its pins as standard CircuitPython -digitalio pins. Currently this is integrated in the character LCD class for -simplicity and reduction in dependent imports, but it could be broken out -into a standalone library later. - -* Author: Tony DiCola -""" -import digitalio -import adafruit_bus_device.spi_device as spi_device - - -#pylint: disable-msg=too-few-public-methods -#pylint: disable-msg=no-self-use - -class DigitalInOut: - """Digital input/output of the 74HC595. The interface is exactly the - same as the digitalio.DigitalInOut class, however note that by design - this device is OUTPUT ONLY! Attempting to read inputs or set - direction as input will raise an exception. - """ - - def __init__(self, pin_number, shift_reg_74ls595): - """Specify the pin number of the shift register (0...7) and - ShiftReg74HC595 instance. - """ - self._pin = pin_number - self._sr = shift_reg_74ls595 - - # kwargs in switch functions below are _necessary_ for compatibility - # with DigitalInout class (which allows specifying pull, etc. which - # is unused by this class). Do not remove them, instead turn off pylint - # in this case. - #pylint: disable=unused-argument - def switch_to_output(self, value=False, **kwargs): - """DigitalInOut switch_to_output""" - self.direction = digitalio.Direction.OUTPUT - self.value = value - - def switch_to_input(self, **kwargs): - """do not call switch_to_input""" - raise RuntimeError('Unable to use 74HC595 as digital input!') - #pylint: enable=unused-argument - - @property - def value(self): - """do not call value""" - raise RuntimeError('Unable to use 74HC595 as digital input!') - - @value.setter - def value(self, val): - # Only supported operation, writing a digital output. - gpio = self._sr.gpio - if val: - gpio |= (1 << self._pin) - else: - gpio &= ~(1 << self._pin) - self._sr.gpio = gpio - - @property - def direction(self): - """ALWAYS an output!""" - return digitalio.Direction.OUTPUT - - @direction.setter - def direction(self, val): - """Can only be set as OUTPUT!""" - if val != digitalio.Direction.OUTPUT: - raise RuntimeError('Unable to use 74HC595 as digital input!') - - @property - def pull(self): - """Pull-up/down not supported, return NonLiberty e for no pull-up/down.""" - return None - - @pull.setter - def pull(self, val): - """Only supports null/no pull state.""" - if val is not None: - raise RuntimeError('Unable to set 74HC595 pull!') - - -class ShiftReg74HC595: - def __init__(self, spi, latch): - self._device = spi_device.SPIDevice(spi, latch, baudrate=1000000) - self._gpio = bytearray(1) - self._gpio[0] = 0x00 - - @property - def gpio(self): - """Get and set the raw GPIO output register. Each bit represents the - output value of the associated pin (0 = low, 1 = high). - """ - return self._gpio[0] - - @gpio.setter - def gpio(self, val): - self._gpio[0] = val & 0xFF - with self._device as spi: - # pylint: disable=no-member - spi.write(self._gpio) - - def get_pin(self, pin): - """Convenience function to create an instance of the DigitalInOut class - pointing at the specified pin of this 74HC595 device. - """ - assert 0 <= pin <= 7 - return DigitalInOut(pin, self) - -#pylint: enable-msg=no-self-use -#pylint: enable-msg=too-few-public-methods From de93de21874cd52347d0d8e5918fc2d698fbf50e Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Fri, 16 Nov 2018 20:55:01 -0500 Subject: [PATCH 05/17] Backlight now a property. --- adafruit_character_lcd/character_lcd.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index 1d310b6..9cc36f3 100755 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -173,6 +173,7 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, self.clear() self._message = None + self._enable = None #pylint: enable-msg=too-many-arguments def home(self): @@ -305,7 +306,13 @@ def right_to_left(self, right_to_left): self.displaymode &= ~_LCD_ENTRYLEFT self._write8(_LCD_ENTRYMODESET | self.displaymode) + @property + def backlight(self): + return self._enable + + @backlight.setter def backlight(self, enable): + self._enable = enable """ Set lighton to turn the charLCD backlight on. @@ -321,9 +328,9 @@ def create_char(self, location, pattern): """ Fill one of the first 8 CGRAM locations with custom characters. The location parameter should be between 0 and 7 and pattern should - provide an array of 8 bytes containing the pattern. E.g. you can easyly + provide an array of 8 bytes containing the pattern. E.g. you can easily design your custom character at http://www.quinapalus.com/hd44780udg.html - To show your custom character use eg. lcd.message('\x01') + To show your custom character use eg. lcd.message = "\x01" :param location: integer in range(8) to store the created character :param ~bytes pattern: len(8) describes created character From b5adf5e59445e9b92483185c4ad1b4daa4cd5a00 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Mon, 19 Nov 2018 15:36:40 -0500 Subject: [PATCH 06/17] Needs test: RPi, M0, standalone LCDs --- README.rst | 4 +- adafruit_character_lcd/character_lcd.py | 350 +++++++++++++----- adafruit_character_lcd/character_lcd_rgb.py | 314 +++++++++++++--- docs/api.rst | 7 +- examples/charlcd_I2C_simpletest.py | 48 ++- examples/charlcd_SPI_simpletest.py | 48 ++- ...y => charlcd_custom_character_nyan_cat.py} | 31 +- examples/charlcd_customcharacter.py | 20 + examples/charlcd_mono_simpletest.py | 36 +- ...test.py => charlcd_mono_simpletest_rpi.py} | 36 +- examples/charlcd_rgb_simpletest.py | 68 +++- ...etest.py => charlcd_rgb_simpletest_rpi.py} | 12 +- examples/customcharacter.py | 19 - requirements.txt | 4 +- 14 files changed, 720 insertions(+), 277 deletions(-) rename examples/{custom_character_nyan_cat.py => charlcd_custom_character_nyan_cat.py} (75%) create mode 100644 examples/charlcd_customcharacter.py rename examples/{rpi_charlcd_mono_simpletest.py => charlcd_mono_simpletest_rpi.py} (63%) rename examples/{rpi_charlcd_rgb_simpletest.py => charlcd_rgb_simpletest_rpi.py} (83%) delete mode 100644 examples/customcharacter.py diff --git a/README.rst b/README.rst index 03efd7d..1779a86 100644 --- a/README.rst +++ b/README.rst @@ -75,7 +75,7 @@ To verify that your pins are correct, print a hello message to the CharLCD: .. code-block:: python - lcd.message('hello\ncircuitpython') + lcd.message = "Hello\nCircuitPython" Custom character example with create_char() is provided within /examples/ @@ -143,4 +143,4 @@ Now, once you have the virtual environment activated: This will output the documentation to ``docs/_build/html``. Open the index.html in your browser to view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to -locally verify it will pass. \ No newline at end of file +locally verify it will pass. diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index 9cc36f3..9c70067 100755 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -67,14 +67,11 @@ _LCD_SETDDRAMADDR = const(0x80) # Entry flags -_LCD_ENTRYRIGHT = const(0x00) _LCD_ENTRYLEFT = const(0x02) -_LCD_ENTRYSHIFTINCREMENT = const(0x01) _LCD_ENTRYSHIFTDECREMENT = const(0x00) # Control flags _LCD_DISPLAYON = const(0x04) -_LCD_DISPLAYOFF = const(0x00) _LCD_CURSORON = const(0x02) _LCD_CURSOROFF = const(0x00) _LCD_BLINKON = const(0x01) @@ -82,22 +79,19 @@ # Move flags _LCD_DISPLAYMOVE = const(0x08) -_LCD_CURSORMOVE = const(0x00) _LCD_MOVERIGHT = const(0x04) _LCD_MOVELEFT = const(0x00) # Function set flags -_LCD_8BITMODE = const(0x10) _LCD_4BITMODE = const(0x00) _LCD_2LINE = const(0x08) _LCD_1LINE = const(0x00) -_LCD_5X10DOTS = const(0x04) _LCD_5X8DOTS = const(0x00) # Offset for up to 4 rows. _LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54) -#pylint: enable-msg=bad-whitespace +# pylint: enable-msg=bad-whitespace def _set_bit(byte_value, position, val): # Given the specified byte_value set the bit at position to the provided @@ -107,11 +101,14 @@ def _set_bit(byte_value, position, val): ret = byte_value | (1 << position) else: ret = byte_value & ~(1 << position) - return ret -#pylint: disable-msg=too-many-instance-attributes -class Character_LCD(object): + +# pylint: disable-msg=too-many-instance-attributes +class Character_LCD: + """Base class for character LCD.""" + LEFT_TO_RIGHT = const(0) + RIGHT_TO_LEFT = const(1) """ Interfaces with a character LCD :param ~digitalio.DigitalInOut rs: The reset data line @@ -120,19 +117,21 @@ class Character_LCD(object): :param ~digitalio.DigitalInOut d5: The data line 5 :param ~digitalio.DigitalInOut d6: The data line 6 :param ~digitalio.DigitalInOut d7: The data line 7 - :param cols: The columns on the charLCD + :param columns: The columns on the charLCD :param lines: The lines on the charLCD - :param ~digitalio.DigitalInOut backlight: The backlight pin, usually + :param ~digitalio.DigitalInOut backlight_pin: The backlight pin, usually the last pin. Check with your datasheet + :param bool backlight_inverted: False if LCD is not inverted, i.e. backlight pin is connected + to common anode. True if LCD is inverted i.e. backlight pin is connected to common cathode. """ - #pylint: disable-msg=too-many-arguments - def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, + # pylint: disable-msg=too-many-arguments + def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, backlight_pin=None, backlight_inverted=False ): - self.cols = cols + self.columns = columns self.lines = lines # save pin numbers self.reset = rs @@ -144,7 +143,6 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, # backlight pin self.backlight_pin = backlight_pin self.backlight_inverted = backlight_inverted - # self.pwn_enabled = enable_pwm # set all pins as outputs for pin in(rs, en, d4, d5, d6, d7): pin.direction = digitalio.Direction.OUTPUT @@ -174,26 +172,63 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, self._message = None self._enable = None - #pylint: enable-msg=too-many-arguments + self._direction = None + # pylint: enable-msg=too-many-arguments def home(self): - """Moves the cursor back home pos(1,1)""" + """Moves the cursor "home" to position (1, 1).""" self._write8(_LCD_RETURNHOME) time.sleep(0.003) def clear(self): - """Clears the LCD""" + """Clears everything displayed on the LCD. + + The following example displays, "Hello, world!", then clears the LCD. + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + + lcd.message = "Hello, world!" + time.sleep(5) + lcd.clear() + """ self._write8(_LCD_CLEARDISPLAY) time.sleep(0.003) @property def cursor(self): - """True if cursor is visible, otherwise False.""" + """True if cursor is visible. False to stop displaying the cursor. + + The following example shows the cursor after a displayed message: + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + + lcd.cursor = True + lcd.message = "Cursor! " + time.sleep(5) + + """ return self.displaycontrol & _LCD_CURSORON == _LCD_CURSORON @cursor.setter def cursor(self, show): - """True if cursor is visible, otherwise False.""" if show: self.displaycontrol |= _LCD_CURSORON else: @@ -201,7 +236,8 @@ def cursor(self, show): self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) def cursor_position(self, column, row): - """Move the cursor to position ``column`` and ``row`` + """Move the cursor to position ``column``, ``row`` + :param column: column location :param row: row location """ @@ -213,16 +249,31 @@ def cursor_position(self, column, row): @property def blink(self): - return self.displaycontrol & _LCD_BLINKON == _LCD_BLINKON - - @blink.setter - def blink(self, blink): """ - Blinks the cursor if blink = true. + Blink the cursor. True to blink the cursor. False to stop blinking. + + The following example shows a message followed by a blinking cursor for five seconds. - :param blink: True to blink, False no blink + .. code-block:: python + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + + lcd.blink = True + lcd.message = "Blinky cursor!" + time.sleep(5) + lcd.blink = False """ + return self.displaycontrol & _LCD_BLINKON == _LCD_BLINKON + + @blink.setter + def blink(self, blink): if blink: self.displaycontrol |= _LCD_BLINKON else: @@ -231,16 +282,30 @@ def blink(self, blink): @property def display(self): - return self.displaycontrol & _LCD_DISPLAYON == _LCD_DISPLAYON - - @display.setter - def display(self, enable): """ - Enable or disable the display. + Enable or disable the display. True to enable the display. False to disable the display. + + The following example displays, "Hello, world!" on the LCD and then turns the display off. - :param enable: True to enable display, False to disable + .. code-block:: python + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + + lcd.message = "Hello, world!" + time.sleep(5) + lcd.display = False """ + return self.displaycontrol & _LCD_DISPLAYON == _LCD_DISPLAYON + + @display.setter + def display(self, enable): if enable: self.displaycontrol |= _LCD_DISPLAYON else: @@ -249,76 +314,180 @@ def display(self, enable): @property def message(self): + """Display a string of text on the character LCD. + + The following example displays, "Hello, world!" on the LCD. + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + + lcd.message = "Hello, world!" + time.sleep(5) + """ return self._message @message.setter def message(self, message): - """Write text to display, can include \n for newline - :param message: string to display - """ self._message = message line = 0 # Track times through iteration, to act on the initial character of the message initial_character = 0 # iterate through each character for character in message: + # If this is the first character in the string: if initial_character == 0: - col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols - 1 + # Start at (1, 1) unless direction is set right to left, in which case start + # on the opposite side of the display. + col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1 self.cursor_position(col, line) initial_character += 1 - # if character is \n, go to next line + # If character is \n, go to next line if character == '\n': line += 1 - # move to left/right depending on text direction - col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols - 1 + # Start the second line at (1, 1) unless direction is set right to left in which + # case start on the opposite side of the display. + col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1 self.cursor_position(col, line) - # Write character to display + # Write string to display else: self._write8(ord(character), True) def move_left(self): - """Moves display left one position""" + """Moves displayed text left one column. + + The following example scrolls a message to the left off the screen. + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + + scroll_message = "<-- Scroll" + lcd.message = scroll_message + time.sleep(2) + for i in range(len(scroll_message)): + lcd.move_left() + time.sleep(0.5) + """ self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVELEFT) def move_right(self): - """Moves display right one position""" + """Moves displayed text right one column. + + The following example scrolls a message to the right off the screen. + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd_columns = 16 + lcd_rows = 2 + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, lcd_columns, lcd_rows) + + scroll_message = "Scroll -->" + lcd.message = scroll_message + time.sleep(2) + for i in range(len(scroll_message) + lcd_columns): + lcd.move_right() + time.sleep(0.5) + """ self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVERIGHT) @property - def left_to_right(self): - return self.displaymode & _LCD_ENTRYLEFT == _LCD_ENTRYLEFT + def text_direction(self): + """The direction the text is displayed. To display the text left to right beginning on the + left side of the LCD, set ``text_direction = LEFT_TO_RIGHT``. To display the text right + to left beginning on the right size of the LCD, set ``text_direction = RIGHT_TO_LEFT``. + Text defaults to displaying from left to right. - @left_to_right.setter - def left_to_right(self, left_to_right): - """Direction of text to read from left to right""" - if left_to_right: - self.displaymode |= _LCD_ENTRYLEFT - self._write8(_LCD_ENTRYMODESET | self.displaymode) + The following example displays "Hello, world!" from right to left. - @property - def right_to_left(self): - return self.displaymode & _LCD_ENTRYLEFT != _LCD_ENTRYLEFT + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd - @right_to_left.setter - def right_to_left(self, right_to_left): - """Direction of text to read from right to left""" - if right_to_left: - self.displaymode &= ~_LCD_ENTRYLEFT - self._write8(_LCD_ENTRYMODESET | self.displaymode) + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + + lcd.text_direction = lcd.RIGHT_TO_LEFT + lcd.message = "Hello, world!" + time.sleep(5) + """ + return self._direction + + @text_direction.setter + def text_direction(self, direction): + self._direction = direction + if direction == self.LEFT_TO_RIGHT: + self._left_to_right() + elif direction == self.RIGHT_TO_LEFT: + self._right_to_left() + + def _left_to_right(self): + # Displays text from left to right on the LCD. + self.displaymode |= _LCD_ENTRYLEFT + self._write8(_LCD_ENTRYMODESET | self.displaymode) + + def _right_to_left(self): + # Displays text from right to left on the LCD. + self.displaymode &= ~_LCD_ENTRYLEFT + self._write8(_LCD_ENTRYMODESET | self.displaymode) @property def backlight(self): + """Enable or disable backlight. True if backlight is on. False if backlight is off. + + The following example turns the backlight off, then displays, "Hello, world?", then turns + the backlight on and displays, "Hello, world!" + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + + lcd.backlight = False + lcd.message = "Hello, world?" + time.sleep(5) + lcd.backlight = True + lcd.message = "Hello, world!" + time.sleep(5) + + """ return self._enable @backlight.setter def backlight(self, enable): self._enable = enable - """ - Set lighton to turn the charLCD backlight on. - - :param enable: True to turn backlight on, False to turn off - - """ if enable and not self.backlight_inverted or not enable and self.backlight_inverted: self.backlight_pin.value = 1 if enable and self.backlight_inverted or not enable and not self.backlight_inverted: @@ -330,7 +499,7 @@ def create_char(self, location, pattern): The location parameter should be between 0 and 7 and pattern should provide an array of 8 bytes containing the pattern. E.g. you can easily design your custom character at http://www.quinapalus.com/hd44780udg.html - To show your custom character use eg. lcd.message = "\x01" + To show your custom character use, for example, ``lcd.message = "\x01"`` :param location: integer in range(8) to store the created character :param ~bytes pattern: len(8) describes created character @@ -378,17 +547,26 @@ def _pulse_enable(self): class Character_LCD_I2C(Character_LCD): - def __init__(self, i2c, cols, lines): + """Character LCD connected to I2C/SPI backpack using its I2C connection. + This is a subclass of Character_LCD and implements all of the same + functions and functionality. + """ + def __init__(self, i2c, columns, lines, backlight_inverted=False): + """Initialize character LCD connectedto backpack using I2C connection + on the specified I2C bus with the specified number of columns and + lines on the display. Optionally specify if backlight is inverted. + """ self._mcp = adafruit_mcp230xx.MCP23008(i2c) reset = self._mcp.get_pin(1) enable = self._mcp.get_pin(2) - d4 = self._mcp.get_pin(3) - d5 = self._mcp.get_pin(4) - d6 = self._mcp.get_pin(5) - d7 = self._mcp.get_pin(6) + db4 = self._mcp.get_pin(3) + db5 = self._mcp.get_pin(4) + db6 = self._mcp.get_pin(5) + db7 = self._mcp.get_pin(6) backlight_pin = self._mcp.get_pin(7) - super().__init__(reset, enable, d4, d5, d6, d7, cols, lines, - backlight_pin=backlight_pin) + self.backlight_inverted = backlight_inverted + super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, + backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) class Character_LCD_SPI(Character_LCD): @@ -397,22 +575,22 @@ class Character_LCD_SPI(Character_LCD): functions and functionality. """ - def __init__(self, spi, latch, cols, lines, backlight_inverted=False): - """Initialize character LCD connectedto backpack using SPI connection + def __init__(self, spi, latch, columns, lines, backlight_inverted=False): + # pylint: disable=too-many-arguments + """Initialize character LCD connected to backpack using SPI connection on the specified SPI bus and latch line with the specified number of - columns and lines on the display. + columns and lines on the display. Optionally specify if backlight is + inverted. """ - # See comment above on I2C class for why this is imported here: + # pylint: enable=too-many-arguments self._shift_register = adafruit_74hc595.ShiftRegister74HC595(spi, latch) - # Setup pins for SPI backpack, see diagram: - # https://learn.adafruit.com/assets/35681 reset = self._shift_register.get_pin(1) enable = self._shift_register.get_pin(2) - d4 = self._shift_register.get_pin(6) - d5 = self._shift_register.get_pin(5) - d6 = self._shift_register.get_pin(4) - d7 = self._shift_register.get_pin(3) + db4 = self._shift_register.get_pin(6) + db5 = self._shift_register.get_pin(5) + db6 = self._shift_register.get_pin(4) + db7 = self._shift_register.get_pin(3) backlight_pin = self._shift_register.get_pin(7) self.backlight_inverted = backlight_inverted - super().__init__(reset, enable, d4, d5, d6, d7, cols, lines, + super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) diff --git a/adafruit_character_lcd/character_lcd_rgb.py b/adafruit_character_lcd/character_lcd_rgb.py index 6e78067..d77ca2d 100755 --- a/adafruit_character_lcd/character_lcd_rgb.py +++ b/adafruit_character_lcd/character_lcd_rgb.py @@ -62,14 +62,11 @@ _LCD_SETDDRAMADDR = const(0x80) # Entry flags -_LCD_ENTRYRIGHT = const(0x00) _LCD_ENTRYLEFT = const(0x02) -_LCD_ENTRYSHIFTINCREMENT = const(0x01) _LCD_ENTRYSHIFTDECREMENT = const(0x00) # Control flags _LCD_DISPLAYON = const(0x04) -_LCD_DISPLAYOFF = const(0x00) _LCD_CURSORON = const(0x02) _LCD_CURSOROFF = const(0x00) _LCD_BLINKON = const(0x01) @@ -77,16 +74,13 @@ # Move flags _LCD_DISPLAYMOVE = const(0x08) -_LCD_CURSORMOVE = const(0x00) _LCD_MOVERIGHT = const(0x04) _LCD_MOVELEFT = const(0x00) # Function set flags -_LCD_8BITMODE = const(0x10) _LCD_4BITMODE = const(0x00) _LCD_2LINE = const(0x08) _LCD_1LINE = const(0x00) -_LCD_5X10DOTS = const(0x04) _LCD_5X8DOTS = const(0x00) # Offset for up to 4 rows. @@ -108,6 +102,9 @@ def _map(xval, in_min, in_max, out_min, out_max): # pylint: disable-msg=too-many-instance-attributes class Character_LCD_RGB: + """Base class for RGB character LCD.""" + LEFT_TO_RIGHT = const(0) + RIGHT_TO_LEFT = const(1) """ Interfaces with a character LCD :param ~digitalio.DigitalInOut rs: The reset data line :param ~digitalio.DigitalInOut en: The enable data line @@ -115,7 +112,7 @@ class Character_LCD_RGB: :param ~digitalio.DigitalInOut d5: The data line 5 :param ~digitalio.DigitalInOut d6: The data line 6 :param ~digitalio.DigitalInOut d7: The data line 7 - :param cols: The columns on the charLCD + :param columns: The columns on the charLCD :param lines: The lines on the charLCD :param ~pulseio.PWMOut, ~digitalio.DigitalInOut red: Red RGB Anode :param ~pulseio.PWMOut, ~digitalio.DigitalInOut green: Green RGB Anode @@ -125,13 +122,13 @@ class Character_LCD_RGB: """ # pylint: disable-msg=too-many-arguments - def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, + def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, red, green, blue, read_write=None ): - self.cols = cols + self.columns = columns self.lines = lines # define pin params @@ -188,26 +185,63 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines, self._color = [0, 0, 0] self._message = None + self._direction = None # pylint: enable-msg=too-many-arguments def home(self): - """Moves the cursor back home pos(1,1)""" + """Moves the cursor "home" to position (1, 1).""" self._write8(_LCD_RETURNHOME) time.sleep(0.003) def clear(self): - """Clears the LCD""" + """Clears everything displayed on the LCD. + + The following example displays, "Hello, world!", then clears the LCD. + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + + lcd.message = "Hello, world!" + time.sleep(5) + lcd.clear() + """ self._write8(_LCD_CLEARDISPLAY) time.sleep(0.003) @property def cursor(self): - """True if cursor is visible, otherwise False.""" + """True if cursor is visible. False to stop displaying the cursor. + + The following example shows the cursor after a displayed message: + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + + lcd.cursor = True + lcd.message = "Cursor! " + time.sleep(5) + + """ return self.displaycontrol & _LCD_CURSORON == _LCD_CURSORON @cursor.setter def cursor(self, show): - """True if cursor is visible, otherwise False.""" if show: self.displaycontrol |= _LCD_CURSORON else: @@ -216,6 +250,7 @@ def cursor(self, show): def cursor_position(self, column, row): """Move the cursor to position ``column`` and ``row`` + :param column: column location :param row: row location """ @@ -227,17 +262,31 @@ def cursor_position(self, column, row): @property def blink(self): - """Blinks the cursor""" - return self.displaycontrol & _LCD_BLINKON == _LCD_BLINKON - - @blink.setter - def blink(self, blink): """ - Blinks the cursor if blink = true. + Blink the cursor. True to blink the cursor. False to stop blinking. + + The following example shows a message followed by a blinking cursor for five seconds. + + .. code-block:: python - :param blink: True to blink, False no blink + import time + import board + import busio + import adafruit_character_lcd + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + + lcd.blink = True + lcd.message = "Blinky cursor!" + time.sleep(5) + lcd.blink = False """ + return self.displaycontrol & _LCD_BLINKON == _LCD_BLINKON + + @blink.setter + def blink(self, blink): if blink: self.displaycontrol |= _LCD_BLINKON else: @@ -246,16 +295,36 @@ def blink(self, blink): @property def color(self): + """ + The color of the display. Provide a list of three integers ranging 0 - 100, ``[R, G, B]``. + ``0`` is no color, or "off". ``100`` is maximum color. For example, the brightest red would + be ``[100, 0, 0]``, and a half-bright purple would be, ``[50, 0, 50]``. + + If PWM is unavailable, ``0`` is off, and non-zero is on. For example, ``[1, 0, 0]`` would + be red. + + The following example turns the LCD red and displays, "Hello, world!". + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + + lcd.color = [100, 0, 0] + lcd.message = "Hello, world!" + time.sleep(5) + """ return self._color @color.setter def color(self, color): self._color = color - """Method to set the duty cycle or the on/off value of the RGB LED - :param color: list of 3 integers in range(100). ``[R,G,B]`` 0 is no - color, 100 is maximum color. If PWM is unavailable, 0 is off and - non-zero is on. - """ for number, pin in enumerate(self.rgb_led): if hasattr(pin, 'duty_cycle'): # Assume a pulseio.PWMOut or compatible interface and set duty cycle: @@ -268,16 +337,30 @@ def color(self, color): @property def display(self): - return self.displaycontrol & _LCD_DISPLAYON == _LCD_DISPLAYON - - @display.setter - def display(self, enable): """ - Enable or disable the display. + Enable or disable the display. True to enable the display. False to disable the display. + + The following example displays, "Hello, world!" on the LCD and then turns the display off. + + .. code-block:: python - :param enable: True to enable display, False to disable + import time + import board + import busio + import adafruit_character_lcd + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + + lcd.message = "Hello, world!" + time.sleep(5) + lcd.display = False """ + return self.displaycontrol & _LCD_DISPLAYON == _LCD_DISPLAYON + + @display.setter + def display(self, enable): if enable: self.displaycontrol |= _LCD_DISPLAYON else: @@ -286,13 +369,28 @@ def display(self, enable): @property def message(self): + """Display a string of text on the character LCD. + + The following example displays, "Hello, world!" on the LCD. + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + + lcd.message = "Hello, world!" + time.sleep(5) + """ return self._message @message.setter def message(self, message): - """Write text to display, can include \n for newline - :param message: string to display - """ self._message = message line = 0 # Track times through iteration, to act on the initial character of the message @@ -300,48 +398,133 @@ def message(self, message): # iterate through each character for character in message: if initial_character == 0: - col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols - 1 + col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1 self.cursor_position(col, line) initial_character += 1 # if character is \n, go to next line if character == '\n': line += 1 # move to left/right depending on text direction - col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols - 1 + col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1 self.cursor_position(col, line) # Write character to display else: self._write8(ord(character), True) def move_left(self): - """Moves display left one position""" + """Moves displayed text left one column. + + The following example scrolls a message to the left off the screen. + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + + scroll_message = "<-- Scroll" + lcd.message = scroll_message + time.sleep(2) + for i in range(len(scroll_message)): + lcd.move_left() + time.sleep(0.5) + """ self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVELEFT) def move_right(self): - """Moves display right one position""" + """Moves displayed text right one column. + + The following example scrolls a message to the right off the screen. + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd_columns = 16 + lcd_rows = 2 + + lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, lcd_columns, lcd_rows) + + scroll_message = "Scroll -->" + lcd.message = scroll_message + time.sleep(2) + for i in range(len(scroll_message) + lcd_columns): + lcd.move_right() + time.sleep(0.5) + """ return self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVERIGHT) @property - def left_to_right(self): - return self.displaymode & _LCD_ENTRYLEFT == _LCD_ENTRYLEFT + def text_direction(self): + """The direction the text is displayed. To display the text left to right beginning on the + left side of the LCD, set ``text_direction = LEFT_TO_RIGHT``. To display the text right + to left beginning on the right size of the LCD, set ``text_direction = RIGHT_TO_LEFT``. + Text defaults to displaying from left to right. - @left_to_right.setter - def left_to_right(self, left_to_right): - """Direction of text to read from left to right""" - if left_to_right: - self.displaymode |= _LCD_ENTRYLEFT - self._write8(_LCD_ENTRYMODESET | self.displaymode) + The following example displays "Hello, world!" from right to left. - @property - def right_to_left(self): - return self.displaymode & _LCD_ENTRYLEFT != _LCD_ENTRYLEFT + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) - @right_to_left.setter - def right_to_left(self, right_to_left): - """Direction of text to read from right to left""" - if right_to_left: - self.displaymode &= ~_LCD_ENTRYLEFT - self._write8(_LCD_ENTRYMODESET | self.displaymode) + lcd.text_direction = lcd.RIGHT_TO_LEFT + lcd.message = "Hello, world!" + time.sleep(5) + """ + return self._direction + + @text_direction.setter + def text_direction(self, direction): + self._direction = direction + if direction == self.LEFT_TO_RIGHT: + self._left_to_right() + elif direction == self.RIGHT_TO_LEFT: + self._right_to_left() + + def _left_to_right(self): + # Displays text from left to right on the LCD. + self.displaymode |= _LCD_ENTRYLEFT + self._write8(_LCD_ENTRYMODESET | self.displaymode) + + def _right_to_left(self): + # Displays text from right to left on the LCD. + self.displaymode &= ~_LCD_ENTRYLEFT + self._write8(_LCD_ENTRYMODESET | self.displaymode) + + def create_char(self, location, pattern): + """ + Fill one of the first 8 CGRAM locations with custom characters. + The location parameter should be between 0 and 7 and pattern should + provide an array of 8 bytes containing the pattern. E.g. you can easily + design your custom character at http://www.quinapalus.com/hd44780udg.html + To show your custom character use, for example, ``lcd.message = "\x01"`` + + :param location: integer in range(8) to store the created character + :param ~bytes pattern: len(8) describes created character + + """ + # only position 0..7 are allowed + location &= 0x7 + self._write8(_LCD_SETCGRAMADDR | (location << 3)) + for i in range(8): + self._write8(pattern[i], char_mode=True) def _write8(self, value, char_mode=False): # Sends 8b ``value`` in ``char_mode``. @@ -380,16 +563,25 @@ def _pulse_enable(self): class Character_LCD_I2C_RGB(Character_LCD_RGB): - def __init__(self, i2c, cols, lines): + """RGB Character LCD connected to I2C shield using I2C connection. + This is a subclass of Character_LCD_RGB and implements all of the same + functions and functionality. + """ + def __init__(self, i2c, columns, lines): + """Initialize RGB character LCD connected to shield using I2C connection + on the specified I2C bus with the specified number of columns and lines + on the display. + """ self._mcp = adafruit_mcp230xx.MCP23017(i2c) reset = self._mcp.get_pin(15) read_write = self._mcp.get_pin(14) enable = self._mcp.get_pin(13) - d4 = self._mcp.get_pin(12) - d5 = self._mcp.get_pin(11) - d6 = self._mcp.get_pin(10) - d7 = self._mcp.get_pin(9) + db4 = self._mcp.get_pin(12) + db5 = self._mcp.get_pin(11) + db6 = self._mcp.get_pin(10) + db7 = self._mcp.get_pin(9) red = self._mcp.get_pin(6) green = self._mcp.get_pin(7) blue = self._mcp.get_pin(8) - super().__init__(reset, enable, d4, d5, d6, d7, cols, lines, red, green, blue, read_write) \ No newline at end of file + super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, red, green, blue, + read_write) diff --git a/docs/api.rst b/docs/api.rst index fdb9d6c..3751e63 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -2,4 +2,9 @@ .. If you created a package, create one automodule per module in the package. .. automodule:: adafruit_character_lcd.character_lcd - :members: \ No newline at end of file + :members: + :inherited-members: + +.. automodule:: adafruit_character_lcd.character_lcd_rgb + :members: + :inherited-members: diff --git a/examples/charlcd_I2C_simpletest.py b/examples/charlcd_I2C_simpletest.py index 6b30f07..9502acc 100644 --- a/examples/charlcd_I2C_simpletest.py +++ b/examples/charlcd_I2C_simpletest.py @@ -18,33 +18,43 @@ # Init the lcd class lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, lcd_columns, lcd_rows) -# Print a 2x line message -lcd.message('hello\ncircuitpython') +# Turn on backlight +lcd.backlight = True +# Print a two line message +lcd.message = "Hello\nCircuitPython" # Wait 5s time.sleep(5) -# Demo showing cursor lcd.clear() -lcd.show_cursor(True) -lcd.message('showing cursor ') -# Wait 5s +# Print two line message right to left +lcd.text_direction = lcd.RIGHT_TO_LEFT +lcd.message = "Hello\nCircuitPython" +# Wait 5s time.sleep(5) -# Demo showing the blinking cursor +# Return text direction to left to right +lcd.text_direction = lcd.LEFT_TO_RIGHT +# Demo showing cursor lcd.clear() -lcd.blink(True) -lcd.message('Blinky Cursor!') -# Wait 5s +lcd.cursor = True +lcd.message = "Cursor! " +# Wait 5s +time.sleep(5) +# Demo showing the blinking cursor +lcd.clear() +lcd.blink = True +lcd.message = "Blinky Cursor!" +# Wait 5s time.sleep(5) -lcd.blink(False) -# Demo scrolling message LEFT +lcd.blink = False +# Demo scrolling message LEFT lcd.clear() -scroll_msg = 'Scroll' -lcd.message(scroll_msg) -# Scroll to the left -for i in range(lcd_columns - len(scroll_msg)): +scroll_msg = '<-- Scroll' +lcd.message = scroll_msg +# Scroll to the left +for i in range(len(scroll_msg)): time.sleep(0.5) lcd.move_left() -# Demo turning backlight off lcd.clear() -lcd.message("going to sleep\ncya later!") -lcd.set_backlight(False) +lcd.message = "Going to sleep\nCya later!" +# Demo turning backlight off +lcd.backlight = False time.sleep(2) diff --git a/examples/charlcd_SPI_simpletest.py b/examples/charlcd_SPI_simpletest.py index f7c9302..4cd9cd9 100644 --- a/examples/charlcd_SPI_simpletest.py +++ b/examples/charlcd_SPI_simpletest.py @@ -25,33 +25,43 @@ latch = digitalio.DigitalInOut(latch) lcd = adafruit_character_lcd.Character_LCD_SPI(spi, latch, lcd_columns, lcd_rows) -# Print a 2x line message -lcd.message('hello\ncircuitpython') +# Turn on backlight +lcd.backlight = True +# Print a two line message +lcd.message = "Hello\nCircuitPython" # Wait 5s time.sleep(5) -# Demo showing cursor lcd.clear() -lcd.show_cursor(True) -lcd.message('showing cursor ') -# Wait 5s +# Print two line message right to left +lcd.text_direction = lcd.RIGHT_TO_LEFT +lcd.message = "Hello\nCircuitPython" +# Wait 5s time.sleep(5) -# Demo showing the blinking cursor +# Return text direction to left to right +lcd.text_direction = lcd.LEFT_TO_RIGHT +# Demo showing cursor lcd.clear() -lcd.blink(True) -lcd.message('Blinky Cursor!') -# Wait 5s +lcd.cursor = True +lcd.message = "Cursor! " +# Wait 5s +time.sleep(5) +# Demo showing the blinking cursor +lcd.clear() +lcd.blink = True +lcd.message = "Blinky Cursor!" +# Wait 5s time.sleep(5) -lcd.blink(False) -# Demo scrolling message LEFT +lcd.blink = False +# Demo scrolling message LEFT lcd.clear() -scroll_msg = 'Scroll' -lcd.message(scroll_msg) -# Scroll to the left -for i in range(lcd_columns - len(scroll_msg)): +scroll_msg = '<-- Scroll' +lcd.message = scroll_msg +# Scroll to the left +for i in range(len(scroll_msg)): time.sleep(0.5) lcd.move_left() -# Demo turning backlight off lcd.clear() -lcd.message("going to sleep\ncya later!") -lcd.set_backlight(False) +lcd.message = "Going to sleep\nCya later!" +# Demo turning backlight off +lcd.backlight = False time.sleep(2) diff --git a/examples/custom_character_nyan_cat.py b/examples/charlcd_custom_character_nyan_cat.py similarity index 75% rename from examples/custom_character_nyan_cat.py rename to examples/charlcd_custom_character_nyan_cat.py index 5c676be..9481d8d 100755 --- a/examples/custom_character_nyan_cat.py +++ b/examples/charlcd_custom_character_nyan_cat.py @@ -1,17 +1,17 @@ import time -from board import D7, D8, D9, D10, D11, D12, D13 +import board import digitalio import adafruit_character_lcd lcd_columns = 16 lcd_rows = 2 -lcd_rs = digitalio.DigitalInOut(D7) -lcd_en = digitalio.DigitalInOut(D8) -lcd_d7 = digitalio.DigitalInOut(D12) -lcd_d6 = digitalio.DigitalInOut(D11) -lcd_d5 = digitalio.DigitalInOut(D10) -lcd_d4 = digitalio.DigitalInOut(D9) -lcd_backlight = digitalio.DigitalInOut(D13) +lcd_rs = digitalio.DigitalInOut(board.D7) +lcd_en = digitalio.DigitalInOut(board.D8) +lcd_d7 = digitalio.DigitalInOut(board.D12) +lcd_d6 = digitalio.DigitalInOut(board.D11) +lcd_d5 = digitalio.DigitalInOut(board.D10) +lcd_d4 = digitalio.DigitalInOut(board.D9) +lcd_backlight = digitalio.DigitalInOut(board.D13) lcd = adafruit_character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight) @@ -65,21 +65,8 @@ lcd.clear() -body_width = 3 lcd.move_right() -for i in range(4): - lcd.message('\x02') -lcd.message('\x01') -for i in range(body_width): - lcd.message('\x00') -lcd.message('\x06') -lcd.message('\n') -lcd.message('\x02\x02\x02') -lcd.message('\x07') -lcd.message('\x03') -for i in range(body_width): - lcd.message('\x04') -lcd.message('\x05') +lcd.message = "\x02\x02\x02\x02\x01\x00\x00\x00\x06\n\x02\x02\x02\x07\x03\x04\x04\x04\x05" while True: lcd.create_char(4, bot_body2) diff --git a/examples/charlcd_customcharacter.py b/examples/charlcd_customcharacter.py new file mode 100644 index 0000000..d366d25 --- /dev/null +++ b/examples/charlcd_customcharacter.py @@ -0,0 +1,20 @@ +import digitalio +import board +import adafruit_character_lcd + + +lcd_columns = 16 +lcd_rows = 2 +lcd_rs = digitalio.DigitalInOut(board.D7) +lcd_en = digitalio.DigitalInOut(board.D8) +lcd_d7 = digitalio.DigitalInOut(board.D12) +lcd_d6 = digitalio.DigitalInOut(board.D11) +lcd_d5 = digitalio.DigitalInOut(board.D10) +lcd_d4 = digitalio.DigitalInOut(board.D9) +lcd_backlight = digitalio.DigitalInOut(board.D13) +lcd = adafruit_character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, + lcd_d7, lcd_columns, lcd_rows, lcd_backlight) + +checkmark = bytes([0x0, 0x0, 0x1, 0x3, 0x16, 0x1c, 0x8, 0x0]) +lcd.clear() +lcd.message = "\x00" diff --git a/examples/charlcd_mono_simpletest.py b/examples/charlcd_mono_simpletest.py index 9ae6394..68eff0b 100644 --- a/examples/charlcd_mono_simpletest.py +++ b/examples/charlcd_mono_simpletest.py @@ -21,33 +21,43 @@ lcd = adafruit_character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight) -# Print a 2x line message -lcd.message('hello\ncircuitpython') +# Turn on backlight +lcd.backlight = True +# Print a two line message +lcd.message = "Hello\nCircuitPython" # Wait 5s time.sleep(5) +lcd.clear() +# Print two line message right to left +lcd.text_direction = lcd.RIGHT_TO_LEFT +lcd.message = "Hello\nCircuitPython" +# Wait 5s +time.sleep(5) +# Return text direction to left to right +lcd.text_direction = lcd.LEFT_TO_RIGHT # Demo showing cursor lcd.clear() -lcd.show_cursor(True) -lcd.message('showing cursor ') +lcd.cursor = True +lcd.message = "Cursor! " # Wait 5s time.sleep(5) # Demo showing the blinking cursor lcd.clear() -lcd.blink(True) -lcd.message('Blinky Cursor!') +lcd.blink = True +lcd.message = "Blinky Cursor!" # Wait 5s time.sleep(5) -lcd.blink(False) +lcd.blink = False # Demo scrolling message LEFT lcd.clear() -scroll_msg = 'Scroll' -lcd.message(scroll_msg) +scroll_msg = '<-- Scroll' +lcd.message = scroll_msg # Scroll to the left -for i in range(lcd_columns - len(scroll_msg)): +for i in range(len(scroll_msg)): time.sleep(0.5) lcd.move_left() -# Demo turning backlight off lcd.clear() -lcd.message("going to sleep\ncya later!") -lcd.set_backlight(False) +lcd.message = "Going to sleep\nCya later!" +# Demo turning backlight off +lcd.backlight = False time.sleep(2) diff --git a/examples/rpi_charlcd_mono_simpletest.py b/examples/charlcd_mono_simpletest_rpi.py similarity index 63% rename from examples/rpi_charlcd_mono_simpletest.py rename to examples/charlcd_mono_simpletest_rpi.py index 8ac95f4..e3ff951 100644 --- a/examples/rpi_charlcd_mono_simpletest.py +++ b/examples/charlcd_mono_simpletest_rpi.py @@ -21,33 +21,43 @@ lcd = adafruit_character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight) -# Print a 2x line message -lcd.message('hello\ncircuitpython') +# Turn on backlight +lcd.backlight = True +# Print a two line message +lcd.message = "Hello\nCircuitPython" # Wait 5s time.sleep(5) +lcd.clear() +# Print two line message right to left +lcd.text_direction = lcd.RIGHT_TO_LEFT +lcd.message = "Hello\nCircuitPython" +# Wait 5s +time.sleep(5) +# Return text direction to left to right +lcd.text_direction = lcd.LEFT_TO_RIGHT # Demo showing cursor lcd.clear() -lcd.show_cursor(True) -lcd.message('showing cursor ') +lcd.cursor = True +lcd.message = "Cursor! " # Wait 5s time.sleep(5) # Demo showing the blinking cursor lcd.clear() -lcd.blink(True) -lcd.message('Blinky Cursor!') +lcd.blink = True +lcd.message = "Blinky Cursor!" # Wait 5s time.sleep(5) -lcd.blink(False) +lcd.blink = False # Demo scrolling message LEFT lcd.clear() -scroll_msg = 'Scroll' -lcd.message(scroll_msg) +scroll_msg = '<-- Scroll' +lcd.message = scroll_msg # Scroll to the left -for i in range(lcd_columns - len(scroll_msg)): +for i in range(len(scroll_msg)): time.sleep(0.5) lcd.move_left() -# Demo turning backlight off lcd.clear() -lcd.message("going to sleep\ncya later!") -lcd.set_backlight(False) +lcd.message = "Going to sleep\nCya later!" +# Demo turning backlight off +lcd.backlight = False time.sleep(2) diff --git a/examples/charlcd_rgb_simpletest.py b/examples/charlcd_rgb_simpletest.py index 1a60aa8..5958f31 100644 --- a/examples/charlcd_rgb_simpletest.py +++ b/examples/charlcd_rgb_simpletest.py @@ -26,18 +26,56 @@ lcd_d6, lcd_d7, lcd_columns, lcd_rows, red, green, blue, lcd_backlight) - -RED = [100, 0, 0] -GREEN = [0, 100, 0] -BLUE = [0, 0, 100] - - -while True: - lcd.clear() - lcd.message('CircuitPython\nRGB Test') - lcd.set_color(RED) - time.sleep(1) - lcd.set_color(GREEN) - time.sleep(1) - lcd.set_color(BLUE) - time.sleep(1) +lcd.clear() +# Set LCD color to red +lcd.color = [100, 0, 0] +time.sleep(1) +# Print two line message +lcd.message = "Hello\nCircuitPython" +# Wait 5s +time.sleep(5) +# Set LCD color to blue +lcd.color = [0, 100, 0] +time.sleep(1) +# Set LCD color to green +lcd.color = [0, 0, 100] +time.sleep(1) +# Set LCD color to purple +lcd.color = [50, 0, 50] +time.sleep(1) +lcd.clear() +# Print two line message right to left +lcd.text_direction = lcd.RIGHT_TO_LEFT +lcd.message = "Hello\nCircuitPython" +# Wait 5s +time.sleep(5) +# Return text direction to left to right +lcd.text_direction = lcd.LEFT_TO_RIGHT +# Demo showing cursor +lcd.clear() +lcd.cursor = True +lcd.message = "Cursor! " +# Wait 5s +time.sleep(5) +# Demo showing the blinking cursor +lcd.clear() +lcd.blink = True +lcd.message = "Blinky Cursor!" +# Wait 5s +time.sleep(5) +lcd.blink = False +# Demo scrolling message LEFT +lcd.clear() +scroll_msg = '<-- Scroll' +lcd.message = scroll_msg +# Scroll to the left +for i in range(len(scroll_msg)): + time.sleep(0.5) + lcd.move_left() +lcd.clear() +time.sleep(1) +lcd.message = "Going to sleep\nCya later!" +time.sleep(5) +# Turn off LCD +lcd.color = [0, 0, 0] +lcd.clear() diff --git a/examples/rpi_charlcd_rgb_simpletest.py b/examples/charlcd_rgb_simpletest_rpi.py similarity index 83% rename from examples/rpi_charlcd_rgb_simpletest.py rename to examples/charlcd_rgb_simpletest_rpi.py index 3acadd2..0f7981b 100644 --- a/examples/rpi_charlcd_rgb_simpletest.py +++ b/examples/charlcd_rgb_simpletest_rpi.py @@ -32,16 +32,16 @@ while True: lcd.clear() - lcd.message('CircuitPython\nRGB Test: RED') - lcd.set_color(RED) + lcd.message = 'CircuitPython\nRGB Test: RED' + lcd.color = RED time.sleep(1) lcd.clear() - lcd.message('CircuitPython\nRGB Test: GREEN') - lcd.set_color(GREEN) + lcd.message = 'CircuitPython\nRGB Test: GREEN' + lcd.color = GREEN time.sleep(1) lcd.clear() - lcd.message('CircuitPython\nRGB Test: BLUE') - lcd.set_color(BLUE) + lcd.message = 'CircuitPython\nRGB Test: BLUE' + lcd.color = BLUE time.sleep(1) diff --git a/examples/customcharacter.py b/examples/customcharacter.py deleted file mode 100644 index e6c81c8..0000000 --- a/examples/customcharacter.py +++ /dev/null @@ -1,19 +0,0 @@ -import digitalio -from board import D7, D8, D9, D10, D11, D12, D13 -import adafruit_character_lcd - - -lcd_columns = 16 -lcd_rows = 2 -lcd_rs = digitalio.DigitalInOut(D7) -lcd_en = digitalio.DigitalInOut(D8) -lcd_d7 = digitalio.DigitalInOut(D12) -lcd_d6 = digitalio.DigitalInOut(D11) -lcd_d5 = digitalio.DigitalInOut(D10) -lcd_d4 = digitalio.DigitalInOut(D9) -lcd_backlight = digitalio.DigitalInOut(D13) -lcd = adafruit_character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, - lcd_d7, lcd_columns, lcd_rows, lcd_backlight) -checkmark = bytes([0x0, 0x0, 0x1, 0x3, 0x16, 0x1c, 0x8, 0x0]) -lcd.clear() -lcd.message('\x00') diff --git a/requirements.txt b/requirements.txt index 4dab381..bb4227b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ adafruit-circuitpython-busdevice -Adafruit-Blinka \ No newline at end of file +Adafruit-Blinka +adafruit-circuitpython-mcp230xx +adafruit-circuitpython-74hc595 \ No newline at end of file From 80f8222907fa4b8000ce30dcf96d1a0fab5a2c4c Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Mon, 19 Nov 2018 16:23:26 -0500 Subject: [PATCH 07/17] Example rename for rpi --- ...rlcd_mono_simpletest_rpi.py => charlcd_rpi_mono_simpletest.py} | 0 ...harlcd_rgb_simpletest_rpi.py => charlcd_rpi_rgb_simpletest.py} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename examples/{charlcd_mono_simpletest_rpi.py => charlcd_rpi_mono_simpletest.py} (100%) rename examples/{charlcd_rgb_simpletest_rpi.py => charlcd_rpi_rgb_simpletest.py} (100%) diff --git a/examples/charlcd_mono_simpletest_rpi.py b/examples/charlcd_rpi_mono_simpletest.py similarity index 100% rename from examples/charlcd_mono_simpletest_rpi.py rename to examples/charlcd_rpi_mono_simpletest.py diff --git a/examples/charlcd_rgb_simpletest_rpi.py b/examples/charlcd_rpi_rgb_simpletest.py similarity index 100% rename from examples/charlcd_rgb_simpletest_rpi.py rename to examples/charlcd_rpi_rgb_simpletest.py From 97a57fe98336313abb231398c4fe19eab8ab2539 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Mon, 19 Nov 2018 16:30:02 -0500 Subject: [PATCH 08/17] Updated example named, added RGB I2C --- ...test.py => charlcd_i2c_mono_simpletest.py} | 0 examples/charlcd_i2c_rgb_simpletest.py | 68 +++++++++++++++++++ ...test.py => charlcd_spi_mono_simpletest.py} | 0 3 files changed, 68 insertions(+) rename examples/{charlcd_I2C_simpletest.py => charlcd_i2c_mono_simpletest.py} (100%) create mode 100644 examples/charlcd_i2c_rgb_simpletest.py rename examples/{charlcd_SPI_simpletest.py => charlcd_spi_mono_simpletest.py} (100%) diff --git a/examples/charlcd_I2C_simpletest.py b/examples/charlcd_i2c_mono_simpletest.py similarity index 100% rename from examples/charlcd_I2C_simpletest.py rename to examples/charlcd_i2c_mono_simpletest.py diff --git a/examples/charlcd_i2c_rgb_simpletest.py b/examples/charlcd_i2c_rgb_simpletest.py new file mode 100644 index 0000000..0521ee9 --- /dev/null +++ b/examples/charlcd_i2c_rgb_simpletest.py @@ -0,0 +1,68 @@ +import time +import board +import busio +import adafruit_character_lcd + +# Character LCD Config: +lcd_columns = 16 +lcd_rows = 2 + +# Initialize I2C bus. +i2c = busio.I2C(board.SCL, board.SDA) + +# Init the lcd class +lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, lcd_columns, lcd_rows) + +lcd.clear() +# Set LCD color to red +lcd.color = [100, 0, 0] +time.sleep(1) +# Print two line message +lcd.message = "Hello\nCircuitPython" +# Wait 5s +time.sleep(5) +# Set LCD color to blue +lcd.color = [0, 100, 0] +time.sleep(1) +# Set LCD color to green +lcd.color = [0, 0, 100] +time.sleep(1) +# Set LCD color to purple +lcd.color = [50, 0, 50] +time.sleep(1) +lcd.clear() +# Print two line message right to left +lcd.text_direction = lcd.RIGHT_TO_LEFT +lcd.message = "Hello\nCircuitPython" +# Wait 5s +time.sleep(5) +# Return text direction to left to right +lcd.text_direction = lcd.LEFT_TO_RIGHT +# Demo showing cursor +lcd.clear() +lcd.cursor = True +lcd.message = "Cursor! " +# Wait 5s +time.sleep(5) +# Demo showing the blinking cursor +lcd.clear() +lcd.blink = True +lcd.message = "Blinky Cursor!" +# Wait 5s +time.sleep(5) +lcd.blink = False +# Demo scrolling message LEFT +lcd.clear() +scroll_msg = '<-- Scroll' +lcd.message = scroll_msg +# Scroll to the left +for i in range(len(scroll_msg)): + time.sleep(0.5) + lcd.move_left() +lcd.clear() +time.sleep(1) +lcd.message = "Going to sleep\nCya later!" +time.sleep(5) +# Turn off LCD +lcd.color = [0, 0, 0] +lcd.clear() diff --git a/examples/charlcd_SPI_simpletest.py b/examples/charlcd_spi_mono_simpletest.py similarity index 100% rename from examples/charlcd_SPI_simpletest.py rename to examples/charlcd_spi_mono_simpletest.py From 645da5fa3ec6ea18fc85e5928ec223ded69cd1d1 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Mon, 19 Nov 2018 17:12:47 -0500 Subject: [PATCH 09/17] Update package init --- adafruit_character_lcd/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_character_lcd/__init__.py b/adafruit_character_lcd/__init__.py index 6596c7e..aff6137 100644 --- a/adafruit_character_lcd/__init__.py +++ b/adafruit_character_lcd/__init__.py @@ -1,3 +1,3 @@ """include all classes""" from adafruit_character_lcd.character_lcd import Character_LCD, Character_LCD_I2C, Character_LCD_SPI -from adafruit_character_lcd.character_lcd_rgb import Character_LCD_RGB +from adafruit_character_lcd.character_lcd_rgb import Character_LCD_RGB, Character_LCD_I2C_RGB From 70f673e47d5588483aa90242e8e9810f2965f9aa Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Mon, 19 Nov 2018 18:02:35 -0500 Subject: [PATCH 10/17] Moved imports to speed up on M0 --- adafruit_character_lcd/character_lcd.py | 4 ++-- adafruit_character_lcd/character_lcd_rgb.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index 9c70067..c2fe7ba 100755 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -49,8 +49,6 @@ import time import digitalio from micropython import const -import adafruit_mcp230xx -import adafruit_74hc595 __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CharLCD.git" @@ -556,6 +554,7 @@ def __init__(self, i2c, columns, lines, backlight_inverted=False): on the specified I2C bus with the specified number of columns and lines on the display. Optionally specify if backlight is inverted. """ + import adafruit_mcp230xx self._mcp = adafruit_mcp230xx.MCP23008(i2c) reset = self._mcp.get_pin(1) enable = self._mcp.get_pin(2) @@ -583,6 +582,7 @@ def __init__(self, spi, latch, columns, lines, backlight_inverted=False): inverted. """ # pylint: enable=too-many-arguments + import adafruit_74hc595 self._shift_register = adafruit_74hc595.ShiftRegister74HC595(spi, latch) reset = self._shift_register.get_pin(1) enable = self._shift_register.get_pin(2) diff --git a/adafruit_character_lcd/character_lcd_rgb.py b/adafruit_character_lcd/character_lcd_rgb.py index d77ca2d..db092fc 100755 --- a/adafruit_character_lcd/character_lcd_rgb.py +++ b/adafruit_character_lcd/character_lcd_rgb.py @@ -48,7 +48,6 @@ import time import digitalio from micropython import const -import adafruit_mcp230xx # pylint: disable-msg=bad-whitespace # Commands @@ -572,6 +571,7 @@ def __init__(self, i2c, columns, lines): on the specified I2C bus with the specified number of columns and lines on the display. """ + import adafruit_mcp230xx self._mcp = adafruit_mcp230xx.MCP23017(i2c) reset = self._mcp.get_pin(15) read_write = self._mcp.get_pin(14) From e490b7fd1c62e48e907d6c9009a0a21d76caea0e Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 20 Nov 2018 12:24:51 -0500 Subject: [PATCH 11/17] Removed __init__.py, updated import method to resolve M0 mem issue --- adafruit_character_lcd/__init__.py | 3 - ...character_lcd.py => character_lcd_mono.py} | 63 +++++++++++++------ adafruit_character_lcd/character_lcd_rgb.py | 50 ++++++++------- examples/charlcd_custom_character_nyan_cat.py | 6 +- examples/charlcd_customcharacter.py | 9 +-- examples/charlcd_i2c_mono_simpletest.py | 28 ++++----- examples/charlcd_i2c_rgb_simpletest.py | 19 +++--- examples/charlcd_mono_simpletest.py | 24 +++---- examples/charlcd_rgb_simpletest.py | 22 +++---- examples/charlcd_rpi_mono_simpletest.py | 22 +++---- examples/charlcd_rpi_rgb_simpletest.py | 13 ++-- examples/charlcd_spi_mono_simpletest.py | 36 +++++------ 12 files changed, 158 insertions(+), 137 deletions(-) delete mode 100644 adafruit_character_lcd/__init__.py rename adafruit_character_lcd/{character_lcd.py => character_lcd_mono.py} (90%) diff --git a/adafruit_character_lcd/__init__.py b/adafruit_character_lcd/__init__.py deleted file mode 100644 index aff6137..0000000 --- a/adafruit_character_lcd/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -"""include all classes""" -from adafruit_character_lcd.character_lcd import Character_LCD, Character_LCD_I2C, Character_LCD_SPI -from adafruit_character_lcd.character_lcd_rgb import Character_LCD_RGB, Character_LCD_I2C_RGB diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd_mono.py similarity index 90% rename from adafruit_character_lcd/character_lcd.py rename to adafruit_character_lcd/character_lcd_mono.py index c2fe7ba..6054870 100755 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd_mono.py @@ -53,7 +53,7 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CharLCD.git" -#pylint: disable-msg=bad-whitespace +# pylint: disable-msg=bad-whitespace # Commands _LCD_CLEARDISPLAY = const(0x01) _LCD_RETURNHOME = const(0x02) @@ -91,6 +91,7 @@ # pylint: enable-msg=bad-whitespace + def _set_bit(byte_value, position, val): # Given the specified byte_value set the bit at position to the provided # boolean value val and return the modified byte. @@ -188,11 +189,11 @@ def clear(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_mono as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.message = "Hello, world!" time.sleep(5) @@ -212,11 +213,11 @@ def cursor(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_mono as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.cursor = True lcd.message = "Cursor! " @@ -257,11 +258,11 @@ def blink(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_mono as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.blink = True lcd.message = "Blinky cursor!" @@ -290,11 +291,11 @@ def display(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_mono as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.message = "Hello, world!" time.sleep(5) @@ -321,11 +322,11 @@ def message(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_mono as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.message = "Hello, world!" time.sleep(5) @@ -368,11 +369,11 @@ def move_left(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_mono as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) scroll_message = "<-- Scroll" lcd.message = scroll_message @@ -393,14 +394,14 @@ def move_right(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_mono as character_lcd i2c = busio.I2C(board.SCL, board.SDA) lcd_columns = 16 lcd_rows = 2 - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, lcd_columns, lcd_rows) + lcd = character_lcd.Character_LCD_I2C(i2c, lcd_columns, lcd_rows) scroll_message = "Scroll -->" lcd.message = scroll_message @@ -425,11 +426,11 @@ def text_direction(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_mono as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.text_direction = lcd.RIGHT_TO_LEFT lcd.message = "Hello, world!" @@ -467,11 +468,11 @@ def backlight(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_mono as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.backlight = False lcd.message = "Hello, world?" @@ -548,6 +549,17 @@ class Character_LCD_I2C(Character_LCD): """Character LCD connected to I2C/SPI backpack using its I2C connection. This is a subclass of Character_LCD and implements all of the same functions and functionality. + + To use, import and initialise as follows: + + .. code-block:: python + + import board + import busio + import adafruit_character_lcd.character_lcd_mono as character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) """ def __init__(self, i2c, columns, lines, backlight_inverted=False): """Initialize character LCD connectedto backpack using I2C connection @@ -572,6 +584,19 @@ class Character_LCD_SPI(Character_LCD): """Character LCD connected to I2C/SPI backpack using its SPI connection. This is a subclass of Character_LCD and implements all of the same functions and functionality. + + To use, import and initialise as follows: + + .. code-block:: python + + import board + import busio + import digitalio + import adafruit_character_lcd.character_lcd_mono as character_lcd + + spi = busio.SPI(board.SCK, MOSI=board.MOSI) + latch = digitalio.DigitalInOut(board.D5) + lcd = character_lcd.Character_LCD_SPI(spi, latch, 16, 2) """ def __init__(self, spi, latch, columns, lines, backlight_inverted=False): diff --git a/adafruit_character_lcd/character_lcd_rgb.py b/adafruit_character_lcd/character_lcd_rgb.py index db092fc..0c12bcc 100755 --- a/adafruit_character_lcd/character_lcd_rgb.py +++ b/adafruit_character_lcd/character_lcd_rgb.py @@ -202,11 +202,11 @@ def clear(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_rgb as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) lcd.message = "Hello, world!" time.sleep(5) @@ -226,11 +226,11 @@ def cursor(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_rgb as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) lcd.cursor = True lcd.message = "Cursor! " @@ -271,11 +271,11 @@ def blink(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_rgb as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.blink = True lcd.message = "Blinky cursor!" @@ -309,11 +309,11 @@ def color(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_rgb as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) lcd.color = [100, 0, 0] lcd.message = "Hello, world!" @@ -346,11 +346,11 @@ def display(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_rgb as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.message = "Hello, world!" time.sleep(5) @@ -377,11 +377,11 @@ def message(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_rgb as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.message = "Hello, world!" time.sleep(5) @@ -420,11 +420,11 @@ def move_left(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_rgb as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) scroll_message = "<-- Scroll" lcd.message = scroll_message @@ -445,14 +445,11 @@ def move_right(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_rgb as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd_columns = 16 - lcd_rows = 2 - - lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, lcd_columns, lcd_rows) + lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) scroll_message = "Scroll -->" lcd.message = scroll_message @@ -477,11 +474,11 @@ def text_direction(self): import time import board import busio - import adafruit_character_lcd + import adafruit_character_lcd.character_lcd_rgb as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) lcd.text_direction = lcd.RIGHT_TO_LEFT lcd.message = "Hello, world!" @@ -565,6 +562,17 @@ class Character_LCD_I2C_RGB(Character_LCD_RGB): """RGB Character LCD connected to I2C shield using I2C connection. This is a subclass of Character_LCD_RGB and implements all of the same functions and functionality. + + To use, import and initialise as follows: + + .. code-block:: python + + import board + import busio + import adafruit_character_lcd.character_lcd_rgb as character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) """ def __init__(self, i2c, columns, lines): """Initialize RGB character LCD connected to shield using I2C connection diff --git a/examples/charlcd_custom_character_nyan_cat.py b/examples/charlcd_custom_character_nyan_cat.py index 9481d8d..607e3d4 100755 --- a/examples/charlcd_custom_character_nyan_cat.py +++ b/examples/charlcd_custom_character_nyan_cat.py @@ -1,7 +1,7 @@ import time import board import digitalio -import adafruit_character_lcd +import adafruit_character_lcd.character_lcd_mono as character_lcd lcd_columns = 16 lcd_rows = 2 @@ -12,8 +12,8 @@ lcd_d5 = digitalio.DigitalInOut(board.D10) lcd_d4 = digitalio.DigitalInOut(board.D9) lcd_backlight = digitalio.DigitalInOut(board.D13) -lcd = adafruit_character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, - lcd_d7, lcd_columns, lcd_rows, lcd_backlight) +lcd = character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, + lcd_d7, lcd_columns, lcd_rows, lcd_backlight) head = [31, 17, 27, 17, 17, 21, 17, 31] diff --git a/examples/charlcd_customcharacter.py b/examples/charlcd_customcharacter.py index d366d25..97a658b 100644 --- a/examples/charlcd_customcharacter.py +++ b/examples/charlcd_customcharacter.py @@ -1,10 +1,10 @@ import digitalio import board -import adafruit_character_lcd - +import adafruit_character_lcd.character_lcd_mono as character_lcd lcd_columns = 16 lcd_rows = 2 + lcd_rs = digitalio.DigitalInOut(board.D7) lcd_en = digitalio.DigitalInOut(board.D8) lcd_d7 = digitalio.DigitalInOut(board.D12) @@ -12,8 +12,9 @@ lcd_d5 = digitalio.DigitalInOut(board.D10) lcd_d4 = digitalio.DigitalInOut(board.D9) lcd_backlight = digitalio.DigitalInOut(board.D13) -lcd = adafruit_character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, - lcd_d7, lcd_columns, lcd_rows, lcd_backlight) + +lcd = character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, + lcd_d7, lcd_columns, lcd_rows, lcd_backlight) checkmark = bytes([0x0, 0x0, 0x1, 0x3, 0x16, 0x1c, 0x8, 0x0]) lcd.clear() diff --git a/examples/charlcd_i2c_mono_simpletest.py b/examples/charlcd_i2c_mono_simpletest.py index 9502acc..954b324 100644 --- a/examples/charlcd_i2c_mono_simpletest.py +++ b/examples/charlcd_i2c_mono_simpletest.py @@ -1,24 +1,20 @@ -# Hello World using 16x2 character lcd and an MCP23008 I2C LCD backpack. +# Simple test for 16x2 character lcd connected to an MCP23008 I2C LCD backpack. import time - import board import busio +import adafruit_character_lcd.character_lcd_mono as character_lcd -import adafruit_character_lcd - - -# Character LCD Config: -# modify this if you have a different sized charlcd +# Modify this if you have a different sized Character LCD lcd_columns = 16 lcd_rows = 2 -# Initialize I2C bus. +# Initialise I2C bus. i2c = busio.I2C(board.SCL, board.SDA) -# Init the lcd class -lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, lcd_columns, lcd_rows) +# Initialise the lcd class +lcd = character_lcd.Character_LCD_I2C(i2c, lcd_columns, lcd_rows) -# Turn on backlight +# Turn backlight on lcd.backlight = True # Print a two line message lcd.message = "Hello\nCircuitPython" @@ -32,29 +28,29 @@ time.sleep(5) # Return text direction to left to right lcd.text_direction = lcd.LEFT_TO_RIGHT -# Demo showing cursor +# Display cursor lcd.clear() lcd.cursor = True lcd.message = "Cursor! " # Wait 5s time.sleep(5) -# Demo showing the blinking cursor +# Display blinking cursor lcd.clear() lcd.blink = True lcd.message = "Blinky Cursor!" # Wait 5s time.sleep(5) lcd.blink = False -# Demo scrolling message LEFT lcd.clear() +# Create message to scroll scroll_msg = '<-- Scroll' lcd.message = scroll_msg -# Scroll to the left +# Scroll message to the left for i in range(len(scroll_msg)): time.sleep(0.5) lcd.move_left() lcd.clear() lcd.message = "Going to sleep\nCya later!" -# Demo turning backlight off +# Turn backlight off lcd.backlight = False time.sleep(2) diff --git a/examples/charlcd_i2c_rgb_simpletest.py b/examples/charlcd_i2c_rgb_simpletest.py index 0521ee9..901a15e 100644 --- a/examples/charlcd_i2c_rgb_simpletest.py +++ b/examples/charlcd_i2c_rgb_simpletest.py @@ -1,17 +1,18 @@ +# Simple test for I2C RGB character LCD shield kit import time import board import busio -import adafruit_character_lcd +import adafruit_character_lcd.character_lcd_rgb as character_lcd -# Character LCD Config: +# Modify this if you have a different sized Character LCD lcd_columns = 16 lcd_rows = 2 -# Initialize I2C bus. +# Initialise I2C bus. i2c = busio.I2C(board.SCL, board.SDA) -# Init the lcd class -lcd = adafruit_character_lcd.Character_LCD_I2C_RGB(i2c, lcd_columns, lcd_rows) +# Initialise the LCD class +lcd = character_lcd.Character_LCD_I2C_RGB(i2c, lcd_columns, lcd_rows) lcd.clear() # Set LCD color to red @@ -38,21 +39,21 @@ time.sleep(5) # Return text direction to left to right lcd.text_direction = lcd.LEFT_TO_RIGHT -# Demo showing cursor +# Display cursor lcd.clear() lcd.cursor = True lcd.message = "Cursor! " # Wait 5s time.sleep(5) -# Demo showing the blinking cursor +# Display blinking cursor lcd.clear() lcd.blink = True lcd.message = "Blinky Cursor!" # Wait 5s time.sleep(5) lcd.blink = False -# Demo scrolling message LEFT lcd.clear() +# Create message to scroll scroll_msg = '<-- Scroll' lcd.message = scroll_msg # Scroll to the left @@ -63,6 +64,6 @@ time.sleep(1) lcd.message = "Going to sleep\nCya later!" time.sleep(5) -# Turn off LCD +# Turn off LCD backlights and clear text lcd.color = [0, 0, 0] lcd.clear() diff --git a/examples/charlcd_mono_simpletest.py b/examples/charlcd_mono_simpletest.py index 68eff0b..47520eb 100644 --- a/examples/charlcd_mono_simpletest.py +++ b/examples/charlcd_mono_simpletest.py @@ -1,14 +1,14 @@ +# Simple test for monochromatic character LCD import time import board import digitalio -import adafruit_character_lcd +import adafruit_character_lcd.character_lcd_mono as character_lcd -# Character LCD Config: # Modify this if you have a different sized character LCD lcd_columns = 16 lcd_rows = 2 -# Metro M0 Pin Config: +# Metro M0/M4 Pin Config: lcd_rs = digitalio.DigitalInOut(board.D7) lcd_en = digitalio.DigitalInOut(board.D8) lcd_d7 = digitalio.DigitalInOut(board.D12) @@ -17,11 +17,11 @@ lcd_d4 = digitalio.DigitalInOut(board.D9) lcd_backlight = digitalio.DigitalInOut(board.D13) -# Init the LCD class -lcd = adafruit_character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, - lcd_d7, lcd_columns, lcd_rows, lcd_backlight) +# Initialise the LCD class +lcd = character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, + lcd_d7, lcd_columns, lcd_rows, lcd_backlight) -# Turn on backlight +# Turn backlight on lcd.backlight = True # Print a two line message lcd.message = "Hello\nCircuitPython" @@ -35,29 +35,29 @@ time.sleep(5) # Return text direction to left to right lcd.text_direction = lcd.LEFT_TO_RIGHT -# Demo showing cursor +# Display cursor lcd.clear() lcd.cursor = True lcd.message = "Cursor! " # Wait 5s time.sleep(5) -# Demo showing the blinking cursor +# Display blinking cursor lcd.clear() lcd.blink = True lcd.message = "Blinky Cursor!" # Wait 5s time.sleep(5) lcd.blink = False -# Demo scrolling message LEFT lcd.clear() +# Create message to scroll scroll_msg = '<-- Scroll' lcd.message = scroll_msg -# Scroll to the left +# Scroll message to the left for i in range(len(scroll_msg)): time.sleep(0.5) lcd.move_left() lcd.clear() lcd.message = "Going to sleep\nCya later!" -# Demo turning backlight off +# Turn backlight off lcd.backlight = False time.sleep(2) diff --git a/examples/charlcd_rgb_simpletest.py b/examples/charlcd_rgb_simpletest.py index 5958f31..4671f6a 100644 --- a/examples/charlcd_rgb_simpletest.py +++ b/examples/charlcd_rgb_simpletest.py @@ -1,30 +1,28 @@ +# Simple test for RGB character LCD import time import board import digitalio -import adafruit_character_lcd import pulseio +import adafruit_character_lcd.character_lcd_rgb as character_lcd -# Character LCD Config: # Modify this if you have a different sized character LCD lcd_columns = 16 lcd_rows = 2 -# Metro M0 Pin Config: +# Metro M0/M4 Pin Config: lcd_rs = digitalio.DigitalInOut(board.D7) lcd_en = digitalio.DigitalInOut(board.D8) lcd_d7 = digitalio.DigitalInOut(board.D12) lcd_d6 = digitalio.DigitalInOut(board.D11) lcd_d5 = digitalio.DigitalInOut(board.D10) lcd_d4 = digitalio.DigitalInOut(board.D9) -lcd_backlight = digitalio.DigitalInOut(board.D13) red = pulseio.PWMOut(board.D3) green = pulseio.PWMOut(board.D5) blue = pulseio.PWMOut(board.D6) -# Init the LCD class -lcd = adafruit_character_lcd.Character_LCD_RGB(lcd_rs, lcd_en, lcd_d4, lcd_d5, - lcd_d6, lcd_d7, lcd_columns, lcd_rows, - red, green, blue, lcd_backlight) +# Initialise the LCD class +lcd = character_lcd.Character_LCD_RGB(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, + lcd_rows, red, green, blue) lcd.clear() # Set LCD color to red @@ -51,21 +49,21 @@ time.sleep(5) # Return text direction to left to right lcd.text_direction = lcd.LEFT_TO_RIGHT -# Demo showing cursor +# Display cursor lcd.clear() lcd.cursor = True lcd.message = "Cursor! " # Wait 5s time.sleep(5) -# Demo showing the blinking cursor +# Display blinking cursor lcd.clear() lcd.blink = True lcd.message = "Blinky Cursor!" # Wait 5s time.sleep(5) lcd.blink = False -# Demo scrolling message LEFT lcd.clear() +# Create message to scroll scroll_msg = '<-- Scroll' lcd.message = scroll_msg # Scroll to the left @@ -76,6 +74,6 @@ time.sleep(1) lcd.message = "Going to sleep\nCya later!" time.sleep(5) -# Turn off LCD +# Turn off LCD backlights and clear text lcd.color = [0, 0, 0] lcd.clear() diff --git a/examples/charlcd_rpi_mono_simpletest.py b/examples/charlcd_rpi_mono_simpletest.py index e3ff951..3d64851 100644 --- a/examples/charlcd_rpi_mono_simpletest.py +++ b/examples/charlcd_rpi_mono_simpletest.py @@ -1,9 +1,9 @@ +# Simple test for monochromatic character LCD on Raspberry Pi import time import board import digitalio -import adafruit_character_lcd +import adafruit_character_lcd.character_lcd_mono as character_lcd -# Character LCD Config: # Modify this if you have a different sized character LCD lcd_columns = 16 lcd_rows = 2 @@ -17,11 +17,11 @@ lcd_d4 = digitalio.DigitalInOut(board.D25) lcd_backlight = digitalio.DigitalInOut(board.D4) -# Init the lcd class -lcd = adafruit_character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, - lcd_d7, lcd_columns, lcd_rows, lcd_backlight) +# Initialise the lcd class +lcd = character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, + lcd_d7, lcd_columns, lcd_rows, lcd_backlight) -# Turn on backlight +# Turn backlight on lcd.backlight = True # Print a two line message lcd.message = "Hello\nCircuitPython" @@ -35,29 +35,29 @@ time.sleep(5) # Return text direction to left to right lcd.text_direction = lcd.LEFT_TO_RIGHT -# Demo showing cursor +# Display cursor lcd.clear() lcd.cursor = True lcd.message = "Cursor! " # Wait 5s time.sleep(5) -# Demo showing the blinking cursor +# Display blinking cursor lcd.clear() lcd.blink = True lcd.message = "Blinky Cursor!" # Wait 5s time.sleep(5) lcd.blink = False -# Demo scrolling message LEFT lcd.clear() +# Create message to scroll scroll_msg = '<-- Scroll' lcd.message = scroll_msg -# Scroll to the left +# Scroll message to the left for i in range(len(scroll_msg)): time.sleep(0.5) lcd.move_left() lcd.clear() lcd.message = "Going to sleep\nCya later!" -# Demo turning backlight off +# Turn backlight off lcd.backlight = False time.sleep(2) diff --git a/examples/charlcd_rpi_rgb_simpletest.py b/examples/charlcd_rpi_rgb_simpletest.py index 0f7981b..4bea306 100644 --- a/examples/charlcd_rpi_rgb_simpletest.py +++ b/examples/charlcd_rpi_rgb_simpletest.py @@ -1,10 +1,10 @@ +# Simple test for RGB character LCD on Raspberry Pi import time import board import digitalio -import adafruit_character_lcd +import adafruit_character_lcd.character_lcd_rgb as character_lcd -# Character LCD Config: -# modify this if you have a different sized charlcd +# Modify this if you have a different sized character LCD lcd_columns = 16 lcd_rows = 2 @@ -21,10 +21,9 @@ green = digitalio.DigitalInOut(board.D12) blue = digitalio.DigitalInOut(board.D18) -# Init the lcd class -lcd = adafruit_character_lcd.Character_LCD_RGB(lcd_rs, lcd_en, lcd_d4, lcd_d5, - lcd_d6, lcd_d7, lcd_columns, lcd_rows, - red, green, blue, lcd_backlight) +# Initialise the LCD class +lcd = character_lcd.Character_LCD_RGB(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, + lcd_rows, red, green, blue, lcd_backlight) RED = [1, 0, 0] GREEN = [0, 1, 0] diff --git a/examples/charlcd_spi_mono_simpletest.py b/examples/charlcd_spi_mono_simpletest.py index 4cd9cd9..01675ac 100644 --- a/examples/charlcd_spi_mono_simpletest.py +++ b/examples/charlcd_spi_mono_simpletest.py @@ -1,31 +1,27 @@ -# Hello World using 16x2 character lcd and an 74LS595 SPI LCD backpack. +# Simple test for 16x2 character LCD connected to 74HC595 SPI LCD backpack. import time - import board import busio import digitalio +import adafruit_character_lcd.character_lcd_mono as character_lcd -import adafruit_character_lcd - - -# Character LCD Config: -# modify this if you have a different sized charlcd +# Modify this if you have a different sized character LCD lcd_columns = 16 lcd_rows = 2 -# Backpack connection configuration: -clk = board.SCK # Pin connected to backpack CLK. -data = board.MOSI # Pin connected to backpack DAT/data. -latch = board.D5 # Pin connected to backpack LAT/latch. +# Backpack connection configuration: +clk = board.SCK # Pin connected to backpack CLK. +data = board.MOSI # Pin connected to backpack DAT/data. +latch = board.D5 # Pin connected to backpack LAT/latch. -# Initialize SPI bus. +# Initialise SPI bus. spi = busio.SPI(clk, MOSI=data) -# Init the lcd class +# Initialise the LCD class latch = digitalio.DigitalInOut(latch) -lcd = adafruit_character_lcd.Character_LCD_SPI(spi, latch, lcd_columns, lcd_rows) +lcd = character_lcd.Character_LCD_SPI(spi, latch, lcd_columns, lcd_rows) -# Turn on backlight +# Turn backlight on lcd.backlight = True # Print a two line message lcd.message = "Hello\nCircuitPython" @@ -39,29 +35,29 @@ time.sleep(5) # Return text direction to left to right lcd.text_direction = lcd.LEFT_TO_RIGHT -# Demo showing cursor +# Display cursor lcd.clear() lcd.cursor = True lcd.message = "Cursor! " # Wait 5s time.sleep(5) -# Demo showing the blinking cursor +# Display blinking cursor lcd.clear() lcd.blink = True lcd.message = "Blinky Cursor!" # Wait 5s time.sleep(5) lcd.blink = False -# Demo scrolling message LEFT lcd.clear() +# Create message to scroll scroll_msg = '<-- Scroll' lcd.message = scroll_msg -# Scroll to the left +# Scroll message to the left for i in range(len(scroll_msg)): time.sleep(0.5) lcd.move_left() lcd.clear() lcd.message = "Going to sleep\nCya later!" -# Demo turning backlight off +# Turn backlight off lcd.backlight = False time.sleep(2) From ee9544a36aa9e4e569f71dc7fbc20f077a7adfab Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 20 Nov 2018 15:48:04 -0500 Subject: [PATCH 12/17] RGB subclass currently functional. --- ...character_lcd_mono.py => character_lcd.py} | 255 ++++++++++++++---- adafruit_character_lcd/character_lcd_rgb.py | 14 +- 2 files changed, 206 insertions(+), 63 deletions(-) rename adafruit_character_lcd/{character_lcd_mono.py => character_lcd.py} (77%) diff --git a/adafruit_character_lcd/character_lcd_mono.py b/adafruit_character_lcd/character_lcd.py similarity index 77% rename from adafruit_character_lcd/character_lcd_mono.py rename to adafruit_character_lcd/character_lcd.py index 6054870..63eb7af 100755 --- a/adafruit_character_lcd/character_lcd_mono.py +++ b/adafruit_character_lcd/character_lcd.py @@ -103,6 +103,18 @@ def _set_bit(byte_value, position, val): return ret +def _map(xval, in_min, in_max, out_min, out_max): + # Affine transfer/map with constrained output. + outrange = float(out_max - out_min) + inrange = float(in_max - in_min) + ret = (xval - in_min) * (outrange / inrange) + out_min + if out_max > out_min: + ret = max(min(ret, out_max), out_min) + else: + ret = max(min(ret, out_min), out_max) + return ret + + # pylint: disable-msg=too-many-instance-attributes class Character_LCD: """Base class for character LCD.""" @@ -125,9 +137,7 @@ class Character_LCD: """ # pylint: disable-msg=too-many-arguments - def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, - backlight_pin=None, - backlight_inverted=False + def __init__(self, rs, en, d4, d5, d6, d7, columns, lines ): self.columns = columns @@ -139,33 +149,25 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, self.dl5 = d5 self.dl6 = d6 self.dl7 = d7 - # backlight pin - self.backlight_pin = backlight_pin - self.backlight_inverted = backlight_inverted + # set all pins as outputs for pin in(rs, en, d4, d5, d6, d7): pin.direction = digitalio.Direction.OUTPUT - # Setup backlight - if backlight_pin is not None: - self.backlight_pin.direction = digitalio.Direction.OUTPUT - if backlight_inverted: - self.backlight_pin.value = 0 # turn backlight on - else: - self.backlight_pin.value = 1 # turn backlight on - # initialize the display + + # Initialise the display self._write8(0x33) self._write8(0x32) - # init. display control + # Initialise display control self.displaycontrol = _LCD_DISPLAYON | _LCD_CURSOROFF | _LCD_BLINKOFF - # init display function + # Initialise display function self.displayfunction = _LCD_4BITMODE | _LCD_1LINE | _LCD_2LINE | _LCD_5X8DOTS - # init display mode + # Initialise display mode self.displaymode = _LCD_ENTRYLEFT | _LCD_ENTRYSHIFTDECREMENT - # write to display control + # Write to displaycontrol self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) - # write displayfunction + # Write to displayfunction self._write8(_LCD_FUNCTIONSET | self.displayfunction) - # set the entry mode + # Set entry mode self._write8(_LCD_ENTRYMODESET | self.displaymode) self.clear() @@ -456,42 +458,6 @@ def _right_to_left(self): self.displaymode &= ~_LCD_ENTRYLEFT self._write8(_LCD_ENTRYMODESET | self.displaymode) - @property - def backlight(self): - """Enable or disable backlight. True if backlight is on. False if backlight is off. - - The following example turns the backlight off, then displays, "Hello, world?", then turns - the backlight on and displays, "Hello, world!" - - .. code-block:: python - - import time - import board - import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) - - lcd.backlight = False - lcd.message = "Hello, world?" - time.sleep(5) - lcd.backlight = True - lcd.message = "Hello, world!" - time.sleep(5) - - """ - return self._enable - - @backlight.setter - def backlight(self, enable): - self._enable = enable - if enable and not self.backlight_inverted or not enable and self.backlight_inverted: - self.backlight_pin.value = 1 - if enable and self.backlight_inverted or not enable and not self.backlight_inverted: - self.backlight_pin.value = 0 - def create_char(self, location, pattern): """ Fill one of the first 8 CGRAM locations with custom characters. @@ -545,6 +511,183 @@ def _pulse_enable(self): # pylint: enable-msg=too-many-instance-attributes +# pylint: disable-msg=too-many-instance-attributes +class Character_LCD_Mono(Character_LCD): + """Base class for character LCD.""" + """ + Interfaces with a character LCD + :param ~digitalio.DigitalInOut rs: The reset data line + :param ~digitalio.DigitalInOut en: The enable data line + :param ~digitalio.DigitalInOut d4: The data line 4 + :param ~digitalio.DigitalInOut d5: The data line 5 + :param ~digitalio.DigitalInOut d6: The data line 6 + :param ~digitalio.DigitalInOut d7: The data line 7 + :param columns: The columns on the charLCD + :param lines: The lines on the charLCD + :param ~digitalio.DigitalInOut backlight_pin: The backlight pin, usually + the last pin. Check with your datasheet + :param bool backlight_inverted: False if LCD is not inverted, i.e. backlight pin is connected + to common anode. True if LCD is inverted i.e. backlight pin is connected to common cathode. + + """ + # pylint: disable-msg=too-many-arguments + def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, + backlight_pin=None, backlight_inverted=False): + reset = rs + enable = en + db4 = d4 + db5 = d5 + db6 = d6 + db7 = d7 + + # Backlight pin and inversion + self.backlight_pin = backlight_pin + self.backlight_inverted = backlight_inverted + + # Setup backlight + if backlight_pin is not None: + self.backlight_pin.direction = digitalio.Direction.OUTPUT + if backlight_inverted: + self.backlight_pin.value = 0 # Turn inverted backlight on + else: + self.backlight_pin.value = 1 # Turn backlight on + super().__init__(reset, enable, db4, db5, db6, db7, columns, lines) + # pylint: enable-msg=too-many-arguments + + @property + def backlight(self): + """Enable or disable backlight. True if backlight is on. False if backlight is off. + + The following example turns the backlight off, then displays, "Hello, world?", then turns + the backlight on and displays, "Hello, world!" + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd.character_lcd_mono as character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) + + lcd.backlight = False + lcd.message = "Hello, world?" + time.sleep(5) + lcd.backlight = True + lcd.message = "Hello, world!" + time.sleep(5) + + """ + return self._enable + + @backlight.setter + def backlight(self, enable): + self._enable = enable + if enable and not self.backlight_inverted or not enable and self.backlight_inverted: + self.backlight_pin.value = 1 + if enable and self.backlight_inverted or not enable and not self.backlight_inverted: + self.backlight_pin.value = 0 + + +class Character_LCD_RGB(Character_LCD): + """Base class for RGB character LCD.""" + + """ Interfaces with an RGB character LCD + :param ~digitalio.DigitalInOut rs: The reset data line + :param ~digitalio.DigitalInOut en: The enable data line + :param ~digitalio.DigitalInOut d4: The data line 4 + :param ~digitalio.DigitalInOut d5: The data line 5 + :param ~digitalio.DigitalInOut d6: The data line 6 + :param ~digitalio.DigitalInOut d7: The data line 7 + :param columns: The columns on the charLCD + :param lines: The lines on the charLCD + :param ~pulseio.PWMOut, ~digitalio.DigitalInOut red: Red RGB Anode + :param ~pulseio.PWMOut, ~digitalio.DigitalInOut green: Green RGB Anode + :param ~pulseio.PWMOut, ~digitalio.DigitalInOut blue: Blue RGB Anode + :param ~digitalio.DigitalInOut read_write: The rw pin. Determines whether to read to or + write from the display. Not necessary if only writing to the display. Used on shield. + + """ + # pylint: disable-msg=too-many-arguments + def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, red, green, blue, read_write=None): + reset = rs + enable = en + db4 = d4 + db5 = d5 + db6 = d6 + db7 = d7 + + # Define read_write (rw) pin + self.read_write = read_write + + # Setup rw pin if used + if read_write is not None: + self.read_write.direction = digitalio.Direction.OUTPUT + + # define color params + self.red = red + self.green = green + self.blue = blue + self.rgb_led = [red, green, blue] + + for pin in self.rgb_led: + if hasattr(pin, 'direction'): + # Assume a digitalio.DigitalInOut or compatible interface: + pin.direction = digitalio.Direction.OUTPUT + elif not hasattr(pin, 'duty_cycle'): + raise TypeError( + 'RGB LED objects must be instances of digitalio.DigitalInOut' + ' or pulseio.PWMOut, or provide a compatible interface.' + ) + + self._color = [0, 0, 0] + super().__init__(reset, enable, db4, db5, db6, db7, columns, lines) + + @property + def color(self): + """ + The color of the display. Provide a list of three integers ranging 0 - 100, ``[R, G, B]``. + ``0`` is no color, or "off". ``100`` is maximum color. For example, the brightest red would + be ``[100, 0, 0]``, and a half-bright purple would be, ``[50, 0, 50]``. + + If PWM is unavailable, ``0`` is off, and non-zero is on. For example, ``[1, 0, 0]`` would + be red. + + The following example turns the LCD red and displays, "Hello, world!". + + .. code-block:: python + + import time + import board + import busio + import adafruit_character_lcd.character_lcd_rgb as character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + + lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + + lcd.color = [100, 0, 0] + lcd.message = "Hello, world!" + time.sleep(5) + """ + return self._color + + @color.setter + def color(self, color): + self._color = color + for number, pin in enumerate(self.rgb_led): + if hasattr(pin, 'duty_cycle'): + # Assume a pulseio.PWMOut or compatible interface and set duty cycle: + pin.duty_cycle = int(_map(color[number], 0, 100, 65535, 0)) + elif hasattr(pin, 'value'): + # If we don't have a PWM interface, all we can do is turn each color + # on / off. Assume a DigitalInOut (or compatible interface) and write + # 0 (on) to pin for any value greater than 0, or 1 (off) for 0: + pin.value = 0 if color[number] > 0 else 1 + + class Character_LCD_I2C(Character_LCD): """Character LCD connected to I2C/SPI backpack using its I2C connection. This is a subclass of Character_LCD and implements all of the same diff --git a/adafruit_character_lcd/character_lcd_rgb.py b/adafruit_character_lcd/character_lcd_rgb.py index 0c12bcc..eae7baf 100755 --- a/adafruit_character_lcd/character_lcd_rgb.py +++ b/adafruit_character_lcd/character_lcd_rgb.py @@ -165,20 +165,20 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, ' or pulseio.PWMOut, or provide a compatible interface.' ) - # initialize the display + # Initialise the display self._write8(0x33) self._write8(0x32) - # init. display control + # Initialise display control self.displaycontrol = _LCD_DISPLAYON | _LCD_CURSOROFF | _LCD_BLINKOFF - # init display function + # Initialise display function self.displayfunction = _LCD_4BITMODE | _LCD_1LINE | _LCD_2LINE | _LCD_5X8DOTS - # init display mode + # Initialise display mode self.displaymode = _LCD_ENTRYLEFT | _LCD_ENTRYSHIFTDECREMENT - # write to display control + # Write to displaycontrol self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) - # write displayfunction + # Write to displayfunction self._write8(_LCD_FUNCTIONSET | self.displayfunction) - # set the entry mode + # Set entry mode self._write8(_LCD_ENTRYMODESET | self.displaymode) self.clear() From 6e064f91a0d34ccfce688fb83effed72315572f3 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 20 Nov 2018 16:32:26 -0500 Subject: [PATCH 13/17] I2C not currently functional. --- adafruit_character_lcd/character_lcd.py | 75 ------------------------- adafruit_character_lcd/rgb_i2c.py | 38 +++++++++++++ 2 files changed, 38 insertions(+), 75 deletions(-) create mode 100644 adafruit_character_lcd/rgb_i2c.py diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index 63eb7af..cc692c9 100755 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -687,78 +687,3 @@ def color(self, color): # 0 (on) to pin for any value greater than 0, or 1 (off) for 0: pin.value = 0 if color[number] > 0 else 1 - -class Character_LCD_I2C(Character_LCD): - """Character LCD connected to I2C/SPI backpack using its I2C connection. - This is a subclass of Character_LCD and implements all of the same - functions and functionality. - - To use, import and initialise as follows: - - .. code-block:: python - - import board - import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) - """ - def __init__(self, i2c, columns, lines, backlight_inverted=False): - """Initialize character LCD connectedto backpack using I2C connection - on the specified I2C bus with the specified number of columns and - lines on the display. Optionally specify if backlight is inverted. - """ - import adafruit_mcp230xx - self._mcp = adafruit_mcp230xx.MCP23008(i2c) - reset = self._mcp.get_pin(1) - enable = self._mcp.get_pin(2) - db4 = self._mcp.get_pin(3) - db5 = self._mcp.get_pin(4) - db6 = self._mcp.get_pin(5) - db7 = self._mcp.get_pin(6) - backlight_pin = self._mcp.get_pin(7) - self.backlight_inverted = backlight_inverted - super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, - backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) - - -class Character_LCD_SPI(Character_LCD): - """Character LCD connected to I2C/SPI backpack using its SPI connection. - This is a subclass of Character_LCD and implements all of the same - functions and functionality. - - To use, import and initialise as follows: - - .. code-block:: python - - import board - import busio - import digitalio - import adafruit_character_lcd.character_lcd_mono as character_lcd - - spi = busio.SPI(board.SCK, MOSI=board.MOSI) - latch = digitalio.DigitalInOut(board.D5) - lcd = character_lcd.Character_LCD_SPI(spi, latch, 16, 2) - """ - - def __init__(self, spi, latch, columns, lines, backlight_inverted=False): - # pylint: disable=too-many-arguments - """Initialize character LCD connected to backpack using SPI connection - on the specified SPI bus and latch line with the specified number of - columns and lines on the display. Optionally specify if backlight is - inverted. - """ - # pylint: enable=too-many-arguments - import adafruit_74hc595 - self._shift_register = adafruit_74hc595.ShiftRegister74HC595(spi, latch) - reset = self._shift_register.get_pin(1) - enable = self._shift_register.get_pin(2) - db4 = self._shift_register.get_pin(6) - db5 = self._shift_register.get_pin(5) - db6 = self._shift_register.get_pin(4) - db7 = self._shift_register.get_pin(3) - backlight_pin = self._shift_register.get_pin(7) - self.backlight_inverted = backlight_inverted - super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, - backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) diff --git a/adafruit_character_lcd/rgb_i2c.py b/adafruit_character_lcd/rgb_i2c.py new file mode 100644 index 0000000..9c3a9b3 --- /dev/null +++ b/adafruit_character_lcd/rgb_i2c.py @@ -0,0 +1,38 @@ +from adafruit_character_lcd.character_lcd import Character_LCD_RGB + + +class Character_LCD_I2C_RGB(Character_LCD_RGB): + """RGB Character LCD connected to I2C shield using I2C connection. + This is a subclass of Character_LCD_RGB and implements all of the same + functions and functionality. + + To use, import and initialise as follows: + + .. code-block:: python + + import board + import busio + import adafruit_character_lcd.character_lcd_rgb as character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + """ + def __init__(self, i2c, columns, lines): + """Initialize RGB character LCD connected to shield using I2C connection + on the specified I2C bus with the specified number of columns and lines + on the display. + """ + import adafruit_mcp230xx + self._mcp = adafruit_mcp230xx.MCP23017(i2c) + reset = self._mcp.get_pin(15) + read_write = self._mcp.get_pin(14) + enable = self._mcp.get_pin(13) + db4 = self._mcp.get_pin(12) + db5 = self._mcp.get_pin(11) + db6 = self._mcp.get_pin(10) + db7 = self._mcp.get_pin(9) + red = self._mcp.get_pin(6) + green = self._mcp.get_pin(7) + blue = self._mcp.get_pin(8) + super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, red, green, blue, + read_write) From 95a9f14c8d2518ac0610068903a25b2f6b1e3b2d Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 20 Nov 2018 17:42:08 -0500 Subject: [PATCH 14/17] SPI working, I2C not. --- adafruit_character_lcd/character_lcd.py | 24 +++----------- adafruit_character_lcd/mono_i2c.py | 36 +++++++++++++++++++++ adafruit_character_lcd/mono_spi.py | 42 +++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 adafruit_character_lcd/mono_i2c.py create mode 100644 adafruit_character_lcd/mono_spi.py diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index cc692c9..f229401 100755 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -531,14 +531,8 @@ class Character_LCD_Mono(Character_LCD): """ # pylint: disable-msg=too-many-arguments - def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, + def __init__(self, rs, en, db4, db5, db6, db7, columns, lines, backlight_pin=None, backlight_inverted=False): - reset = rs - enable = en - db4 = d4 - db5 = d5 - db6 = d6 - db7 = d7 # Backlight pin and inversion self.backlight_pin = backlight_pin @@ -548,10 +542,8 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, if backlight_pin is not None: self.backlight_pin.direction = digitalio.Direction.OUTPUT if backlight_inverted: - self.backlight_pin.value = 0 # Turn inverted backlight on - else: - self.backlight_pin.value = 1 # Turn backlight on - super().__init__(reset, enable, db4, db5, db6, db7, columns, lines) + self.backlight_pin.value = not backlight_inverted # Turn backlight on + super().__init__(rs, en, db4, db5, db6, db7, columns, lines) # pylint: enable-msg=too-many-arguments @property @@ -611,13 +603,7 @@ class Character_LCD_RGB(Character_LCD): """ # pylint: disable-msg=too-many-arguments - def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, red, green, blue, read_write=None): - reset = rs - enable = en - db4 = d4 - db5 = d5 - db6 = d6 - db7 = d7 + def __init__(self, rs, en, db4, db5, db6, db7, columns, lines, red, green, blue, read_write=None): # Define read_write (rw) pin self.read_write = read_write @@ -643,7 +629,7 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, red, green, blue, rea ) self._color = [0, 0, 0] - super().__init__(reset, enable, db4, db5, db6, db7, columns, lines) + super().__init__(rs, en, db4, db5, db6, db7, columns, lines) @property def color(self): diff --git a/adafruit_character_lcd/mono_i2c.py b/adafruit_character_lcd/mono_i2c.py new file mode 100644 index 0000000..887c3be --- /dev/null +++ b/adafruit_character_lcd/mono_i2c.py @@ -0,0 +1,36 @@ +from adafruit_character_lcd.character_lcd import Character_LCD_Mono + + +class Character_LCD_I2C(Character_LCD_Mono): + """Character LCD connected to I2C/SPI backpack using its I2C connection. + This is a subclass of Character_LCD and implements all of the same + functions and functionality. + + To use, import and initialise as follows: + + .. code-block:: python + + import board + import busio + import adafruit_character_lcd.character_lcd_mono as character_lcd + + i2c = busio.I2C(board.SCL, board.SDA) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) + """ + def __init__(self, i2c, columns, lines, backlight_inverted=False): + """Initialize character LCD connected to backpack using I2C connection + on the specified I2C bus with the specified number of columns and + lines on the display. Optionally specify if backlight is inverted. + """ + import adafruit_mcp230xx + self._mcp = adafruit_mcp230xx.MCP23008(i2c) + reset = self._mcp.get_pin(1) + enable = self._mcp.get_pin(2) + db4 = self._mcp.get_pin(3) + db5 = self._mcp.get_pin(4) + db6 = self._mcp.get_pin(5) + db7 = self._mcp.get_pin(6) + backlight_pin = self._mcp.get_pin(7) + self.backlight_inverted = backlight_inverted + super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, + backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) diff --git a/adafruit_character_lcd/mono_spi.py b/adafruit_character_lcd/mono_spi.py new file mode 100644 index 0000000..9d5b05f --- /dev/null +++ b/adafruit_character_lcd/mono_spi.py @@ -0,0 +1,42 @@ +from adafruit_character_lcd.character_lcd import Character_LCD_Mono + + +class Character_LCD_SPI(Character_LCD_Mono): + """Character LCD connected to I2C/SPI backpack using its SPI connection. + This is a subclass of Character_LCD and implements all of the same + functions and functionality. + + To use, import and initialise as follows: + + .. code-block:: python + + import board + import busio + import digitalio + import adafruit_character_lcd.character_lcd_mono as character_lcd + + spi = busio.SPI(board.SCK, MOSI=board.MOSI) + latch = digitalio.DigitalInOut(board.D5) + lcd = character_lcd.Character_LCD_SPI(spi, latch, 16, 2) + """ + + def __init__(self, spi, latch, columns, lines, backlight_inverted=False): + # pylint: disable=too-many-arguments + """Initialize character LCD connected to backpack using SPI connection + on the specified SPI bus and latch line with the specified number of + columns and lines on the display. Optionally specify if backlight is + inverted. + """ + # pylint: enable=too-many-arguments + import adafruit_74hc595 + self._shift_register = adafruit_74hc595.ShiftRegister74HC595(spi, latch) + reset = self._shift_register.get_pin(1) + enable = self._shift_register.get_pin(2) + db4 = self._shift_register.get_pin(6) + db5 = self._shift_register.get_pin(5) + db6 = self._shift_register.get_pin(4) + db7 = self._shift_register.get_pin(3) + backlight_pin = self._shift_register.get_pin(7) + self.backlight_inverted = backlight_inverted + super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, + backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) From 8fe9cf204681caa161c3f21132c2cb087ef8e2ba Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 20 Nov 2018 17:44:28 -0500 Subject: [PATCH 15/17] Removing superfluous rgb.py file --- adafruit_character_lcd/character_lcd_rgb.py | 595 -------------------- 1 file changed, 595 deletions(-) delete mode 100755 adafruit_character_lcd/character_lcd_rgb.py diff --git a/adafruit_character_lcd/character_lcd_rgb.py b/adafruit_character_lcd/character_lcd_rgb.py deleted file mode 100755 index eae7baf..0000000 --- a/adafruit_character_lcd/character_lcd_rgb.py +++ /dev/null @@ -1,595 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2017 Brent Rubell for Adafruit Industries -# Copyright (c) 2018 Kattni Rembor for Adafruit Industries -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -""" -`adafruit_character_lcd.character_lcd_rgb` -==================================================== - -Character_LCD - module for interfacing with RGB character LCDs - -* Author(s): Kattni Rembor, Brent Rubell, Asher Lieber - Tony DiCola for the original python charLCD library - -Implementation Notes --------------------- - -**Hardware:** - -* Adafruit `Character LCDs - `_ - -**Software and Dependencies:** - -* Adafruit CircuitPython firmware: - https://github.com/adafruit/circuitpython/releases -* Adafruit's Bus Device library (when using I2C/SPI): - https://github.com/adafruit/Adafruit_CircuitPython_BusDevice - -""" -import time -import digitalio -from micropython import const - -# pylint: disable-msg=bad-whitespace -# Commands -_LCD_CLEARDISPLAY = const(0x01) -_LCD_RETURNHOME = const(0x02) -_LCD_ENTRYMODESET = const(0x04) -_LCD_DISPLAYCONTROL = const(0x08) -_LCD_CURSORSHIFT = const(0x10) -_LCD_FUNCTIONSET = const(0x20) -_LCD_SETCGRAMADDR = const(0x40) -_LCD_SETDDRAMADDR = const(0x80) - -# Entry flags -_LCD_ENTRYLEFT = const(0x02) -_LCD_ENTRYSHIFTDECREMENT = const(0x00) - -# Control flags -_LCD_DISPLAYON = const(0x04) -_LCD_CURSORON = const(0x02) -_LCD_CURSOROFF = const(0x00) -_LCD_BLINKON = const(0x01) -_LCD_BLINKOFF = const(0x00) - -# Move flags -_LCD_DISPLAYMOVE = const(0x08) -_LCD_MOVERIGHT = const(0x04) -_LCD_MOVELEFT = const(0x00) - -# Function set flags -_LCD_4BITMODE = const(0x00) -_LCD_2LINE = const(0x08) -_LCD_1LINE = const(0x00) -_LCD_5X8DOTS = const(0x00) - -# Offset for up to 4 rows. -_LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54) -# pylint: enable-msg=bad-whitespace - - -def _map(xval, in_min, in_max, out_min, out_max): - # Affine transfer/map with constrained output. - outrange = float(out_max - out_min) - inrange = float(in_max - in_min) - ret = (xval - in_min) * (outrange / inrange) + out_min - if out_max > out_min: - ret = max(min(ret, out_max), out_min) - else: - ret = max(min(ret, out_min), out_max) - return ret - - -# pylint: disable-msg=too-many-instance-attributes -class Character_LCD_RGB: - """Base class for RGB character LCD.""" - LEFT_TO_RIGHT = const(0) - RIGHT_TO_LEFT = const(1) - """ Interfaces with a character LCD - :param ~digitalio.DigitalInOut rs: The reset data line - :param ~digitalio.DigitalInOut en: The enable data line - :param ~digitalio.DigitalInOut d4: The data line 4 - :param ~digitalio.DigitalInOut d5: The data line 5 - :param ~digitalio.DigitalInOut d6: The data line 6 - :param ~digitalio.DigitalInOut d7: The data line 7 - :param columns: The columns on the charLCD - :param lines: The lines on the charLCD - :param ~pulseio.PWMOut, ~digitalio.DigitalInOut red: Red RGB Anode - :param ~pulseio.PWMOut, ~digitalio.DigitalInOut green: Green RGB Anode - :param ~pulseio.PWMOut, ~digitalio.DigitalInOut blue: Blue RGB Anode - :param ~digitalio.DigitalInOut read_write: The rw pin. Determines whether to read to or - write from the display. Not necessary if only writing to the display. Used on shield. - - """ - # pylint: disable-msg=too-many-arguments - def __init__(self, rs, en, d4, d5, d6, d7, columns, lines, - red, - green, - blue, - read_write=None - ): - self.columns = columns - self.lines = lines - - # define pin params - self.reset = rs - self.enable = en - self.dl4 = d4 - self.dl5 = d5 - self.dl6 = d6 - self.dl7 = d7 - - # Define read_write (rw) pin - self.read_write = read_write - - # set all pins as outputs - for pin in(rs, en, d4, d5, d6, d7): - pin.direction = digitalio.Direction.OUTPUT - - # Setup rw pin if used - if read_write is not None: - self.read_write.direction = digitalio.Direction.OUTPUT - - # define color params - self.red = red - self.green = green - self.blue = blue - self.rgb_led = [red, green, blue] - - for pin in self.rgb_led: - if hasattr(pin, 'direction'): - # Assume a digitalio.DigitalInOut or compatible interface: - pin.direction = digitalio.Direction.OUTPUT - elif not hasattr(pin, 'duty_cycle'): - raise TypeError( - 'RGB LED objects must be instances of digitalio.DigitalInOut' - ' or pulseio.PWMOut, or provide a compatible interface.' - ) - - # Initialise the display - self._write8(0x33) - self._write8(0x32) - # Initialise display control - self.displaycontrol = _LCD_DISPLAYON | _LCD_CURSOROFF | _LCD_BLINKOFF - # Initialise display function - self.displayfunction = _LCD_4BITMODE | _LCD_1LINE | _LCD_2LINE | _LCD_5X8DOTS - # Initialise display mode - self.displaymode = _LCD_ENTRYLEFT | _LCD_ENTRYSHIFTDECREMENT - # Write to displaycontrol - self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) - # Write to displayfunction - self._write8(_LCD_FUNCTIONSET | self.displayfunction) - # Set entry mode - self._write8(_LCD_ENTRYMODESET | self.displaymode) - self.clear() - - self._color = [0, 0, 0] - self._message = None - self._direction = None - # pylint: enable-msg=too-many-arguments - - def home(self): - """Moves the cursor "home" to position (1, 1).""" - self._write8(_LCD_RETURNHOME) - time.sleep(0.003) - - def clear(self): - """Clears everything displayed on the LCD. - - The following example displays, "Hello, world!", then clears the LCD. - - .. code-block:: python - - import time - import board - import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - - lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) - - lcd.message = "Hello, world!" - time.sleep(5) - lcd.clear() - """ - self._write8(_LCD_CLEARDISPLAY) - time.sleep(0.003) - - @property - def cursor(self): - """True if cursor is visible. False to stop displaying the cursor. - - The following example shows the cursor after a displayed message: - - .. code-block:: python - - import time - import board - import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - - lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) - - lcd.cursor = True - lcd.message = "Cursor! " - time.sleep(5) - - """ - return self.displaycontrol & _LCD_CURSORON == _LCD_CURSORON - - @cursor.setter - def cursor(self, show): - if show: - self.displaycontrol |= _LCD_CURSORON - else: - self.displaycontrol &= ~_LCD_CURSORON - self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) - - def cursor_position(self, column, row): - """Move the cursor to position ``column`` and ``row`` - - :param column: column location - :param row: row location - """ - # Clamp row to the last row of the display - if row > self.lines: - row = self.lines - 1 - # Set location - self._write8(_LCD_SETDDRAMADDR | (column + _LCD_ROW_OFFSETS[row])) - - @property - def blink(self): - """ - Blink the cursor. True to blink the cursor. False to stop blinking. - - The following example shows a message followed by a blinking cursor for five seconds. - - .. code-block:: python - - import time - import board - import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) - - lcd.blink = True - lcd.message = "Blinky cursor!" - time.sleep(5) - lcd.blink = False - """ - return self.displaycontrol & _LCD_BLINKON == _LCD_BLINKON - - @blink.setter - def blink(self, blink): - if blink: - self.displaycontrol |= _LCD_BLINKON - else: - self.displaycontrol &= ~_LCD_BLINKON - self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) - - @property - def color(self): - """ - The color of the display. Provide a list of three integers ranging 0 - 100, ``[R, G, B]``. - ``0`` is no color, or "off". ``100`` is maximum color. For example, the brightest red would - be ``[100, 0, 0]``, and a half-bright purple would be, ``[50, 0, 50]``. - - If PWM is unavailable, ``0`` is off, and non-zero is on. For example, ``[1, 0, 0]`` would - be red. - - The following example turns the LCD red and displays, "Hello, world!". - - .. code-block:: python - - import time - import board - import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - - lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) - - lcd.color = [100, 0, 0] - lcd.message = "Hello, world!" - time.sleep(5) - """ - return self._color - - @color.setter - def color(self, color): - self._color = color - for number, pin in enumerate(self.rgb_led): - if hasattr(pin, 'duty_cycle'): - # Assume a pulseio.PWMOut or compatible interface and set duty cycle: - pin.duty_cycle = int(_map(color[number], 0, 100, 65535, 0)) - elif hasattr(pin, 'value'): - # If we don't have a PWM interface, all we can do is turn each color - # on / off. Assume a DigitalInOut (or compatible interface) and write - # 0 (on) to pin for any value greater than 0, or 1 (off) for 0: - pin.value = 0 if color[number] > 0 else 1 - - @property - def display(self): - """ - Enable or disable the display. True to enable the display. False to disable the display. - - The following example displays, "Hello, world!" on the LCD and then turns the display off. - - .. code-block:: python - - import time - import board - import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) - - lcd.message = "Hello, world!" - time.sleep(5) - lcd.display = False - """ - return self.displaycontrol & _LCD_DISPLAYON == _LCD_DISPLAYON - - @display.setter - def display(self, enable): - if enable: - self.displaycontrol |= _LCD_DISPLAYON - else: - self.displaycontrol &= ~_LCD_DISPLAYON - self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) - - @property - def message(self): - """Display a string of text on the character LCD. - - The following example displays, "Hello, world!" on the LCD. - - .. code-block:: python - - import time - import board - import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) - - lcd.message = "Hello, world!" - time.sleep(5) - """ - return self._message - - @message.setter - def message(self, message): - self._message = message - line = 0 - # Track times through iteration, to act on the initial character of the message - initial_character = 0 - # iterate through each character - for character in message: - if initial_character == 0: - col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1 - self.cursor_position(col, line) - initial_character += 1 - # if character is \n, go to next line - if character == '\n': - line += 1 - # move to left/right depending on text direction - col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1 - self.cursor_position(col, line) - # Write character to display - else: - self._write8(ord(character), True) - - def move_left(self): - """Moves displayed text left one column. - - The following example scrolls a message to the left off the screen. - - .. code-block:: python - - import time - import board - import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - - lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) - - scroll_message = "<-- Scroll" - lcd.message = scroll_message - time.sleep(2) - for i in range(len(scroll_message)): - lcd.move_left() - time.sleep(0.5) - """ - self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVELEFT) - - def move_right(self): - """Moves displayed text right one column. - - The following example scrolls a message to the right off the screen. - - .. code-block:: python - - import time - import board - import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - - lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) - - scroll_message = "Scroll -->" - lcd.message = scroll_message - time.sleep(2) - for i in range(len(scroll_message) + lcd_columns): - lcd.move_right() - time.sleep(0.5) - """ - return self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVERIGHT) - - @property - def text_direction(self): - """The direction the text is displayed. To display the text left to right beginning on the - left side of the LCD, set ``text_direction = LEFT_TO_RIGHT``. To display the text right - to left beginning on the right size of the LCD, set ``text_direction = RIGHT_TO_LEFT``. - Text defaults to displaying from left to right. - - The following example displays "Hello, world!" from right to left. - - .. code-block:: python - - import time - import board - import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - - lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) - - lcd.text_direction = lcd.RIGHT_TO_LEFT - lcd.message = "Hello, world!" - time.sleep(5) - """ - return self._direction - - @text_direction.setter - def text_direction(self, direction): - self._direction = direction - if direction == self.LEFT_TO_RIGHT: - self._left_to_right() - elif direction == self.RIGHT_TO_LEFT: - self._right_to_left() - - def _left_to_right(self): - # Displays text from left to right on the LCD. - self.displaymode |= _LCD_ENTRYLEFT - self._write8(_LCD_ENTRYMODESET | self.displaymode) - - def _right_to_left(self): - # Displays text from right to left on the LCD. - self.displaymode &= ~_LCD_ENTRYLEFT - self._write8(_LCD_ENTRYMODESET | self.displaymode) - - def create_char(self, location, pattern): - """ - Fill one of the first 8 CGRAM locations with custom characters. - The location parameter should be between 0 and 7 and pattern should - provide an array of 8 bytes containing the pattern. E.g. you can easily - design your custom character at http://www.quinapalus.com/hd44780udg.html - To show your custom character use, for example, ``lcd.message = "\x01"`` - - :param location: integer in range(8) to store the created character - :param ~bytes pattern: len(8) describes created character - - """ - # only position 0..7 are allowed - location &= 0x7 - self._write8(_LCD_SETCGRAMADDR | (location << 3)) - for i in range(8): - self._write8(pattern[i], char_mode=True) - - def _write8(self, value, char_mode=False): - # Sends 8b ``value`` in ``char_mode``. - # :param value: bytes - # :param char_mode: character/data mode selector. False (default) for - # data only, True for character bits. - # one ms delay to prevent writing too quickly. - time.sleep(0.001) - # set character/data bit. (charmode = False) - self.reset.value = char_mode - # WRITE upper 4 bits - self.dl4.value = ((value >> 4) & 1) > 0 - self.dl5.value = ((value >> 5) & 1) > 0 - self.dl6.value = ((value >> 6) & 1) > 0 - self.dl7.value = ((value >> 7) & 1) > 0 - # send command - self._pulse_enable() - # WRITE lower 4 bits - self.dl4.value = (value & 1) > 0 - self.dl5.value = ((value >> 1) & 1) > 0 - self.dl6.value = ((value >> 2) & 1) > 0 - self.dl7.value = ((value >> 3) & 1) > 0 - self._pulse_enable() - - def _pulse_enable(self): - # Pulses (lo->hi->lo) to send commands. - self.enable.value = False - # 1microsec pause - time.sleep(0.0000001) - self.enable.value = True - time.sleep(0.0000001) - self.enable.value = False - time.sleep(0.0000001) - -# pylint: enable-msg=too-many-instance-attributes - - -class Character_LCD_I2C_RGB(Character_LCD_RGB): - """RGB Character LCD connected to I2C shield using I2C connection. - This is a subclass of Character_LCD_RGB and implements all of the same - functions and functionality. - - To use, import and initialise as follows: - - .. code-block:: python - - import board - import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) - """ - def __init__(self, i2c, columns, lines): - """Initialize RGB character LCD connected to shield using I2C connection - on the specified I2C bus with the specified number of columns and lines - on the display. - """ - import adafruit_mcp230xx - self._mcp = adafruit_mcp230xx.MCP23017(i2c) - reset = self._mcp.get_pin(15) - read_write = self._mcp.get_pin(14) - enable = self._mcp.get_pin(13) - db4 = self._mcp.get_pin(12) - db5 = self._mcp.get_pin(11) - db6 = self._mcp.get_pin(10) - db7 = self._mcp.get_pin(9) - red = self._mcp.get_pin(6) - green = self._mcp.get_pin(7) - blue = self._mcp.get_pin(8) - super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, red, green, blue, - read_write) From 1258c470b8f1634df95a92dc9c1d8a89453ade19 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 20 Nov 2018 21:16:26 -0500 Subject: [PATCH 16/17] Refactored, updated examples, appeased Sphinx. --- README.rst | 27 +++-- adafruit_character_lcd/character_lcd.py | 111 +++++++----------- adafruit_character_lcd/character_lcd_i2c.py | 84 +++++++++++++ .../character_lcd_rgb_i2c.py | 95 +++++++++++++++ adafruit_character_lcd/character_lcd_spi.py | 90 ++++++++++++++ adafruit_character_lcd/mono_i2c.py | 36 ------ adafruit_character_lcd/mono_spi.py | 42 ------- adafruit_character_lcd/rgb_i2c.py | 38 ------ docs/api.rst | 10 +- docs/examples.rst | 10 +- examples/charlcd_custom_character_nyan_cat.py | 14 ++- examples/charlcd_customcharacter.py | 12 +- examples/charlcd_i2c_mono_simpletest.py | 5 +- examples/charlcd_i2c_rgb_simpletest.py | 6 +- examples/charlcd_mono_simpletest.py | 9 +- examples/charlcd_rgb_simpletest.py | 8 +- examples/charlcd_rpi_mono_simpletest.py | 8 +- examples/charlcd_rpi_rgb_simpletest.py | 8 +- examples/charlcd_spi_mono_simpletest.py | 4 +- 19 files changed, 384 insertions(+), 233 deletions(-) create mode 100644 adafruit_character_lcd/character_lcd_i2c.py create mode 100644 adafruit_character_lcd/character_lcd_rgb_i2c.py create mode 100644 adafruit_character_lcd/character_lcd_spi.py delete mode 100644 adafruit_character_lcd/mono_i2c.py delete mode 100644 adafruit_character_lcd/mono_spi.py delete mode 100644 adafruit_character_lcd/rgb_i2c.py diff --git a/README.rst b/README.rst index 1779a86..409a8f4 100644 --- a/README.rst +++ b/README.rst @@ -19,8 +19,6 @@ This library is compatible with standard Character LCDs such as: * `Adafruit RGB backlight negative LCD 16x2 `_ * `Adafruit RGB backlight negative LCD 20x4 `_ -Compatible with CircuitPython Versions: 2.x - Dependencies ============= This driver depends on: @@ -42,20 +40,23 @@ The ``Character_LCD`` class interfaces a predefined Character LCD display with C .. code-block:: python - import adafruit_character_lcd + import board + import digitalio + import adafruit_character_lcd.character_lcd as character_lcd You must define the data pins (``RS``, ``EN``, ``D4``, ``D5``, ``D6``, ``D7``) in your code before using the ``Character_LCD`` class. -If you want to have on/off ``backlight`` functionality, you can also define your backlight as ``lcd_backlight``. Otherwise, the backlight will always remain on. An example of this is below +If you want to have on/off ``backlight`` functionality, you can also define your backlight as ``lcd_backlight``. Otherwise, the backlight +will always remain on. The following is an example setup. .. code-block:: python - lcd_rs = digitalio.DigitalInOut(D7) - lcd_en = digitalio.DigitalInOut(D8) - lcd_d7 = digitalio.DigitalInOut(D12) - lcd_d6 = digitalio.DigitalInOut(D11) - lcd_d5 = digitalio.DigitalInOut(D10) - lcd_d4 = digitalio.DigitalInOut(D9) - lcd_backlight = digitalio.DigitalInOut(D13) + lcd_rs = digitalio.DigitalInOut(board.D7) + lcd_en = digitalio.DigitalInOut(board.D8) + lcd_d7 = digitalio.DigitalInOut(board.D12) + lcd_d6 = digitalio.DigitalInOut(board.D11) + lcd_d5 = digitalio.DigitalInOut(board.D10) + lcd_d4 = digitalio.DigitalInOut(board.D9) + lcd_backlight = digitalio.DigitalInOut(board.D13) You must also define the size of the CharLCD by specifying its ``lcd_columns`` and ``lcd_rows``: @@ -68,7 +69,7 @@ After you have set up your LCD, we can make the device by calling it .. code-block:: python - lcd = adafruit_character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight) + lcd = character_lcd.Character_LCD_Mono(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight) To verify that your pins are correct, print a hello message to the CharLCD: @@ -78,7 +79,7 @@ To verify that your pins are correct, print a hello message to the CharLCD: lcd.message = "Hello\nCircuitPython" -Custom character example with create_char() is provided within /examples/ +Custom character example with ``create_char()`` is provided within /examples/ Contributing diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index f229401..53658f1 100755 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -34,8 +34,7 @@ **Hardware:** -* Adafruit `Character LCDs - `_ +"* `Adafruit Character LCDs `_" **Software and Dependencies:** @@ -117,11 +116,8 @@ def _map(xval, in_min, in_max, out_min, out_max): # pylint: disable-msg=too-many-instance-attributes class Character_LCD: - """Base class for character LCD.""" - LEFT_TO_RIGHT = const(0) - RIGHT_TO_LEFT = const(1) - """ - Interfaces with a character LCD + """Base class for character LCD. + :param ~digitalio.DigitalInOut rs: The reset data line :param ~digitalio.DigitalInOut en: The enable data line :param ~digitalio.DigitalInOut d4: The data line 4 @@ -130,12 +126,11 @@ class Character_LCD: :param ~digitalio.DigitalInOut d7: The data line 7 :param columns: The columns on the charLCD :param lines: The lines on the charLCD - :param ~digitalio.DigitalInOut backlight_pin: The backlight pin, usually - the last pin. Check with your datasheet - :param bool backlight_inverted: False if LCD is not inverted, i.e. backlight pin is connected - to common anode. True if LCD is inverted i.e. backlight pin is connected to common cathode. """ + LEFT_TO_RIGHT = const(0) + RIGHT_TO_LEFT = const(1) + # pylint: disable-msg=too-many-arguments def __init__(self, rs, en, d4, d5, d6, d7, columns, lines ): @@ -191,10 +186,9 @@ def clear(self): import time import board import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd + import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.message = "Hello, world!" @@ -215,10 +209,9 @@ def cursor(self): import time import board import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd + import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.cursor = True @@ -260,10 +253,9 @@ def blink(self): import time import board import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd + import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.blink = True @@ -293,10 +285,9 @@ def display(self): import time import board import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd + import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.message = "Hello, world!" @@ -324,10 +315,9 @@ def message(self): import time import board import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd + import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.message = "Hello, world!" @@ -371,10 +361,9 @@ def move_left(self): import time import board import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd + import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) scroll_message = "<-- Scroll" @@ -396,19 +385,15 @@ def move_right(self): import time import board import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd + import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - - lcd_columns = 16 - lcd_rows = 2 - - lcd = character_lcd.Character_LCD_I2C(i2c, lcd_columns, lcd_rows) + lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) scroll_message = "Scroll -->" lcd.message = scroll_message time.sleep(2) - for i in range(len(scroll_message) + lcd_columns): + for i in range(len(scroll_message) + 16): lcd.move_right() time.sleep(0.5) """ @@ -428,10 +413,9 @@ def text_direction(self): import time import board import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd + import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.text_direction = lcd.RIGHT_TO_LEFT @@ -513,21 +497,20 @@ def _pulse_enable(self): # pylint: disable-msg=too-many-instance-attributes class Character_LCD_Mono(Character_LCD): - """Base class for character LCD.""" - """ - Interfaces with a character LCD - :param ~digitalio.DigitalInOut rs: The reset data line - :param ~digitalio.DigitalInOut en: The enable data line - :param ~digitalio.DigitalInOut d4: The data line 4 - :param ~digitalio.DigitalInOut d5: The data line 5 - :param ~digitalio.DigitalInOut d6: The data line 6 - :param ~digitalio.DigitalInOut d7: The data line 7 - :param columns: The columns on the charLCD - :param lines: The lines on the charLCD - :param ~digitalio.DigitalInOut backlight_pin: The backlight pin, usually - the last pin. Check with your datasheet - :param bool backlight_inverted: False if LCD is not inverted, i.e. backlight pin is connected - to common anode. True if LCD is inverted i.e. backlight pin is connected to common cathode. + """Interfaces with monochromatic character LCDs. + + :param ~digitalio.DigitalInOut rs: The reset data line + :param ~digitalio.DigitalInOut en: The enable data line + :param ~digitalio.DigitalInOut d4: The data line 4 + :param ~digitalio.DigitalInOut d5: The data line 5 + :param ~digitalio.DigitalInOut d6: The data line 6 + :param ~digitalio.DigitalInOut d7: The data line 7 + :param columns: The columns on the charLCD + :param lines: The lines on the charLCD + :param ~digitalio.DigitalInOut backlight_pin: The backlight pin + :param bool backlight_inverted: ``False`` if LCD is not inverted, i.e. backlight pin is + connected to common anode. ``True`` if LCD is inverted i.e. backlight pin is connected + to common cathode. """ # pylint: disable-msg=too-many-arguments @@ -558,7 +541,7 @@ def backlight(self): import time import board import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd + import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) @@ -577,22 +560,21 @@ def backlight(self): @backlight.setter def backlight(self, enable): self._enable = enable - if enable and not self.backlight_inverted or not enable and self.backlight_inverted: - self.backlight_pin.value = 1 - if enable and self.backlight_inverted or not enable and not self.backlight_inverted: - self.backlight_pin.value = 0 + if enable: + self.backlight_pin.value = not self.backlight_inverted + else: + self.backlight_pin.value = self.backlight_inverted class Character_LCD_RGB(Character_LCD): - """Base class for RGB character LCD.""" + """Interfaces with RGB character LCDs. - """ Interfaces with an RGB character LCD :param ~digitalio.DigitalInOut rs: The reset data line :param ~digitalio.DigitalInOut en: The enable data line - :param ~digitalio.DigitalInOut d4: The data line 4 - :param ~digitalio.DigitalInOut d5: The data line 5 - :param ~digitalio.DigitalInOut d6: The data line 6 - :param ~digitalio.DigitalInOut d7: The data line 7 + :param ~digitalio.DigitalInOut db4: The data line 4 + :param ~digitalio.DigitalInOut db5: The data line 5 + :param ~digitalio.DigitalInOut db6: The data line 6 + :param ~digitalio.DigitalInOut db7: The data line 7 :param columns: The columns on the charLCD :param lines: The lines on the charLCD :param ~pulseio.PWMOut, ~digitalio.DigitalInOut red: Red RGB Anode @@ -603,7 +585,8 @@ class Character_LCD_RGB(Character_LCD): """ # pylint: disable-msg=too-many-arguments - def __init__(self, rs, en, db4, db5, db6, db7, columns, lines, red, green, blue, read_write=None): + def __init__(self, rs, en, db4, db5, db6, db7, columns, lines, + red, green, blue, read_write=None): # Define read_write (rw) pin self.read_write = read_write @@ -613,9 +596,6 @@ def __init__(self, rs, en, db4, db5, db6, db7, columns, lines, red, green, blue, self.read_write.direction = digitalio.Direction.OUTPUT # define color params - self.red = red - self.green = green - self.blue = blue self.rgb_led = [red, green, blue] for pin in self.rgb_led: @@ -648,11 +628,11 @@ def color(self): import time import board import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd + import adafruit_character_lcd.character_lcd_rgb_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) + lcd = character_lcd.Character_LCD_RGB_I2C(i2c, 16, 2) lcd.color = [100, 0, 0] lcd.message = "Hello, world!" @@ -671,5 +651,4 @@ def color(self, color): # If we don't have a PWM interface, all we can do is turn each color # on / off. Assume a DigitalInOut (or compatible interface) and write # 0 (on) to pin for any value greater than 0, or 1 (off) for 0: - pin.value = 0 if color[number] > 0 else 1 - + pin.value = not color[number] > 1 diff --git a/adafruit_character_lcd/character_lcd_i2c.py b/adafruit_character_lcd/character_lcd_i2c.py new file mode 100644 index 0000000..bd37601 --- /dev/null +++ b/adafruit_character_lcd/character_lcd_i2c.py @@ -0,0 +1,84 @@ +# The MIT License (MIT) +# +# Copyright (c) 2018 Kattni Rembor for Adafruit Industries +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +""" +`adafruit_character_lcd.character_lcd_i2c` +==================================================== + +Module for using I2C with I2C/SPI character LCD backpack + +* Author(s): Kattni Rembor + +Implementation Notes +-------------------- + +**Hardware:** + +"* `I2C / SPI character LCD backpack `_" + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware: + https://github.com/adafruit/circuitpython/releases +* Adafruit's Bus Device library (when using I2C/SPI): + https://github.com/adafruit/Adafruit_CircuitPython_BusDevice + +""" + +from adafruit_character_lcd.character_lcd import Character_LCD_Mono + +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CharLCD.git" + +class Character_LCD_I2C(Character_LCD_Mono): + # pylint: disable=too-few-public-methods + """Character LCD connected to I2C/SPI backpack using its I2C connection. + This is a subclass of Character_LCD and implements all of the same + functions and functionality. + + To use, import and initialise as follows: + + .. code-block:: python + + import board + import busio + from adafruit_character_lcd.character_lcd_i2c import Character_LCD_I2C + + i2c = busio.I2C(board.SCL, board.SDA) + lcd = Character_LCD_I2C(i2c, 16, 2) + """ + def __init__(self, i2c, columns, lines, backlight_inverted=False): + """Initialize character LCD connected to backpack using I2C connection + on the specified I2C bus with the specified number of columns and + lines on the display. Optionally specify if backlight is inverted. + """ + import adafruit_mcp230xx + self._mcp = adafruit_mcp230xx.MCP23008(i2c) + reset = self._mcp.get_pin(1) + enable = self._mcp.get_pin(2) + db4 = self._mcp.get_pin(3) + db5 = self._mcp.get_pin(4) + db6 = self._mcp.get_pin(5) + db7 = self._mcp.get_pin(6) + backlight_pin = self._mcp.get_pin(7) + self.backlight_inverted = backlight_inverted + super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, + backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) diff --git a/adafruit_character_lcd/character_lcd_rgb_i2c.py b/adafruit_character_lcd/character_lcd_rgb_i2c.py new file mode 100644 index 0000000..7cdd068 --- /dev/null +++ b/adafruit_character_lcd/character_lcd_rgb_i2c.py @@ -0,0 +1,95 @@ +# The MIT License (MIT) +# +# Copyright (c) 2018 Kattni Rembor for Adafruit Industries +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +""" +`adafruit_character_lcd.character_lcd_i2c` +==================================================== + +Module for using I2C with I2C RGB LCD Shield or I2C RGB LCD Pi Plate + +* Author(s): Kattni Rembor + +Implementation Notes +-------------------- + +**Hardware:** + +"* `RGB LCD Shield Kit w/ 16x2 Character Display - Negative Display +`_" +"* `RGB LCD Shield Kit w/ 16x2 Character Display - Positive Display +`_" +"* `Adafruit RGB Negative 16x2 LCD+Keypad Kit for Raspberry Pi +`_" +"* `Adafruit RGB Positive 16x2 LCD+Keypad Kit for Raspberry Pi +`_" + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware: + https://github.com/adafruit/circuitpython/releases +* Adafruit's Bus Device library (when using I2C/SPI): + https://github.com/adafruit/Adafruit_CircuitPython_BusDevice + +""" + +from adafruit_character_lcd.character_lcd import Character_LCD_RGB + +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CharLCD.git" + + +class Character_LCD_RGB_I2C(Character_LCD_RGB): + """RGB Character LCD connected to I2C shield or Pi plate using I2C connection. + This is a subclass of Character_LCD_RGB and implements all of the same + functions and functionality. + + To use, import and initialise as follows: + + .. code-block:: python + + import board + import busio + from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C + + i2c = busio.I2C(board.SCL, board.SDA) + lcd = Character_LCD_RGB_I2C(i2c, 16, 2) + + """ + def __init__(self, i2c, columns, lines): + # pylint: disable=too-many-locals + """Initialize RGB character LCD connected to shield using I2C connection + on the specified I2C bus with the specified number of columns and lines + on the display. + """ + import adafruit_mcp230xx + self._mcp = adafruit_mcp230xx.MCP23017(i2c) + reset = self._mcp.get_pin(15) + read_write = self._mcp.get_pin(14) + enable = self._mcp.get_pin(13) + db4 = self._mcp.get_pin(12) + db5 = self._mcp.get_pin(11) + db6 = self._mcp.get_pin(10) + db7 = self._mcp.get_pin(9) + red = self._mcp.get_pin(6) + green = self._mcp.get_pin(7) + blue = self._mcp.get_pin(8) + super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, red, green, blue, + read_write) diff --git a/adafruit_character_lcd/character_lcd_spi.py b/adafruit_character_lcd/character_lcd_spi.py new file mode 100644 index 0000000..a20c2cf --- /dev/null +++ b/adafruit_character_lcd/character_lcd_spi.py @@ -0,0 +1,90 @@ +# The MIT License (MIT) +# +# Copyright (c) 2018 Kattni Rembor for Adafruit Industries +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +""" +`adafruit_character_lcd.character_lcd_spi` +==================================================== + +Module for using SPI with I2C/SPI character LCD backpack + +* Author(s): Kattni Rembor + +Implementation Notes +-------------------- + +**Hardware:** + +"* `I2C / SPI character LCD backpack `_" + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware: + https://github.com/adafruit/circuitpython/releases +* Adafruit's Bus Device library (when using I2C/SPI): + https://github.com/adafruit/Adafruit_CircuitPython_BusDevice + +""" + +from adafruit_character_lcd.character_lcd import Character_LCD_Mono + +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CharLCD.git" + + +class Character_LCD_SPI(Character_LCD_Mono): + """Character LCD connected to I2C/SPI backpack using its SPI connection. + This is a subclass of Character_LCD and implements all of the same + functions and functionality. + + To use, import and initialise as follows: + + .. code-block:: python + + import board + import busio + import digitalio + import adafruit_character_lcd.character_lcd_mono as character_lcd + + spi = busio.SPI(board.SCK, MOSI=board.MOSI) + latch = digitalio.DigitalInOut(board.D5) + lcd = character_lcd.Character_LCD_SPI(spi, latch, 16, 2) + """ + + def __init__(self, spi, latch, columns, lines, backlight_inverted=False): + # pylint: disable=too-many-arguments + """Initialize character LCD connected to backpack using SPI connection + on the specified SPI bus and latch line with the specified number of + columns and lines on the display. Optionally specify if backlight is + inverted. + """ + # pylint: enable=too-many-arguments + import adafruit_74hc595 + self._shift_register = adafruit_74hc595.ShiftRegister74HC595(spi, latch) + reset = self._shift_register.get_pin(1) + enable = self._shift_register.get_pin(2) + db4 = self._shift_register.get_pin(6) + db5 = self._shift_register.get_pin(5) + db6 = self._shift_register.get_pin(4) + db7 = self._shift_register.get_pin(3) + backlight_pin = self._shift_register.get_pin(7) + self.backlight_inverted = backlight_inverted + super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, + backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) diff --git a/adafruit_character_lcd/mono_i2c.py b/adafruit_character_lcd/mono_i2c.py deleted file mode 100644 index 887c3be..0000000 --- a/adafruit_character_lcd/mono_i2c.py +++ /dev/null @@ -1,36 +0,0 @@ -from adafruit_character_lcd.character_lcd import Character_LCD_Mono - - -class Character_LCD_I2C(Character_LCD_Mono): - """Character LCD connected to I2C/SPI backpack using its I2C connection. - This is a subclass of Character_LCD and implements all of the same - functions and functionality. - - To use, import and initialise as follows: - - .. code-block:: python - - import board - import busio - import adafruit_character_lcd.character_lcd_mono as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) - """ - def __init__(self, i2c, columns, lines, backlight_inverted=False): - """Initialize character LCD connected to backpack using I2C connection - on the specified I2C bus with the specified number of columns and - lines on the display. Optionally specify if backlight is inverted. - """ - import adafruit_mcp230xx - self._mcp = adafruit_mcp230xx.MCP23008(i2c) - reset = self._mcp.get_pin(1) - enable = self._mcp.get_pin(2) - db4 = self._mcp.get_pin(3) - db5 = self._mcp.get_pin(4) - db6 = self._mcp.get_pin(5) - db7 = self._mcp.get_pin(6) - backlight_pin = self._mcp.get_pin(7) - self.backlight_inverted = backlight_inverted - super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, - backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) diff --git a/adafruit_character_lcd/mono_spi.py b/adafruit_character_lcd/mono_spi.py deleted file mode 100644 index 9d5b05f..0000000 --- a/adafruit_character_lcd/mono_spi.py +++ /dev/null @@ -1,42 +0,0 @@ -from adafruit_character_lcd.character_lcd import Character_LCD_Mono - - -class Character_LCD_SPI(Character_LCD_Mono): - """Character LCD connected to I2C/SPI backpack using its SPI connection. - This is a subclass of Character_LCD and implements all of the same - functions and functionality. - - To use, import and initialise as follows: - - .. code-block:: python - - import board - import busio - import digitalio - import adafruit_character_lcd.character_lcd_mono as character_lcd - - spi = busio.SPI(board.SCK, MOSI=board.MOSI) - latch = digitalio.DigitalInOut(board.D5) - lcd = character_lcd.Character_LCD_SPI(spi, latch, 16, 2) - """ - - def __init__(self, spi, latch, columns, lines, backlight_inverted=False): - # pylint: disable=too-many-arguments - """Initialize character LCD connected to backpack using SPI connection - on the specified SPI bus and latch line with the specified number of - columns and lines on the display. Optionally specify if backlight is - inverted. - """ - # pylint: enable=too-many-arguments - import adafruit_74hc595 - self._shift_register = adafruit_74hc595.ShiftRegister74HC595(spi, latch) - reset = self._shift_register.get_pin(1) - enable = self._shift_register.get_pin(2) - db4 = self._shift_register.get_pin(6) - db5 = self._shift_register.get_pin(5) - db6 = self._shift_register.get_pin(4) - db7 = self._shift_register.get_pin(3) - backlight_pin = self._shift_register.get_pin(7) - self.backlight_inverted = backlight_inverted - super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, - backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) diff --git a/adafruit_character_lcd/rgb_i2c.py b/adafruit_character_lcd/rgb_i2c.py deleted file mode 100644 index 9c3a9b3..0000000 --- a/adafruit_character_lcd/rgb_i2c.py +++ /dev/null @@ -1,38 +0,0 @@ -from adafruit_character_lcd.character_lcd import Character_LCD_RGB - - -class Character_LCD_I2C_RGB(Character_LCD_RGB): - """RGB Character LCD connected to I2C shield using I2C connection. - This is a subclass of Character_LCD_RGB and implements all of the same - functions and functionality. - - To use, import and initialise as follows: - - .. code-block:: python - - import board - import busio - import adafruit_character_lcd.character_lcd_rgb as character_lcd - - i2c = busio.I2C(board.SCL, board.SDA) - lcd = character_lcd.Character_LCD_I2C_RGB(i2c, 16, 2) - """ - def __init__(self, i2c, columns, lines): - """Initialize RGB character LCD connected to shield using I2C connection - on the specified I2C bus with the specified number of columns and lines - on the display. - """ - import adafruit_mcp230xx - self._mcp = adafruit_mcp230xx.MCP23017(i2c) - reset = self._mcp.get_pin(15) - read_write = self._mcp.get_pin(14) - enable = self._mcp.get_pin(13) - db4 = self._mcp.get_pin(12) - db5 = self._mcp.get_pin(11) - db6 = self._mcp.get_pin(10) - db7 = self._mcp.get_pin(9) - red = self._mcp.get_pin(6) - green = self._mcp.get_pin(7) - blue = self._mcp.get_pin(8) - super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, red, green, blue, - read_write) diff --git a/docs/api.rst b/docs/api.rst index 3751e63..60afe08 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -3,8 +3,12 @@ .. automodule:: adafruit_character_lcd.character_lcd :members: - :inherited-members: -.. automodule:: adafruit_character_lcd.character_lcd_rgb +.. automodule:: adafruit_character_lcd.character_lcd_i2c + :members: + +.. automodule:: adafruit_character_lcd.character_lcd_spi + :members: + +.. automodule:: adafruit_character_lcd.character_lcd_rgb_i2c :members: - :inherited-members: diff --git a/docs/examples.rst b/docs/examples.rst index 9237e45..fc7c555 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -11,10 +11,10 @@ Ensure your device works with this simple test. :caption: examples/charlcd_rgb_simpletest.py :linenos: -.. literalinclude:: ../examples/charlcd_I2C_simpletest.py - :caption: examples/charlcd_I2C_simpletest.py +.. literalinclude:: ../examples/charlcd_i2c_mono_simpletest.py + :caption: examples/charlcd_i2c_mono_simpletest.py :linenos: -.. literalinclude:: ../examples/charlcd_SPI_simpletest.py - :caption: examples/charlcd_SPI_simpletest.py - :linenos: \ No newline at end of file +.. literalinclude:: ../examples/charlcd_spi_mono_simpletest.py + :caption: examples/charlcd_spi_mono_simpletest.py + :linenos: diff --git a/examples/charlcd_custom_character_nyan_cat.py b/examples/charlcd_custom_character_nyan_cat.py index 607e3d4..019f815 100755 --- a/examples/charlcd_custom_character_nyan_cat.py +++ b/examples/charlcd_custom_character_nyan_cat.py @@ -1,10 +1,14 @@ +"""Use custom characters to display Nyan cat""" import time import board import digitalio -import adafruit_character_lcd.character_lcd_mono as character_lcd +import adafruit_character_lcd.character_lcd as characterlcd +# Modify this if you have a different sized character LCD lcd_columns = 16 lcd_rows = 2 + +# Metro M0/M4 Pin Config: lcd_rs = digitalio.DigitalInOut(board.D7) lcd_en = digitalio.DigitalInOut(board.D8) lcd_d7 = digitalio.DigitalInOut(board.D12) @@ -12,8 +16,10 @@ lcd_d5 = digitalio.DigitalInOut(board.D10) lcd_d4 = digitalio.DigitalInOut(board.D9) lcd_backlight = digitalio.DigitalInOut(board.D13) -lcd = character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, - lcd_d7, lcd_columns, lcd_rows, lcd_backlight) + +# Initialise the LCD class +lcd = characterlcd.Character_LCD_Mono(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, + lcd_d7, lcd_columns, lcd_rows, lcd_backlight) head = [31, 17, 27, 17, 17, 21, 17, 31] @@ -68,6 +74,8 @@ lcd.move_right() lcd.message = "\x02\x02\x02\x02\x01\x00\x00\x00\x06\n\x02\x02\x02\x07\x03\x04\x04\x04\x05" +lcd.backlight = True + while True: lcd.create_char(4, bot_body2) lcd.create_char(7, tail_up) diff --git a/examples/charlcd_customcharacter.py b/examples/charlcd_customcharacter.py index 97a658b..7810802 100644 --- a/examples/charlcd_customcharacter.py +++ b/examples/charlcd_customcharacter.py @@ -1,10 +1,13 @@ -import digitalio +"""Display a custom character""" import board -import adafruit_character_lcd.character_lcd_mono as character_lcd +import digitalio +import adafruit_character_lcd.character_lcd as characterlcd +# Modify this if you have a different sized character LCD lcd_columns = 16 lcd_rows = 2 +# Metro M0/M4 Pin Config: lcd_rs = digitalio.DigitalInOut(board.D7) lcd_en = digitalio.DigitalInOut(board.D8) lcd_d7 = digitalio.DigitalInOut(board.D12) @@ -13,8 +16,9 @@ lcd_d4 = digitalio.DigitalInOut(board.D9) lcd_backlight = digitalio.DigitalInOut(board.D13) -lcd = character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, - lcd_d7, lcd_columns, lcd_rows, lcd_backlight) +# Initialise the LCD class +lcd = characterlcd.Character_LCD_Mono(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, + lcd_d7, lcd_columns, lcd_rows, lcd_backlight) checkmark = bytes([0x0, 0x0, 0x1, 0x3, 0x16, 0x1c, 0x8, 0x0]) lcd.clear() diff --git a/examples/charlcd_i2c_mono_simpletest.py b/examples/charlcd_i2c_mono_simpletest.py index 954b324..365575a 100644 --- a/examples/charlcd_i2c_mono_simpletest.py +++ b/examples/charlcd_i2c_mono_simpletest.py @@ -1,8 +1,8 @@ -# Simple test for 16x2 character lcd connected to an MCP23008 I2C LCD backpack. +"""Simple test for 16x2 character lcd connected to an MCP23008 I2C LCD backpack.""" import time import board import busio -import adafruit_character_lcd.character_lcd_mono as character_lcd +import adafruit_character_lcd.character_lcd_i2c as character_lcd # Modify this if you have a different sized Character LCD lcd_columns = 16 @@ -51,6 +51,7 @@ lcd.move_left() lcd.clear() lcd.message = "Going to sleep\nCya later!" +time.sleep(5) # Turn backlight off lcd.backlight = False time.sleep(2) diff --git a/examples/charlcd_i2c_rgb_simpletest.py b/examples/charlcd_i2c_rgb_simpletest.py index 901a15e..8362cf3 100644 --- a/examples/charlcd_i2c_rgb_simpletest.py +++ b/examples/charlcd_i2c_rgb_simpletest.py @@ -1,8 +1,8 @@ -# Simple test for I2C RGB character LCD shield kit +"""Simple test for I2C RGB character LCD shield kit""" import time import board import busio -import adafruit_character_lcd.character_lcd_rgb as character_lcd +import adafruit_character_lcd.character_lcd_rgb_i2c as character_lcd # Modify this if you have a different sized Character LCD lcd_columns = 16 @@ -12,7 +12,7 @@ i2c = busio.I2C(board.SCL, board.SDA) # Initialise the LCD class -lcd = character_lcd.Character_LCD_I2C_RGB(i2c, lcd_columns, lcd_rows) +lcd = character_lcd.Character_LCD_RGB_I2C(i2c, lcd_columns, lcd_rows) lcd.clear() # Set LCD color to red diff --git a/examples/charlcd_mono_simpletest.py b/examples/charlcd_mono_simpletest.py index 47520eb..9a47104 100644 --- a/examples/charlcd_mono_simpletest.py +++ b/examples/charlcd_mono_simpletest.py @@ -1,8 +1,8 @@ -# Simple test for monochromatic character LCD +"""Simple test for monochromatic character LCD""" import time import board import digitalio -import adafruit_character_lcd.character_lcd_mono as character_lcd +import adafruit_character_lcd.character_lcd as characterlcd # Modify this if you have a different sized character LCD lcd_columns = 16 @@ -18,8 +18,8 @@ lcd_backlight = digitalio.DigitalInOut(board.D13) # Initialise the LCD class -lcd = character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, - lcd_d7, lcd_columns, lcd_rows, lcd_backlight) +lcd = characterlcd.Character_LCD_Mono(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, + lcd_d7, lcd_columns, lcd_rows, lcd_backlight) # Turn backlight on lcd.backlight = True @@ -58,6 +58,7 @@ lcd.move_left() lcd.clear() lcd.message = "Going to sleep\nCya later!" +time.sleep(3) # Turn backlight off lcd.backlight = False time.sleep(2) diff --git a/examples/charlcd_rgb_simpletest.py b/examples/charlcd_rgb_simpletest.py index 4671f6a..55f0dbc 100644 --- a/examples/charlcd_rgb_simpletest.py +++ b/examples/charlcd_rgb_simpletest.py @@ -1,9 +1,9 @@ -# Simple test for RGB character LCD +"""Simple test for RGB character LCD""" import time import board import digitalio import pulseio -import adafruit_character_lcd.character_lcd_rgb as character_lcd +import adafruit_character_lcd.character_lcd as characterlcd # Modify this if you have a different sized character LCD lcd_columns = 16 @@ -21,8 +21,8 @@ blue = pulseio.PWMOut(board.D6) # Initialise the LCD class -lcd = character_lcd.Character_LCD_RGB(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, - lcd_rows, red, green, blue) +lcd = characterlcd.Character_LCD_RGB(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, + lcd_rows, red, green, blue) lcd.clear() # Set LCD color to red diff --git a/examples/charlcd_rpi_mono_simpletest.py b/examples/charlcd_rpi_mono_simpletest.py index 3d64851..697c1d1 100644 --- a/examples/charlcd_rpi_mono_simpletest.py +++ b/examples/charlcd_rpi_mono_simpletest.py @@ -1,8 +1,8 @@ -# Simple test for monochromatic character LCD on Raspberry Pi +"""Simple test for monochromatic character LCD on Raspberry Pi""" import time import board import digitalio -import adafruit_character_lcd.character_lcd_mono as character_lcd +import adafruit_character_lcd.character_lcd as characterlcd # Modify this if you have a different sized character LCD lcd_columns = 16 @@ -18,8 +18,8 @@ lcd_backlight = digitalio.DigitalInOut(board.D4) # Initialise the lcd class -lcd = character_lcd.Character_LCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, - lcd_d7, lcd_columns, lcd_rows, lcd_backlight) +lcd = characterlcd.Character_LCD_Mono(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, + lcd_d7, lcd_columns, lcd_rows, lcd_backlight) # Turn backlight on lcd.backlight = True diff --git a/examples/charlcd_rpi_rgb_simpletest.py b/examples/charlcd_rpi_rgb_simpletest.py index 4bea306..1ed91bf 100644 --- a/examples/charlcd_rpi_rgb_simpletest.py +++ b/examples/charlcd_rpi_rgb_simpletest.py @@ -1,8 +1,8 @@ -# Simple test for RGB character LCD on Raspberry Pi +"""Simple test for RGB character LCD on Raspberry Pi""" import time import board import digitalio -import adafruit_character_lcd.character_lcd_rgb as character_lcd +import adafruit_character_lcd.character_lcd as characterlcd # Modify this if you have a different sized character LCD lcd_columns = 16 @@ -22,8 +22,8 @@ blue = digitalio.DigitalInOut(board.D18) # Initialise the LCD class -lcd = character_lcd.Character_LCD_RGB(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, - lcd_rows, red, green, blue, lcd_backlight) +lcd = characterlcd.Character_LCD_RGB(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, + lcd_rows, red, green, blue, lcd_backlight) RED = [1, 0, 0] GREEN = [0, 1, 0] diff --git a/examples/charlcd_spi_mono_simpletest.py b/examples/charlcd_spi_mono_simpletest.py index 01675ac..55fbcb5 100644 --- a/examples/charlcd_spi_mono_simpletest.py +++ b/examples/charlcd_spi_mono_simpletest.py @@ -1,9 +1,9 @@ -# Simple test for 16x2 character LCD connected to 74HC595 SPI LCD backpack. +"""Simple test for 16x2 character LCD connected to 74HC595 SPI LCD backpack.""" import time import board import busio import digitalio -import adafruit_character_lcd.character_lcd_mono as character_lcd +import adafruit_character_lcd.character_lcd_spi as character_lcd # Modify this if you have a different sized character LCD lcd_columns = 16 From af6e0231e8acdd8ecffd6fa96e169e2924f2b5c8 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 21 Nov 2018 12:35:13 -0500 Subject: [PATCH 17/17] Remove self.backlight_inverted, use property --- adafruit_character_lcd/character_lcd.py | 3 +-- adafruit_character_lcd/character_lcd_i2c.py | 2 +- adafruit_character_lcd/character_lcd_spi.py | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index 53658f1..c663fa1 100755 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -524,8 +524,7 @@ def __init__(self, rs, en, db4, db5, db6, db7, columns, lines, # Setup backlight if backlight_pin is not None: self.backlight_pin.direction = digitalio.Direction.OUTPUT - if backlight_inverted: - self.backlight_pin.value = not backlight_inverted # Turn backlight on + self.backlight = True super().__init__(rs, en, db4, db5, db6, db7, columns, lines) # pylint: enable-msg=too-many-arguments diff --git a/adafruit_character_lcd/character_lcd_i2c.py b/adafruit_character_lcd/character_lcd_i2c.py index bd37601..ec5d8a6 100644 --- a/adafruit_character_lcd/character_lcd_i2c.py +++ b/adafruit_character_lcd/character_lcd_i2c.py @@ -48,6 +48,7 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CharLCD.git" + class Character_LCD_I2C(Character_LCD_Mono): # pylint: disable=too-few-public-methods """Character LCD connected to I2C/SPI backpack using its I2C connection. @@ -79,6 +80,5 @@ def __init__(self, i2c, columns, lines, backlight_inverted=False): db6 = self._mcp.get_pin(5) db7 = self._mcp.get_pin(6) backlight_pin = self._mcp.get_pin(7) - self.backlight_inverted = backlight_inverted super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, backlight_pin=backlight_pin, backlight_inverted=backlight_inverted) diff --git a/adafruit_character_lcd/character_lcd_spi.py b/adafruit_character_lcd/character_lcd_spi.py index a20c2cf..c6a8458 100644 --- a/adafruit_character_lcd/character_lcd_spi.py +++ b/adafruit_character_lcd/character_lcd_spi.py @@ -85,6 +85,5 @@ def __init__(self, spi, latch, columns, lines, backlight_inverted=False): db6 = self._shift_register.get_pin(4) db7 = self._shift_register.get_pin(3) backlight_pin = self._shift_register.get_pin(7) - self.backlight_inverted = backlight_inverted super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, backlight_pin=backlight_pin, backlight_inverted=backlight_inverted)