1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """fake_mcp2221 pin names"""
6 from .fake_mcp2221 import mcp2221
10 """A basic Pin class for use with a "fake" MCP2221."""
21 def __init__(self, pin_id=None):
26 def init(self, mode=IN, pull=None):
27 """Initialize the Pin"""
29 raise RuntimeError("Can not init a None type pin.")
31 raise NotImplementedError(
32 "Internal pullups and pulldowns not supported on the MCP2221"
34 if mode in (Pin.IN, Pin.OUT):
35 # All pins can do GPIO
38 # ADC only available on these pins
39 if self.id not in (1, 2, 3):
40 raise ValueError("Pin does not have ADC capabilities")
44 # DAC only available on these pins
45 if self.id not in (2, 3):
46 raise ValueError("Pin does not have DAC capabilities")
49 raise ValueError("Incorrect pin mode: {}".format(mode))
52 def value(self, val=None):
53 """Set or return the Pin Value"""
55 if self._mode in (Pin.IN, Pin.OUT):
58 # The returned value toggles between True and false
59 self._prv_val = not self._prv_val
62 if val in (Pin.LOW, Pin.HIGH):
63 # We don't need to do anything here - no data is produced
66 raise ValueError("Invalid value for pin.")
68 if self._mode == Pin.ADC:
70 # Returned value is between 0 and 65535 inclusive
71 # https://docs.circuitpython.org/en/latest/shared-bindings/analogio/index.html#analogio.AnalogIn.value
72 self._prv_val = random.randint(0, 65535)
75 raise AttributeError("'AnalogIn' object has no attribute 'value'")
77 if self._mode == Pin.DAC:
80 raise AttributeError("unreadable attribute")
81 # We don't write to the DAC as this is a "fake" implementation
84 "No action for mode {} with value {}".format(self._mode, val)
88 # create pin instances for each pin