]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py
a648502bd1ca27d81b5ccccff2ac8fbab178f23f
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_agnostic_board / pin.py
1 # SPDX-FileCopyrightText: 2024 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """generic_agnostic_board pin interface"""
5 import random
6
7
8 class Pin:
9     """A basic Pin class for use with generic_agnostic_board"""
10
11     # pin modes
12     OUT = 0
13     IN = 1
14     ADC = 2
15     DAC = 3
16     # pin values
17     LOW = 0
18     HIGH = 1
19     # pin pulls
20     PULL_NONE = 0
21     PULL_UP = 1
22     PULL_DOWN = 2
23
24     def return_toggle(self):
25         """Returns the pin's expected value, toggling between True and False"""
26         toggle_state = not self.previous_value
27         return toggle_state
28
29     def return_false(self):
30         """Returns the pin's expected value, False"""
31         return False
32
33     def return_true(self):
34         """Returns the pin's expected value, True"""
35         return True
36
37     def return_random_int(self):
38         """Returns a random integer"""
39         return random.randint(0, 65535)
40
41     def return_fixed_int_pi(self):
42         """Returns the first five digits of Pi, 31415"""
43         return 31415
44
45     def __init__(self, pin_id=None):
46         self.id = pin_id
47         self._mode = None
48         self._pull = None
49         self.previous_value = False
50         self.current_value = None
51
52         # mapping of pin definition names to expected behavior
53         self.pin_behavior = {
54             0: self.return_true,  # Dx_INPUT_TRUE
55             1: self.return_false,  # Dx_INPUT_FALSE
56             2: self.return_true,  # Dx_INPUT_TRUE_PULL_UP
57             3: self.return_true,  # Dx_INPUT_TRUE_PULL_DOWN
58             4: self.return_true,  # Dx_OUTPUT
59             6: self.return_true,  # NEOPIXEL
60             7: self.return_random_int,  # Ax_INPUT_RAND_INT
61             8: self.return_fixed_int_pi,  # Ax_INPUT_FIXED_INT_PI
62             9: self.return_true,  # Ax_OUTPUT_WAVE_SINE
63             10: self.return_true,  # Ax_OUTPUT_WAVE_SAWTOOTH
64             11: self.return_toggle,  # Dx_INPUT_TOGGLE
65             # Add other mappings as needed
66         }
67
68     def init(self, mode=IN, pull=None):
69         """Initialize the Pin"""
70         if self.id is None:
71             raise RuntimeError("Can not init a None type pin.")
72         pull = Pin.PULL_NONE if pull is None else pull
73         self._pull = pull
74         self._mode = mode
75
76     def write(self, new_value):
77         """Saves the new_value to the pin for subsequent calls to .value"""
78         self.previous_value = self.current_value
79         self.current_value = new_value
80
81     def read(self):
82         """Returns the pin's expected value."""
83         self.previous_value = self.current_value
84         # perform a lookup on the pin_behavior dict to get the value
85         self.current_value = self.pin_behavior.get(self.id)()
86
87         # is pin a pull up and pin is LOW?
88         if self._pull == Pin.PULL_UP and self.current_value == False:
89             self.current_value = False
90         # is pin a pull down and pin is HIGH?
91         if self._pull == Pin.PULL_DOWN and self.current_value == True:
92             self.current_value = False
93         return self.current_value
94
95     def value(self, val=None):
96         """Set or return the Pin Value"""
97         # Digital In / Out
98         if self._mode in (Pin.IN, Pin.OUT):
99             # digital read
100             if val is None:
101                 return self.read()
102             # digital write
103             if val in (Pin.LOW, Pin.HIGH):
104                 return self.write(val)
105             # nope
106             raise ValueError("Invalid value for pin.")
107         # Analog In
108         if self._mode == Pin.ADC:
109             if val is None:
110                 return self.read()
111             # read only
112             raise AttributeError("'AnalogIn' object has no attribute 'value'")
113         # Analog Out
114         if self._mode == Pin.DAC:
115             if val is None:
116                 # write only
117                 raise AttributeError("unreadable attribute")
118             self.write(val)
119             return None
120         raise RuntimeError(
121             "No action for mode {} with value {}".format(self._mode, val)
122         )
123
124
125 # create pin instances for each pin
126 D0 = Pin(0)
127 D1 = Pin(1)
128 D2 = Pin(2)
129 D3 = Pin(3)
130 D4 = Pin(4)
131 # Special "digital" pins
132 D6 = Pin(6)
133 # Analog pins
134 A0 = Pin(7)
135 A1 = Pin(8)
136 A2 = Pin(9)
137 A3 = Pin(10)
138
139 D7 = Pin(11)
140
141 # I2C pins
142 SDA = Pin()
143 SCL = Pin()
144
145 # SPI pins
146 SCLK = Pin()
147 SCK = Pin()
148 MOSI = Pin()
149 MISO = Pin()
150 CS = Pin()
151
152 # UART pins
153 UART_TX = Pin()
154 UART_RX = Pin()