-# Small values seem to help on some Windows setups
-MCP2221_HID_DELAY = float(os.environ.get('BLINKA_MCP2221_HID_DELAY', 0))
-# Windows also seems to want some time after a reset
-MCP2221_RESET_DELAY = float(os.environ.get('BLINKA_MCP2221_RESET_DELAY', 0.1))
+# Here if you need it
+MCP2221_HID_DELAY = float(os.environ.get("BLINKA_MCP2221_HID_DELAY", 0))
+# Use to set delay between reset and device reopen
+MCP2221_RESET_DELAY = float(os.environ.get("BLINKA_MCP2221_RESET_DELAY", 0.5))
 
 # from the C driver
 # http://ww1.microchip.com/downloads/en/DeviceDoc/mcp2221_0_1.tar.gz
 # others (???) determined during driver developement
 # pylint: disable=bad-whitespace
 
 # from the C driver
 # http://ww1.microchip.com/downloads/en/DeviceDoc/mcp2221_0_1.tar.gz
 # others (???) determined during driver developement
 # pylint: disable=bad-whitespace
-RESP_ERR_NOERR              = 0x00
-RESP_ADDR_NACK              = 0x25
-RESP_READ_ERR               = 0x7F
-RESP_READ_COMPL             = 0x55
-RESP_READ_PARTIAL           = 0x54 # ???
-RESP_I2C_IDLE               = 0x00
-RESP_I2C_START_TOUT         = 0x12
-RESP_I2C_RSTART_TOUT        = 0x17
-RESP_I2C_WRADDRL_TOUT       = 0x23
-RESP_I2C_WRADDRL_WSEND      = 0x21
-RESP_I2C_WRADDRL_NACK       = 0x25
-RESP_I2C_WRDATA_TOUT        = 0x44
-RESP_I2C_RDDATA_TOUT        = 0x52
-RESP_I2C_STOP_TOUT          = 0x62
-
-RESP_I2C_MOREDATA           = 0x43 # ???
-RESP_I2C_PARTIALDATA        = 0x41 # ???
-RESP_I2C_WRITINGNOSTOP      = 0x45 # ???
-
-MCP2221_RETRY_MAX           = 50
-MCP2221_MAX_I2C_DATA_LEN    = 60
-MASK_ADDR_NACK              = 0x40
+RESP_ERR_NOERR = 0x00
+RESP_ADDR_NACK = 0x25
+RESP_READ_ERR = 0x7F
+RESP_READ_COMPL = 0x55
+RESP_READ_PARTIAL = 0x54  # ???
+RESP_I2C_IDLE = 0x00
+RESP_I2C_START_TOUT = 0x12
+RESP_I2C_RSTART_TOUT = 0x17
+RESP_I2C_WRADDRL_TOUT = 0x23
+RESP_I2C_WRADDRL_WSEND = 0x21
+RESP_I2C_WRADDRL_NACK = 0x25
+RESP_I2C_WRDATA_TOUT = 0x44
+RESP_I2C_RDDATA_TOUT = 0x52
+RESP_I2C_STOP_TOUT = 0x62
+
+RESP_I2C_MOREDATA = 0x43  # ???
+RESP_I2C_PARTIALDATA = 0x41  # ???
+RESP_I2C_WRITINGNOSTOP = 0x45  # ???
+
+MCP2221_RETRY_MAX = 50
+MCP2221_MAX_I2C_DATA_LEN = 60
+MASK_ADDR_NACK = 0x40
         # first byte is report ID, which =0 for MCP2221
         # remaing bytes = 64 byte report data
         # https://github.com/libusb/hidapi/blob/083223e77952e1ef57e6b77796536a3359c1b2a3/hidapi/hidapi.h#L185
         # first byte is report ID, which =0 for MCP2221
         # remaing bytes = 64 byte report data
         # https://github.com/libusb/hidapi/blob/083223e77952e1ef57e6b77796536a3359c1b2a3/hidapi/hidapi.h#L185
         time.sleep(MCP2221_HID_DELAY)
         if response:
             # return is 64 byte response report
             return self._hid.read(64)
         time.sleep(MCP2221_HID_DELAY)
         if response:
             # return is 64 byte response report
             return self._hid.read(64)
-        # get current settings
-        current = self._hid_xfer(b'\x61')
+        """Set Current Pin Mode"""
+        # already set to that mode?
+        mode &= 0x07
+        if mode == (self._gp_config[pin] & 0x07):
+            return
+        # update GP mode for pin
+        self._gp_config[pin] = mode
-        # each pin can be set individually
-        # but all 4 get set at once, so we need to
-        # transpose current settings
-        report[8]  = current[22]  # GP0
-        report[9]  = current[23]  # GP1
-        report[10] = current[24]  # GP2
-        report[11] = current[25]  # GP3
-        # then change only the one
-        report[8+pin] = mode & 0x07
+        # add GP setttings
+        report[8] = self._gp_config[0]
+        report[9] = self._gp_config[1]
+        report[10] = self._gp_config[2]
+        report[11] = self._gp_config[3]
-        report = bytearray(b'\x50'+b'\x00'*63)  # empty set GPIO report
+        """Set Current GPIO Pin Direction"""
+        if mode:
+            # set bit 3 for INPUT
+            self._gp_config[pin] |= 1 << 3
+        else:
+            # clear bit 3 for OUTPUT
+            self._gp_config[pin] &= ~(1 << 3)
+        report = bytearray(b"\x50" + b"\x00" * 63)  # empty set GPIO report
-        report = bytearray(b'\x50'+b'\x00'*63)  # empty set GPIO report
+        """Set Current GPIO Pin Value"""
+        if value:
+            # set bit 4
+            self._gp_config[pin] |= 1 << 4
+        else:
+            # clear bit 4
+            self._gp_config[pin] &= ~(1 << 4)
+        report = bytearray(b"\x50" + b"\x00" * 63)  # empty set GPIO report
     def _i2c_write(self, cmd, address, buffer, start=0, end=None):
         if self._i2c_state() != 0x00:
             self._i2c_cancel()
     def _i2c_write(self, cmd, address, buffer, start=0, end=None):
         if self._i2c_state() != 0x00:
             self._i2c_cancel()
         while (end - start) > 0:
             chunk = min(end - start, MCP2221_MAX_I2C_DATA_LEN)
             # write out current chunk
         while (end - start) > 0:
             chunk = min(end - start, MCP2221_MAX_I2C_DATA_LEN)
             # write out current chunk
-            resp = self._hid_xfer(bytes([cmd,
-                                         length & 0xFF,
-                                         (length >> 8) & 0xFF,
-                                         address << 1]) +
-                                         buffer[start:(start+chunk)])
+            resp = self._hid_xfer(
+                bytes([cmd, length & 0xFF, (length >> 8) & 0xFF, address << 1])
+                + buffer[start : (start + chunk)]
+            )
                     raise RuntimeError("Unrecoverable I2C state failure")
                 retries += 1
                 if retries >= MCP2221_RETRY_MAX:
                     raise RuntimeError("I2C write error, max retries reached.")
                 time.sleep(0.001)
                     raise RuntimeError("Unrecoverable I2C state failure")
                 retries += 1
                 if retries >= MCP2221_RETRY_MAX:
                     raise RuntimeError("I2C write error, max retries reached.")
                 time.sleep(0.001)
-        resp = self._hid_xfer(bytes([cmd,
-                                     length & 0xFF,
-                                     (length >> 8) & 0xFF,
-                                     (address << 1) | 0x01]))
+        resp = self._hid_xfer(
+            bytes([cmd, length & 0xFF, (length >> 8) & 0xFF, (address << 1) | 0x01])
+        )
-        self._hid_xfer(bytes([0x10,  # set parameters
-                              0x00,  # don't care
-                              0x00,  # no effect
-                              0x20,  # next byte is clock divider
-                              12000000 // baudrate - 3]))
+        """Configure I2C"""
+        self._hid_xfer(
+            bytes(
+                [
+                    0x10,  # set parameters
+                    0x00,  # don't care
+                    0x00,  # no effect
+                    0x20,  # next byte is clock divider
+                    12000000 // baudrate - 3,
+                ]
+            )
+        )
 
     def i2c_writeto(self, address, buffer, *, start=0, end=None):
 
     def i2c_writeto(self, address, buffer, *, start=0, end=None):
         self._i2c_write(0x90, address, buffer, start, end)
 
     def i2c_readfrom_into(self, address, buffer, *, start=0, end=None):
         self._i2c_write(0x90, address, buffer, start, end)
 
     def i2c_readfrom_into(self, address, buffer, *, start=0, end=None):
         self._i2c_read(0x91, address, buffer, start, end)
 
         self._i2c_read(0x91, address, buffer, start, end)
 
-    def i2c_writeto_then_readfrom(self, address, out_buffer, in_buffer, *,
-                                  out_start=0, out_end=None,
-                                  in_start=0, in_end=None):
+    def i2c_writeto_then_readfrom(
+        self,
+        address,
+        out_buffer,
+        in_buffer,
+        *,
+        out_start=0,
+        out_end=None,
+        in_start=0,
+        in_end=None
+    ):
+        """Write data from buffer_out to an address and then
+        read data from an address and into buffer_in
+        """
         self._i2c_write(0x94, address, out_buffer, out_start, out_end)
         self._i2c_read(0x93, address, in_buffer, in_start, in_end)
 
     def i2c_scan(self, *, start=0, end=0x79):
         self._i2c_write(0x94, address, out_buffer, out_start, out_end)
         self._i2c_read(0x93, address, in_buffer, in_start, in_end)
 
     def i2c_scan(self, *, start=0, end=0x79):
-                self.i2c_writeto(addr, b'\x00')
-            except RuntimeError: # no reply!
+                self.i2c_writeto(addr, b"\x00")
+            except RuntimeError:  # no reply!