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