Added SIGHUP signal handling, adjusted reloading of file changes, added return value of CheckTrackList()
This commit is contained in:
116
clocky.py
116
clocky.py
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import pickle
|
||||
import os.path
|
||||
import os
|
||||
import time
|
||||
import sys
|
||||
import tm1637
|
||||
@@ -35,13 +35,26 @@ o_defaultbrightness=0
|
||||
o_loopdelay=0.25
|
||||
track=0
|
||||
|
||||
stopprogram=0
|
||||
reload=False
|
||||
|
||||
#GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering
|
||||
#GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_UP)
|
||||
|
||||
def signal_handler(signal,frame):
|
||||
global quit
|
||||
print("")
|
||||
quit=1
|
||||
#def signal_handler(signal,frame):
|
||||
# global quit
|
||||
# print("")
|
||||
# quit=1
|
||||
|
||||
def receive_signal(signum, stack):
|
||||
global stopprogram
|
||||
global reload
|
||||
print('Received signal: %d' %signum)
|
||||
if signum == signal.SIGHUP:
|
||||
reload=True
|
||||
if signum == signal.SIGINT:
|
||||
stopprogram=1
|
||||
|
||||
|
||||
def stamp():
|
||||
sys.stdout.write(strftime("[%Y%m%d %H%M%S] "))
|
||||
@@ -52,11 +65,10 @@ def writesettings():
|
||||
pickle.dump(settings, handle)
|
||||
handle.close()
|
||||
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
|
||||
looptype=0
|
||||
colon=0
|
||||
count=0
|
||||
digit=1
|
||||
showtime=0
|
||||
playtime=0
|
||||
@@ -67,36 +79,52 @@ tvol=0
|
||||
volume=0.0
|
||||
brightness=0
|
||||
snooze=0
|
||||
|
||||
signal.signal(signal.SIGINT,signal_handler)
|
||||
#display=tm1637.TM1637(CLK=14, DIO=4, brightness=1)
|
||||
display=max7219.MAX7219(brightness=1)
|
||||
|
||||
display.Clear()
|
||||
display.SetBrightnessRaw(o_defaultbrightness)
|
||||
count=0
|
||||
|
||||
print("")
|
||||
print("reading mp3s from directory:")
|
||||
tracks=clockytracklist.ClockyTracklist("mp3",recursive=True,verbose=True)
|
||||
allmp3s=tracks.tracklist
|
||||
if allmp3s==0:
|
||||
print('My PID is %d' %os.getpid())
|
||||
signal.signal(signal.SIGHUP, receive_signal)
|
||||
signal.signal(signal.SIGINT, receive_signal)
|
||||
|
||||
but1=button.Button(21)
|
||||
but2=button.Button(20)
|
||||
but3=button.Button(26)
|
||||
|
||||
#------------------------------------------------------------------
|
||||
#------------------------------------------------------------------
|
||||
|
||||
while not stopprogram:
|
||||
print("reload config...")
|
||||
reload=False
|
||||
|
||||
|
||||
# display=tm1637.TM1637(CLK=14, DIO=4, brightness=1)
|
||||
display=max7219.MAX7219(brightness=1)
|
||||
|
||||
display.Clear()
|
||||
display.SetBrightnessRaw(o_defaultbrightness)
|
||||
|
||||
print("reading mp3s from directory:")
|
||||
tracks=clockytracklist.ClockyTracklist("mp3",recursive=True,verbose=True)
|
||||
allmp3s=tracks.tracklist
|
||||
if allmp3s==0:
|
||||
print("ERROR: No mp3's found!")
|
||||
display.showclock(" E rr")
|
||||
sleep(30)
|
||||
sys.exit()
|
||||
|
||||
c=0
|
||||
for m in allmp3s:
|
||||
|
||||
c=0
|
||||
for m in allmp3s:
|
||||
print(" %2d : %s"%(c+1,m))
|
||||
c+=1
|
||||
|
||||
print("")
|
||||
but1=button.Button(21)
|
||||
but2=button.Button(20)
|
||||
but3=button.Button(26)
|
||||
print("")
|
||||
|
||||
if not os.path.isfile(o_configfilename):
|
||||
stamp()
|
||||
print("Started")
|
||||
sys.stdout.flush()
|
||||
|
||||
# .................................................................
|
||||
if not os.path.isfile(o_configfilename):
|
||||
print("Createconf: \""+o_configfilename+"\":")
|
||||
|
||||
settings = {'track0':1, 'track1':2, 'track2':3, 'track3':4, 'track4':5, 'track5':6, 'track6':7, 'track7':8, 'track8':9, 'track9':10 }
|
||||
@@ -108,20 +136,20 @@ if not os.path.isfile(o_configfilename):
|
||||
pickle.dump(settings, handle)
|
||||
handle.close()
|
||||
|
||||
else:
|
||||
else:
|
||||
print("Configfile: \""+o_configfilename+"\":")
|
||||
|
||||
with open(o_configfilename, 'rb') as handle:
|
||||
settings = pickle.load(handle)
|
||||
handle.close()
|
||||
|
||||
a_mins=settings['alarmtime']%60
|
||||
a_hour=(settings['alarmtime']-a_mins)/60
|
||||
print("Alarmtime : %02d:%02d"%(a_hour,a_mins))
|
||||
print("Alarmdisable: %d"%(settings['alarmdisabled']))
|
||||
print("Starttrack : %d"%(settings['starttrack']))
|
||||
a_mins=settings['alarmtime']%60
|
||||
a_hour=(settings['alarmtime']-a_mins)/60
|
||||
print("Alarmtime : %02d:%02d"%(a_hour,a_mins))
|
||||
print("Alarmdisable: %d"%(settings['alarmdisabled']))
|
||||
print("Starttrack : %d"%(settings['starttrack']))
|
||||
|
||||
for i in range(0,10):
|
||||
for i in range(0,10):
|
||||
if i==settings['starttrack']:
|
||||
print("=>",end='')
|
||||
else:
|
||||
@@ -132,15 +160,13 @@ for i in range(0,10):
|
||||
else:
|
||||
print("")
|
||||
settings["track%d"%i]=0
|
||||
writesettings()
|
||||
writesettings()
|
||||
|
||||
print("")
|
||||
stamp()
|
||||
print("Started")
|
||||
sys.stdout.flush()
|
||||
#------------------------------------------------------------------
|
||||
#------------------------------------------------------------------
|
||||
while quit!=True:
|
||||
|
||||
|
||||
# .................................................................
|
||||
|
||||
while not reload and not stopprogram:
|
||||
count+=1;
|
||||
sleep(o_loopdelay)
|
||||
|
||||
@@ -571,9 +597,9 @@ while quit!=True:
|
||||
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
if count % 40 == 0:
|
||||
print("#")
|
||||
tracks.CheckTrackList()
|
||||
if count % 40 == 0: # every 10 seconds
|
||||
if tracks.CheckTrackList():
|
||||
reload = True;
|
||||
|
||||
#------------------------------------------------------------------
|
||||
if pygame.mixer.get_init():
|
||||
|
||||
@@ -6,11 +6,13 @@ EXTENSIONS=(".mp3",".ogg",".wav",".mpa",".m4a",".flac")
|
||||
|
||||
class ClockyTracklist:
|
||||
"""read and monitor directory with audio files"""
|
||||
def __init__(self,path=".",recursive=False,sort=False,verbose=False):
|
||||
def __init__(self,path=".",recursive=False,sort=True,verbose=False):
|
||||
"""Read (current or supplied) path for all audio files. Add recursive=True to check all subdirectories too"""
|
||||
self.recursive=recursive
|
||||
self.path=path
|
||||
self.__verbose=verbose
|
||||
self.__sort=sort
|
||||
self.__reloaded=False
|
||||
|
||||
p=Path(self.path)
|
||||
if(not p.is_dir()):
|
||||
@@ -18,8 +20,6 @@ class ClockyTracklist:
|
||||
exit();
|
||||
|
||||
self.__ReadTrackList()
|
||||
if sort:
|
||||
self.tracklist.sort()
|
||||
if(self.__verbose):
|
||||
self.ShowTrackList()
|
||||
|
||||
@@ -38,6 +38,8 @@ class ClockyTracklist:
|
||||
if entry.suffix in EXTENSIONS:
|
||||
self.tracklist.append(entry.as_posix())
|
||||
self.count+=1
|
||||
if self.__sort:
|
||||
self.tracklist.sort()
|
||||
|
||||
|
||||
def ShowTrackList(self):
|
||||
@@ -58,6 +60,9 @@ class ClockyTracklist:
|
||||
newtracklist=[]
|
||||
newcount=0
|
||||
|
||||
if self.__reloaded:
|
||||
self.__reloaded=False
|
||||
|
||||
newpath=Path(self.path)
|
||||
if(self.recursive):
|
||||
selection='**/*'
|
||||
@@ -67,7 +72,12 @@ class ClockyTracklist:
|
||||
if newentry.suffix in EXTENSIONS:
|
||||
newtracklist.append(newentry.as_posix())
|
||||
newcount+=1
|
||||
if self.__sort:
|
||||
newtracklist.sort()
|
||||
if(newcount != self.count) or (newtracklist != self.tracklist):
|
||||
self.__ReadTrackList()
|
||||
if(self.__verbose):
|
||||
print("Changes in filenames or number of files detected! Re-read tracklist.")
|
||||
self.__ReadTrackList()
|
||||
self.__reloaded=True
|
||||
|
||||
return self.__reloaded
|
||||
|
||||
25
tracktest.py
25
tracktest.py
@@ -1,25 +0,0 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import time
|
||||
import clockytracklist
|
||||
|
||||
print("-------------------------------------")
|
||||
print("-------------------------------------")
|
||||
tl=clockytracklist.ClockyTracklist("mp3",recursive=True,sort=False,verbose=True)
|
||||
tl.ShowTrackList()
|
||||
print("-------------------------------------")
|
||||
for e in tl.tracklist:
|
||||
print(e);
|
||||
|
||||
|
||||
|
||||
t=0
|
||||
while True:
|
||||
t+=1
|
||||
tl.CheckTrackList()
|
||||
time.sleep(1)
|
||||
|
||||
if(t>30):
|
||||
break
|
||||
print(t)
|
||||
|
||||
Reference in New Issue
Block a user