1111from Adafruit_MCP230xx import Adafruit_MCP230XX
1212import smbus
1313
14- # change busnum = 0 to bbusnum = 1 if you have a rev 2 Pi!
15- mcp = Adafruit_MCP230XX (busnum = 0 , address = 0x20 , num_gpios = 16 )
1614
17- class Adafruit_CharLCD :
15+ class Adafruit_CharLCDPlate :
1816
1917 OUTPUT = 0
2018 INPUT = 1
@@ -80,20 +78,23 @@ class Adafruit_CharLCD:
8078
8179
8280
83- 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 ):
8483 self .pin_rs = pin_rs
8584 self .pin_e = pin_e
8685 self .pin_rw = pin_rw
8786 self .pins_db = pins_db
8887
89- mcp .config (self .pin_e , self .OUTPUT )
90- mcp .config (self .pin_rs , self .OUTPUT )
91- mcp .config (self .pin_rw , self .OUTPUT )
92- mcp .output (self .pin_rw , 0 )
93- 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 )
9495
9596 for pin in self .pins_db :
96- mcp .config (pin , self .OUTPUT )
97+ self . mcp .config (pin , self .OUTPUT )
9798
9899 self .write4bits (0x33 ) # initialization
99100 self .write4bits (0x32 ) # initialization
@@ -111,24 +112,24 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], pin_rw=0):
111112 self .write4bits (self .LCD_ENTRYMODESET | self .displaymode ) # set the entry mode
112113
113114 # turn on backlights!
114- mcp .config (6 , mcp .OUTPUT )
115- mcp .config (7 , mcp .OUTPUT )
116- mcp .config (8 , mcp .OUTPUT )
117- mcp .output (6 , 0 ) # red
118- mcp .output (7 , 0 ) # green
119- 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
120121
121122 # turn on pullups
122- mcp .pullup (self .SELECT , True )
123- mcp .pullup (self .LEFT , True )
124- mcp .pullup (self .RIGHT , True )
125- mcp .pullup (self .UP , True )
126- mcp .pullup (self .DOWN , True )
127- mcp .config (self .SELECT , mcp .INPUT )
128- mcp .config (self .LEFT , mcp .INPUT )
129- mcp .config (self .RIGHT , mcp .INPUT )
130- mcp .config (self .DOWN , mcp .INPUT )
131- 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 )
132133
133134 def begin (self , cols , lines ):
134135 if (lines > 1 ):
@@ -214,30 +215,30 @@ def write4bits(self, bits, char_mode=False):
214215 """ Send command to LCD """
215216 #self.delayMicroseconds(1000) # 1000 microsecond sleep
216217 bits = bin (bits )[2 :].zfill (8 )
217- mcp .output (self .pin_rs , char_mode )
218+ self . mcp .output (self .pin_rs , char_mode )
218219
219220 for i in range (4 ):
220221 if bits [i ] == "1" :
221- mcp .output (self .pins_db [::- 1 ][i ], True )
222+ self . mcp .output (self .pins_db [::- 1 ][i ], True )
222223 else :
223- mcp .output (self .pins_db [::- 1 ][i ], False )
224+ self . mcp .output (self .pins_db [::- 1 ][i ], False )
224225 self .pulseEnable ()
225226
226227 for i in range (4 ,8 ):
227228 if bits [i ] == "1" :
228- mcp .output (self .pins_db [::- 1 ][i - 4 ], True )
229+ self . mcp .output (self .pins_db [::- 1 ][i - 4 ], True )
229230 else :
230- mcp .output (self .pins_db [::- 1 ][i - 4 ], False )
231+ self . mcp .output (self .pins_db [::- 1 ][i - 4 ], False )
231232 self .pulseEnable ()
232233
233234 def delayMicroseconds (self , microseconds ):
234235 seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
235236 sleep (seconds )
236237
237238 def pulseEnable (self ):
238- mcp .output (self .pin_e , True )
239+ self . mcp .output (self .pin_e , True )
239240 self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
240- mcp .output (self .pin_e , False )
241+ self . mcp .output (self .pin_e , False )
241242 #self.delayMicroseconds(1) # commands need > 37us to settle
242243
243244 def message (self , text ):
@@ -249,39 +250,37 @@ def message(self, text):
249250 self .write4bits (ord (char ),True )
250251
251252 def backlight (self , color ):
252- mcp .output (6 , not color & 0x01 )
253- mcp .output (7 , not color & 0x02 )
254- 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 )
255256
257+ def buttonPressed (self , buttonname ):
258+ if (buttonname > self .LEFT ):
259+ return false
256260
257- if __name__ == '__main__' :
261+ return not self . mcp . input ( buttonname )
258262
259263
260- # input test
261- # for i in range(16):
262- # mcp.pullup(i, 1)
263- # while (True):
264- # for i in range(16):
265- # print "%d: %x" % (i, mcp.input(i) >> i)
264+ if __name__ == '__main__' :
266265
267- lcd = Adafruit_CharLCD ( 15 , 13 , [ 12 , 11 , 10 , 9 ], 14 )
266+ lcd = Adafruit_CharLCDPlate ( busnum = 0 )
268267 lcd .clear ()
269268 lcd .message ("Adafruit RGB LCD\n Plate w/Keypad!" )
270269 sleep (1 )
271270 while 1 :
272- if (not mcp . input (lcd .LEFT )):
271+ if (lcd . buttonPressed (lcd .LEFT )):
273272 lcd .backlight (lcd .RED )
274273
275- if (not mcp . input (lcd .UP )):
274+ if (lcd . buttonPressed (lcd .UP )):
276275 lcd .backlight (lcd .BLUE )
277276
278- if (not mcp . input (lcd .DOWN )):
277+ if (lcd . buttonPressed (lcd .DOWN )):
279278 lcd .backlight (lcd .GREEN )
280279
281- if (not mcp . input (lcd .RIGHT )):
280+ if (lcd . buttonPressed (lcd .RIGHT )):
282281 lcd .backlight (lcd .VIOLET )
283282
284- if (not mcp . input (lcd .SELECT )):
283+ if (lcd . buttonPressed (lcd .SELECT )):
285284 lcd .backlight (lcd .ON )
286285
287286
0 commit comments