99# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
1010
1111from Adafruit_I2C import Adafruit_I2C
12-
12+ from time import sleep
1313
1414class Adafruit_CharLCDPlate (Adafruit_I2C ):
1515
@@ -18,7 +18,7 @@ class Adafruit_CharLCDPlate(Adafruit_I2C):
1818
1919 # Port expander registers
2020 MCP23017_IOCON_BANK0 = 0x0A # IOCON when Bank 0 active
21- MCP23017_IOCON_BANK1 = 0x05 # IOCON when Bank 1 active
21+ MCP23017_IOCON_BANK1 = 0x15 # IOCON when Bank 1 active
2222 # These are register addresses when in Bank 1 only:
2323 MCP23017_GPIOA = 0x09
2424 MCP23017_IODIRB = 0x10
@@ -263,6 +263,42 @@ def begin(self, cols, lines):
263263 self .clear ()
264264
265265
266+ # Puts the MCP23017 back in Bank 0 + sequential write mode so
267+ # that other code using the 'classic' library can still work.
268+ # Any code using this newer version of the library should
269+ # consider adding an atexit() handler that calls this.
270+ def stop (self ):
271+ self .porta = 0b11000000 # Turn off LEDs on the way out
272+ self .portb = 0b00000001
273+ sleep (0.0015 )
274+ self .i2c .bus .write_byte_data (
275+ self .i2c .address , self .MCP23017_IOCON_BANK1 , 0 )
276+ self .i2c .bus .write_i2c_block_data (
277+ self .i2c .address , 0 ,
278+ [ 0b00111111 , # IODIRA
279+ self .ddrb , # IODIRB
280+ 0b00000000 , # IPOLA
281+ 0b00000000 , # IPOLB
282+ 0b00000000 , # GPINTENA
283+ 0b00000000 , # GPINTENB
284+ 0b00000000 , # DEFVALA
285+ 0b00000000 , # DEFVALB
286+ 0b00000000 , # INTCONA
287+ 0b00000000 , # INTCONB
288+ 0b00000000 , # IOCON
289+ 0b00000000 , # IOCON
290+ 0b00111111 , # GPPUA
291+ 0b00000000 , # GPPUB
292+ 0b00000000 , # INTFA
293+ 0b00000000 , # INTFB
294+ 0b00000000 , # INTCAPA
295+ 0b00000000 , # INTCAPB
296+ self .porta , # GPIOA
297+ self .portb , # GPIOB
298+ self .porta , # OLATA
299+ self .portb ]) # OLATB
300+
301+
266302 def clear (self ):
267303 self .write (self .LCD_CLEARDISPLAY )
268304
@@ -397,8 +433,6 @@ def buttonPressed(self, b):
397433
398434if __name__ == '__main__' :
399435
400- from time import sleep
401-
402436 lcd = Adafruit_CharLCDPlate ()
403437 lcd .begin (16 , 2 )
404438 lcd .clear ()
0 commit comments