From: brentru Date: Fri, 29 Mar 2024 13:55:43 +0000 (-0400) Subject: detecting X-Git-Tag: 8.39.0^2~14 X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/commitdiff_plain/66df9c4b8688960e98e67692074a1d8e4ffb6444?hp=60f2f025ef249781f54a63e5275cbe8ea565891d detecting --- diff --git a/examples/digital_input.py b/examples/digital_input.py new file mode 100644 index 0000000..73da800 --- /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 0000000..6aae9d5 --- /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 0000000..e69de29 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 0000000..b684763 --- /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 0000000..39e01de --- /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 0000000..9b55c68 --- /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 0000000..e2cb40f --- /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 216c4d5..6165708 100644 --- a/src/board.py +++ b/src/board.py @@ -17,7 +17,7 @@ __blinka__ = True import sys - +import os import adafruit_platformdetect.constants.boards as ap_board from adafruit_blinka.agnostic import board_id, detector @@ -224,7 +224,10 @@ elif board_id == ap_board.BINHO_NOVA: 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 640a8cf..2a5724a 100644 --- a/src/digitalio.py +++ b/src/digitalio.py @@ -9,7 +9,7 @@ See `CircuitPython:digitalio` in CircuitPython for more details. * 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.binho_nova: 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 2c51e3b..785e0bc 100644 --- a/src/microcontroller/__init__.py +++ b/src/microcontroller/__init__.py @@ -123,7 +123,10 @@ elif chip_id == ap_chip.BINHO: 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 c499364..911eefe 100644 --- a/src/microcontroller/pin.py +++ b/src/microcontroller/pin.py @@ -97,7 +97,11 @@ elif chip_id == ap_chip.BINHO: 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: