@@ -85,7 +85,9 @@ def __init__(self, busnum=-1, addr=0x20, debug=False):
8585 self .displayshift = (self .LCD_CURSORMOVE |
8686 self .LCD_MOVERIGHT )
8787
88- self .write4 (0x20 ) # Select 4-bit interface
88+ # self.write4(0x20) # Select 4-bit interface
89+ self .write4 (0x33 ) # Init
90+ self .write4 (0x32 ) # Init
8991 self .write4 (0x28 ) # 2 line 5x8 matrix
9092 self .write4 (self .LCD_CLEARDISPLAY )
9193 self .write4 (self .LCD_CURSORSHIFT | self .displayshift )
@@ -103,6 +105,28 @@ def __init__(self, busnum=-1, addr=0x20, debug=False):
103105 0b00000010 , 0b00010010 , 0b00001010 , 0b00011010 ,
104106 0b00000110 , 0b00010110 , 0b00001110 , 0b00011110 )
105107
108+ # Low-level 4 bit output interface
109+ def out4 (self , bitmask , value ):
110+ b = bitmask | self .flip [value >> 4 ] # Insert high 4 bits of data
111+ # Write initial !E state, data is sampled on rising strobe edge
112+ self .mcp .i2c .bus .write_byte_data (
113+ self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b )
114+ # Strobe high
115+ self .mcp .i2c .bus .write_byte_data (
116+ self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b | 0b00100000 )
117+ # Strobe low
118+ self .mcp .i2c .bus .write_byte_data (
119+ self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b )
120+ b = bitmask | self .flip [value & 0x0F ] # Insert low 4 bits
121+ self .mcp .i2c .bus .write_byte_data (
122+ self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b )
123+ self .mcp .i2c .bus .write_byte_data (
124+ self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b | 0b00100000 )
125+ self .mcp .i2c .bus .write_byte_data (
126+ self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b )
127+ return b
128+
129+
106130 # Write 8-bit value to LCD over 4-bit interface
107131 def write4 (self , value , char_mode = False ):
108132 """ Send command/data to LCD """
@@ -158,23 +182,13 @@ def write4(self, value, char_mode=False):
158182 a &= 0b00000001 # Mask out data bits & RW from current OLATB value
159183 if char_mode : a |= 0b10000000 # RS = Command/data
160184
161- b = a | self .flip [value >> 4 ] # Insert high 4 bits of data
162- # Write initial !E state, data is sampled on rising strobe edge
163- self .mcp .i2c .bus .write_byte_data (
164- self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b )
165- # Strobe high
166- self .mcp .i2c .bus .write_byte_data (
167- self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b | 0b00100000 )
168- # Strobe low
169- self .mcp .i2c .bus .write_byte_data (
170- self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b )
171- b = a | self .flip [value & 0x0F ] # Insert low 4 bits
172- self .mcp .i2c .bus .write_byte_data (
173- self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b )
174- self .mcp .i2c .bus .write_byte_data (
175- self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b | 0b00100000 )
176- self .mcp .i2c .bus .write_byte_data (
177- self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b )
185+ # If string or list, iterate through a write iteration
186+ if isinstance (value , str ):
187+ for v in value : b = self .out4 (a , ord (v ))
188+ elif isinstance (value , list ):
189+ for v in value : b = self .out4 (a , v )
190+ else :
191+ b = self .out4 (a , value )
178192
179193 # Change data pins back to inputs
180194 self .mcp .i2c .bus .write_byte_data (
@@ -290,18 +304,17 @@ def noAutoscroll(self):
290304
291305 def createChar (self , location , bitmap ):
292306 self .write4 (self .LCD_SETCGRAMADDR | ((location & 7 ) << 3 ))
293- for i in range (8 ):
294- self .write4 (bitmap [i ], True )
307+ self .write4 (bitmap , True )
295308 self .write4 (self .LCD_SETDDRAMADDR )
296309
297310
298311 def message (self , text ):
299312 """ Send string to LCD. Newline wraps to second line"""
300- for char in text :
301- if char == ' \n ' :
302- self .write4 (0xC0 ) # set DDRAM address to second line
303- else :
304- self .write4 (ord ( char ), True )
313+ lines = text . split ( ' \n ' ) # Split at newline(s)
314+ for line in lines : # Render each substring...
315+ self .write4 (line , True )
316+ if len ( lines ) > 1 : # If newline(s),
317+ self .write4 (0xC0 ) # set DDRAM address to second line
305318
306319
307320 def backlight (self , color ):
0 commit comments