Compare commits

9 Commits

Author SHA1 Message Date
Jan
124ab39f82 Expanded verbose options 2019-09-26 23:39:32 +02:00
Jan
eaaeadf642 Fixed bug where modulo functions returned float instead of int 2019-09-26 20:44:02 +02:00
Jan
ce9a4837e1 Updated changes.txt and README.md 2019-09-26 17:57:14 +02:00
Jan
c25733b64c Added SIGHUP signal handling, adjusted reloading of file changes, added return value of CheckTrackList() 2019-09-26 17:41:05 +02:00
Jan
88dce016de Added tracklist code to replace readmp3 routine 2019-09-26 16:03:19 +02:00
Jan
20f0eb71b9 Merge branch 'clockytracklist' 2019-09-26 14:01:03 +02:00
Jan
4a5498866c added ChecktTrackList 2019-09-25 17:11:51 +02:00
Jan
1555050909 1st setup for tracklist reading 2019-09-25 16:43:37 +02:00
Jan
3edf9ba34c 1st test of track stuff 2019-09-25 12:32:18 +02:00
6 changed files with 217 additions and 93 deletions

View File

@@ -38,6 +38,16 @@ Say what the step will be
Give the example Give the example
``` ```
### Users Manual
Button 1:
short press: show alarm time
long press: enter edit mode
Button 1 & Button 2 simutaneous:
long press: turn alarm off
### Hardware ### Hardware
Overview of used hardware: Overview of used hardware:

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2.7 #!/usr/bin/env python3
import time import time
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
@@ -16,7 +16,8 @@ class Button:
presstime=0 presstime=0
ignore=0 ignore=0
def __init__(self, gpio): def __init__(self, gpio,verbose=False):
if verbose:
print("initialize button on gpio %d" %gpio) print("initialize button on gpio %d" %gpio)
self.gpio = gpio self.gpio = gpio
GPIO.setup(self.gpio, GPIO.IN, pull_up_down = GPIO.PUD_UP) GPIO.setup(self.gpio, GPIO.IN, pull_up_down = GPIO.PUD_UP)

View File

@@ -1,15 +1,15 @@
v0.2.5 * Changed readmp3s routine to clockytracklist (merge)
v0.2.0 * Adjusted code for python 3
v0.1.0 * added max7219 support, added signal handling
0.1.0:
* Added additional logging messages for end of alarm * Added additional logging messages for end of alarm
* implemented SIGINT (Ctrl-C) handling to exit gracefully * implemented SIGINT (Ctrl-C) handling to exit gracefully
0.0.5: v0.0.5 * initial working version
* initial working version
* show alarmtime in startup * show alarmtime in startup
------------------------------------------------------------ ------------------------------------------------------------
@@ -24,5 +24,5 @@ issues
todo todo
---- ----
add SIGHUP handling for reload config
changes config file format to json changes config file format to json
make documentation

149
clocky.py
View File

@@ -1,26 +1,29 @@
#!/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
import max7219 import max7219
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame import pygame
from time import sleep,strftime from time import sleep,strftime
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import button import button
import signal import signal
import clockytracklist
VERBOSE=False
settings= {} settings= {}
looptype=0 looptype=0
quit=0 quit=0
print("\n +----------+\n | Clocky |\n | v0.2.0 |\n +----------+\n") print("\n +----------+\n | Clocky |\n | v0.2.5 |\n +----------+\n")
o_configfilename = "clocky.conf" o_configfilename = "clocky.conf"
o_defaultalarmtime = 60*13+24 # hh*60+mm o_defaultalarmtime = 60*14+10 # hh*60+mm
o_showalarmtime=3 # time that the alarmtime shows after short press o_showalarmtime=3 # time that the alarmtime shows after short press
o_showtracktime=2 # note that this is per track, so total time is 10 times longer o_showtracktime=2 # note that this is per track, so total time is 10 times longer
o_editdelaytime=3 # time it takes to qualify as longpress o_editdelaytime=3 # time it takes to qualify as longpress
@@ -34,13 +37,29 @@ 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("")
# quit=1
def receive_signal(signum, stack):
global stopprogram
global reload
if VERBOSE:
print('Received signal: %d' %signum)
else:
print("") print("")
quit=1 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] "))
@@ -51,29 +70,10 @@ def writesettings():
pickle.dump(settings, handle) pickle.dump(settings, handle)
handle.close() handle.close()
def readmp3list(): # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
c=0
mp3s=[]
dirs=os.listdir('.')
for ent in os.listdir('.'):
if ent.lower().endswith(".mp3") and os.access(ent,os.R_OK):
mp3s.append(ent)
mp3s.sort()
for m in mp3s:
c+=1
if c>99: break
if c:
return(mp3s)
else:
return(0)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
looptype=0 looptype=0
colon=0 colon=0
count=0
digit=1 digit=1
showtime=0 showtime=0
playtime=0 playtime=0
@@ -84,34 +84,48 @@ tvol=0
volume=0.0 volume=0.0
brightness=0 brightness=0
snooze=0 snooze=0
count=0
signal.signal(signal.SIGINT,signal_handler) if VERBOSE:
#display=tm1637.TM1637(CLK=14, DIO=4, brightness=1) print('My PID is %d' %os.getpid())
display=max7219.MAX7219(brightness=1) signal.signal(signal.SIGHUP, receive_signal)
signal.signal(signal.SIGINT, receive_signal)
but1=button.Button(21,verbose=VERBOSE)
but2=button.Button(20,verbose=VERBOSE)
but3=button.Button(26,verbose=VERBOSE)
#------------------------------------------------------------------
#------------------------------------------------------------------
while not stopprogram:
if VERBOSE:
print("Load config...")
reload=False
display=tm1637.TM1637(CLK=14, DIO=4, brightness=1)
# display=max7219.MAX7219(brightness=1)
display.Clear() display.Clear()
display.SetBrightnessRaw(o_defaultbrightness) display.SetBrightnessRaw(o_defaultbrightness)
print("") if VERBOSE:
print("reading mp3s from directory:") print("Reading mp3s from directory:")
allmp3s=readmp3list() tracks=clockytracklist.ClockyTracklist("mp3",recursive=True,verbose=VERBOSE)
if allmp3s==0: allmp3s=tracks.tracklist
print("ERROR: No mp3's found!")
display.showclock(" E rr")
sleep(30)
sys.exit()
c=0 c=0
if VERBOSE:
print("\nTracks:")
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("") stamp()
but1=button.Button(21) print("Started")
but2=button.Button(20) sys.stdout.flush()
but3=button.Button(26)
print("")
# .................................................................
if not os.path.isfile(o_configfilename): if not os.path.isfile(o_configfilename):
print("Createconf: \""+o_configfilename+"\":") print("Createconf: \""+o_configfilename+"\":")
@@ -125,38 +139,43 @@ if not os.path.isfile(o_configfilename):
handle.close() handle.close()
else: else:
if VERBOSE:
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=int(settings['alarmtime']%60)
a_hour=(settings['alarmtime']-a_mins)/60 a_hour=int((settings['alarmtime']-a_mins)/60)
if VERBOSE:
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']:
if VERBOSE:
print("=>",end='') print("=>",end='')
else: else:
if VERBOSE:
print(" ",end='') print(" ",end='')
if VERBOSE:
print(" [T%01dM%02d] "%(i,settings["track%d"%i]),end='') print(" [T%01dM%02d] "%(i,settings["track%d"%i]),end='')
if settings["track%d"%i] and (settings["track%d"%i]-1 < len(allmp3s)): if settings["track%d"%i] and (settings["track%d"%i]-1 < len(allmp3s)):
if VERBOSE:
print("\""+allmp3s[settings["track%d"%i]-1]+"\"") print("\""+allmp3s[settings["track%d"%i]-1]+"\"")
else: else:
if VERBOSE:
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)
@@ -226,8 +245,8 @@ while quit!=True:
if looptype==1: # show alarmtime if looptype==1: # show alarmtime
if not settings["alarmdisabled"]==1: if not settings["alarmdisabled"]==1:
a_mins=settings['alarmtime']%60 a_mins=int(settings['alarmtime']%60)
a_hour=(settings['alarmtime']-a_mins)/60 a_hour=int((settings['alarmtime']-a_mins)/60)
if colon: if colon:
tim="%02d:%02d" % (a_hour, a_mins) tim="%02d:%02d" % (a_hour, a_mins)
@@ -254,11 +273,12 @@ while quit!=True:
if looptype==2: # edit alarmtime if looptype==2: # edit alarmtime
delta=0 delta=0
sm=settings['alarmtime']%10 sm=int(settings['alarmtime']%10)
dm=((settings['alarmtime']-sm)/10)%6 dm=int(((settings['alarmtime']-sm)/10)%6)
sh=(settings['alarmtime']-dm)/60%10 sh=int((settings['alarmtime']-dm)/60%10)
dh=(settings['alarmtime']-sh)/600 dh=int((settings['alarmtime']-sh)/600)
if colon: if colon:
tim="%0d%0d:%0d%0d" % (dh,sh,dm,sm) tim="%0d%0d:%0d%0d" % (dh,sh,dm,sm)
@@ -366,8 +386,8 @@ while quit!=True:
soundpreview=1 soundpreview=1
tr=track tr=track
tl=settings["track%d"%tr]%10 tl=int(settings["track%d"%tr]%10)
th=(settings["track%d"%tr]-tl)/10 th=int((settings["track%d"%tr]-tl)/10)
if colon: if colon:
tim="%d=:%d%d" % (tr,th,tl) tim="%d=:%d%d" % (tr,th,tl)
@@ -408,8 +428,8 @@ while quit!=True:
delta=0 delta=0
tr=track tr=track
tl=settings["track%d"%tr]%10 tl=int(settings["track%d"%tr]%10)
th=(settings["track%d"%tr]-tl)/10 th=int((settings["track%d"%tr]-tl)/10)
if colon: if colon:
tim="%d=:%d%d" % (tr,th,tl) tim="%d=:%d%d" % (tr,th,tl)
@@ -469,7 +489,7 @@ while quit!=True:
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if looptype==5: # Alarm! if looptype==5: # Alarm loop!
#o_timetomaxvol = 60 # seconds it takes to go from 0% to 100% volume #o_timetomaxvol = 60 # seconds it takes to go from 0% to 100% volume
if colon: if colon:
@@ -585,6 +605,11 @@ while quit!=True:
settings['starttrack']=track; settings['starttrack']=track;
writesettings() writesettings()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if count % 40 == 0: # every 10 seconds
if tracks.CheckTrackList():
reload = True;
#------------------------------------------------------------------ #------------------------------------------------------------------
if pygame.mixer.get_init(): if pygame.mixer.get_init():

92
clockytracklist.py Normal file
View File

@@ -0,0 +1,92 @@
#!/usr/bin/python3
from pathlib import Path
EXTENSIONS=(".mp3",".ogg",".wav",".mpa",".m4a",".flac")
class ClockyTracklist:
"""read and monitor directory with audio files"""
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
self.supportedformats=EXTENSIONS
p=Path(self.path)
if not p.exists():
print("ERROR: directory \""+path+"\" does not exist")
exit();
if not p.is_dir():
print("ERROR: \""+path+"\" is not an directory")
exit();
self.__ReadTrackList()
if self.count==0:
print("ERROR: no (supported) files found in directory \""+path+"\"")
exit();
if(self.__verbose):
self.ShowTrackList()
def __ReadTrackList(self):
"""do the actual reading"""
self.tracklist=[]
self.count=0
path=Path(self.path)
if(self.recursive):
selection='**/*'
else:
selection='*'
for entry in path.glob(selection):
if entry.suffix in EXTENSIONS:
self.tracklist.append(entry.as_posix())
self.count+=1
if self.__sort:
self.tracklist.sort()
def ShowTrackList(self):
"""Print the list"""
print("Tracklist:")
print(" path = %s" %self.path)
print(" recursive = %s" %self.recursive)
print(" count = %d" %self.count)
print(" tracks:")
for track in self.tracklist:
print(" "+track)
f=open(track,"r")
if(f):
f.close()
def CheckTrackList(self):
"""Check path for filechanges"""
newtracklist=[]
newcount=0
if self.__reloaded:
self.__reloaded=False
newpath=Path(self.path)
if(self.recursive):
selection='**/*'
else:
selection='*'
for newentry in newpath.glob(selection):
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):
if(self.__verbose):
print("Changes in filenames or number of files detected! Re-read tracklist.")
self.__ReadTrackList()
self.__reloaded=True
return self.__reloaded

View File

@@ -1,8 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2017-18 Richard Hull and contributors
# See LICENSE.rst for details.
import re import re
import time import time