1 # SPDX-FileCopyrightText: 2024 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """generic_agnostic_board pin interface"""
9 """A basic Pin class for use with generic_agnostic_board"""
20 def return_toggle(self):
21 """Returns the pin's expected value, toggling between True and False"""
22 toggle_state = not self.previous_value
25 def return_false(self):
26 """Returns the pin's expected value, False"""
29 def return_true(self):
30 """Returns the pin's expected value, True"""
33 def return_random_int(self):
34 """Returns a random integer"""
35 return random.randint(0, 65535)
37 def return_fixed_int_pi(self):
38 """Returns the first five digits of Pi, 31415"""
41 def __init__(self, pin_id=None):
44 self.previous_value = None
45 self.current_value = None
47 # mapping of pin definition names to expected 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
63 def init(self, mode=IN, pull=None):
64 """Initialize the Pin"""
66 raise RuntimeError("Can not init a None type pin.")
68 raise NotImplementedError("Internal pullups and pulldowns not supported")
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
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
83 def value(self, val=None):
84 """Set or return the Pin Value"""
86 if self._mode in (Pin.IN, Pin.OUT):
91 if val in (Pin.LOW, Pin.HIGH):
92 return self.write(val)
94 raise ValueError("Invalid value for pin.")
96 if self._mode == Pin.ADC:
100 raise AttributeError("'AnalogIn' object has no attribute 'value'")
102 if self._mode == Pin.DAC:
105 raise AttributeError("unreadable attribute")
109 "No action for mode {} with value {}".format(self._mode, val)
113 # create pin instances for each pin
120 # Special "digital" pins