9
9
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
10
10
11
11
from Adafruit_I2C import Adafruit_I2C
12
-
12
+ from time import sleep
13
13
14
14
class Adafruit_CharLCDPlate (Adafruit_I2C ):
15
15
@@ -18,7 +18,7 @@ class Adafruit_CharLCDPlate(Adafruit_I2C):
18
18
19
19
# Port expander registers
20
20
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
22
22
# These are register addresses when in Bank 1 only:
23
23
MCP23017_GPIOA = 0x09
24
24
MCP23017_IODIRB = 0x10
@@ -263,6 +263,42 @@ def begin(self, cols, lines):
263
263
self .clear ()
264
264
265
265
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
+
266
302
def clear (self ):
267
303
self .write (self .LCD_CLEARDISPLAY )
268
304
@@ -397,8 +433,6 @@ def buttonPressed(self, b):
397
433
398
434
if __name__ == '__main__' :
399
435
400
- from time import sleep
401
-
402
436
lcd = Adafruit_CharLCDPlate ()
403
437
lcd .begin (16 , 2 )
404
438
lcd .clear ()
0 commit comments