1 # SPDX-FileCopyrightText: 2024 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """generic_agnostic_board pin interface"""
8 """A basic Pin class for use with generic_agnostic_board"""
18 def return_toggle(self):
19 """Returns the pin's expected value, toggling between True and False"""
20 toggle_state = not self.previous_value
23 def return_false(self):
24 """Returns the pin's expected value, False"""
27 def return_true(self):
28 """Returns the pin's expected value, True"""
31 def return_random_int(self):
32 """Returns a random integer"""
33 return random.randint(0, 65535)
35 def return_fixed_int_pi(self):
36 """Returns the first five digits of Pi, 31415"""
39 expected_pin_behavior = {
40 'Dx_INPUT_TRUE': return_true,
41 'Dx_INPUT_FALSE': return_false,
42 'Dx_INPUT_TRUE_THEN_FALSE': return_toggle,
43 'Dx_INPUT_TRUE_PULL_UP': return_true,
44 'Dx_INPUT_TRUE_PULL_DOWN': return_true,
45 'Dx_OUTPUT_TRUE': return_true,
46 'Dx_OUTPUT_FALSE': return_true,
47 'Ax_INPUT_RAND_INT': return_random_int
50 def __init__(self, pin_id=None):
53 self.previous_value = None
54 self.current_value = None
56 def init(self, mode=IN, pull=None):
57 """Initialize the Pin"""
59 raise RuntimeError("Can not init a None type pin.")
61 raise NotImplementedError("Internal pullups and pulldowns not supported")
64 def write(self, new_value):
65 """Saves the new_value to the pin for subsequent calls to .value"""
66 self.previous_value = self.current_value
67 self.current_value = new_value
70 """Returns the pin's expected value."""
71 self.previous_value = self.current_value
72 self.current_value = self.expected_pin_behavior.get(self.pin_id)
73 return self.current_value
75 def value(self, val=None):
76 """Set or return the Pin Value"""
78 if self._mode in (Pin.IN, Pin.OUT):
83 if val in (Pin.LOW, Pin.HIGH):
84 return self.write(val)
86 raise ValueError("Invalid value for pin.")
88 if self._mode == Pin.ADC:
92 raise AttributeError("'AnalogIn' object has no attribute 'value'")
94 if self._mode == Pin.DAC:
97 raise AttributeError("unreadable attribute")
101 "No action for mode {} with value {}".format(self._mode, val)
104 # create pin instances for each pin
111 # Special "digital" pins