-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_loop.py
127 lines (108 loc) · 3.35 KB
/
main_loop.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
import logging
import signal
import sys
import time
import threading
from pirc522 import RFID
import async_button
import mpd_player
import music_copy
import rgb_led
import sound_effect
import usb
from config import LOGGER as LOG_CONF
run = True
rdr = RFID()
util = rdr.util()
util.debug = True
# Logger configuration
logging.basicConfig(level=logging.DEBUG,
format=LOG_CONF.LOG_FORMAT,
datefmt=LOG_CONF.LOG_DATE_FORMAT,
filename=LOG_CONF.LOG_FILE)
log = logging.getLogger(__name__)
def end_read(signal, frame):
global run
print("\nCtrl+C captured, ending read.")
log.info("Ctrl+C captured, ending read.")
run = False
player.stop()
button_stop_event.set()
time.sleep(2)
# rgb_led.ready_led_stop_pulse()
rgb_led.off()
rdr.cleanup()
sys.exit()
signal.signal(signal.SIGINT, end_read)
player = mpd_player.Player()
player.stop()
playing_uid = ''
config_mode = False
config_started = False
rgb_led.setup()
rgb_led.ready_pause_led_on()
sound_effect.started()
log.info('************ START ************')
def play_list_on_tag():
global playing_uid
global config_mode
global config_started
# rdr.wait_for_tag()
if config_started:
return
if usb.has_usb_stick():
if not config_mode:
sound_effect.start_config()
log.info("config mode")
config_mode = True
player.stop()
# rgb_led.ready_led_stop_pulse()
rgb_led.config_led_on()
else:
if config_mode:
log.info("player mode")
config_mode = False
# rgb_led.config_led_stop_blink()
if player.is_playing():
rgb_led.ready_led_on()
else:
rgb_led.ready_pause_led_on()
(request_error, data) = rdr.request()
if not request_error and data is not None:
(anti_coll_error, uid) = rdr.anticoll()
if not anti_coll_error:
dash_uid = "-".join([str(i) for i in uid])
if config_mode:
config_started = True
log.info('config started')
# rgb_led.config_led_blink()
rgb_led.copy_led_on()
music_copy.copy_from_usb(dash_uid)
sound_effect.end_config()
# leave config mode when usb stick is removed
while usb.has_usb_stick():
time.sleep(0.08)
config_started = False
if dash_uid == playing_uid:
log.info(dash_uid + " is already selected")
if not player.is_playing():
player.play()
else:
log.debug("UID: " + dash_uid)
log.debug("Registered: " + str(music_copy.is_uid_registered(dash_uid)))
if music_copy.is_uid_registered(dash_uid):
sound_effect.tag_scanned()
player.stop()
player.load_list(dash_uid)
player.play()
# rgb_led.ready_led_stop_pulse()
rgb_led.ready_led_on()
playing_uid = dash_uid
# start button check loop
log.info('Start async_button thread')
main_event = threading.Event()
button_stop_event = async_button.start(main_event)
log.info('start main loop')
while run and not main_event.is_set():
play_list_on_tag()
time.sleep(0.08)