1 """MCP2221 pin names"""
2 from .mcp2221 import mcp2221
6 """A basic Pin class for use with MCP2221."""
17 def __init__(self, pin_id=None):
21 def init(self, mode=IN, pull=None):
22 """Initialize the Pin"""
24 raise RuntimeError("Can not init a None type pin.")
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)
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()
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()
44 raise ValueError("Incorrect pin mode: {}".format(mode))
47 def value(self, val=None):
48 """Set or return the Pin Value"""
50 if self._mode in (Pin.IN, Pin.OUT):
53 return mcp2221.gpio_get_pin(self.id)
55 if val in (Pin.LOW, Pin.HIGH):
56 mcp2221.gpio_set_pin(self.id, val)
59 raise ValueError("Invalid value for pin.")
61 if self._mode == Pin.ADC:
63 # MCP2221 ADC is 10 bit, scale to 16 bit per CP API
64 return mcp2221.adc_read(self.id) * 64
66 raise AttributeError("'AnalogIn' object has no attribute 'value'")
68 if self._mode == Pin.DAC:
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)
76 "No action for mode {} with value {}".format(self._mode, val)
80 # create pin instances for each pin