|
| 1 | +"""GPIO abstractions for note-python.""" |
| 2 | + |
| 3 | +from .platform import platform |
| 4 | + |
| 5 | +if platform == 'circuitpython': |
| 6 | + import digitalio |
| 7 | +elif platform == 'micropython': |
| 8 | + import machine |
| 9 | +elif platform == 'raspbian': |
| 10 | + import RPi.GPIO as rpi_gpio |
| 11 | + |
| 12 | + |
| 13 | +class GPIO: |
| 14 | + """GPIO abstraction. |
| 15 | +
|
| 16 | + Supports GPIO on CircuitPython, MicroPython, and Raspbian (Raspberry Pi). |
| 17 | + """ |
| 18 | + |
| 19 | + IN = 0 |
| 20 | + OUT = 1 |
| 21 | + PULL_UP = 2 |
| 22 | + PULL_DOWN = 3 |
| 23 | + PULL_NONE = 4 |
| 24 | + |
| 25 | + def direction(self, direction): |
| 26 | + """Set the direction of the pin. |
| 27 | +
|
| 28 | + Does nothing in this base class. Should be implemented by subclasses. |
| 29 | + """ |
| 30 | + pass |
| 31 | + |
| 32 | + def pull(self, pull): |
| 33 | + """Set the pull of the pin. |
| 34 | +
|
| 35 | + Does nothing in this base class. Should be implemented by subclasses. |
| 36 | + """ |
| 37 | + pass |
| 38 | + |
| 39 | + def value(self, value=None): |
| 40 | + """Set the output or get the current level of the pin. |
| 41 | +
|
| 42 | + Does nothing in this base class. Should be implemented by subclasses. |
| 43 | + """ |
| 44 | + pass |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def setup(pin, direction, pull=None, value=None): |
| 48 | + """Set up a GPIO. |
| 49 | +
|
| 50 | + The platform is detected internally so that the user doesn't need to |
| 51 | + write platform-specific code themselves. |
| 52 | + """ |
| 53 | + if platform == 'circuitpython': |
| 54 | + return CircuitPythonGPIO(pin, direction, pull, value) |
| 55 | + elif platform == 'micropython': |
| 56 | + return MicroPythonGPIO(pin, direction, pull, value) |
| 57 | + elif platform == 'raspbian': |
| 58 | + return RpiGPIO(pin, direction, pull, value) |
| 59 | + |
| 60 | + def __init__(self, pin, direction, pull=None, value=None): |
| 61 | + """Initialize the GPIO. |
| 62 | +
|
| 63 | + Pin and direction are required arguments. Pull and value will be set |
| 64 | + only if given. |
| 65 | + """ |
| 66 | + self.direction(direction) |
| 67 | + |
| 68 | + if pull is not None: |
| 69 | + self.pull(pull) |
| 70 | + |
| 71 | + if value is not None: |
| 72 | + self.value(value) |
| 73 | + |
| 74 | + |
| 75 | +class CircuitPythonGPIO(GPIO): |
| 76 | + """GPIO for CircuitPython.""" |
| 77 | + |
| 78 | + def direction(self, direction): |
| 79 | + """Set the direction of the pin. |
| 80 | +
|
| 81 | + Allowed direction values are GPIO.IN and GPIO.OUT. Other values cause a |
| 82 | + ValueError. |
| 83 | + """ |
| 84 | + if direction == GPIO.IN: |
| 85 | + self.pin.direction = digitalio.Direction.INPUT |
| 86 | + elif direction == GPIO.OUT: |
| 87 | + self.pin.direction = digitalio.Direction.OUTPUT |
| 88 | + else: |
| 89 | + raise ValueError(f"Invalid pin direction: {direction}.") |
| 90 | + |
| 91 | + def pull(self, pull): |
| 92 | + """Set the pull of the pin. |
| 93 | +
|
| 94 | + Allowed pull values are GPIO.PULL_UP, GPIO.PULL_DOWN, and |
| 95 | + GPIO.PULL_NONE. Other values cause a ValueError. |
| 96 | + """ |
| 97 | + if pull == GPIO.PULL_UP: |
| 98 | + self.pin.pull = digitalio.Pull.UP |
| 99 | + elif pull == GPIO.PULL_DOWN: |
| 100 | + self.pin.pull = digitalio.Pull.DOWN |
| 101 | + elif pull == GPIO.PULL_NONE: |
| 102 | + self.pin.pull = None |
| 103 | + else: |
| 104 | + raise ValueError(f"Invalid pull value: {pull}.") |
| 105 | + |
| 106 | + def value(self, value=None): |
| 107 | + """Set the output or get the current level of the pin. |
| 108 | +
|
| 109 | + If value is not given, returns the level of the pin (i.e. the pin is an |
| 110 | + input). If value is given, sets the level of the pin (i.e. the pin is an |
| 111 | + output). |
| 112 | + """ |
| 113 | + if value is None: |
| 114 | + return self.pin.value |
| 115 | + else: |
| 116 | + self.pin.value = value |
| 117 | + |
| 118 | + def __init__(self, pin, direction, pull=None, value=None): |
| 119 | + """Initialize the GPIO. |
| 120 | +
|
| 121 | + Pin and direction are required arguments. Pull and value will be set |
| 122 | + only if given. |
| 123 | + """ |
| 124 | + self.pin = digitalio.DigitalInOut(pin) |
| 125 | + super().__init__(pin, direction, pull, value) |
| 126 | + |
| 127 | + |
| 128 | +class MicroPythonGPIO(GPIO): |
| 129 | + """GPIO for MicroPython.""" |
| 130 | + |
| 131 | + def direction(self, direction): |
| 132 | + """Set the direction of the pin. |
| 133 | +
|
| 134 | + Allowed direction values are GPIO.IN and GPIO.OUT. Other values cause a |
| 135 | + ValueError. |
| 136 | + """ |
| 137 | + if direction == GPIO.IN: |
| 138 | + self.pin.init(mode=machine.Pin.IN) |
| 139 | + elif direction == GPIO.OUT: |
| 140 | + self.pin.init(mode=machine.Pin.OUT) |
| 141 | + else: |
| 142 | + raise ValueError(f"Invalid pin direction: {direction}.") |
| 143 | + |
| 144 | + def pull(self, pull): |
| 145 | + """Set the pull of the pin. |
| 146 | +
|
| 147 | + Allowed pull values are GPIO.PULL_UP, GPIO.PULL_DOWN, and |
| 148 | + GPIO.PULL_NONE. Other values cause a ValueError. |
| 149 | + """ |
| 150 | + if pull == GPIO.PULL_UP: |
| 151 | + self.pin.init(pull=machine.Pin.PULL_UP) |
| 152 | + elif pull == GPIO.PULL_DOWN: |
| 153 | + self.pin.init(pull=machine.Pin.PULL_DOWN) |
| 154 | + elif pull == GPIO.PULL_NONE: |
| 155 | + self.pin.init(pull=None) |
| 156 | + else: |
| 157 | + raise ValueError(f"Invalid pull value: {pull}.") |
| 158 | + |
| 159 | + def value(self, value=None): |
| 160 | + """Set the output or get the current level of the pin. |
| 161 | +
|
| 162 | + If value is not given, returns the level of the pin (i.e. the pin is an |
| 163 | + input). If value is given, sets the level of the pin (i.e. the pin is an |
| 164 | + output). |
| 165 | + """ |
| 166 | + if value is None: |
| 167 | + return self.pin.value() |
| 168 | + else: |
| 169 | + self.pin.init(value=value) |
| 170 | + |
| 171 | + def __init__(self, pin, direction, pull=None, value=None): |
| 172 | + """Initialize the GPIO. |
| 173 | +
|
| 174 | + Pin and direction are required arguments. Pull and value will be set |
| 175 | + only if given. |
| 176 | + """ |
| 177 | + self.pin = machine.Pin(pin) |
| 178 | + super().__init__(pin, direction, pull, value) |
| 179 | + |
| 180 | + |
| 181 | +class RpiGPIO(GPIO): |
| 182 | + """GPIO for Raspbian (Raspberry Pi).""" |
| 183 | + |
| 184 | + def direction(self, direction): |
| 185 | + """Set the direction of the pin. |
| 186 | +
|
| 187 | + Allowed direction values are GPIO.IN and GPIO.OUT. Other values cause a |
| 188 | + ValueError. |
| 189 | + """ |
| 190 | + if direction == GPIO.IN: |
| 191 | + self.rpi_direction = rpi_gpio.IN |
| 192 | + rpi_gpio.setup(self.pin, direction=rpi_gpio.IN) |
| 193 | + elif direction == GPIO.OUT: |
| 194 | + self.rpi_direction = rpi_gpio.OUT |
| 195 | + rpi_gpio.setup(self.pin, direction=rpi_gpio.OUT) |
| 196 | + else: |
| 197 | + raise ValueError(f"Invalid pin direction: {direction}.") |
| 198 | + |
| 199 | + def pull(self, pull): |
| 200 | + """Set the pull of the pin. |
| 201 | +
|
| 202 | + Allowed pull values are GPIO.PULL_UP, GPIO.PULL_DOWN, and |
| 203 | + GPIO.PULL_NONE. Other values cause a ValueError. |
| 204 | + """ |
| 205 | + if pull == GPIO.PULL_UP: |
| 206 | + rpi_gpio.setup(self.pin, |
| 207 | + direction=self.rpi_direction, |
| 208 | + pull_up_down=rpi_gpio.PUD_UP) |
| 209 | + elif pull == GPIO.PULL_DOWN: |
| 210 | + rpi_gpio.setup(self.pin, |
| 211 | + direction=self.rpi_direction, |
| 212 | + pull_up_down=GPIO.PUD_DOWN) |
| 213 | + elif pull == GPIO.PULL_NONE: |
| 214 | + rpi_gpio.setup(self.pin, |
| 215 | + direction=self.rpi_direction, |
| 216 | + pull_up_down=rpi_gpio.PUD_OFF) |
| 217 | + else: |
| 218 | + raise ValueError(f"Invalid pull value: {pull}.") |
| 219 | + |
| 220 | + def value(self, value=None): |
| 221 | + """Set the output or get the current level of the pin. |
| 222 | +
|
| 223 | + If value is not given, returns the level of the pin (i.e. the pin is an |
| 224 | + input). If value is given, sets the level of the pin (i.e. the pin is an |
| 225 | + output). |
| 226 | + """ |
| 227 | + if value is None: |
| 228 | + return rpi_gpio.input(self.pin) |
| 229 | + else: |
| 230 | + rpi_gpio.output(self.pin, value) |
| 231 | + |
| 232 | + def __init__(self, pin, direction, pull=None, value=None): |
| 233 | + """Initialize the GPIO. |
| 234 | +
|
| 235 | + Pin and direction are required arguments. Pull and value will be set |
| 236 | + only if given. |
| 237 | + """ |
| 238 | + self.pin = pin |
| 239 | + super().__init__(pin, direction, pull, value) |
0 commit comments