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