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