]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/mcp2221/pin.py
Merge pull request #449 from caternuson/pico_u2if
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / mcp2221 / pin.py
1 """MCP2221 pin names"""
2 from .mcp2221 import mcp2221
3
4
5 class Pin:
6     """A basic Pin class for use with MCP2221."""
7
8     # pin modes
9     OUT = 0
10     IN = 1
11     ADC = 2
12     DAC = 3
13     # pin values
14     LOW = 0
15     HIGH = 1
16
17     def __init__(self, pin_id=None):
18         self.id = pin_id
19         self._mode = None
20
21     def init(self, mode=IN, pull=None):
22         """Initialize the Pin"""
23         if self.id is None:
24             raise RuntimeError("Can not init a None type pin.")
25         if pull is not None:
26             raise NotImplementedError("Internal pullups and pulldowns not supported")
27         if mode in (Pin.IN, Pin.OUT):
28             # All pins can do GPIO
29             mcp2221.gp_set_mode(self.id, mcp2221.GP_GPIO)
30             mcp2221.gpio_set_direction(self.id, mode)
31         elif mode == Pin.ADC:
32             # ADC only available on these pins
33             if self.id not in (1, 2, 3):
34                 raise ValueError("Pin does not have ADC capabilities")
35             mcp2221.gp_set_mode(self.id, mcp2221.GP_ALT0)
36             mcp2221.adc_configure()
37         elif mode == Pin.DAC:
38             # DAC only available on these pins
39             if self.id not in (2, 3):
40                 raise ValueError("Pin does not have DAC capabilities")
41             mcp2221.gp_set_mode(self.id, mcp2221.GP_ALT1)
42             mcp2221.dac_configure()
43         else:
44             raise ValueError("Incorrect pin mode: {}".format(mode))
45         self._mode = mode
46
47     def value(self, val=None):
48         """Set or return the Pin Value"""
49         # Digital In / Out
50         if self._mode in (Pin.IN, Pin.OUT):
51             # digital read
52             if val is None:
53                 return mcp2221.gpio_get_pin(self.id)
54             # digital write
55             if val in (Pin.LOW, Pin.HIGH):
56                 mcp2221.gpio_set_pin(self.id, val)
57                 return None
58             # nope
59             raise ValueError("Invalid value for pin.")
60         # Analog In
61         if self._mode == Pin.ADC:
62             if val is None:
63                 # MCP2221 ADC is 10 bit, scale to 16 bit per CP API
64                 return mcp2221.adc_read(self.id) * 64
65             # read only
66             raise AttributeError("'AnalogIn' object has no attribute 'value'")
67         # Analog Out
68         if self._mode == Pin.DAC:
69             if val is None:
70                 # write only
71                 raise AttributeError("unreadable attribute")
72             # scale 16 bit value to MCP2221 5 bit DAC (yes 5 bit)
73             mcp2221.dac_write(self.id, val // 2048)
74             return None
75         raise RuntimeError(
76             "No action for mode {} with value {}".format(self._mode, val)
77         )
78
79
80 # create pin instances for each pin
81 G0 = Pin(0)
82 G1 = Pin(1)
83 G2 = Pin(2)
84 G3 = Pin(3)
85
86 SCL = Pin()
87 SDA = Pin()