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 expected_pin_behavior = {
19 'Dx_INPUT_TRUE': return_true,
20 'Dx_INPUT_FALSE': return_false,
21 'Dx_INPUT_TRUE_THEN_FALSE': return_toggle,
22 'Dx_INPUT_TRUE_PULL_UP': return_true,
23 'Dx_INPUT_TRUE_PULL_DOWN': return_true,
24 'Dx_OUTPUT_TRUE': return_true,
25 'Dx_OUTPUT_FALSE': return_true,
26 'Ax_INPUT_RAND_INT': return_random_int
29 def __init__(self, pin_id=None):
32 self.previous_value = None
33 self.current_value = None
35 def init(self, mode=IN, pull=None):
36 """Initialize the Pin"""
38 raise RuntimeError("Can not init a None type pin.")
40 raise NotImplementedError("Internal pullups and pulldowns not supported")
43 def write(self, new_value):
44 """Saves the new_value to the pin for subsequent calls to .value"""
45 self.previous_value = self.current_value
46 self.current_value = new_value
49 """Returns the pin's expected value."""
50 self.previous_value = self.current_value
51 self.current_value = self.expected_pin_behavior.get(self.pin_id)
52 return self.current_value
54 def return_toggle(self):
55 """Returns the pin's expected value, toggling between True and False"""
56 toggle_state = not self.previous_value
59 def return_false(self):
60 """Returns the pin's expected value, False"""
63 def return_true(self):
64 """Returns the pin's expected value, True"""
67 def return_random_int(self):
68 """Returns a random integer"""
69 return random.randint(0, 65535)
71 def return_fixed_int_pi(self):
72 """Returns the first five digits of Pi, 31415"""
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)