-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalarm.py
executable file
·94 lines (76 loc) · 2.93 KB
/
alarm.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
#!/usr/bin/python
#
#
#
from ledStrip import ledstrip
import time
import argparse
import datetime
from datetime import date
import logging
import ConfigParser
# Open the config file
Config = ConfigParser.RawConfigParser()
Config.readfp(open('config.ini'))
# Define app description and optional parameters
parser = argparse.ArgumentParser(description = 'Example sketch that controls an LED strip via Spacesb. It uses the LED Strip Python library for Adafruit\'s LPD8806 LED strips.')
# Define the led strip length optional parameter
parser.add_argument('-l', '--leds', '--pixels',
nargs = 1, type = int, default = 32,
help = 'Length of led strip leds or pixels')
# Read all command line parameters
args = parser.parse_args()
# Set up logging
logging.basicConfig(filename='alarm.log', level=logging.INFO, format='%(asctime)s %(message)s')
def main():
day = time.strftime('%A')
start_time = Config.get('StartTimes', day)
if (start_time != 'None'):
logging.info('Passed cron')
start_day = time.strftime('%Y-%m-%d')
start = ' ' . join([start_day, start_time])
now = time.time()
start_now = time.mktime(datetime.datetime.strptime(start, '%Y-%m-%d %H:%M').timetuple())
next_cron = now + 900
if start_now > now and start_now <= next_cron:
alarm_start(start_now)
def alarm_start(start_now):
while True:
now = time.time()
if now >= start_now and now <= start_now + 1:
# initialize spi and leds objects
spidev = file("/dev/spidev0.0", "wb") # ref to spi connection to the led bar
leds = ledstrip.LEDStrip(pixels = args.leds, spi = spidev)
all_off(leds)
logging.info('Starting alarm')
alarm_on(leds, debug = False)
time.sleep(600)
logging.info('Stopping alarm')
all_off(leds)
break
def alarm_on(leds, debug = False):
if debug:
pause = 1
else:
pause = 10
colors = {'red':[255, 0, 0], 'pink':[127, 7, 2], 'yellow':[124, 51, 1], 'blue':[135, 206, 235]}
phases = ['red', 'pink', 'yellow', 'blue']
for phase in phases:
for pix in range(32):
if phase != 'blue':
leds.setPixelColorRGB(pixel = pix, red = colors[phase][0], green = colors[phase][1], blue = colors[phase][1])
leds.show()
time.sleep(pause)
else:
if pix % 2 == 0:
leds.setPixelColorRGB(pixel = pix, red = colors[phase][0], green = colors[phase][1], blue = colors[phase][1])
else:
leds.setPixelColorRGB(pixel = pix, red = 127, green = 127, blue = 127)
leds.show()
time.sleep(pause)
def all_off(leds):
for each in range(32):
leds.setPixelColorRGB(pixel = each, red = 0, green = 0, blue = 0)
leds.show()
if __name__ == "__main__":
main()