-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
117 lines (92 loc) · 3.99 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
import configparser
import logging
import time
import RPi.GPIO as GPIO
from modules.RFIDModule import RFIDModule
from modules.LCDModule import LCDModule
from modules.MPDModule import MPDModule
from modules.StateManagerModule import StateManagerModule
from modules.TagManagerModule import TagManagerModule
from modules.USBModule import USBModule
from modules.WebServerModule import WebServerModule
from threading import Thread
def main():
config = configparser.ConfigParser()
config.read('config.ini')
logfile = config.get('DEFAULT', 'logfile', fallback='SimplicityPlayer.log')
logging.basicConfig(filename=logfile,
format="%(asctime)s [%(module)s.%(funcName)s] %("
"levelname)s: %(message)s")
logging.getLogger('sp').setLevel(logging.DEBUG)
logging.getLogger('sp').info('Starting...')
rfid_read_delay = config.getint('DEFAULT', 'rfid_read_delay', fallback=2)
toggle_button_pin = config.getint('DEFAULT', 'toggle_button_pin',
fallback=None)
mpd = MPDModule(config)
rfid = RFIDModule(config)
lcd = LCDModule(config, mpd)
usb = USBModule(config, lcd, mpd)
tag_manager = TagManagerModule(config)
state_manager = StateManagerModule(config)
def button_callback(_):
mpd.toggle_playback()
if toggle_button_pin is not None:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(toggle_button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
try:
GPIO.add_event_detect(toggle_button_pin, GPIO.FALLING,
callback=button_callback, bouncetime=800)
except RuntimeError as e:
GPIO.cleanup()
logging.getLogger('sp').error('Failed to add edge detection')
logging.getLogger('sp').error(e)
return
# start the web server in a separate thread
web_server = WebServerModule(config, lcd, rfid, tag_manager, state_manager)
web_server_thread = Thread(target=web_server.run)
web_server_thread.start()
last_state_store = time.time()
last_read_tag = None
try:
while True:
if not rfid.is_locked() and not usb.is_working():
lcd.clear_message()
# read tag from RFID reader
tag = rfid.wait_for_tag(rfid_read_delay)
# if tag is not None and not the same as the last tag read
if tag and tag != last_read_tag:
last_read_tag = tag
# get the file name from the tag
file_name = tag_manager.get_tag(tag)
if file_name is not None:
# get the elapsed time from the state manager
elapsed = state_manager.get_elapsed(file_name)
mpd.play(file_name, elapsed)
else:
lcd.set_message('Unbekannter Tag')
# if tag is the same as the last tag read
elif tag and tag == last_read_tag:
time.sleep(rfid_read_delay)
# if tag is None, reset the last read tag
elif tag is None and last_read_tag is not None:
last_read_tag = None
# if the RFID reader is locked
else:
time.sleep(rfid_read_delay)
# store the current state every 60 seconds
state_time_diff = time.time() - last_state_store
if state_time_diff > 60:
state_manager.set_current(mpd.get_song())
last_state_store = time.time()
# check if a USB stick is inserted
if not usb.is_working() and usb.exists():
usb.copy_files()
# do normal stuff here per tick
lcd.do_tick()
except KeyboardInterrupt:
logging.getLogger('sp').info('Received KeyboardInterrupt.')
logging.getLogger('sp').info('Stopping...')
GPIO.cleanup()
if __name__ == "__main__":
main()