]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py
add all files, untested
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_agnostic_board / pin.py
1 # SPDX-FileCopyrightText: 2024 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """generic_agnostic_board pin interface"""
5 import random
6
7 class Pin:
8     """A basic Pin class for use with generic_agnostic_board"""
9     # pin modes
10     OUT = 0
11     IN = 1
12     ADC = 2
13     DAC = 3
14     # pin values
15     LOW = 0
16     HIGH = 1
17
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
27     }
28
29     def __init__(self, pin_id=None):
30       self.id = pin_id
31       self._mode = None
32       self.previous_value = None
33       self.current_value = None
34
35     def init(self, mode=IN, pull=None):
36         """Initialize the Pin"""
37         if self.id is None:
38             raise RuntimeError("Can not init a None type pin.")
39         if pull is not None:
40             raise NotImplementedError("Internal pullups and pulldowns not supported")
41         self._mode = mode
42
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
47
48     def read(self):
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
53
54     def return_toggle(self):
55       """Returns the pin's expected value, toggling between True and False"""
56       toggle_state = not self.previous_value
57       return toggle_state
58
59     def return_false(self):
60       """Returns the pin's expected value, False"""
61       return False
62
63     def return_true(self):
64       """Returns the pin's expected value, True"""
65       return True
66
67     def return_random_int(self):
68       """Returns a random integer"""
69       return random.randint(0, 65535)
70
71     def return_fixed_int_pi(self):
72       """Returns the first five digits of Pi, 31415"""
73       return 31415
74
75     def value(self, val=None):
76         """Set or return the Pin Value"""
77         # Digital In / Out
78         if self._mode in (Pin.IN, Pin.OUT):
79             # digital read
80             if val is None:
81                 return self.read()
82             # digital write
83             if val in (Pin.LOW, Pin.HIGH):
84                 return self.write(val)
85             # nope
86             raise ValueError("Invalid value for pin.")
87         # Analog In
88         if self._mode == Pin.ADC:
89             if val is None:
90                 return self.read()
91             # read only
92             raise AttributeError("'AnalogIn' object has no attribute 'value'")
93         # Analog Out
94         if self._mode == Pin.DAC:
95             if val is None:
96                 # write only
97                 raise AttributeError("unreadable attribute")
98             self.write(val)
99             return None
100         raise RuntimeError(
101             "No action for mode {} with value {}".format(self._mode, val)
102         )