]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/mcp2221/pin.py
Merge pull request #287 from makermelissa/libgpiod_pulsein
[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 mode in (Pin.IN, Pin.OUT):
26             # All pins can do GPIO
27             mcp2221.gp_set_mode(self.id, mcp2221.GP_GPIO)
28             mcp2221.gpio_set_direction(self.id, mode)
29         elif mode == Pin.ADC:
30             # ADC only available on these pins
31             if self.id not in (1, 2, 3):
32                 raise ValueError("Pin does not have ADC capabilities")
33             mcp2221.gp_set_mode(self.id, mcp2221.GP_ALT0)
34             mcp2221.adc_configure()
35         elif mode == Pin.DAC:
36             # DAC only available on these pins
37             if self.id not in (2, 3):
38                 raise ValueError("Pin does not have DAC capabilities")
39             mcp2221.gp_set_mode(self.id, mcp2221.GP_ALT1)
40             mcp2221.dac_configure()
41         else:
42             raise ValueError("Incorrect pin mode: {}".format(mode))
43         self._mode = mode
44
45     def value(self, val=None):
46         """Set or return the Pin Value"""
47         # Digital In / Out
48         if self._mode in (Pin.IN, Pin.OUT):
49             # digital read
50             if val is None:
51                 return mcp2221.gpio_get_pin(self.id)
52             # digital write
53             if val in (Pin.LOW, Pin.HIGH):
54                 mcp2221.gpio_set_pin(self.id, val)
55                 return None
56             # nope
57             raise ValueError("Invalid value for pin.")
58         # Analog In
59         if self._mode == Pin.ADC:
60             if val is None:
61                 # MCP2221 ADC is 10 bit, scale to 16 bit per CP API
62                 return mcp2221.adc_read(self.id) * 64
63             # read only
64             raise AttributeError("'AnalogIn' object has no attribute 'value'")
65         # Analog Out
66         if self._mode == Pin.DAC:
67             if val is None:
68                 # write only
69                 raise AttributeError("unreadable attribute")
70             # scale 16 bit value to MCP2221 5 bit DAC (yes 5 bit)
71             mcp2221.dac_write(self.id, val // 2048)
72             return None
73         raise RuntimeError(
74             "No action for mode {} with value {}".format(self._mode, val)
75         )
76
77
78 # create pin instances for each pin
79 G0 = Pin(0)
80 G1 = Pin(1)
81 G2 = Pin(2)
82 G3 = Pin(3)
83
84 SCL = Pin()
85 SDA = Pin()