-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpi_main.py
122 lines (96 loc) · 4.11 KB
/
rpi_main.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
"""
Main Loop
"""
from datetime import datetime
import time
# Custom imports
from rpi import *
from utilities import *
from precomputations import *
from graphics import *
from images import *
from ultrasonics import *
# Returns the current time (timezone sensitive) in the format [hour, minute, second]
# where each value is an integer
def getTime():
time = str(datetime.now()).split(" ")[1].split(".")[0]
time_array = time.split(":")
for i in range(len(time_array)):
time_array[i] = int(time_array[i])
return time_array
# Formats the getTime output into "hh:mm:ss"
def formatTime(time_array, hour = False, minute = False, second = False, ampm = False):
# Don't alter original data
time_array = time_array.copy()
time_value = ""
# Remove any values that user doesn't want
if second == False:
time_array.pop(2)
if minute == False:
time_array.pop(1)
if hour == False:
time_array.pop(0)
elif ampm == True: # If user wants it in 24-hour time
if time_array[0] >= 12:
time_array[0] -= 12
time_value = "PM"
else:
time_value = "AM"
if time_array[0] == 0:
time_array[0] = 12
for i in range(len(time_array)):
time_array[i] = str(time_array[i])
time = f"{':'.join(time_array)} {time_value}"
return time
# Returns a string of today's day
def getDay():
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
day = datetime.weekday(datetime.now())
return days[day], day
# Allowed time and day ranges (24-hour format)
on_days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
on_time_range = [[8, 0, 0], [16, 30, 0]]
# Reset board
setAllPixelsColour(pixels, COLOURS["Black"])
# Startup information
print("\nStarting display.")
print(f"\n{formatTime(getTime(), hour=True, minute=True, second=True, ampm=True)}")
print(getDay()[0])
pixels, pixel_framebuf = changeBrightness(PIXEL_BRIGHTNESS)
# Main loop
while True:
# Get today's day
this_day = getDay()
run_check = False
# While it's a weekday
while this_day[0] in on_days:
# Get current time
this_time = getTime()
print(f"\nCurrent time: {formatTime(this_time, hour=True, minute=True, second=True, ampm=True)}")
# Checks if current time and day is within valid range
run_check = False
# I've shortened it to hopefully make it check faster
if (this_day[0] in on_days) and ((this_time[0] > on_time_range[0][0] and this_time[0] < on_time_range[1][0]) or ((this_time[0] == on_time_range[0][0]) and ((this_time[1] > on_time_range[0][1]) or ((this_time[1] == on_time_range[0][1]) and (this_time[2] >= on_time_range[0][2])))) or ((this_time[0] == on_time_range[1][0]) and ((this_time[1] < on_time_range[1][1]) or ((this_time[1] == on_time_range[1][1]) and (this_time[2] < on_time_range[1][2]))))):
run_check = True
# If it's supposed to be displaying
if run_check == True:
checkUltrasonics()
print("Calculating waves...")
merged, non_merged = randomisePatterns()
print("Displaying waves.")
displayRandomPatterns(pixel_framebuf, merged, non_merged)
setAllPixelsColour(pixels, COLOURS["Black"])
else:
# Wait for 5 minutes before rechecking the day and time
time.sleep(60 * 5)
# While it's a weekend
while this_day[0] not in on_days:
print(f"\nStopped for 8 hours because today is {this_day[0]}.\nWait until one of these days: {on_days}.")
# Don't do anything for the minimum amount of time possible - 10 seconds
# I choose the number so that there's enough time for it to
# recheck the day and go into normal operation by it's specified on time.
time.sleep((on_time_range[0][0] * 60 * 60) + (on_time_range[0][1] * 60) + (on_time_range[0][2]) - 10)
# Recheck the day after sleeping
# This fixes an issue where it would get the day but finish the loop
# once more before exiting, which meant it sleeps for 8 hours on Monday
this_day = getDay()