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.")
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)
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()
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()
42 raise ValueError("Incorrect pin mode: {}".format(mode))
45 def value(self, val=None):
46 """Set or return the Pin Value"""
48 if self._mode in (Pin.IN, Pin.OUT):
51 return mcp2221.gpio_get_pin(self.id)
53 if val in (Pin.LOW, Pin.HIGH):
54 mcp2221.gpio_set_pin(self.id, val)
57 raise ValueError("Invalid value for pin.")
59 if self._mode == Pin.ADC:
61 # MCP2221 ADC is 10 bit, scale to 16 bit per CP API
62 return mcp2221.adc_read(self.id) * 64
64 raise AttributeError("'AnalogIn' object has no attribute 'value'")
66 if self._mode == Pin.DAC:
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)
74 "No action for mode {} with value {}".format(self._mode, val)
78 # create pin instances for each pin