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
59 6: self.return_true, # NEOPIXEL
60 7: self.return_random_int, # Ax_INPUT_RAND_INT
61 8: self.return_fixed_int_pi, # Ax_INPUT_FIXED_INT_PI
62 9: self.return_true, # Ax_OUTPUT_WAVE_SINE
63 10: self.return_true, # Ax_OUTPUT_WAVE_SAWTOOTH
64 11: self.return_toggle, # Dx_INPUT_TOGGLE
65 # Add other mappings as needed
68 def init(self, mode=IN, pull=None):
69 """Initialize the Pin"""
71 raise RuntimeError("Can not init a None type pin.")
72 pull = Pin.PULL_NONE if pull is None else pull
76 def write(self, new_value):
77 """Saves the new_value to the pin for subsequent calls to .value"""
78 self.previous_value = self.current_value
79 self.current_value = new_value
82 """Returns the pin's expected value."""
83 self.previous_value = self.current_value
84 # perform a lookup on the pin_behavior dict to get the value
85 self.current_value = self.pin_behavior.get(self.id)()
87 # is pin a pull up and pin is LOW?
88 if self._pull == Pin.PULL_UP and self.current_value == False:
89 self.current_value = False
90 # is pin a pull down and pin is HIGH?
91 if self._pull == Pin.PULL_DOWN and self.current_value == True:
92 self.current_value = False
94 return self.current_value
96 def value(self, val=None):
97 """Set or return the Pin Value"""
99 if self._mode in (Pin.IN, Pin.OUT):
104 if val in (Pin.LOW, Pin.HIGH):
105 return self.write(val)
107 raise ValueError("Invalid value for pin.")
109 if self._mode == Pin.ADC:
113 raise AttributeError("'AnalogIn' object has no attribute 'value'")
115 if self._mode == Pin.DAC:
118 raise AttributeError("unreadable attribute")
122 "No action for mode {} with value {}".format(self._mode, val)
126 # create pin instances for each pin
132 # Special "digital" pins