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"""
24 def return_toggle(self):
25 """Returns the pin's expected value, toggling between True and False"""
26 toggle_state = not self.previous_value
29 def return_false(self):
30 """Returns the pin's expected value, False"""
33 def return_true(self):
34 """Returns the pin's expected value, True"""
37 def return_random_int(self):
38 """Returns a random integer"""
39 return random.randint(0, 65535)
41 def return_fixed_int_pi(self):
42 """Returns the first five digits of Pi, 31415"""
45 def __init__(self, pin_id=None):
49 self.previous_value = False
50 self.current_value = None
52 # mapping of pin definition names to expected 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
69 def init(self, mode=IN, pull=None):
70 """Initialize the Pin"""
72 raise RuntimeError("Can not init a None type pin.")
73 pull = Pin.PULL_NONE if pull is None else pull
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
83 print("\nread mode: ", self._mode)
84 """Returns the pin's expected value."""
85 self.previous_value = self.current_value
86 # perform a lookup on the pin_behavior dict to get the value
87 self.current_value = self.pin_behavior.get(self.id)()
89 # is pin a pull up and pin is LOW?
90 if self._pull == Pin.PULL_UP and self.current_value == False:
91 self.current_value = True
92 # is pin a pull down and pin is HIGH?
93 if self._pull == Pin.PULL_DOWN and self.current_value == True:
94 print("switching PDR to False")
95 self.current_value = False
97 return self.current_value
99 def value(self, val=None):
100 """Set or return the Pin Value"""
102 if self._mode in (Pin.IN, Pin.OUT):
107 if val in (Pin.LOW, Pin.HIGH):
108 return self.write(val)
110 raise ValueError("Invalid value for pin.")
112 if self._mode == Pin.ADC:
116 raise AttributeError("'AnalogIn' object has no attribute 'value'")
118 if self._mode == Pin.DAC:
121 raise AttributeError("unreadable attribute")
125 "No action for mode {} with value {}".format(self._mode, val)
129 # create pin instances for each pin
136 # Special "digital" pins