Skip to content

Instantly share code, notes, and snippets.

@matsjoyce
Last active September 13, 2017 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsjoyce/34e4900d00ff9792e9264974676153ef to your computer and use it in GitHub Desktop.
Save matsjoyce/34e4900d00ff9792e9264974676153ef to your computer and use it in GitHub Desktop.
# edukit1.py - A wrapper around RPi.GPIO setup to make CamJam EduKit 1 look nicer
# Copyright (C) 2017 Matthew Joyce matsjoyce@gmail.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from RPi import GPIO
import atexit
import time
GPIO.setmode(GPIO.BCM)
atexit.register(GPIO.cleanup)
class OutPin:
def __init__(self, pin, name="", value=False, invert=False):
self.pin = pin
self.name = name
self.invert = invert
GPIO.setup(self.pin, GPIO.OUT)
self(value)
def __call__(self, value):
GPIO.output(self.pin, not value if self.invert else value)
def __bool__(self):
return bool(GPIO.input(self.pin)) == self.invert
def __repr__(self):
return "{}({})".format(self.name, bool(self))
def toggle(self):
self(not self)
def toggle_for(self, t):
self.toggle()
time.sleep(t)
self.toggle()
class InPin:
def __init__(self, pin, name, invert=False):
self.pin = pin
self.name = name
self.invert = invert
GPIO.setup(self.pin, GPIO.IN)
def __bool__(self):
return bool(GPIO.input(self.pin)) == self.invert
def __call__(self):
return bool(self)
def __repr__(self):
return "{}({})".format(self.name, bool(self))
red = OutPin(pin=18, name="red", value=False)
yellow = OutPin(pin=23, name="yellow", value=False)
green = OutPin(pin=24, name="green", value=False)
buzzer = OutPin(pin=22, name="buzzer", value=False)
button = InPin(pin=25, name="button", invert=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment