Skip to content

Instantly share code, notes, and snippets.

@reidblomquist
Created March 27, 2016 08:45
Show Gist options
  • Select an option

  • Save reidblomquist/8a5cca2943852816b3f8 to your computer and use it in GitHub Desktop.

Select an option

Save reidblomquist/8a5cca2943852816b3f8 to your computer and use it in GitHub Desktop.
Simple RPi.GPIO Toggle button (pull up) Class
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
class Toggle:
enabled = False
def __init__(self, pin):
self.pin = pin
GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def readState(self):
input_state = GPIO.input(self.pin)
toggle_state = Toggle.enabled
if input_state == False and toggle_state == False:
Toggle.enabled = True
elif input_state == False and toggle_state == True:
Toggle.enabled = False
def printState(self):
print "Toggle is currently: %d" % Toggle.enabled
toggle = Toggle(17)
while True:
toggle.readState()
toggle.printState()
time.sleep(0.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment