-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpio.py
More file actions
37 lines (29 loc) · 816 Bytes
/
gpio.py
File metadata and controls
37 lines (29 loc) · 816 Bytes
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
from RPi import GPIO
import time
class IO:
FRONT = 18
LEFT = 25
RIGHT = 26
IN = GPIO.IN
OUT = GPIO.OUT
def __init__(self, port, io):
self.port = port
self.io = io
GPIO.setmode(GPIO.BCM)
if self.io == IO.IN:
GPIO.setup(self.port, self.io, pull_up_down=GPIO.PUD_UP)
elif self.io == IO.OUT:
GPIO.setup(self.port, self.io)
def __del__(self):
GPIO.cleanup(self.port)
def read(self):
return GPIO.input(self.port)
def write(self, state):
if self.io == IO.OUT:
GPIO.output(self.port, state)
return True
else:
print("Port {0} is not OUTPUT".format(self.port))
return False
def toggle_write(self):
self.write(not self.read())