1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """fake_mcp2221 pin names"""
9 """A basic Pin class for use with a "fake" MCP2221."""
20 def __init__(self, pin_id=None):
25 def init(self, mode=IN, pull=None):
26 """Initialize the Pin"""
28 raise RuntimeError("Can not init a None type pin.")
30 raise NotImplementedError(
31 "Internal pullups and pulldowns not supported on the MCP2221"
34 # ADC only available on these pins
35 if self.id not in (1, 2, 3):
36 raise ValueError("Pin does not have ADC capabilities")
39 # DAC only available on these pins
40 if self.id not in (2, 3):
41 raise ValueError("Pin does not have DAC capabilities")
43 raise ValueError("Incorrect pin mode: {}".format(mode))
46 def value(self, val=None):
47 """Set or return the Pin Value"""
49 if self._mode in (Pin.IN, Pin.OUT):
52 # The returned value toggles between True and false
53 self._prv_val = not self._prv_val
56 if val in (Pin.LOW, Pin.HIGH):
57 # We don't need to do anything here - no data is produced
60 raise ValueError("Invalid value for pin.")
62 if self._mode == Pin.ADC:
64 # Returned value is between 0 and 65535 inclusive
65 # https://docs.circuitpython.org/en/latest/shared-bindings/analogio/index.html#analogio.AnalogIn.value
66 self._prv_val = random.randint(0, 65535)
69 raise AttributeError("'AnalogIn' object has no attribute 'value'")
71 if self._mode == Pin.DAC:
74 raise AttributeError("unreadable attribute")
75 # We don't write to the DAC as this is a "fake" implementation
78 "No action for mode {} with value {}".format(self._mode, val)
82 # create pin instances for each pin