-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
110 lines (90 loc) · 3.02 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
import utime
from machine import Pin
import os
import uasyncio
import set_time
import door_state
import buzzer_music
import config
opened_counter = 0
door_state.is_open = 0
door_open = False
loop = uasyncio.get_event_loop()
led = Pin(2, Pin.OUT) # on-board LED
pin = Pin(config.BUTTON_PIN, Pin.IN, Pin.PULL_UP)
if config.ENABLE_LOG:
if 'logs' not in os.listdir():
os.mkdir('logs')
log = open('logs/door.log', 'w')
log.write('action;time')
log.close()
async def log_door(action, time):
log = open('logs/door.log', 'a')
log.write(action + ';' + str("{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}".format(*time)) + '\n')
log.close()
async def play_melody(song_name):
if song_name == 'random':
song_name = buzzer_music.get_random_song()
print('Playing "{}"...'.format(song_name))
loop.create_task(log_door('playing: {}'.format(song_name), utime.localtime()))
await buzzer_music.play_song(song_name)
async def play_sfx(song_name):
if song_name == 'random':
song_name = buzzer_music.get_random_song()
print('Playing "{}"...'.format(song_name))
loop.create_task(log_door('playing: {}'.format(song_name), utime.localtime()))
await buzzer_music.play_completely(song_name)
async def door_opened():
global opened_counter
door_state.is_open += 1
opened_counter += 1
do_reset = False
if opened_counter == config.COUNTER_MAX and config.ONE_UP:
await play_sfx('1up')
do_reset = True
token = opened_counter
print('door opened')
if config.ENABLE_LOG:
loop.create_task(log_door('opened', utime.localtime()))
led.off()
await uasyncio.sleep(config.LEGIT_OPEN_TIME)
# start 'forgot to close'-code if still open
# if door_state.is_open > 0 and token == opened_counter:
while door_state.is_open > 0 and token == opened_counter:
print('Close the door!!111')
await play_melody('random')
await uasyncio.sleep(3)
print('Door has been opened {} times since last reset.\n'.format(opened_counter))
if do_reset:
do_reset = False
opened_counter = 0
async def door_closed():
door_state.is_open -= 1
print('door closed')
await uasyncio.sleep_ms(100)
if config.CLOSING_SOUND:
await play_sfx('closed')
if config.ENABLE_LOG:
loop.create_task(log_door('closed', utime.localtime()))
led.on()
async def check_door():
global door_open
global opened_counter
global loop
while True:
await uasyncio.sleep_ms(100)
if not pin.value() == door_open:
await uasyncio.sleep_ms(100) # debouncing
if not pin.value() == door_open:
door_open = pin.value()
if door_open:
loop.create_task(door_opened())
else:
loop.create_task(door_closed())
def main():
global loop
loop.create_task(set_time.keep_time_synced())
loop.create_task(check_door())
loop.run_forever()
if __name__ == '__main__':
main()