]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py
c9c15a3a6d8eba805a87fb67ea34bf9e1bcf2e09
[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     def return_toggle(self):
19       """Returns the pin's expected value, toggling between True and False"""
20       toggle_state = not self.previous_value
21       return toggle_state
22
23     def return_false(self):
24       """Returns the pin's expected value, False"""
25       return False
26
27     def return_true(self):
28       """Returns the pin's expected value, True"""
29       return True
30
31     def return_random_int(self):
32       """Returns a random integer"""
33       return random.randint(0, 65535)
34
35     def return_fixed_int_pi(self):
36       """Returns the first five digits of Pi, 31415"""
37       return 31415
38
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
48     }
49
50     def __init__(self, pin_id=None):
51       self.id = pin_id
52       self._mode = None
53       self.previous_value = None
54       self.current_value = None
55
56     def init(self, mode=IN, pull=None):
57         """Initialize the Pin"""
58         if self.id is None:
59             raise RuntimeError("Can not init a None type pin.")
60         if pull is not None:
61             raise NotImplementedError("Internal pullups and pulldowns not supported")
62         self._mode = mode
63
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
68
69     def read(self):
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
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         )
103
104 # create pin instances for each pin
105 D0 = Pin(0)
106 D1 = Pin(1)
107 D2 = Pin(2)
108 D3 = Pin(3)
109 D4 = Pin(4)
110 D5 = Pin(5)
111 # Special "digital" pins
112 D6 = Pin(6)
113 # Analog pins
114 A0 = Pin(7)
115 A1 = Pin(8)
116 A2 = Pin(9)
117 A3 = Pin(10)
118
119 # I2C pins
120 SDA = Pin()
121 SCL = Pin()
122
123 # SPI pins
124 SCLK = Pin()
125 SCK = Pin()
126 MOSI = Pin()
127 MISO = Pin()
128 CS = Pin()
129
130 # UART pins
131 UART_TX = Pin()
132 UART_RX = Pin()