1 # SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """A Pin class for use with Rpi.GPIO."""
8 GPIO.setmode(GPIO.BCM) # Use BCM pins D4 = GPIO #4
9 GPIO.setwarnings(False) # shh!
13 """Pins dont exist in CPython so...lets make our own!"""
27 def __init__(self, bcm_number):
33 def __eq__(self, other):
34 return self.id == other
36 def init(self, mode=IN, pull=None):
37 """Initialize the Pin"""
41 GPIO.setup(self.id, GPIO.IN)
42 elif mode == self.OUT:
44 GPIO.setup(self.id, GPIO.OUT)
46 raise RuntimeError("Invalid mode for pin: %s" % self.id)
48 if self._mode != self.IN:
49 raise RuntimeError("Cannot set pull resistor on output")
50 if pull == self.PULL_UP:
51 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
52 elif pull == self.PULL_DOWN:
53 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
55 raise RuntimeError("Invalid pull for pin: %s" % self.id)
57 def value(self, val=None):
58 """Set or return the Pin Value"""
62 GPIO.output(self.id, val)
63 elif val == self.HIGH:
65 GPIO.output(self.id, val)
67 raise RuntimeError("Invalid value for pin")
69 return GPIO.input(self.id)