1 # SPDX-FileCopyrightText: 2024 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """generic_agnostic_board pin interface"""
8 # (data points = 20, amplitude=100, frequency=1)
32 # Values for a sawtooth wave
33 # (data points = 20, amplitude=100)
59 """A basic Pin class for use with generic_agnostic_board"""
74 def return_toggle(self):
75 """Returns the pin's expected value, toggling between True and False"""
76 toggle_state = not self.previous_value
79 def return_false(self):
80 """Returns the pin's expected value, False"""
83 def return_true(self):
84 """Returns the pin's expected value, True"""
87 def return_random_int(self):
88 """Returns a random integer"""
89 return random.randint(0, 65535)
91 def return_fixed_int_pi(self):
92 """Returns the first five digits of Pi, 31415"""
95 def return_sine_wave(self):
96 """Returns the next value in the sine wave"""
97 if self._wave_idx is None:
100 self._wave_idx = (self._wave_idx + 1) % len(sine_wave)
101 return sine_wave[self._wave_idx]
103 def return_sawtooth_wave(self):
104 """Returns the next value in the sawtooth wave"""
105 if self._wave_idx is None:
108 self._wave_idx = (self._wave_idx + 1) % len(sawtooth_wave)
109 return sawtooth_wave[self._wave_idx]
111 def __init__(self, pin_id=None):
115 self.previous_value = False
116 self.current_value = None
117 self._wave_idx = None
119 # mapping of pin definition names to expected behavior
120 self.pin_behavior = {
121 0: self.return_true, # Dx_INPUT_TRUE
122 1: self.return_false, # Dx_INPUT_FALSE
123 2: self.return_true, # Dx_INPUT_TRUE_PULL_UP
124 3: self.return_true, # Dx_INPUT_TRUE_PULL_DOWN
125 4: self.return_true, # Dx_OUTPUT
126 7: self.return_random_int, # Ax_INPUT_RAND_INT
127 8: self.return_fixed_int_pi, # Ax_INPUT_FIXED_INT_PI
128 9: self.return_sine_wave, # Ax_OUTPUT_WAVE_SINE
129 10: self.return_sawtooth_wave, # Ax_OUTPUT_WAVE_SAW
130 11: self.return_toggle, # Dx_INPUT_TOGGLE
133 def init(self, mode=IN, pull=None):
134 """Initialize the Pin"""
136 raise RuntimeError("Can not init a None type pin.")
137 pull = Pin.PULL_NONE if pull is None else pull
141 def write(self, new_value):
142 """Saves the new_value to the pin for subsequent calls to .value"""
143 self.previous_value = self.current_value
144 self.current_value = new_value
147 """Returns the pin's expected value."""
148 self.previous_value = self.current_value
149 # perform a lookup on the pin_behavior dict to get the value
150 self.current_value = self.pin_behavior.get(self.id)()
152 # is pin a pull up and pin is LOW?
153 if self._pull == Pin.PULL_UP and self.current_value == False:
154 self.current_value = False
155 # is pin a pull down and pin is HIGH?
156 if self._pull == Pin.PULL_DOWN and self.current_value == True:
157 self.current_value = False
158 return self.current_value
160 def value(self, val=None):
161 """Set or return the Pin Value"""
163 if self._mode in (Pin.IN, Pin.OUT):
168 if val in (Pin.LOW, Pin.HIGH):
169 return self.write(val)
171 raise ValueError("Invalid value for pin.")
173 if self._mode == Pin.ADC:
177 raise AttributeError("'AnalogIn' object has no attribute 'value'")
179 if self._mode == Pin.DAC:
182 raise AttributeError("unreadable attribute")
186 "No action for mode {} with value {}".format(self._mode, val)
190 # create pin instances for each pin
196 # Special "digital" pins