1111from Adafruit_MCP230xx import Adafruit_MCP230XX
1212import smbus
1313
14- mcp = Adafruit_MCP230XX (address = 0x20 , num_gpios = 16 )
1514
16- class Adafruit_CharLCD :
15+ class Adafruit_CharLCDPlate :
1716
1817 OUTPUT = 0
1918 INPUT = 1
@@ -79,20 +78,23 @@ class Adafruit_CharLCD:
7978
8079
8180
82- def __init__ (self , pin_rs = 25 , pin_e = 24 , pins_db = [23 , 17 , 21 , 22 ], pin_rw = 0 ):
81+
82+ def __init__ (self , busnum = 0 , pin_rs = 15 , pin_e = 13 , pins_db = [12 , 11 , 10 , 9 ], pin_rw = 14 ):
8383 self .pin_rs = pin_rs
8484 self .pin_e = pin_e
8585 self .pin_rw = pin_rw
8686 self .pins_db = pins_db
8787
88- mcp .config (self .pin_e , self .OUTPUT )
89- mcp .config (self .pin_rs , self .OUTPUT )
90- mcp .config (self .pin_rw , self .OUTPUT )
91- mcp .output (self .pin_rw , 0 )
92- mcp .output (self .pin_e , 0 )
88+ self .mcp = Adafruit_MCP230XX (busnum = busnum , address = 0x20 , num_gpios = 16 )
89+
90+ self .mcp .config (self .pin_e , self .OUTPUT )
91+ self .mcp .config (self .pin_rs , self .OUTPUT )
92+ self .mcp .config (self .pin_rw , self .OUTPUT )
93+ self .mcp .output (self .pin_rw , 0 )
94+ self .mcp .output (self .pin_e , 0 )
9395
9496 for pin in self .pins_db :
95- mcp .config (pin , self .OUTPUT )
97+ self . mcp .config (pin , self .OUTPUT )
9698
9799 self .write4bits (0x33 ) # initialization
98100 self .write4bits (0x32 ) # initialization
@@ -110,24 +112,24 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], pin_rw=0):
110112 self .write4bits (self .LCD_ENTRYMODESET | self .displaymode ) # set the entry mode
111113
112114 # turn on backlights!
113- mcp .config (6 , mcp .OUTPUT )
114- mcp .config (7 , mcp .OUTPUT )
115- mcp .config (8 , mcp .OUTPUT )
116- mcp .output (6 , 0 ) # red
117- mcp .output (7 , 0 ) # green
118- mcp .output (8 , 0 ) # blue
115+ self . mcp .config (6 , self . mcp .OUTPUT )
116+ self . mcp .config (7 , self . mcp .OUTPUT )
117+ self . mcp .config (8 , self . mcp .OUTPUT )
118+ self . mcp .output (6 , 0 ) # red
119+ self . mcp .output (7 , 0 ) # green
120+ self . mcp .output (8 , 0 ) # blue
119121
120122 # turn on pullups
121- mcp .pullup (self .SELECT , True )
122- mcp .pullup (self .LEFT , True )
123- mcp .pullup (self .RIGHT , True )
124- mcp .pullup (self .UP , True )
125- mcp .pullup (self .DOWN , True )
126- mcp .config (self .SELECT , mcp .INPUT )
127- mcp .config (self .LEFT , mcp .INPUT )
128- mcp .config (self .RIGHT , mcp .INPUT )
129- mcp .config (self .DOWN , mcp .INPUT )
130- mcp .config (self .UP , mcp .INPUT )
123+ self . mcp .pullup (self .SELECT , True )
124+ self . mcp .pullup (self .LEFT , True )
125+ self . mcp .pullup (self .RIGHT , True )
126+ self . mcp .pullup (self .UP , True )
127+ self . mcp .pullup (self .DOWN , True )
128+ self . mcp .config (self .SELECT , self . mcp .INPUT )
129+ self . mcp .config (self .LEFT , self . mcp .INPUT )
130+ self . mcp .config (self .RIGHT , self . mcp .INPUT )
131+ self . mcp .config (self .DOWN , self . mcp .INPUT )
132+ self . mcp .config (self .UP , self . mcp .INPUT )
131133
132134 def begin (self , cols , lines ):
133135 if (lines > 1 ):
@@ -213,30 +215,30 @@ def write4bits(self, bits, char_mode=False):
213215 """ Send command to LCD """
214216 #self.delayMicroseconds(1000) # 1000 microsecond sleep
215217 bits = bin (bits )[2 :].zfill (8 )
216- mcp .output (self .pin_rs , char_mode )
218+ self . mcp .output (self .pin_rs , char_mode )
217219
218220 for i in range (4 ):
219221 if bits [i ] == "1" :
220- mcp .output (self .pins_db [::- 1 ][i ], True )
222+ self . mcp .output (self .pins_db [::- 1 ][i ], True )
221223 else :
222- mcp .output (self .pins_db [::- 1 ][i ], False )
224+ self . mcp .output (self .pins_db [::- 1 ][i ], False )
223225 self .pulseEnable ()
224226
225227 for i in range (4 ,8 ):
226228 if bits [i ] == "1" :
227- mcp .output (self .pins_db [::- 1 ][i - 4 ], True )
229+ self . mcp .output (self .pins_db [::- 1 ][i - 4 ], True )
228230 else :
229- mcp .output (self .pins_db [::- 1 ][i - 4 ], False )
231+ self . mcp .output (self .pins_db [::- 1 ][i - 4 ], False )
230232 self .pulseEnable ()
231233
232234 def delayMicroseconds (self , microseconds ):
233235 seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
234236 sleep (seconds )
235237
236238 def pulseEnable (self ):
237- mcp .output (self .pin_e , True )
239+ self . mcp .output (self .pin_e , True )
238240 self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
239- mcp .output (self .pin_e , False )
241+ self . mcp .output (self .pin_e , False )
240242 #self.delayMicroseconds(1) # commands need > 37us to settle
241243
242244 def message (self , text ):
@@ -248,28 +250,20 @@ def message(self, text):
248250 self .write4bits (ord (char ),True )
249251
250252 def backlight (self , color ):
251- mcp .output (6 , not color & 0x01 )
252- mcp .output (7 , not color & 0x02 )
253- mcp .output (8 , not color & 0x04 )
253+ self . mcp .output (6 , not color & 0x01 )
254+ self . mcp .output (7 , not color & 0x02 )
255+ self . mcp .output (8 , not color & 0x04 )
254256
255257 def buttonPressed (self , buttonname ):
256258 if (buttonname > self .LEFT ):
257259 return false
258260
259- return not mcp .input (buttonname )
261+ return not self . mcp .input (buttonname )
260262
261263
262264if __name__ == '__main__' :
263265
264-
265- # input test
266- # for i in range(16):
267- # mcp.pullup(i, 1)
268- # while (True):
269- # for i in range(16):
270- # print "%d: %x" % (i, mcp.input(i) >> i)
271-
272- lcd = Adafruit_CharLCD (15 , 13 , [12 ,11 ,10 ,9 ], 14 )
266+ lcd = Adafruit_CharLCDPlate (busnum = 0 )
273267 lcd .clear ()
274268 lcd .message ("Adafruit RGB LCD\n Plate w/Keypad!" )
275269 sleep (1 )
0 commit comments