/
Light.py
395 lines (328 loc) · 11.9 KB
/
Light.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'''
## License
The MIT License (MIT)
GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
Copyright (C) 2015 Dexter Industries
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
from grovepi import *
from grove_rgb_lcd import *
import grovepi
import time
import requests
import Private
import random
import tkinter
# Configure buzzer on D4
buzzer = 4
grovepi.pinMode(buzzer,"OUTPUT")
# Connect the Grove LED to digital port D7-8
gled = 7
rled = 8
grovepi.pinMode(gled,"OUTPUT")
grovepi.pinMode(rled,"OUTPUT")
# Connect the Grove Ultrasonic Ranger to digital port D2
ultrasonic_ranger = 2
# Connect the Grove Button to digital port D3
button = 3
grovepi.pinMode(button,"INPUT")
# Connect the Grove Temperature Sensor to analog port A0
sensor = 0
# Connect the Grove Rotary Angle Sensor to analog port A1
potentiometer = 1
time.sleep(1)
### Reference voltage of ADC is 5v
adc_ref = 5
### Vcc of the grove interface is normally 5v
grove_vcc = 5
### Full value of the rotary angle is 300 degrees, as per it's specs (0 to 300)
full_angle = 300
#This is how I'm trying to safely include my IFTTT key to the Maker channel
MAKER_SECRET = Private.Private.MAKER_SECRET
########################
##WELCOME TO SABER CLASS
########################
class Saber:
root = Tk()
root.title("TheLanceVote")
def __init__(self):
print "Saber instantiated."
######################
######################
### GUI
######################
######################
def calculate(*args):
try:
value = float(feet.get())
meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
except ValueError:
pass
######################
######################
### DEMOS
######################
######################
############
#LCD demo
############
def demoLCD(self):
setText("Hello world\nLCD test")
setRGB(0,128,64)
for c in range(0,255):
setRGB(c,255-c,0)
time.sleep(0.01)
setRGB(0,255,0)
setText("Bye bye, this should wrap")
############
#Temp demo
############
def demoTemp(self):
while True:
try:
temp = grovepi.temp(sensor,'1.1')
print("temp =", temp)
time.sleep(.5)
except KeyboardInterrupt:
break
except IOError:
print ("Error")
############
#Buzzer demo
############
def demoBuzzer(self):
while True:
try:
# Buzz for 1 second
grovepi.digitalWrite(buzzer,1)
print ('start')
time.sleep(1)
# Stop buzzing for 1 second and repeat
grovepi.digitalWrite(buzzer,0)
print ('stop')
time.sleep(1)
except KeyboardInterrupt:
grovepi.digitalWrite(buzzer,0)
break
except IOError:
print ("Error")
############
#Button demo
############
def demoButton(self):
while True:
try:
print(grovepi.digitalRead(button))
time.sleep(.5)
except IOError:
print ("Error")
except KeyboardInterrupt:
break
############
#LED demo
############
def demoLED(self):
while True:
try:
#Blink the LED
digitalWrite(gled,1) # Send HIGH to switch on LED
print "Green ON!"
digitalWrite(rled,0)
time.sleep(1)
digitalWrite(gled,0) # Send LOW to switch off LED
digitalWrite(rled,1)
print "Green OFF!"
time.sleep(1)
except KeyboardInterrupt: # Turn LED off before stopping
digitalWrite(gled,0)
break
except IOError: # Print "Error" if communication error encountered
print ("Error")
except KeyboardInterrupt:
digitalWrite(rled,0)
digitalWrite(gled,0)
break
############
#US_Range demo
############
def demoRange(self):
while True:
try:
# Read distance value from Ultrasonic
print (grovepi.ultrasonicRead(ultrasonic_ranger))
except TypeError:
print ("Error")
except IOError:
print ("Error")
except KeyboardInterrupt:
break
############
#Knob demo
############
def demoKnob(self):
while True:
try:
# Read sensor value from potentiometer
sensor_value = grovepi.analogRead(potentiometer)
# Calculate voltage
voltage = round((float)(sensor_value) * adc_ref / 1023, 2)
# Calculate rotation in degrees (0 to 300)
degrees = round((voltage * full_angle) / grove_vcc, 2)
# Calculate LED brightess (0 to 255) from degrees (0 to 300)
brightness = int(degrees / full_angle * 255)
# Give PWM output to LED
grovepi.analogWrite(gled,brightness)
print("sensor_value = %d voltage = %.2f degrees = %.1f brightness = %d" %(sensor_value, voltage, degrees, brightness))
except KeyboardInterrupt:
grovepi.analogWrite(gled,0)
break
except IOError:
print ("Error")
############
#Maker channel demo
############
def demoMaker(self):
print("Sending dark mode trigger.")
payload = "{ 'value1' : 'hello', 'value2' : 'hello', 'value3' : 'hello'}"
requests.post("https://maker.ifttt.com/trigger/demoMaker/with/key/"+MAKER_SECRET, data=payload)
######################
######################
### LOGIC
######################
######################
############
#Menu display and selection
############
def formMenu(self, items):
itemRange = int(1000/len(items))
while True:
try:
# Read sensor value from potentiometer
sensor_value = grovepi.analogRead(potentiometer)
selection = int(sensor_value/itemRange)
# check to make sure that our index value isn't going over
if selection >= len(items):
selection = len(items) - 1
setText(list(items[selection]))
self.colorSelect(selection)
if grovepi.digitalRead(button) == 1:
self.clear()
return selection
time.sleep(0.5)
except KeyboardInterrupt:
grovepi.analogWrite(gled,0)
break
except IOError:
print ("Error")
############
#Returns colors based on index
############
def colorSelect(self, x):
if x == 0:
setRGB(255, 255, 255)
elif x == 1:
setRGB(205, 0, 0)
elif x == 2:
setRGB(0, 205, 0)
elif x == 3:
setRGB(0, 0, 205)
elif x == 4:
setRGB(205, 0, 205)
elif x == 5:
setRGB(150, 255, 255)
elif x == 6:
setRGB(255, 178, 102)
else:
setRGB(100, 0, 200)
############
#Launch GUI
############
def launchGUI(self):
root = Tk()
root.title("Feet to Meters")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
feet = StringVar()
meters = StringVar()
feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
feet_entry.grid(column=2, row=1, sticky=(W, E))
ttk.Label(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)
ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
feet_entry.focus()
root.bind('<Return>', calculate)
root.mainloop()
############
#Sensor in box checks if something (box's lid) is right
#Above. Triggers alarm if opened. Button disables
############
def coverCheck(self):
counter = 0
while True:
try:
dist = grovepi.ultrasonicRead(ultrasonic_ranger)
print "DIST: " + str(dist) + " cm away."
counter +=1
if counter % 50 == 0 and dist > 500:
print "My cover is just sitting open, isn't it?"
elif dist > 50:
try:
#Blink the LED
digitalWrite(rled,1) # Send HIGH to switch on LED
digitalWrite(buzzer,1)
print "Hey!"
time.sleep(1)
digitalWrite(rled,0) # Send LOW to switch off LED
digitalWrite(buzzer,0)
print "Close the lid."
time.sleep(1)
except IOError: # Print "Error" if communication error encountered
print ("Error")
except TypeError:
print ("Error")
except IOError:
print ("Error")
except KeyboardInterrupt:
digitalWrite(rled,0)
digitalWrite(buzzer,0)
break
############
## CLEAR
############
def clear(self):
digitalWrite(rled,0)
digitalWrite(buzzer,0)
digitalWrite(gled,0)
setText("")
setRGB(0,0,0)
#############
### RootMenu
#############
def rootMenu(self):
root = ["Function Demos", "Light Settings", "Phone Options"]
light = ["All off", "All on", "Work mode", "Movie mode", "Bed chill"]
people = ["Dan", "Laura", "MamaMarti", "MamaSusan"]
choice = self.formMenu(root)
if selection == 1:
self.formMenu(light)
elif selection == 2:
self.formMenu(people)