-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
220 lines (182 loc) · 6.6 KB
/
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
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
"""
Jaccuse!
A way to keep track of how long it has been since you have been jaccused of something.
Run this in https://trinket.io/sense-hat to emulate a pi sense-hat.
sense-hat api: https://pythonhosted.org/sense-hat/api/
"""
from time import sleep
from sense_hat import SenseHat
from pixel_dict import O, B, W, pixel_dict, pixel_map
sense = SenseHat()
def get_orientation():
'''
Description:
Set the orientation of the Pi, using an question mark.
Use the Sense Hat buttons to pick the orientation
Inputs:
- None
Outputs:
- None
'''
sense.clear()
flipped = True
direction_dict = {"up": 0, "down": 180, "left": 90, "right": 270}
arrow = "?"
while flipped is True:
sense.show_letter(arrow)
for event in sense.stick.get_events():
# Check if the joystick was pressed
if event.action == "pressed":
# Check which direction
if event.direction == "up": # Up button
sense.set_rotation(direction_dict[event.direction])
sense.show_letter(arrow)
elif event.direction == "down": # Down button
sense.set_rotation(direction_dict[event.direction])
sense.show_letter(arrow)
elif event.direction == "left": # Left button
sense.set_rotation(direction_dict[event.direction])
sense.show_letter(arrow)
elif event.direction == "right": # Right button
sense.set_rotation(direction_dict[event.direction])
sense.show_letter(arrow)
elif event.direction == "middle": # Enter key
flipped = False # exits the loop
def get_duration():
'''
Description:
Gets the duration from the user, returning a string
Inputs:
- None
Outputs:
- choice: string
'''
sense.clear() # clears LED screen
duration_list = ["sec", "min", "hour", "day"] # list of time options
pointer = 0 # this pointer will be used to keep track of the users cycling of the list
choice = ""
loop = True
# sense.show_message("Duration?", scroll_speed=0.05)
sense.show_message(duration_list[pointer], scroll_speed=0.05) # show the first item in the list
while loop is True:
for event in sense.stick.get_events():
# Check if the joystick was pressed
if event.action == "pressed":
# Check which direction
if event.direction == "left": # Left arrow
pointer -= 1
sense.show_message(duration_list[pointer], scroll_speed=0.05)
elif event.direction == "right": # Right arrow
pointer += 1
if pointer == len(duration_list):
pointer = 0
sense.show_message(duration_list[pointer], scroll_speed=0.05)
elif event.direction == "middle": # Enter key
choice = duration_list[pointer]
sense.clear()
loop = False
return choice
def change(canvas, paint):
'''
Description:
Changes the pixel map by using points as pain
Inputs:
- canvas: list
- paint: list
Outputs:
- list
'''
# TODO this code is working (Somehow!) and not efficient at all
# 128 comparisons for each painting, 3 paintings = 384 comparisons every iteration
# this happens either every second or minute or hour or day, depending on choice
# this is not good
# also it was only fixed by some voodoo magic on line 8
for i in range(len(canvas)):
if paint[i] == W:
canvas[i] = paint[i]
elif paint[i] == B:
canvas[i] = O
elif paint[i] == O:
pass
return canvas
def update_display(counter, duration):
'''
Description:
Updates the display with the counter and duration
Inputs:
- counter: Int
- duration: Str
Outputs:
- None
'''
sense.clear()
string_counter = str(counter)
canvas = pixel_map
if len(string_counter) == 1:
left = "0"
right = string_counter[0]
elif len(string_counter) == 2:
left = string_counter[0]
right = string_counter[1]
else:
pass
canvas1 = change(canvas, pixel_dict["first_decimal"][right])
canvas2 = change(canvas1, pixel_dict["second_decimal"][left])
canvas3 = change(canvas2, pixel_dict["time"][duration])
sense.set_pixels(canvas3)
# if counter < 10: # we can use sense.show_letter for single digits
# print("it has been "+ str(counter) + " " + duration + " since the button was pressed")
# sense.show_letter(str(counter))
# elif counter >=10: #otherwise we need to use sense.show_message
# print("it has been "+ str(counter) + " " + duration + " since the button was pressed")
# sense.show_message(str(counter))
def main():
'''
Description:
1. Will get the duration and then
2. start the timer
Inputs:
- None
Outputs:
- None
'''
main_loop = True
halt_button_pressed = False
# get_orientation()
duration = "sec" # string
while main_loop is True:
counter = 0
halt_button_pressed = False
while halt_button_pressed is False:
events = sense.stick.get_events()
if events:
for button in events:
if button.direction == "middle" and button.action == "pressed":
counter = 0
halt_button_pressed = True
while halt_button_pressed is False:
update_display(counter, duration)
sleep(1)
counter += 1
if counter == 60 and duration == "sec":
duration = "min"
counter = 1
update_display(counter, duration)
sleep(60)
counter += 1
if counter == 60 and duration == "min":
duration = "hour"
counter = 1
update_display(counter, duration)
sleep(3600)
counter += 1
if counter == 24 and duration == "hour":
duration = "day"
counter = 1
update_display(counter, duration)
sleep(86400)
counter += 1
else:
break
if __name__ == "__main__":
main()