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