@@ -123,10 +123,8 @@ def out4(self, bitmask, value):
123123 self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b | 0b00100000 )
124124 # There's no need for delay calls when strobing, as the limited
125125 # I2C throughput already ensures the strobe is held long enough.
126- # Strobe low (!enable)
127- self .mcp .i2c .bus .write_byte_data (
128- self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b )
129126 b = bitmask | self .flip [value & 0x0F ] # Insert low 4 bits
127+ # This also does strobe low (!enable) for prior nybble
130128 self .mcp .i2c .bus .write_byte_data (
131129 self .mcp .i2c .address , self .mcp .MCP23017_OLATB , b )
132130 self .mcp .i2c .bus .write_byte_data (
@@ -142,12 +140,12 @@ def out4(self, bitmask, value):
142140 # can't even be twiddled that fast through I2C, so it's a safe bet
143141 # with these instructions to not waste time polling (which requires
144142 # several I2C transfers for reconfiguring the port direction).
145- # 'pollflag' is set when a potentially time-consuming instruction
146- # has been issued (e.g. screen clear), as well as on startup, and
147- # polling will then occur before more commands or data are issued.
143+ # I/O pins are set as inputs when a potentially time-consuming
144+ # instruction has been issued (e.g. screen clear), as well as on
145+ # startup, and polling will then occur before more commands or data
146+ # are issued.
148147
149148 pollables = ( LCD_CLEARDISPLAY , LCD_RETURNHOME )
150- pollflag = True
151149
152150 # Write 8-bit value to LCD
153151 def write (self , value , char_mode = False ):
@@ -167,10 +165,8 @@ def write(self, value, char_mode=False):
167165 # LCD pin E = MCP pin 13 (PORTB5) Strobe
168166 # LCD D4...D7 = MCP 12...9 (PORTB4...1) Data (see notes later)
169167
170- # If pollflag is set, poll LCD busy state until clear. Data
171- # pins were previously set as inputs, no need to reconfigure
172- # I/O yet.
173- if self .pollflag :
168+ # If I/O pins are in input state, poll LCD busy flag until clear.
169+ if self .mcp .direction & 0b0001111000000000 > 0 :
174170 # Current PORTB pin state RS=0 RW=1
175171 a = ((self .mcp .outputvalue >> 8 ) & 0b00000001 ) | 0b01000000
176172 b = a | 0b00100000 # E=1
@@ -194,9 +190,9 @@ def write(self, value, char_mode=False):
194190 self .mcp .i2c .address , self .mcp .MCP23017_OLATB , a )
195191
196192 # Polling complete, change data pins to outputs
197- save = self .mcp .direction >> 8 # PORTB
198- self .mcp .i2c .bus .write_byte_data (
199- self .mcp .i2c . address , self .mcp .MCP23017_IODIRB , save & 0b11100001 )
193+ self .mcp .direction &= 0b1110000111111111
194+ self .mcp .i2c .bus .write_byte_data (self . mcp . i2c . address ,
195+ self .mcp .MCP23017_IODIRB , self .mcp .direction >> 8 )
200196
201197 # Mask out data bits & RW from current OLATB value
202198 a = ((self .mcp .outputvalue >> 8 ) & 0b00000001 )
@@ -210,14 +206,15 @@ def write(self, value, char_mode=False):
210206 else :
211207 b = self .out4 (a , value )
212208
209+ # Update mcp outputvalue state to reflect changes here
210+ self .mcp .outputvalue = (self .mcp .outputvalue & 0x00FF ) | (b << 8 )
211+
213212 # If a poll-worthy instruction was issued, reconfigure
214- # data pins as inputs and set flag to poll on next call.
213+ # data pins as inputs to indicate need for poll on next call.
215214 if (not char_mode ) and (value in self .pollables ):
216- self .mcp .i2c .bus .write_byte_data (
217- self .mcp .i2c .address , self .mcp .MCP23017_IODIRB , save )
218- # Update mcp outputvalue state to reflect changes here
219- self .mcp .outputvalue = (self .mcp .outputvalue & 0x00FF ) | (b << 8 )
220- self .pollflag = True
215+ self .mcp .direction |= 0b0001111000000000
216+ self .mcp .i2c .bus .write_byte_data (self .mcp .i2c .address ,
217+ self .mcp .MCP23017_IODIRB , self .mcp .direction >> 8 )
221218
222219
223220 # ----------------------------------------------------------------------
0 commit comments