1 # SPDX-FileCopyrightText: 2024 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """generic_agnostic_board pin interface"""
7 # Values for sine wave output
8 # (data points = 20, amplitude=100, frequency=1)
34 """A basic Pin class for use with generic_agnostic_board"""
49 def return_toggle(self):
50 """Returns the pin's expected value, toggling between True and False"""
51 toggle_state = not self.previous_value
54 def return_false(self):
55 """Returns the pin's expected value, False"""
58 def return_true(self):
59 """Returns the pin's expected value, True"""
62 def return_random_int(self):
63 """Returns a random integer"""
64 return random.randint(0, 65535)
66 def return_fixed_int_pi(self):
67 """Returns the first five digits of Pi, 31415"""
70 def return_sine_wave(self):
71 """Returns the next value in the sine wave"""
72 if self._wave_idx is None:
76 if self._wave_idx >= len(sine_wave):
78 return sine_wave[self._wave_idx]
80 def __init__(self, pin_id=None):
84 self.previous_value = False
85 self.current_value = None
88 # mapping of pin definition names to expected behavior
90 0: self.return_true, # Dx_INPUT_TRUE
91 1: self.return_false, # Dx_INPUT_FALSE
92 2: self.return_true, # Dx_INPUT_TRUE_PULL_UP
93 3: self.return_true, # Dx_INPUT_TRUE_PULL_DOWN
94 4: self.return_true, # Dx_OUTPUT
95 6: self.return_true, # NEOPIXEL
96 7: self.return_random_int, # Ax_INPUT_RAND_INT
97 8: self.return_fixed_int_pi, # Ax_INPUT_FIXED_INT_PI
98 9: self.return_sine_wave, # Ax_OUTPUT_WAVE_SINE
99 10: self.return_true, # Ax_OUTPUT_WAVE_SAWTOOTH
100 11: self.return_toggle, # Dx_INPUT_TOGGLE
101 # Add other mappings as needed
104 def init(self, mode=IN, pull=None):
105 """Initialize the Pin"""
107 raise RuntimeError("Can not init a None type pin.")
108 pull = Pin.PULL_NONE if pull is None else pull
112 def write(self, new_value):
113 """Saves the new_value to the pin for subsequent calls to .value"""
114 self.previous_value = self.current_value
115 self.current_value = new_value
118 """Returns the pin's expected value."""
119 self.previous_value = self.current_value
120 # perform a lookup on the pin_behavior dict to get the value
121 self.current_value = self.pin_behavior.get(self.id)()
123 # is pin a pull up and pin is LOW?
124 if self._pull == Pin.PULL_UP and self.current_value == False:
125 self.current_value = False
126 # is pin a pull down and pin is HIGH?
127 if self._pull == Pin.PULL_DOWN and self.current_value == True:
128 self.current_value = False
129 return self.current_value
131 def value(self, val=None):
132 """Set or return the Pin Value"""
134 if self._mode in (Pin.IN, Pin.OUT):
139 if val in (Pin.LOW, Pin.HIGH):
140 return self.write(val)
142 raise ValueError("Invalid value for pin.")
144 if self._mode == Pin.ADC:
148 raise AttributeError("'AnalogIn' object has no attribute 'value'")
150 if self._mode == Pin.DAC:
153 raise AttributeError("unreadable attribute")
157 "No action for mode {} with value {}".format(self._mode, val)
161 # create pin instances for each pin
167 # Special "digital" pins