first live version: v 0.0.5

This commit is contained in:
Jan
2019-09-24 16:44:24 +02:00
parent 4373290b66
commit b9c4cdf93a
2 changed files with 244 additions and 99 deletions

314
clocky.py
View File

@@ -4,40 +4,50 @@ import pickle
import os.path import os.path
import time import time
import sys import sys
import keyboard
import tm1637 import tm1637
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
settings= {} settings= {}
looptype=0 looptype=0
quit=0
print "\n +----------+\n | Clocky |\n | v0.0.5 |\n +----------+\n"
o_configfilename = "clocky.conf" o_configfilename = "clocky.conf"
o_defaultalarmtime = 60*7+45 # hh*60+mm o_defaultalarmtime = 60*7+45 # hh*60+mm
o_showalarmtime=5 # 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
o_showtimeout=6 # timeout to exit edit modus o_showtimeout=6 # timeout to exit edit modus
o_snoozetime = 9 # time to snooze o_snoozetime = 9 # time to snooze
o_timetomaxvol = 150# seconds it takes to go from 0% to 100% volume o_timetomaxvol = 150 # seconds it takes to go from 0% to 100% volume
o_maxplaytime = 900 # seconds it to play o_maxplaytime = 1800 # seconds it to play
o_soundpreviewtime=5 o_soundpreviewtime=5
o_defaultbrightness=0 o_defaultbrightness=1
o_loopdelay=0.25 o_loopdelay=0.25
track=0 track=0
GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering #GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering
GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP) #GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(20, GPIO.IN, pull_up_down = GPIO.PUD_UP)
def signal_handler(signal,frame):
global quit
print ""
quit=1
def stamp():
sys.stdout.write(strftime("[%Y%m%d %H%M%S] "))
def showclock(tstr): def showclock(tstr):
"Show da clock" "Show da clock"
sys.stdout.write( "%s [%d]\r" % (tstr,looptype)) # sys.stdout.write( "%s [%d]\r" % (tstr,looptype))
sys.stdout.flush() # sys.stdout.flush()
d=0 d=0
if tstr[2]==':': if tstr[2]==':':
@@ -98,15 +108,14 @@ digit=1
showtime=0 showtime=0
playtime=0 playtime=0
soundpreview=0 soundpreview=0
alarmoff=0
alarmstarted=0 alarmstarted=0
silencedalarm=0
tvol=0 tvol=0
volume=0.0 volume=0.0
brightness=0.0 brightness=0.0
snooze=0 snooze=0
print "\n +--------+\n | Clocky |\n | v0.1 |\n +--------+\n" signal.signal(signal.SIGINT,signal_handler)
display=tm1637.TM1637(CLK=14, DIO=4, brightness=1.0) display=tm1637.TM1637(CLK=14, DIO=4, brightness=1.0)
display.Clear() display.Clear()
display.SetBrightnessRaw(o_defaultbrightness) display.SetBrightnessRaw(o_defaultbrightness)
@@ -132,21 +141,23 @@ for m in allmp3s:
print "" print ""
but1=button.Button(21) but1=button.Button(21)
but2=button.Button(20) but2=button.Button(20)
but3=button.Button(26)
print "" print ""
if not os.path.isfile(o_configfilename): if not os.path.isfile(o_configfilename):
print "creating new configfile \""+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 }
settings['alarmtime']=o_defaultalarmtime; settings['alarmtime']=o_defaultalarmtime;
settings['starttrack']=0; settings['starttrack']=0;
settings['alarmdisabled']=0;
with open(o_configfilename, 'wb') as handle: with open(o_configfilename, 'wb') as handle:
pickle.dump(settings, handle) pickle.dump(settings, handle)
handle.close() handle.close()
else: else:
print "reading from previous 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)
@@ -154,21 +165,28 @@ else:
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 - Starttrack: %d"%(a_hour,a_mins,settings['starttrack']) 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):
print " track %d: %02d"%(i,settings["track%d"%i]), if i==settings['starttrack']:
print "=>",
else:
print " ",
print "[T%02dM%02d]"%(i,settings["track%d"%i]),
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)):
print " => "+allmp3s[settings["track%d"%i]-1] print "\""+allmp3s[settings["track%d"%i]-1]+"\""
else: else:
print "" print ""
settings["track%d"%i]=0 settings["track%d"%i]=0
writesettings() writesettings()
print "" print ""
stamp()
print strftime("Starting: %d %b %Y %H:%M:%S") print "Started"
sys.stdout.flush()
#------------------------------------------------------------------
#------------------------------------------------------------------ #------------------------------------------------------------------
while quit!=True: while quit!=True:
count+=1; count+=1;
@@ -177,12 +195,12 @@ while quit!=True:
mytime=time.localtime(time.time()) mytime=time.localtime(time.time())
currenttime=mytime[3]*60 + mytime[4]; currenttime=mytime[3]*60 + mytime[4];
# settings['alarmtime']= currenttime if not settings["alarmdisabled"]:
if currenttime == settings['alarmtime']: if currenttime == settings['alarmtime']:
if not alarmoff: if looptype==0 and not silencedalarm:
looptype=5 looptype=5
else: else:
alarmoff=0 silencedalarm=0
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -191,6 +209,20 @@ while quit!=True:
if but1.WasPressed(): if but1.WasPressed():
looptype=1 looptype=1
if but1.WasLong(): if but1.WasLong():
if but2.WasPressed():
if but2.WasLong():
if settings["alarmdisabled"]==0:
settings["alarmdisabled"]=1
stamp()
print "Alarm disabled"
writesettings()
else:
settings["alarmdisabled"]=0
stamp()
print "Alarm enabled"
writesettings()
looptype=1
else:
looptype=2 looptype=2
if but2.WasPressed(): if but2.WasPressed():
@@ -199,6 +231,21 @@ while quit!=True:
if but2.WasLong(): if but2.WasLong():
looptype=4 looptype=4
if but3.WasPressed():
if but3.WasLong():
if settings["alarmdisabled"]==0:
settings["alarmdisabled"]=1
stamp()
print "Alarm disabled"
writesettings()
else:
settings["alarmdisabled"]=0
stamp()
print "Alarm enabled"
writesettings()
looptype=1
if colon: if colon:
tim="%02d:%02d" % (mytime[3] , mytime[4]) tim="%02d:%02d" % (mytime[3] , mytime[4])
else: else:
@@ -210,6 +257,7 @@ while quit!=True:
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if looptype==1: # show alarmtime if looptype==1: # show alarmtime
if not settings["alarmdisabled"]==1:
a_mins=settings['alarmtime']%60 a_mins=settings['alarmtime']%60
a_hour=(settings['alarmtime']-a_mins)/60 a_hour=(settings['alarmtime']-a_mins)/60
@@ -219,15 +267,24 @@ while quit!=True:
tim="%02d %02d" % (a_hour, a_mins) tim="%02d %02d" % (a_hour, a_mins)
showclock(tim) showclock(tim)
colon=1-colon colon=1-colon
if showtime > o_showalarmtime: else:
if but1.WasPressed(): # drop intermediate presses display.ShowDoublepoint(False)
{} display.Show1(0,0x7f);
if but2.WasPressed(): display.Show1(1,0);
{} display.Show1(2,15);
showtime=0 display.Show1(3,15);
looptype=0
else: if showtime > o_showalarmtime:
showtime+=o_loopdelay if but1.WasPressed(): # drop intermediate presses
{}
if but2.WasPressed():
{}
if but3.WasPressed():
{}
showtime=0
looptype=0
else:
showtime+=o_loopdelay
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -245,46 +302,67 @@ while quit!=True:
if but2.WasPressed(): if but2.WasPressed():
delta=1 delta=1
showtime=0 showtime=0
if but3.WasPressed():
delta=-1
showtime=0
if digit==1: if digit==1:
if delta: if delta:
dh+=delta dh+=delta
if dh>2: if dh>2:
dh=0 dh=0
if dh==2 and sh>3: if dh<0:
sh=3 dh=2
tim=" %0d:%0d%0d" % ( sh,dm,sm) tim=" %0d:%0d%0d" % ( sh,dm,sm)
elif digit==2: elif digit==2:
if delta: if delta:
sh+=delta sh+=delta
if sh>9: if sh>9:
sh=0 sh=0
if dh==2 and sh>3: if dh==2 and sh>3:
sh=0 sh=0
if sh<0:
sh=9
tim="%0d :%0d%0d" % (dh, dm,sm) tim="%0d :%0d%0d" % (dh, dm,sm)
elif digit==3: elif digit==3:
if delta: if delta:
dm+=delta dm+=delta
if dm>5: if dm>5:
dm=0 dm=0
if dm<0:
dm=5
tim="%0d%0d: %0d" % (dh,sh, sm) tim="%0d%0d: %0d" % (dh,sh, sm)
elif digit==4: elif digit==4:
if delta: if delta:
sm+=delta sm+=delta
if sm>9: if sm>9:
sm=0 sm=0
if sm<0:
sm=9
tim="%0d%0d:%0d " % (dh,sh,dm ) tim="%0d%0d:%0d " % (dh,sh,dm )
delta=0 delta=0
showclock(tim) showclock(tim)
colon=1-colon colon=1-colon
if showtime > o_showtimeout: if showtime > o_showtimeout:
showtime=0 showtime=0
looptype=0 if dh==2 and sh>3:
sh=3
looptype=1
else:
looptype=0
if settings["alarmdisabled"]==1:
settings["alarmdisabled"]=0
stamp()
print "Alarm enabled"
settings['alarmtime']=dh*600+sh*60+dm*10+sm
writesettings() writesettings()
sys.stdout.write(strftime("%d %b %Y %H:%M:%S : ")) stamp()
print "set alarmtime = %d%d:%d%d"%(dh,sh,dm,sm) print "Set alarmtime = %d%d:%d%d"%(dh,sh,dm,sm)
sys.stdout.flush()
digit=1
else: else:
showtime+=o_loopdelay showtime+=o_loopdelay
@@ -299,12 +377,12 @@ while quit!=True:
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if looptype==3: # show tracks if looptype==3: # show tracks mode
if but1.WasPressed(): if but1.WasPressed():
pygame.mixer.music.set_volume(0)
pygame.mixer.music.stop()
if pygame.mixer.get_init(): if pygame.mixer.get_init():
pygame.mixer.music.set_volume(0)
pygame.mixer.music.stop()
pygame.mixer.quit() pygame.mixer.quit()
soundpreview=0 soundpreview=0
looptype=1 looptype=1
@@ -314,11 +392,13 @@ while quit!=True:
if but2.WasPressed(): if but2.WasPressed():
if settings["track%d"%track]>0: if settings["track%d"%track]>0:
print (allmp3s[settings["track%d"%track]-1]) print (allmp3s[settings["track%d"%track]-1])
sys.stdout.flush()
if not pygame.mixer.get_init(): if not pygame.mixer.get_init():
pygame.mixer.init() pygame.mixer.init()
pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1]) if pygame.mixer.get_init():
pygame.mixer.music.set_volume(1) pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1])
pygame.mixer.music.play() pygame.mixer.music.set_volume(1)
pygame.mixer.music.play()
soundpreview=1 soundpreview=1
tr=track tr=track
@@ -338,8 +418,10 @@ while quit!=True:
if soundpreview: if soundpreview:
if pygame.mixer.music.get_pos() >o_soundpreviewtime*1000: if pygame.mixer.music.get_pos() >o_soundpreviewtime*1000:
pygame.mixer.music.set_volume(0) if pygame.mixer.get_init():
pygame.mixer.music.stop() pygame.mixer.music.set_volume(0)
pygame.mixer.music.stop()
pygame.mixer.quit()
soundpreview=0 soundpreview=0
showtime=0 showtime=0
track+=1; track+=1;
@@ -371,6 +453,9 @@ while quit!=True:
if but2.WasPressed(): if but2.WasPressed():
delta=1 delta=1
showtime=0 showtime=0
if but3.WasPressed():
delta=-1
showtime=0
if digit==1: if digit==1:
if delta: if delta:
@@ -434,14 +519,16 @@ while quit!=True:
colon=1-colon colon=1-colon
if not alarmstarted: if not alarmstarted:
sys.stdout.write(strftime("%d %b %Y %H:%M:%S : "))
track=settings['starttrack']; track=settings['starttrack'];
print "[%d] - %02d %s"%(track,settings["track%d"%track],(allmp3s[settings["track%d"%track]-1])) stamp()
print "[T%02dM%02d] \"%s\""%(track,settings["track%d"%track],(allmp3s[settings["track%d"%track]-1]))
sys.stdout.flush()
if not pygame.mixer.get_init(): if not pygame.mixer.get_init():
pygame.mixer.init() pygame.mixer.init()
pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1]) if pygame.mixer.get_init():
pygame.mixer.music.set_volume(volume) pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1])
pygame.mixer.music.play() pygame.mixer.music.set_volume(volume)
pygame.mixer.music.play()
track+=1 track+=1
if track > 9: if track > 9:
track=0 track=0
@@ -451,22 +538,79 @@ while quit!=True:
writesettings() writesettings()
playtime=0 playtime=0
alarmstarted=1 alarmstarted=1
else: else: # alarmstarted
if playtime < o_timetomaxvol: if playtime < o_timetomaxvol:
brightness=(playtime / o_timetomaxvol)*6 brightness=(playtime / o_timetomaxvol)*6
volume = float(int((playtime*100) / o_timetomaxvol))/100 volume = float(int((playtime*100) / o_timetomaxvol))/100
# print "------> %d / %d => %f - %f"%(playtime,o_timetomaxvol,tvol,volume) # print "------> %d / %d => %f - %f"%(playtime,o_timetomaxvol,tvol,volume)
pygame.mixer.music.set_volume(volume)
else: else:
pygame.mixer.music.set_volume(1) volume=1
if pygame.mixer.get_init():
pygame.mixer.music.set_volume(volume)
playtime+=o_loopdelay playtime+=o_loopdelay
if not pygame.mixer.music.get_busy(): if pygame.mixer.get_init():
if not pygame.mixer.music.get_busy():
track=settings['starttrack']
stamp()
print "[T%02dM%02d] \"%s\""%(track,settings["track%d"%track],(allmp3s[settings["track%d"%track]-1]))
sys.stdout.flush()
pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1])
pygame.mixer.music.play()
track+=1
if track > 9:
track=0
if settings["track%d"%track]==0:
track=0
settings['starttrack']=track;
writesettings()
if playtime > o_maxplaytime:
looptype=0
playtime=0
if pygame.mixer.get_init():
pygame.mixer.music.set_volume(0)
pygame.mixer.music.stop()
pygame.mixer.quit()
display.SetBrightnessRaw(o_defaultbrightness)
alarmstarted=0
if but1.WasPressed():
if but1.WasLong(): # Finished
if pygame.mixer.get_init():
pygame.mixer.music.set_volume(0)
pygame.mixer.music.stop()
pygame.mixer.quit()
display.SetBrightnessRaw(o_defaultbrightness)
playtime=0
if but1.WasPressed(): # flush intermediate presses
{}
if but2.WasPressed():
{}
if but3.WasPressed():
{}
looptype=0
alarmstarted=0
silencedalarm=1
stamp()
print "Silenced alarm"
if but2.WasPressed():
if not but1.WasLong():
if pygame.mixer.get_init():
if pygame.mixer.music.get_busy():
pygame.mixer.music.stop()
track=settings['starttrack'] track=settings['starttrack']
sys.stdout.write(strftime("%d %b %Y %H:%M:%S : ")) stamp()
print "[%d] - %02d %s"%(track,settings["track%d"%track],(allmp3s[settings["track%d"%track]-1])) print "[T%02dM%02d] \"%s\""%(track,settings["track%d"%track],(allmp3s[settings["track%d"%track]-1]))
pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1]) sys.stdout.flush()
pygame.mixer.music.play() if pygame.mixer.get_init():
pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1])
pygame.mixer.music.play()
track+=1 track+=1
if track > 9: if track > 9:
track=0 track=0
@@ -475,31 +619,9 @@ while quit!=True:
settings['starttrack']=track; settings['starttrack']=track;
writesettings() writesettings()
if playtime > o_maxplaytime:
looptype=0
playtime=0
pygame.mixer.music.set_volume(0)
pygame.mixer.music.stop()
if pygame.mixer.get_init():
pygame.mixer.quit()
display.SetBrightnessRaw(o_defaultbrightness)
if but1.WasPressed(): #------------------------------------------------------------------
if but1.WasLong(): # Finished stamp()
pygame.mixer.music.set_volume(0) print "Shutdown!"
pygame.mixer.music.stop() sys.stdout.flush()
if pygame.mixer.get_init():
pygame.mixer.get_init()
display.SetBrightnessRaw(o_defaultbrightness)
playtime=0
if but1.WasPressed(): # drop intermediate presses
{}
if but2.WasPressed():
{}
looptype=0
alarmoff=1
print "\nQuit!"
display.Clear()
display.cleanup() display.cleanup()

View File

@@ -12,6 +12,7 @@ HexDigits = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d,
# 0 1 2 3 4 5 6 # 0 1 2 3 4 5 6
# 7 8 9 A B C D E F = r # 7 8 9 A B C D E F = r
# 10 11 12 13 14 15 16 17
# #
# 0 # 0
# --- # ---
@@ -46,8 +47,6 @@ class TM1637:
def cleanup(self): def cleanup(self):
"""Stop updating clock, turn off display, and cleanup GPIO""" """Stop updating clock, turn off display, and cleanup GPIO"""
self.StopClock()
self.Clear()
IO.cleanup() IO.cleanup()
def Clear(self): def Clear(self):
@@ -212,7 +211,7 @@ class TM1637:
if __name__ == "__main__": if __name__ == "__main__":
"""Confirm the display operation""" """Confirm the display operation"""
display = TM1637(CLK=26, DIO=16, brightness=1.0) display = TM1637(CLK=14, DIO=4, brightness=1.0)
display.Clear() display.Clear()
@@ -254,3 +253,27 @@ if __name__ == "__main__":
display.Show1(3,17); display.Show1(3,17);
# See clock.py for how to use the clock functions! # See clock.py for how to use the clock functions!
display.SetBrightnessRaw(0)
print "level=0"
scrap=raw_input()
display.SetBrightnessRaw(1)
print "level=1"
scrap=raw_input()
display.SetBrightnessRaw(2)
print "level=2"
scrap=raw_input()
display.SetBrightnessRaw(3)
print "level=3"
scrap=raw_input()
display.SetBrightnessRaw(4)
print "level=4"
scrap=raw_input()
display.SetBrightnessRaw(5)
print "level=5"
scrap=raw_input()
display.SetBrightnessRaw(6)
print "level=6"
scrap=raw_input()
display.SetBrightnessRaw(7)
print "level=7"
scrap=raw_input()