]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py
cb21e06e29b3a560bd0ed91170bea5214a14833f
[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_TRUE
59             5: self.return_false,  # Dx_OUTPUT_FALSE
60             6: self.return_true,  # NEOPIXEL
61             7: self.return_random_int,  # Ax_INPUT_RAND_INT
62             8: self.return_fixed_int_pi,  # Ax_INPUT_FIXED_INT_PI
63             9: self.return_true,  # Ax_OUTPUT_WAVE_SINE
64             10: self.return_true,  # Ax_OUTPUT_WAVE_SAWTOOTH
65             11: self.return_toggle,  # Dx_INPUT_TOGGLE
66             # Add other mappings as needed
67         }
68
69     def init(self, mode=IN, pull=None):
70         """Initialize the Pin"""
71         if self.id is None:
72             raise RuntimeError("Can not init a None type pin.")
73         pull = Pin.PULL_NONE if pull is None else pull
74         self._pull = pull
75         self._mode = mode
76
77     def write(self, new_value):
78         """Saves the new_value to the pin for subsequent calls to .value"""
79         self.previous_value = self.current_value
80         self.current_value = new_value
81
82     def read(self):
83         """Returns the pin's expected value."""
84         self.previous_value = self.current_value
85         # perform a lookup on the pin_behavior dict to get the value
86         self.current_value = self.pin_behavior.get(self.id)()
87
88         # is pin a pull up and pin is LOW?
89         if self._pull == Pin.PULL_UP and self.current_value == False:
90             self.current_value = False
91         # is pin a pull down and pin is HIGH?
92         if self._pull == Pin.PULL_DOWN and self.current_value == True:
93             self.current_value = False
94
95         return self.current_value
96
97     def value(self, val=None):
98         """Set or return the Pin Value"""
99         # Digital In / Out
100         if self._mode in (Pin.IN, Pin.OUT):
101             # digital read
102             if val is None:
103                 return self.read()
104             # digital write
105             if val in (Pin.LOW, Pin.HIGH):
106                 return self.write(val)
107             # nope
108             raise ValueError("Invalid value for pin.")
109         # Analog In
110         if self._mode == Pin.ADC:
111             if val is None:
112                 return self.read()
113             # read only
114             raise AttributeError("'AnalogIn' object has no attribute 'value'")
115         # Analog Out
116         if self._mode == Pin.DAC:
117             if val is None:
118                 # write only
119                 raise AttributeError("unreadable attribute")
120             self.write(val)
121             return None
122         raise RuntimeError(
123             "No action for mode {} with value {}".format(self._mode, val)
124         )
125
126
127 # create pin instances for each pin
128 D0 = Pin(0)
129 D1 = Pin(1)
130 D2 = Pin(2)
131 D3 = Pin(3)
132 D4 = Pin(4)
133 D5 = Pin(5)
134 # Special "digital" pins
135 D6 = Pin(6)
136 # Analog pins
137 A0 = Pin(7)
138 A1 = Pin(8)
139 A2 = Pin(9)
140 A3 = Pin(10)
141
142 D7 = Pin(11)
143
144 # I2C pins
145 SDA = Pin()
146 SCL = Pin()
147
148 # SPI pins
149 SCLK = Pin()
150 SCK = Pin()
151 MOSI = Pin()
152 MISO = Pin()
153 CS = Pin()
154
155 # UART pins
156 UART_TX = Pin()
157 UART_RX = Pin()