1 from .mcp2221 import mcp2221
4 """A basic Pin class for use with MCP2221."""
15 def __init__(self, pin_id=None):
19 def init(self, mode=IN, pull=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)
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()
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()
39 raise ValueError("Incorrect pin mode: {}".format(mode))
42 def value(self, val=None):
44 if self._mode in (Pin.IN, Pin.OUT):
47 return mcp2221.gpio_get_pin(self.id)
49 elif val in (Pin.LOW, Pin.HIGH):
50 mcp2221.gpio_set_pin(self.id, val)
53 raise ValueError("Invalid value for pin.")
55 elif self._mode == Pin.ADC:
57 # MCP2221 ADC is 10 bit, scale to 16 bit per CP API
58 return mcp2221.adc_read(self.id) * 64
61 raise AttributeError("'AnalogIn' object has no attribute 'value'")
63 elif self._mode == Pin.DAC:
66 raise AttributeError("unreadable attribute")
68 # scale 16 bit value to MCP2221 5 bit DAC (yes 5 bit)
69 mcp2221.dac_write(self.id, val // 2048)
71 raise RuntimeError("No action for mode {} with value {}".format(self._mode, val))
74 # create pin instances for each pin