-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.py
81 lines (68 loc) · 1.71 KB
/
gpio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# coding: utf-8
import RPi.GPIO as GPIO
import subprocess
import math
import time
import os
import sys
def eprint(err):
sys.stderr.write(err)
def GPIO_init():
GPIO.setmode(GPIO.BCM)
def GPIO_cleanup():
print "Cleaning up GPIO"
GPIO.cleanup()
class Button():
def __init__(self, pin, debounce_ms=100):
self.pin = pin
self.debounce_ms = debounce_ms
if pin < 5:
GPIO.setup(pin, GPIO.IN)
else:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
self._start_listening()
def _start_listening(self):
GPIO.add_event_detect(self.pin, GPIO.FALLING, callback=self._on_pushed_once, bouncetime=self.debounce_ms)
def _on_pushed_once(self, channel):
GPIO.remove_event_detect (self.pin)
if hasattr(self, 'on_pushed'):
self.on_pushed()
else:
eprint('Button.on_pushed() not defined!')
self._start_listening()
class Led():
def __init__(self, pin):
self.pin = pin
GPIO.setup(pin, GPIO.OUT)
def on(self):
GPIO.output(self.pin, GPIO.HIGH)
def off(self):
GPIO.output(self.pin, GPIO.LOW)
def set(self, on):
GPIO.output(self.pin, on)
def toggle(self):
GPIO.output(self.pin, not GPIO.input(self.pin))
#BUTTON = 3 # connects to ground
#LED = 4 # connects to ground
#running = True
#
#def main():
# button = Button(BUTTON, 100)
# led = Led(LED)
# led.off()
# def t():
# print("Button pushed")
# led.toggle()
#
# button.on_pushed = t
#
# while running:
# time.sleep(1)
#
#
#try:
# GPIO_init()
# print("starting")
# main()
#except KeyboardInterrupt:
# GPIO_cleanup()