]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/fake_mcp2221/pin.py
27a1c562e6bfa77daeb991e1fb16cfdc028df147
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / fake_mcp2221 / pin.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """fake_mcp2221 pin names"""
5 import random
6 from .fake_mcp2221 import mcp2221
7
8
9 class Pin:
10     """A basic Pin class for use with a "fake" MCP2221."""
11
12     # pin modes
13     OUT = 0
14     IN = 1
15     ADC = 2
16     DAC = 3
17     # pin values
18     LOW = 0
19     HIGH = 1
20
21     def __init__(self, pin_id=None):
22         self.id = pin_id
23         self._mode = None
24         self._prv_val = False
25
26     def init(self, mode=IN, pull=None):
27         """Initialize the Pin"""
28         if self.id is None:
29             raise RuntimeError("Can not init a None type pin.")
30         if pull is not None:
31             raise NotImplementedError("Internal pullups and pulldowns not supported on the MCP2221")
32         if mode in (Pin.IN, Pin.OUT):
33             # All pins can do GPIO
34             pass
35         elif mode == Pin.ADC:
36             # ADC only available on these pins
37             if self.id not in (1, 2, 3):
38                 raise ValueError("Pin does not have ADC capabilities")
39             pass
40             # Do nothing
41         elif mode == Pin.DAC:
42             # DAC only available on these pins
43             if self.id not in (2, 3):
44                 raise ValueError("Pin does not have DAC capabilities")
45             pass
46         else:
47             raise ValueError("Incorrect pin mode: {}".format(mode))
48         self._mode = mode
49
50     def value(self, val=None):
51         """Set or return the Pin Value"""
52         # Digital In / Out
53         if self._mode in (Pin.IN, Pin.OUT):
54             # digital read
55             if val is None:
56                 # The returned value toggles between True and false
57                 self._prv_val = not self._prv_val
58                 return self._prv_val
59             # digital write
60             if val in (Pin.LOW, Pin.HIGH):
61                 # We don't need to do anything here - no data is produced
62                 return None
63             # nope
64             raise ValueError("Invalid value for pin.")
65         # Analog In
66         if self._mode == Pin.ADC:
67             if val is None:
68                 # Returned value is between 0 and 65535 inclusive
69                 # https://docs.circuitpython.org/en/latest/shared-bindings/analogio/index.html#analogio.AnalogIn.value
70                 self._prv_val = random.randint(0, 65535)
71                 return self._prv_val
72             # read only
73             raise AttributeError("'AnalogIn' object has no attribute 'value'")
74         # Analog Out
75         if self._mode == Pin.DAC:
76             if val is None:
77                 # write only
78                 raise AttributeError("unreadable attribute")
79             # We don't write to the DAC as this is a "fake" implementation
80             return None
81         raise RuntimeError(
82             "No action for mode {} with value {}".format(self._mode, val)
83         )
84
85
86 # create pin instances for each pin
87 G0 = Pin(0)
88 G1 = Pin(1)
89 G2 = Pin(2)
90 G3 = Pin(3)
91
92 SCL = Pin()
93 SDA = Pin()