@@ -85,7 +85,9 @@ def __init__(self, busnum=-1, addr=0x20, debug=False):
85
85
self .displayshift = (self .LCD_CURSORMOVE |
86
86
self .LCD_MOVERIGHT )
87
87
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
89
91
self .write4 (0x28 ) # 2 line 5x8 matrix
90
92
self .write4 (self .LCD_CLEARDISPLAY )
91
93
self .write4 (self .LCD_CURSORSHIFT | self .displayshift )
@@ -103,6 +105,28 @@ def __init__(self, busnum=-1, addr=0x20, debug=False):
103
105
0b00000010 , 0b00010010 , 0b00001010 , 0b00011010 ,
104
106
0b00000110 , 0b00010110 , 0b00001110 , 0b00011110 )
105
107
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
+
106
130
# Write 8-bit value to LCD over 4-bit interface
107
131
def write4 (self , value , char_mode = False ):
108
132
""" Send command/data to LCD """
@@ -158,23 +182,13 @@ def write4(self, value, char_mode=False):
158
182
a &= 0b00000001 # Mask out data bits & RW from current OLATB value
159
183
if char_mode : a |= 0b10000000 # RS = Command/data
160
184
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 )
178
192
179
193
# Change data pins back to inputs
180
194
self .mcp .i2c .bus .write_byte_data (
@@ -290,18 +304,17 @@ def noAutoscroll(self):
290
304
291
305
def createChar (self , location , bitmap ):
292
306
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 )
295
308
self .write4 (self .LCD_SETDDRAMADDR )
296
309
297
310
298
311
def message (self , text ):
299
312
""" 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
305
318
306
319
307
320
def backlight (self , color ):
0 commit comments