-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerplug
95 lines (72 loc) · 2.31 KB
/
powerplug
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
'''
General notes:
gpio pins i have hardware wired into the breadboard:
5 6 12 13 16 19 20 21 23 24
command line command should look like this:
powerplug 1 on
powerplug 2 off
powerplug 4 off
powerplug 3 on
powerplug all on
powerplug all off
'''
import RPi.GPIO as GPIO
import time, sys
# i dont know what this does but it was in a tutorial
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
button_on = {}
button_off = {}
def init_gpio():
# mapping of gpio pin number and the powerplug button number
# there are 10 buttons in total
# button 5 is the 'all on' or the 'all off' button on the remote
button_on[1] = 5
button_on[2] = 6
button_on[3] = 12
button_on[4] = 13
button_on[5] = 16
button_off[1] = 23
button_off[2] = 20 #not working for some reason!
button_off[3] = 21
button_off[4] = 24
button_off[5] = 19
# set all gpio pins to output instead of input
for buttons in (button_on.values(), button_off.values()):
for b in buttons:
GPIO.setup(b, GPIO.OUT)
# print 'gpio ' + str(b) + ' set to output'
#will press a button for one second
def press_button(button):
# press it for one second
GPIO.output(button, GPIO.HIGH)
print 'pressing gpio number: ' + str(button)
time.sleep(1)
# stop pressing it
GPIO.output(button,GPIO.LOW)
print '\t stop pressing gpio number: ' + str(button)
def command_line_input(command):
# get the last one
on_or_off = command[-1]
# get which button should do something
button = command[-2]
# i know there is probably a better way
# but we will just check for the last two arguments to be one of our keywords
# then we will turn on off the buttons
if on_or_off not in ['on', 'ON', 'off', 'OFF'] or button not in ['1', '2', '3', '4', '5', 'all', 'ALL']:
print 'wrong input '+ str(button) + ' ' + str(on_or_off)
return
if button in ['all', 'ALL']:
button = 5
# tansform into an int
button = int(button)
if on_or_off in ['on', 'ON']:
press_button(button_on[button])
elif on_or_off in ['off', 'OFF']:
press_button(button_off[button])
def main():
init_gpio()
command_line_input(sys.argv)
if __name__ == "__main__":
main()