From 66df9c4b8688960e98e67692074a1d8e4ffb6444 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 29 Mar 2024 09:55:43 -0400 Subject: [PATCH 01/15] detecting --- examples/digital_input.py | 10 ++ .../board/fake_microchip_mcp2221.py | 13 ++ .../microcontroller/fake_mcp2221/__init__.py | 0 .../microcontroller/fake_mcp2221/analogio.py | 57 +++++++ .../fake_mcp2221/fake_mcp2221.py | 150 ++++++++++++++++++ .../microcontroller/fake_mcp2221/i2c.py | 57 +++++++ .../microcontroller/fake_mcp2221/pin.py | 98 ++++++++++++ src/board.py | 7 +- src/digitalio.py | 7 +- src/microcontroller/__init__.py | 5 +- src/microcontroller/pin.py | 6 +- 11 files changed, 404 insertions(+), 6 deletions(-) create mode 100644 examples/digital_input.py create mode 100644 src/adafruit_blinka/board/fake_microchip_mcp2221.py create mode 100644 src/adafruit_blinka/microcontroller/fake_mcp2221/__init__.py create mode 100644 src/adafruit_blinka/microcontroller/fake_mcp2221/analogio.py create mode 100644 src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py create mode 100644 src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py create mode 100644 src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py diff --git a/examples/digital_input.py b/examples/digital_input.py new file mode 100644 index 00000000..73da800c --- /dev/null +++ b/examples/digital_input.py @@ -0,0 +1,10 @@ +import time +import board +import digitalio + +button = digitalio.DigitalInOut(board.G0) +button.direction = digitalio.Direction.INPUT + +while True: + print(f"Button value: {button.value}") + time.sleep(0.5) \ No newline at end of file diff --git a/src/adafruit_blinka/board/fake_microchip_mcp2221.py b/src/adafruit_blinka/board/fake_microchip_mcp2221.py new file mode 100644 index 00000000..6aae9d56 --- /dev/null +++ b/src/adafruit_blinka/board/fake_microchip_mcp2221.py @@ -0,0 +1,13 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT +"""Pin definitions for the MicroChip MCP2221""" +from adafruit_blinka.microcontroller.fake_mcp2221 import pin + +G0 = pin.G0 +G1 = pin.G1 +G2 = pin.G2 +G3 = pin.G3 + +SCL = pin.SCL +SDA = pin.SDA diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/__init__.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/analogio.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/analogio.py new file mode 100644 index 00000000..b684763a --- /dev/null +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/analogio.py @@ -0,0 +1,57 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT +""" +`analogio` - Analog input and output control +================================================= +See `CircuitPython:analogio` in CircuitPython for more details. +* Author(s): Carter Nelson +""" + +from adafruit_blinka.microcontroller.fake_mcp2221.pin import Pin +from adafruit_blinka import ContextManaged + + +class AnalogIn(ContextManaged): + """Analog Input Class""" + + def __init__(self, pin): + self._pin = Pin(pin.id) + self._pin.init(mode=Pin.ADC) + + @property + def value(self): + """Read the ADC and return the value""" + return self._pin.value() + + # pylint: disable=no-self-use + @value.setter + def value(self, value): + # emulate what CircuitPython does + raise AttributeError("'AnalogIn' object has no attribute 'value'") + + # pylint: enable=no-self-use + + def deinit(self): + del self._pin + + +class AnalogOut(ContextManaged): + """Analog Output Class""" + + def __init__(self, pin): + self._pin = Pin(pin.id) + self._pin.init(mode=Pin.DAC) + + @property + def value(self): + """Return an error. This is output only.""" + # emulate what CircuitPython does + raise AttributeError("unreadable attribute") + + @value.setter + def value(self, value): + self._pin.value(value) + + def deinit(self): + del self._pin \ No newline at end of file diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py new file mode 100644 index 00000000..39e01de7 --- /dev/null +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py @@ -0,0 +1,150 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT +"""Chip Definition for MCP2221""" + +import os +import time +import atexit + +import hid + + +class MCP2221: + """MCP2221 Device Class Definition""" + + def __init__(self): + pass # This is a "fake" implementation + + def close(self): + """Close the hid device. Does nothing if the device is not open.""" + pass + + def __del__(self): + # try to close the device before destroying the instance + pass + + + def _hid_xfer(self, report, response=True): + """Perform HID Transfer""" + return None + + # ---------------------------------------------------------------- + # MISC + # ---------------------------------------------------------------- + def gp_get_mode(self, pin): + """Get Current Pin Mode""" + pass + + def gp_set_mode(self, pin, mode): + """Set Current Pin Mode""" + pass + + def _pretty_report(self, register): + pass + + def _status_dump(self): + pass + + def _sram_dump(self): + pass + + def _reset(self): + pass + + # ---------------------------------------------------------------- + # GPIO + # ---------------------------------------------------------------- + def gpio_set_direction(self, pin, mode): + """Set Current GPIO Pin Direction""" + pass + + def gpio_set_pin(self, pin, value): + """Set Current GPIO Pin Value""" + pass + + def gpio_get_pin(self, pin): + """Get Current GPIO Pin Value""" + pass + + + # ---------------------------------------------------------------- + # I2C + # ---------------------------------------------------------------- + def _i2c_status(self): + pass + + def _i2c_state(self): + pass + + def _i2c_cancel(self): + pass + + # pylint: disable=too-many-arguments,too-many-branches + def _i2c_write(self, cmd, address, buffer, start=0, end=None): + pass + + def _i2c_read(self, cmd, address, buffer, start=0, end=None): + pass + + # pylint: enable=too-many-arguments + def _i2c_configure(self, baudrate=100000): + """Configure I2C""" + pass + + def i2c_writeto(self, address, buffer, *, start=0, end=None): + """Write data from the buffer to an address""" + pass + + def i2c_readfrom_into(self, address, buffer, *, start=0, end=None): + """Read data from an address and into the buffer""" + pass + + 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 + """ + pass + + def i2c_scan(self, *, start=0, end=0x79): + """Perform an I2C Device Scan""" + pass + + + # ---------------------------------------------------------------- + # ADC + # ---------------------------------------------------------------- + def adc_configure(self, vref=0): + """Configure the Analog-to-Digital Converter""" + pass + + def adc_read(self, pin): + """Read from the Analog-to-Digital Converter""" + pass + + # ---------------------------------------------------------------- + # DAC + # ---------------------------------------------------------------- + def dac_configure(self, vref=0): + """Configure the Digital-to-Analog Converter""" + pass + + # pylint: disable=unused-argument + def dac_write(self, pin, value): + """Write to the Digital-to-Analog Converter""" + pass + + # pylint: enable=unused-argument + + +mcp2221 = MCP2221() \ No newline at end of file diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py new file mode 100644 index 00000000..9b55c683 --- /dev/null +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py @@ -0,0 +1,57 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT +"""I2C Class for MCP2221""" +from .fake_mcp2221 import mcp2221 + + +class I2C: + """Custom I2C Class for MCP2221""" + + def __init__(self, *, frequency=100000): + self._mcp2221 = mcp2221 + # self._mcp2221._i2c_configure(frequency) + + def scan(self): + """Perform an I2C Device Scan""" + # TODO: We need to fake an I2C scan here + return self._mcp2221.i2c_scan() + + # pylint: disable=unused-argument + def writeto(self, address, buffer, *, start=0, end=None, stop=True): + """Write data from the buffer to an address""" + #self._mcp2221.i2c_writeto(address, buffer, start=start, end=end) + + def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True): + """Read data from an address and into the buffer""" + #self._mcp2221.i2c_readfrom_into(address, buffer, start=start, end=end) + + def writeto_then_readfrom( + self, + address, + buffer_out, + buffer_in, + *, + out_start=0, + out_end=None, + in_start=0, + in_end=None, + stop=False, + ): + """Write data from buffer_out to an address and then + read data from an address and into buffer_in + """ + + """ + self._mcp2221.i2c_writeto_then_readfrom( + address, + buffer_out, + buffer_in, + out_start=out_start, + out_end=out_end, + in_start=in_start, + in_end=in_end, + ) + """ + + # pylint: enable=unused-argument \ No newline at end of file diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py new file mode 100644 index 00000000..e2cb40f8 --- /dev/null +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py @@ -0,0 +1,98 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT +"""fake_mcp2221 pin names""" +import random +from .fake_mcp2221 import mcp2221 + + +class Pin: + """A basic Pin class for use with a "fake" MCP2221.""" + + # pin modes + OUT = 0 + IN = 1 + ADC = 2 + DAC = 3 + # pin values + LOW = 0 + HIGH = 1 + + def __init__(self, pin_id=None): + self.id = pin_id + self._mode = None + self._prv_val = None + + def init(self, mode=IN, pull=None): + """Initialize the Pin""" + if self.id is None: + raise RuntimeError("Can not init a None type pin.") + if pull is not None: + raise NotImplementedError("Internal pullups and pulldowns not supported on the MCP2221") + if mode in (Pin.IN, Pin.OUT): + # All pins can do GPIO + pass + elif mode == Pin.ADC: + # ADC only available on these pins + if self.id not in (1, 2, 3): + raise ValueError("Pin does not have ADC capabilities") + pass + # Do nothing + elif mode == Pin.DAC: + # DAC only available on these pins + if self.id not in (2, 3): + raise ValueError("Pin does not have DAC capabilities") + pass + else: + raise ValueError("Incorrect pin mode: {}".format(mode)) + self._mode = mode + + def value(self, val=None): + """Set or return the Pin Value""" + # Digital In / Out + if self._mode in (Pin.IN, Pin.OUT): + # digital read + if val is None: + # The value returned will toggle between True and False + # and will be True on the first digital read + # TODO: Behavior needs to be tested + if self._prv_val == None or False: + self._prv_val = True + else: + self._prv_val = False + return self._prv_val + # digital write + if val in (Pin.LOW, Pin.HIGH): + # We don't need to do anything here - no data is produced + return None + # nope + raise ValueError("Invalid value for pin.") + # Analog In + if self._mode == Pin.ADC: + if val is None: + # Returned value is between 0 and 65535 inclusive + # https://docs.circuitpython.org/en/latest/shared-bindings/analogio/index.html#analogio.AnalogIn.value + self._prv_val = random.randint(0, 65535) + return self._prv_val + # read only + raise AttributeError("'AnalogIn' object has no attribute 'value'") + # Analog Out + if self._mode == Pin.DAC: + if val is None: + # write only + raise AttributeError("unreadable attribute") + # We don't write to the DAC as this is a "fake" implementation + return None + raise RuntimeError( + "No action for mode {} with value {}".format(self._mode, val) + ) + + +# create pin instances for each pin +G0 = Pin(0) +G1 = Pin(1) +G2 = Pin(2) +G3 = Pin(3) + +SCL = Pin() +SDA = Pin() \ No newline at end of file diff --git a/src/board.py b/src/board.py index 216c4d56..61657080 100644 --- a/src/board.py +++ b/src/board.py @@ -17,7 +17,7 @@ import sys - +import os import adafruit_platformdetect.constants.boards as ap_board from adafruit_blinka.agnostic import board_id, detector @@ -224,7 +224,10 @@ from adafruit_blinka.board.binho_nova import * elif board_id == ap_board.MICROCHIP_MCP2221: - from adafruit_blinka.board.microchip_mcp2221 import * + if "BLINKA_FORCEBOARD" not in os.environ: + from adafruit_blinka.board.microchip_mcp2221 import * + elif os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + from adafruit_blinka.board.fake_microchip_mcp2221 import * elif board_id == ap_board.GREATFET_ONE: from adafruit_blinka.board.greatfet_one import * diff --git a/src/digitalio.py b/src/digitalio.py index 640a8cfc..2a5724a2 100644 --- a/src/digitalio.py +++ b/src/digitalio.py @@ -9,7 +9,7 @@ * Author(s): cefn """ - +import os from adafruit_blinka.agnostic import board_id, detector # pylint: disable=ungrouped-imports,wrong-import-position,unused-wildcard-import,wildcard-import @@ -127,7 +127,10 @@ elif detector.board.greatfet_one: from adafruit_blinka.microcontroller.nxp_lpc4330.pin import Pin elif detector.board.microchip_mcp2221: - from adafruit_blinka.microcontroller.mcp2221.pin import Pin + if "BLINKA_FORCEBOARD" not in os.environ: + from adafruit_blinka.microcontroller.mcp2221.pin import Pin + elif os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + from adafruit_blinka.microcontroller.fake_mcp2221.pin import Pin elif detector.chip.RP2040_U2IF: from adafruit_blinka.microcontroller.rp2040_u2if.pin import Pin # MicroPython Chips diff --git a/src/microcontroller/__init__.py b/src/microcontroller/__init__.py index 2c51e3b0..785e0bc4 100644 --- a/src/microcontroller/__init__.py +++ b/src/microcontroller/__init__.py @@ -123,7 +123,10 @@ def delay_us(delay): elif chip_id == ap_chip.LPC4330: from adafruit_blinka.microcontroller.nxp_lpc4330 import * elif chip_id == ap_chip.MCP2221: - from adafruit_blinka.microcontroller.mcp2221 import * + if "BLINKA_FORCECHIP" not in os.environ: + from adafruit_blinka.microcontroller.mcp2221 import * + elif os.environ["BLINKA_FORCECHIP"] == "MCP2221": + from adafruit_blinka.microcontroller.fake_mcp2221 import * elif chip_id == ap_chip.MIPS24KC: from adafruit_blinka.microcontroller.atheros.ar9331 import * elif chip_id == ap_chip.MIPS24KEC: diff --git a/src/microcontroller/pin.py b/src/microcontroller/pin.py index c499364f..911eefe6 100644 --- a/src/microcontroller/pin.py +++ b/src/microcontroller/pin.py @@ -97,7 +97,11 @@ elif chip_id == ap_chip.LPC4330: from adafruit_blinka.microcontroller.nxp_lpc4330.pin import * elif chip_id == ap_chip.MCP2221: - from adafruit_blinka.microcontroller.mcp2221.pin import * + if "BLINKA_FORCECHIP" not in os.environ: + from adafruit_blinka.microcontroller.mcp2221.pin import * + elif os.environ["BLINKA_FORCECHIP"] == "MCP2221": + from adafruit_blinka.microcontroller.fake_mcp2221.pin import * + elif chip_id == ap_chip.A10: from adafruit_blinka.microcontroller.allwinner.a20.pin import * elif chip_id == ap_chip.A20: From 45c6059e81fcdc21409153ffa553700874ed7634 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 29 Mar 2024 09:57:40 -0400 Subject: [PATCH 02/15] toggle --- .../microcontroller/fake_mcp2221/pin.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py index e2cb40f8..27a1c562 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py @@ -21,7 +21,7 @@ class Pin: def __init__(self, pin_id=None): self.id = pin_id self._mode = None - self._prv_val = None + self._prv_val = False def init(self, mode=IN, pull=None): """Initialize the Pin""" @@ -53,13 +53,8 @@ def value(self, val=None): if self._mode in (Pin.IN, Pin.OUT): # digital read if val is None: - # The value returned will toggle between True and False - # and will be True on the first digital read - # TODO: Behavior needs to be tested - if self._prv_val == None or False: - self._prv_val = True - else: - self._prv_val = False + # The returned value toggles between True and false + self._prv_val = not self._prv_val return self._prv_val # digital write if val in (Pin.LOW, Pin.HIGH): From 8a2c94484ad5cc4a22270c8478042209f0055846 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 29 Mar 2024 10:22:47 -0400 Subject: [PATCH 03/15] hook analogio --- examples/analog_in.py | 2 +- src/analogio.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/analog_in.py b/examples/analog_in.py index af936d1a..52abb43f 100644 --- a/examples/analog_in.py +++ b/examples/analog_in.py @@ -6,7 +6,7 @@ import board from analogio import AnalogIn -analog_in = AnalogIn(board.A1) +analog_in = AnalogIn(board.G1) def get_voltage(pin): diff --git a/src/analogio.py b/src/analogio.py index ed1adfe8..7d3f92af 100644 --- a/src/analogio.py +++ b/src/analogio.py @@ -9,7 +9,7 @@ * Author(s): Carter Nelson, Melissa LeBlanc-Williams """ - +import os import sys from adafruit_blinka.agnostic import detector @@ -17,8 +17,12 @@ # pylint: disable=ungrouped-imports,wrong-import-position,unused-import if detector.board.microchip_mcp2221: - from adafruit_blinka.microcontroller.mcp2221.analogio import AnalogIn - from adafruit_blinka.microcontroller.mcp2221.analogio import AnalogOut + if "BLINKA_FORCECHIP" not in os.environ: + from adafruit_blinka.microcontroller.mcp2221.analogio import AnalogIn + from adafruit_blinka.microcontroller.mcp2221.analogio import AnalogOut + elif os.environ["BLINKA_FORCECHIP"] == "MCP2221": + from adafruit_blinka.microcontroller.fake_mcp2221.analogio import AnalogIn + from adafruit_blinka.microcontroller.fake_mcp2221.analogio import AnalogOut elif detector.board.greatfet_one: from adafruit_blinka.microcontroller.nxp_lpc4330.analogio import AnalogIn from adafruit_blinka.microcontroller.nxp_lpc4330.analogio import AnalogOut From 0e0e0f1a1ef91a20236627aaf4cf82df03f9a836 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 29 Mar 2024 11:18:48 -0400 Subject: [PATCH 04/15] fake i2c device scan, allow stimulus --- .../microcontroller/fake_mcp2221/i2c.py | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py index 9b55c683..5f280513 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT """I2C Class for MCP2221""" +import random from .fake_mcp2221 import mcp2221 @@ -10,21 +11,28 @@ class I2C: def __init__(self, *, frequency=100000): self._mcp2221 = mcp2221 - # self._mcp2221._i2c_configure(frequency) - def scan(self): - """Perform an I2C Device Scan""" - # TODO: We need to fake an I2C scan here - return self._mcp2221.i2c_scan() + def scan(self, address_list = None): + """Mocks an I2C scan. + If address_list is not provided, this function returns a list of 3 randomly generated I2C addresses from 0x0 to 0x79. + For a stimulus-driven test: If address_list is provided, this function returns the provided address_list. + """ + if address_list == None: + # Generate a list of 3 randomly generated addresses from 0x0 to 0x79 + address_list = [] + for _ in range(3): + address_list.append(random.randint(0x0, 0x79)) + return address_list + return address_list # pylint: disable=unused-argument def writeto(self, address, buffer, *, start=0, end=None, stop=True): """Write data from the buffer to an address""" - #self._mcp2221.i2c_writeto(address, buffer, start=start, end=end) + pass def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True): """Read data from an address and into the buffer""" - #self._mcp2221.i2c_readfrom_into(address, buffer, start=start, end=end) + pass def writeto_then_readfrom( self, @@ -41,17 +49,6 @@ def writeto_then_readfrom( """Write data from buffer_out to an address and then read data from an address and into buffer_in """ - - """ - self._mcp2221.i2c_writeto_then_readfrom( - address, - buffer_out, - buffer_in, - out_start=out_start, - out_end=out_end, - in_start=in_start, - in_end=in_end, - ) - """ + pass # pylint: enable=unused-argument \ No newline at end of file From 11d8e60c2f755a1410b5be5a5ede56ad513fffeb Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 2 Apr 2024 16:30:37 -0400 Subject: [PATCH 05/15] i2c scan and fake_mcp --- src/busio.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/busio.py b/src/busio.py index 6912f0cc..cdce3211 100644 --- a/src/busio.py +++ b/src/busio.py @@ -9,6 +9,7 @@ * Author(s): cefn """ +import os try: import threading @@ -50,8 +51,10 @@ def init(self, scl, sda, frequency): self._i2c = _I2C(frequency=frequency) return if detector.board.microchip_mcp2221: - from adafruit_blinka.microcontroller.mcp2221.i2c import I2C as _I2C - + if "BLINKA_FORCEBOARD" not in os.environ: + from adafruit_blinka.microcontroller.mcp2221.i2c import I2C as _I2C + elif os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + from adafruit_blinka.microcontroller.fake_mcp2221.i2c import I2C as _I2C self._i2c = _I2C(frequency=frequency) return if detector.board.greatfet_one: From 987e70e7a61beeb703cf33ab9f9fa6b0ebc6ce1f Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 2 Apr 2024 16:43:11 -0400 Subject: [PATCH 06/15] rm examples --- examples/analog_in.py | 2 +- examples/digital_input.py | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) delete mode 100644 examples/digital_input.py diff --git a/examples/analog_in.py b/examples/analog_in.py index 52abb43f..af936d1a 100644 --- a/examples/analog_in.py +++ b/examples/analog_in.py @@ -6,7 +6,7 @@ import board from analogio import AnalogIn -analog_in = AnalogIn(board.G1) +analog_in = AnalogIn(board.A1) def get_voltage(pin): diff --git a/examples/digital_input.py b/examples/digital_input.py deleted file mode 100644 index 73da800c..00000000 --- a/examples/digital_input.py +++ /dev/null @@ -1,10 +0,0 @@ -import time -import board -import digitalio - -button = digitalio.DigitalInOut(board.G0) -button.direction = digitalio.Direction.INPUT - -while True: - print(f"Button value: {button.value}") - time.sleep(0.5) \ No newline at end of file From 2bb509c5e122b4e0538bd5a87364c3c5a524fc0c Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 2 Apr 2024 16:47:34 -0400 Subject: [PATCH 07/15] blacken --- .../microcontroller/fake_mcp2221/analogio.py | 2 +- .../microcontroller/fake_mcp2221/fake_mcp2221.py | 7 ++----- src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py | 4 ++-- src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py | 6 ++++-- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/analogio.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/analogio.py index b684763a..a70d7fc4 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/analogio.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/analogio.py @@ -54,4 +54,4 @@ def value(self, value): self._pin.value(value) def deinit(self): - del self._pin \ No newline at end of file + del self._pin diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py index 39e01de7..7a75dec3 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py @@ -14,7 +14,7 @@ class MCP2221: """MCP2221 Device Class Definition""" def __init__(self): - pass # This is a "fake" implementation + pass # This is a "fake" implementation def close(self): """Close the hid device. Does nothing if the device is not open.""" @@ -24,7 +24,6 @@ def __del__(self): # try to close the device before destroying the instance pass - def _hid_xfer(self, report, response=True): """Perform HID Transfer""" return None @@ -67,7 +66,6 @@ def gpio_get_pin(self, pin): """Get Current GPIO Pin Value""" pass - # ---------------------------------------------------------------- # I2C # ---------------------------------------------------------------- @@ -120,7 +118,6 @@ def i2c_scan(self, *, start=0, end=0x79): """Perform an I2C Device Scan""" pass - # ---------------------------------------------------------------- # ADC # ---------------------------------------------------------------- @@ -147,4 +144,4 @@ def dac_write(self, pin, value): # pylint: enable=unused-argument -mcp2221 = MCP2221() \ No newline at end of file +mcp2221 = MCP2221() diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py index 5f280513..b98c70c2 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py @@ -12,7 +12,7 @@ class I2C: def __init__(self, *, frequency=100000): self._mcp2221 = mcp2221 - def scan(self, address_list = None): + def scan(self, address_list=None): """Mocks an I2C scan. If address_list is not provided, this function returns a list of 3 randomly generated I2C addresses from 0x0 to 0x79. For a stimulus-driven test: If address_list is provided, this function returns the provided address_list. @@ -51,4 +51,4 @@ def writeto_then_readfrom( """ pass - # pylint: enable=unused-argument \ No newline at end of file + # pylint: enable=unused-argument diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py index 27a1c562..f4e1b94a 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py @@ -28,7 +28,9 @@ def init(self, mode=IN, pull=None): if self.id is None: raise RuntimeError("Can not init a None type pin.") if pull is not None: - raise NotImplementedError("Internal pullups and pulldowns not supported on the MCP2221") + raise NotImplementedError( + "Internal pullups and pulldowns not supported on the MCP2221" + ) if mode in (Pin.IN, Pin.OUT): # All pins can do GPIO pass @@ -90,4 +92,4 @@ def value(self, val=None): G3 = Pin(3) SCL = Pin() -SDA = Pin() \ No newline at end of file +SDA = Pin() From 54a64285fbc416a587a0894e1495f891e5c6080a Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 2 Apr 2024 17:04:39 -0400 Subject: [PATCH 08/15] pylint --- .../fake_mcp2221/fake_mcp2221.py | 123 +----------------- .../microcontroller/fake_mcp2221/i2c.py | 37 +----- .../microcontroller/fake_mcp2221/pin.py | 8 +- src/microcontroller/__init__.py | 2 +- src/microcontroller/pin.py | 2 +- 5 files changed, 10 insertions(+), 162 deletions(-) diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py index 7a75dec3..5312e1af 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py @@ -16,130 +16,9 @@ class MCP2221: def __init__(self): pass # This is a "fake" implementation - def close(self): - """Close the hid device. Does nothing if the device is not open.""" - pass - def __del__(self): # try to close the device before destroying the instance - pass - - def _hid_xfer(self, report, response=True): - """Perform HID Transfer""" - return None - - # ---------------------------------------------------------------- - # MISC - # ---------------------------------------------------------------- - def gp_get_mode(self, pin): - """Get Current Pin Mode""" - pass - - def gp_set_mode(self, pin, mode): - """Set Current Pin Mode""" - pass - - def _pretty_report(self, register): - pass - - def _status_dump(self): - pass - - def _sram_dump(self): - pass - - def _reset(self): - pass - - # ---------------------------------------------------------------- - # GPIO - # ---------------------------------------------------------------- - def gpio_set_direction(self, pin, mode): - """Set Current GPIO Pin Direction""" - pass - - def gpio_set_pin(self, pin, value): - """Set Current GPIO Pin Value""" - pass - - def gpio_get_pin(self, pin): - """Get Current GPIO Pin Value""" - pass - - # ---------------------------------------------------------------- - # I2C - # ---------------------------------------------------------------- - def _i2c_status(self): - pass - - def _i2c_state(self): - pass - - def _i2c_cancel(self): - pass - - # pylint: disable=too-many-arguments,too-many-branches - def _i2c_write(self, cmd, address, buffer, start=0, end=None): - pass - - def _i2c_read(self, cmd, address, buffer, start=0, end=None): - pass - - # pylint: enable=too-many-arguments - def _i2c_configure(self, baudrate=100000): - """Configure I2C""" - pass - - def i2c_writeto(self, address, buffer, *, start=0, end=None): - """Write data from the buffer to an address""" - pass - - def i2c_readfrom_into(self, address, buffer, *, start=0, end=None): - """Read data from an address and into the buffer""" - pass - - 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 - """ - pass - - def i2c_scan(self, *, start=0, end=0x79): - """Perform an I2C Device Scan""" - pass - - # ---------------------------------------------------------------- - # ADC - # ---------------------------------------------------------------- - def adc_configure(self, vref=0): - """Configure the Analog-to-Digital Converter""" - pass - - def adc_read(self, pin): - """Read from the Analog-to-Digital Converter""" - pass - - # ---------------------------------------------------------------- - # DAC - # ---------------------------------------------------------------- - def dac_configure(self, vref=0): - """Configure the Digital-to-Analog Converter""" - pass - - # pylint: disable=unused-argument - def dac_write(self, pin, value): - """Write to the Digital-to-Analog Converter""" - pass + return # pylint: enable=unused-argument diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py index b98c70c2..30f0b1de 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py @@ -11,44 +11,19 @@ class I2C: def __init__(self, *, frequency=100000): self._mcp2221 = mcp2221 + self._freq = frequency def scan(self, address_list=None): """Mocks an I2C scan. - If address_list is not provided, this function returns a list of 3 randomly generated I2C addresses from 0x0 to 0x79. - For a stimulus-driven test: If address_list is provided, this function returns the provided address_list. + If address_list is not provided, this function returns a + list of 3 randomly generated I2C addresses from 0x0 to 0x79. + For a stimulus-driven test: If address_list is provided, + this function returns the provided address_list. """ - if address_list == None: + if address_list is None: # Generate a list of 3 randomly generated addresses from 0x0 to 0x79 address_list = [] for _ in range(3): address_list.append(random.randint(0x0, 0x79)) return address_list return address_list - - # pylint: disable=unused-argument - def writeto(self, address, buffer, *, start=0, end=None, stop=True): - """Write data from the buffer to an address""" - pass - - def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True): - """Read data from an address and into the buffer""" - pass - - def writeto_then_readfrom( - self, - address, - buffer_out, - buffer_in, - *, - out_start=0, - out_end=None, - in_start=0, - in_end=None, - stop=False, - ): - """Write data from buffer_out to an address and then - read data from an address and into buffer_in - """ - pass - - # pylint: enable=unused-argument diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py index f4e1b94a..f3b6b1fb 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py @@ -3,7 +3,6 @@ # SPDX-License-Identifier: MIT """fake_mcp2221 pin names""" import random -from .fake_mcp2221 import mcp2221 class Pin: @@ -31,20 +30,15 @@ def init(self, mode=IN, pull=None): raise NotImplementedError( "Internal pullups and pulldowns not supported on the MCP2221" ) - if mode in (Pin.IN, Pin.OUT): - # All pins can do GPIO - pass - elif mode == Pin.ADC: + if mode == Pin.ADC: # ADC only available on these pins if self.id not in (1, 2, 3): raise ValueError("Pin does not have ADC capabilities") - pass # Do nothing elif mode == Pin.DAC: # DAC only available on these pins if self.id not in (2, 3): raise ValueError("Pin does not have DAC capabilities") - pass else: raise ValueError("Incorrect pin mode: {}".format(mode)) self._mode = mode diff --git a/src/microcontroller/__init__.py b/src/microcontroller/__init__.py index 785e0bc4..b381b6f7 100644 --- a/src/microcontroller/__init__.py +++ b/src/microcontroller/__init__.py @@ -7,7 +7,7 @@ * Author(s): Melissa LeBlanc-Williams """ - +import os import sys import time diff --git a/src/microcontroller/pin.py b/src/microcontroller/pin.py index 911eefe6..0a33ec6a 100644 --- a/src/microcontroller/pin.py +++ b/src/microcontroller/pin.py @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: MIT """Pins named after their chip name.""" - +import os import sys from adafruit_platformdetect.constants import chips as ap_chip, boards as ap_boards from adafruit_blinka.agnostic import board_id, chip_id From bf0cdc66803a9dea3e6dc3cc885c4e108874e000 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 2 Apr 2024 17:07:56 -0400 Subject: [PATCH 09/15] lint2 --- .../microcontroller/fake_mcp2221/fake_mcp2221.py | 6 ------ src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py index 5312e1af..74a0713e 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/fake_mcp2221.py @@ -3,12 +3,6 @@ # SPDX-License-Identifier: MIT """Chip Definition for MCP2221""" -import os -import time -import atexit - -import hid - class MCP2221: """MCP2221 Device Class Definition""" diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py index 30f0b1de..c9c077b5 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py @@ -13,7 +13,7 @@ def __init__(self, *, frequency=100000): self._mcp2221 = mcp2221 self._freq = frequency - def scan(self, address_list=None): + def scan(address_list=None): """Mocks an I2C scan. If address_list is not provided, this function returns a list of 3 randomly generated I2C addresses from 0x0 to 0x79. From ab3a51a7ab11ed84f5760f10d47024b93f028254 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 3 Apr 2024 10:44:59 -0400 Subject: [PATCH 10/15] fix no self use --- src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py index c9c077b5..0f55a0e3 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py @@ -13,7 +13,8 @@ def __init__(self, *, frequency=100000): self._mcp2221 = mcp2221 self._freq = frequency - def scan(address_list=None): + # pylint: disable=no-self-use + def scan(self, address_list=None): """Mocks an I2C scan. If address_list is not provided, this function returns a list of 3 randomly generated I2C addresses from 0x0 to 0x79. From 455fc42423d886c8dbd0f3ff7cc1acae29aafbfd Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 3 Apr 2024 10:48:36 -0400 Subject: [PATCH 11/15] add staticmethod decorator --- src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py index 0f55a0e3..7937cdbe 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py @@ -14,6 +14,7 @@ def __init__(self, *, frequency=100000): self._freq = frequency # pylint: disable=no-self-use + @staticmethod def scan(self, address_list=None): """Mocks an I2C scan. If address_list is not provided, this function returns a From b47de02bd942c98e397ab0e931818de5f838582c Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 3 Apr 2024 10:59:35 -0400 Subject: [PATCH 12/15] decorator --- src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py index 7937cdbe..d4b2c6f8 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/i2c.py @@ -13,9 +13,8 @@ def __init__(self, *, frequency=100000): self._mcp2221 = mcp2221 self._freq = frequency - # pylint: disable=no-self-use @staticmethod - def scan(self, address_list=None): + def scan(address_list=None): """Mocks an I2C scan. If address_list is not provided, this function returns a list of 3 randomly generated I2C addresses from 0x0 to 0x79. From 6ed1e2116d59fa76a9819191b3bd84b9bd3d02e7 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 3 Apr 2024 11:26:11 -0400 Subject: [PATCH 13/15] blacken --- .../microcontroller/fake_mcp2221/pin.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py b/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py index f3b6b1fb..f522a39e 100644 --- a/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py +++ b/src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py @@ -27,18 +27,24 @@ def init(self, mode=IN, pull=None): if self.id is None: raise RuntimeError("Can not init a None type pin.") if pull is not None: - raise NotImplementedError( - "Internal pullups and pulldowns not supported on the MCP2221" - ) - if mode == Pin.ADC: + raise NotImplementedError("Internal pullups and pulldowns not supported") + if mode in (Pin.IN, Pin.OUT): + # All pins can do GPIO + # mcp2221.gp_set_mode(self.id, mcp2221.GP_GPIO) + # mcp2221.gpio_set_direction(self.id, mode) + self._mode = mode + elif mode == Pin.ADC: # ADC only available on these pins if self.id not in (1, 2, 3): raise ValueError("Pin does not have ADC capabilities") - # Do nothing + # mcp2221.gp_set_mode(self.id, mcp2221.GP_ALT0) + # mcp2221.adc_configure() elif mode == Pin.DAC: # DAC only available on these pins if self.id not in (2, 3): raise ValueError("Pin does not have DAC capabilities") + # mcp2221.gp_set_mode(self.id, mcp2221.GP_ALT1) + # mcp2221.dac_configure() else: raise ValueError("Incorrect pin mode: {}".format(mode)) self._mode = mode From a2213644fcf96fa91d09ecb443eaed997468752e Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 4 Apr 2024 16:10:11 -0400 Subject: [PATCH 14/15] fixes per review --- src/analogio.py | 8 ++++---- src/board.py | 6 +++--- src/busio.py | 6 +++--- src/digitalio.py | 6 +++--- src/microcontroller/__init__.py | 6 +++--- src/microcontroller/pin.py | 7 +++---- 6 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/analogio.py b/src/analogio.py index 7d3f92af..645a5c46 100644 --- a/src/analogio.py +++ b/src/analogio.py @@ -17,12 +17,12 @@ # pylint: disable=ungrouped-imports,wrong-import-position,unused-import if detector.board.microchip_mcp2221: - if "BLINKA_FORCECHIP" not in os.environ: - from adafruit_blinka.microcontroller.mcp2221.analogio import AnalogIn - from adafruit_blinka.microcontroller.mcp2221.analogio import AnalogOut - elif os.environ["BLINKA_FORCECHIP"] == "MCP2221": + if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": from adafruit_blinka.microcontroller.fake_mcp2221.analogio import AnalogIn from adafruit_blinka.microcontroller.fake_mcp2221.analogio import AnalogOut + else: + from adafruit_blinka.microcontroller.mcp2221.analogio import AnalogIn + from adafruit_blinka.microcontroller.mcp2221.analogio import AnalogOut elif detector.board.greatfet_one: from adafruit_blinka.microcontroller.nxp_lpc4330.analogio import AnalogIn from adafruit_blinka.microcontroller.nxp_lpc4330.analogio import AnalogOut diff --git a/src/board.py b/src/board.py index 61657080..e8eee04d 100644 --- a/src/board.py +++ b/src/board.py @@ -224,10 +224,10 @@ from adafruit_blinka.board.binho_nova import * elif board_id == ap_board.MICROCHIP_MCP2221: - if "BLINKA_FORCEBOARD" not in os.environ: - from adafruit_blinka.board.microchip_mcp2221 import * - elif os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": from adafruit_blinka.board.fake_microchip_mcp2221 import * + else: + from adafruit_blinka.board.microchip_mcp2221 import * elif board_id == ap_board.GREATFET_ONE: from adafruit_blinka.board.greatfet_one import * diff --git a/src/busio.py b/src/busio.py index cdce3211..ec9f6589 100644 --- a/src/busio.py +++ b/src/busio.py @@ -51,10 +51,10 @@ def init(self, scl, sda, frequency): self._i2c = _I2C(frequency=frequency) return if detector.board.microchip_mcp2221: - if "BLINKA_FORCEBOARD" not in os.environ: - from adafruit_blinka.microcontroller.mcp2221.i2c import I2C as _I2C - elif os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": from adafruit_blinka.microcontroller.fake_mcp2221.i2c import I2C as _I2C + else: + from adafruit_blinka.microcontroller.mcp2221.i2c import I2C as _I2C self._i2c = _I2C(frequency=frequency) return if detector.board.greatfet_one: diff --git a/src/digitalio.py b/src/digitalio.py index 2a5724a2..07db7f1b 100644 --- a/src/digitalio.py +++ b/src/digitalio.py @@ -127,10 +127,10 @@ elif detector.board.greatfet_one: from adafruit_blinka.microcontroller.nxp_lpc4330.pin import Pin elif detector.board.microchip_mcp2221: - if "BLINKA_FORCEBOARD" not in os.environ: - from adafruit_blinka.microcontroller.mcp2221.pin import Pin - elif os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": from adafruit_blinka.microcontroller.fake_mcp2221.pin import Pin + else: + from adafruit_blinka.microcontroller.mcp2221.pin import Pin elif detector.chip.RP2040_U2IF: from adafruit_blinka.microcontroller.rp2040_u2if.pin import Pin # MicroPython Chips diff --git a/src/microcontroller/__init__.py b/src/microcontroller/__init__.py index b381b6f7..f7d134f8 100644 --- a/src/microcontroller/__init__.py +++ b/src/microcontroller/__init__.py @@ -123,10 +123,10 @@ def delay_us(delay): elif chip_id == ap_chip.LPC4330: from adafruit_blinka.microcontroller.nxp_lpc4330 import * elif chip_id == ap_chip.MCP2221: - if "BLINKA_FORCECHIP" not in os.environ: - from adafruit_blinka.microcontroller.mcp2221 import * - elif os.environ["BLINKA_FORCECHIP"] == "MCP2221": + if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": from adafruit_blinka.microcontroller.fake_mcp2221 import * + else: + from adafruit_blinka.microcontroller.mcp2221 import * elif chip_id == ap_chip.MIPS24KC: from adafruit_blinka.microcontroller.atheros.ar9331 import * elif chip_id == ap_chip.MIPS24KEC: diff --git a/src/microcontroller/pin.py b/src/microcontroller/pin.py index 0a33ec6a..9138989a 100644 --- a/src/microcontroller/pin.py +++ b/src/microcontroller/pin.py @@ -97,11 +97,10 @@ elif chip_id == ap_chip.LPC4330: from adafruit_blinka.microcontroller.nxp_lpc4330.pin import * elif chip_id == ap_chip.MCP2221: - if "BLINKA_FORCECHIP" not in os.environ: - from adafruit_blinka.microcontroller.mcp2221.pin import * - elif os.environ["BLINKA_FORCECHIP"] == "MCP2221": + if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": from adafruit_blinka.microcontroller.fake_mcp2221.pin import * - + else: + from adafruit_blinka.microcontroller.mcp2221.pin import * elif chip_id == ap_chip.A10: from adafruit_blinka.microcontroller.allwinner.a20.pin import * elif chip_id == ap_chip.A20: From 9f4b4a3304d6aa0045b6769ba721a578f7205345 Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 4 Apr 2024 16:14:37 -0400 Subject: [PATCH 15/15] blacken --- src/analogio.py | 5 ++++- src/board.py | 5 ++++- src/busio.py | 5 ++++- src/digitalio.py | 5 ++++- src/microcontroller/__init__.py | 5 ++++- src/microcontroller/pin.py | 5 ++++- 6 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/analogio.py b/src/analogio.py index 645a5c46..c132ba38 100644 --- a/src/analogio.py +++ b/src/analogio.py @@ -17,7 +17,10 @@ # pylint: disable=ungrouped-imports,wrong-import-position,unused-import if detector.board.microchip_mcp2221: - if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + if ( + "BLINKA_FORCECHIP" in os.environ + and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221" + ): from adafruit_blinka.microcontroller.fake_mcp2221.analogio import AnalogIn from adafruit_blinka.microcontroller.fake_mcp2221.analogio import AnalogOut else: diff --git a/src/board.py b/src/board.py index e8eee04d..7a3bc969 100644 --- a/src/board.py +++ b/src/board.py @@ -224,7 +224,10 @@ from adafruit_blinka.board.binho_nova import * elif board_id == ap_board.MICROCHIP_MCP2221: - if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + if ( + "BLINKA_FORCECHIP" in os.environ + and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221" + ): from adafruit_blinka.board.fake_microchip_mcp2221 import * else: from adafruit_blinka.board.microchip_mcp2221 import * diff --git a/src/busio.py b/src/busio.py index ec9f6589..0a3f5299 100644 --- a/src/busio.py +++ b/src/busio.py @@ -51,7 +51,10 @@ def init(self, scl, sda, frequency): self._i2c = _I2C(frequency=frequency) return if detector.board.microchip_mcp2221: - if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + if ( + "BLINKA_FORCECHIP" in os.environ + and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221" + ): from adafruit_blinka.microcontroller.fake_mcp2221.i2c import I2C as _I2C else: from adafruit_blinka.microcontroller.mcp2221.i2c import I2C as _I2C diff --git a/src/digitalio.py b/src/digitalio.py index 07db7f1b..7f57e315 100644 --- a/src/digitalio.py +++ b/src/digitalio.py @@ -127,7 +127,10 @@ elif detector.board.greatfet_one: from adafruit_blinka.microcontroller.nxp_lpc4330.pin import Pin elif detector.board.microchip_mcp2221: - if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + if ( + "BLINKA_FORCECHIP" in os.environ + and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221" + ): from adafruit_blinka.microcontroller.fake_mcp2221.pin import Pin else: from adafruit_blinka.microcontroller.mcp2221.pin import Pin diff --git a/src/microcontroller/__init__.py b/src/microcontroller/__init__.py index f7d134f8..5ab316c9 100644 --- a/src/microcontroller/__init__.py +++ b/src/microcontroller/__init__.py @@ -123,7 +123,10 @@ def delay_us(delay): elif chip_id == ap_chip.LPC4330: from adafruit_blinka.microcontroller.nxp_lpc4330 import * elif chip_id == ap_chip.MCP2221: - if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + if ( + "BLINKA_FORCECHIP" in os.environ + and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221" + ): from adafruit_blinka.microcontroller.fake_mcp2221 import * else: from adafruit_blinka.microcontroller.mcp2221 import * diff --git a/src/microcontroller/pin.py b/src/microcontroller/pin.py index 9138989a..8d30cbb2 100644 --- a/src/microcontroller/pin.py +++ b/src/microcontroller/pin.py @@ -97,7 +97,10 @@ elif chip_id == ap_chip.LPC4330: from adafruit_blinka.microcontroller.nxp_lpc4330.pin import * elif chip_id == ap_chip.MCP2221: - if "BLINKA_FORCECHIP" in os.environ and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221": + if ( + "BLINKA_FORCECHIP" in os.environ + and os.environ["BLINKA_FORCEBOARD"] == "MICROCHIP_MCP2221" + ): from adafruit_blinka.microcontroller.fake_mcp2221.pin import * else: from adafruit_blinka.microcontroller.mcp2221.pin import *