1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """MCP2221 pin names"""
5 from .mcp2221 import mcp2221
9 """A basic Pin class for use with MCP2221."""
20 def __init__(self, pin_id=None):
24 def init(self, mode=IN, pull=None):
25 """Initialize the Pin"""
27 raise RuntimeError("Can not init a None type pin.")
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)
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()
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()
47 raise ValueError("Incorrect pin mode: {}".format(mode))
50 def value(self, val=None):
51 """Set or return the Pin Value"""
53 if self._mode in (Pin.IN, Pin.OUT):
56 return mcp2221.gpio_get_pin(self.id)
58 if val in (Pin.LOW, Pin.HIGH):
59 mcp2221.gpio_set_pin(self.id, val)
62 raise ValueError("Invalid value for pin.")
64 if self._mode == Pin.ADC:
66 # MCP2221 ADC is 10 bit, scale to 16 bit per CP API
67 return mcp2221.adc_read(self.id) * 64
69 raise AttributeError("'AnalogIn' object has no attribute 'value'")
71 if self._mode == Pin.DAC:
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)
79 "No action for mode {} with value {}".format(self._mode, val)
83 # create pin instances for each pin