]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/pico_u2if/analogio.py
Merge pull request #460 from makermelissa/master
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / pico_u2if / analogio.py
1 """
2 `analogio` - Analog input and output control
3 =================================================
4 See `CircuitPython:analogio` in CircuitPython for more details.
5 * Author(s): Carter Nelson
6 """
7 from adafruit_blinka import ContextManaged
8 from .pico_u2if import pico_u2if
9
10
11 class AnalogIn(ContextManaged):
12     """Analog Input Class"""
13
14     def __init__(self, pin):
15         # per their pinout, why only two?
16         if pin.id not in (26, 27):
17             raise ValueError("Pin does not support ADC.")
18         self.pin_id = pin.id
19         pico_u2if.adc_init_pin(self.pin_id)
20
21     @property
22     def value(self):
23         """Read the ADC and return the value"""
24         return pico_u2if.adc_get_value(self.pin_id) << 4
25
26     # pylint: disable=no-self-use
27     @value.setter
28     def value(self, value):
29         # emulate what CircuitPython does
30         raise AttributeError("'AnalogIn' object has no attribute 'value'")
31
32     # pylint: enable=no-self-use
33
34     def deinit(self):
35         pass