-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsunfounder-io-controller
executable file
·68 lines (60 loc) · 1.93 KB
/
sunfounder-io-controller
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
#!/usr/bin/env python
import RPi.GPIO as GPIO
import sys
import os
import keyboard
from time import sleep
#script_name_full = os.path.realpath(sys.argv[0])
#config_file = script_name_full.replace('.py', '.conf')
config_file = "/usr/local/etc/sunfounder-io-controller.conf"
print "Configure file is at:"
print config_file
KEYS = []
BUTTONS = []
PINS = []
def read_config():
global PINS
conf = open(config_file, 'r')
lines=conf.readlines()
conf.close()
# Find the arguement and set the value
for line in lines:
if line[0] != '#':
configs = line.strip().split(',')
if configs != ['']:
BUTTONS.append(configs[0].strip())
KEYS.append(configs[1].strip())
PINS.append(int(configs[2].strip()))
def setup():
read_config()
GPIO.setmode(GPIO.BCM)
GPIO.setup(PINS, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def main():
last_pin_state = []
for i in PINS:
last_pin_state.append(1)
while True:
for i in range(len(BUTTONS)):
current_pin_state = GPIO.input(PINS[i])
if current_pin_state != last_pin_state[i]:
sleep(20/1000.0)
if current_pin_state != last_pin_state[i]:
if current_pin_state:
print "Channel GPIO%s Released!" % PINS[i]
try:
keyboard.release(KEYS[i])
#break
except Exception, e:
print e
else:
print "Channel GPIO%s Pressed!" % PINS[i]
try:
keyboard.press(KEYS[i])
#break
except Exception, e:
print e
last_pin_state[i] = current_pin_state
sleep(0.01)
if __name__ == '__main__':
setup()
main()