Changed code to python3, add README.md
This commit is contained in:
48
README.md
Normal file
48
README.md
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
# Clocky
|
||||||
|
|
||||||
|
A minimal, Raspberry Pi based, music playing alarmclock.
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
Clocky is build with python 3 and is using the following libraries:
|
||||||
|
|
||||||
|
[RPi.GPIO](http://sourceforge.net/projects/raspberry-gpio-python/) - handling hardware I/O for the buttons and 7-segment display
|
||||||
|
[Luma.LED_Matrix](https://github.com/rm-hull/luma.led_matrix) - for driving the matrix display
|
||||||
|
[Pygame](https://www.pygame.org/) - for playing audio
|
||||||
|
|
||||||
|
|
||||||
|
Needed to install before running Clocky:
|
||||||
|
|
||||||
|
```
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get upgrade
|
||||||
|
sudo apt-get install python3-pip3
|
||||||
|
sudo apt-get install build-essential python-dev python-pip libfreetype6-dev libjpeg-dev libopenjp2-7 libtiff5
|
||||||
|
|
||||||
|
pip3 install RPI.GPIO
|
||||||
|
pip3 install luma.led_matrix
|
||||||
|
pip3 install pygame
|
||||||
|
```
|
||||||
|
|
||||||
|
### Installing
|
||||||
|
|
||||||
|
A step by step series of examples that tell you how to get a development env running
|
||||||
|
|
||||||
|
Say what the step will be
|
||||||
|
|
||||||
|
```
|
||||||
|
Give the example
|
||||||
|
```
|
||||||
|
|
||||||
|
### Hardware
|
||||||
|
|
||||||
|
Overview of used hardware:
|
||||||
|
|
||||||
|
[Raspberry Pi Zero-W](https://www.raspberrypi.org/products/raspberry-pi-zero-w/)
|
||||||
|
max7219 4*8*8 LED Matrix module
|
||||||
|
tm1637 4*7-Segment dispplay
|
||||||
|
|
||||||
94
button.py
94
button.py
@@ -9,54 +9,54 @@ GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering
|
|||||||
#GPIO.setup(20, GPIO.IN, pull_up_down = GPIO.PUD_UP)
|
#GPIO.setup(20, GPIO.IN, pull_up_down = GPIO.PUD_UP)
|
||||||
|
|
||||||
class Button:
|
class Button:
|
||||||
'Handle button stuff'
|
"""Handle clocky button stuff"""
|
||||||
starttime=0
|
starttime=0
|
||||||
ispushed=0
|
ispushed=0
|
||||||
waspushed=0
|
waspushed=0
|
||||||
presstime=0
|
presstime=0
|
||||||
ignore=0
|
ignore=0
|
||||||
|
|
||||||
def __init__(self, gpio):
|
def __init__(self, gpio):
|
||||||
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)
|
||||||
GPIO.add_event_detect(self.gpio, GPIO.BOTH, callback=self.my_callback)
|
GPIO.add_event_detect(self.gpio, GPIO.BOTH, callback=self.my_callback)
|
||||||
self.presstime=0
|
self.presstime=0
|
||||||
self.starttime=0
|
self.starttime=0
|
||||||
self.presstime=0
|
self.presstime=0
|
||||||
self.waslong=0
|
self.waslong=0
|
||||||
|
|
||||||
def my_callback(self,channel):
|
def my_callback(self,channel):
|
||||||
if not GPIO.input(channel): # button is pressed
|
if not GPIO.input(channel): # button is pressed
|
||||||
# print "[press] on %d"%self.gpio
|
# print "[press] on %d"%self.gpio
|
||||||
self.starttime=time.time()
|
self.starttime=time.time()
|
||||||
self.ispushed=1
|
self.ispushed=1
|
||||||
else: # button released
|
else: # button released
|
||||||
self.presstime=time.time()-self.starttime
|
self.presstime=time.time()-self.starttime
|
||||||
# print "[release] on %d / <pressed> for=%f sconds"%(self.gpio,self.presstime)
|
# print "[release] on %d / <pressed> for=%f sconds"%(self.gpio,self.presstime)
|
||||||
if self.ispushed:
|
if self.ispushed:
|
||||||
self.waspushed=1
|
self.waspushed=1
|
||||||
self.ispushed=0
|
self.ispushed=0
|
||||||
|
|
||||||
def WasPressed(self):
|
def WasPressed(self):
|
||||||
if self.waspushed:
|
if self.waspushed:
|
||||||
self.waspushed=0
|
self.waspushed=0
|
||||||
return 1
|
return 1
|
||||||
elif self.ispushed:
|
elif self.ispushed:
|
||||||
self.presstime=time.time()-self.starttime
|
self.presstime=time.time()-self.starttime
|
||||||
if self.presstime > 2:
|
if self.presstime > 2:
|
||||||
self.ispushed=0
|
self.ispushed=0
|
||||||
self.waspushed=1
|
self.waspushed=1
|
||||||
self.waslong=1
|
self.waslong=1
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def WasLong(self):
|
def WasLong(self):
|
||||||
if self.presstime > 2:
|
if self.presstime > 2:
|
||||||
self.waslong=0
|
self.waslong=0
|
||||||
self.ignore=1
|
self.ignore=1
|
||||||
return 1
|
return 1
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
|
|
||||||
to use the 6x8 font, cat clocky-font.py >>/home/pi/.local/lib/python2.7/site-packages/luma/core/legacy/font.py
|
to use the 6x8 font,
|
||||||
cat clocky-font.py >>/usr/local/lib/python2.7/dist-packages/luma/core/legacy/font.py
|
cat clocky-font.py >>/home/pi/.local/lib/python3.5/site-packages/luma/core/legacy/font.py
|
||||||
|
|
||||||
|
|
||||||
or wherever the luma-core font.py is. This can be found by running python manually:
|
or wherever the luma-core font.py is. This can be found by running python manually:
|
||||||
|
|
||||||
python
|
python3
|
||||||
>>> import from luma.core.legacy.font
|
>>> import luma.core.legacy.font
|
||||||
>>> luma.core.legacy.font
|
>>> luma.core.legacy.font
|
||||||
<module 'luma.core.legacy.font' from '/home/pi/.local/lib/python2.7/site-packages/luma/core/legacy/font.pyc'>
|
<module 'luma.core.legacy.font' from '/home/pi/.local/lib/python3.5/site-packages/luma/core/legacy/font.py'>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
74
clocky.py
74
clocky.py
@@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import pickle
|
import pickle
|
||||||
import os.path
|
import os.path
|
||||||
@@ -17,10 +17,10 @@ looptype=0
|
|||||||
quit=0
|
quit=0
|
||||||
|
|
||||||
|
|
||||||
print "\n +----------+\n | Clocky |\n | v0.1.0 |\n +----------+\n"
|
print("\n +----------+\n | Clocky |\n | v0.2.0 |\n +----------+\n")
|
||||||
|
|
||||||
o_configfilename = "clocky.conf"
|
o_configfilename = "clocky.conf"
|
||||||
o_defaultalarmtime = 60*7+45 # hh*60+mm
|
o_defaultalarmtime = 60*13+24 # 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
|
||||||
@@ -39,7 +39,7 @@ track=0
|
|||||||
|
|
||||||
def signal_handler(signal,frame):
|
def signal_handler(signal,frame):
|
||||||
global quit
|
global quit
|
||||||
print ""
|
print("")
|
||||||
quit=1
|
quit=1
|
||||||
|
|
||||||
def stamp():
|
def stamp():
|
||||||
@@ -92,28 +92,28 @@ display=max7219.MAX7219(brightness=1)
|
|||||||
display.Clear()
|
display.Clear()
|
||||||
display.SetBrightnessRaw(o_defaultbrightness)
|
display.SetBrightnessRaw(o_defaultbrightness)
|
||||||
|
|
||||||
print ""
|
print("")
|
||||||
print "reading mp3s from directory:"
|
print("reading mp3s from directory:")
|
||||||
allmp3s=readmp3list()
|
allmp3s=readmp3list()
|
||||||
if allmp3s==0:
|
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 ""
|
print("")
|
||||||
but1=button.Button(21)
|
but1=button.Button(21)
|
||||||
but2=button.Button(20)
|
but2=button.Button(20)
|
||||||
but3=button.Button(26)
|
but3=button.Button(26)
|
||||||
print ""
|
print("")
|
||||||
|
|
||||||
if not os.path.isfile(o_configfilename):
|
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 }
|
||||||
settings['alarmtime']=o_defaultalarmtime;
|
settings['alarmtime']=o_defaultalarmtime;
|
||||||
@@ -125,7 +125,7 @@ if not os.path.isfile(o_configfilename):
|
|||||||
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)
|
||||||
@@ -133,26 +133,26 @@ 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"%(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 "=>",
|
print("=>",end='')
|
||||||
else:
|
else:
|
||||||
print " ",
|
print(" ",end='')
|
||||||
print "[T%02dM%02d]"%(i,settings["track%d"%i]),
|
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)):
|
||||||
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()
|
stamp()
|
||||||
print "Started"
|
print("Started")
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
#------------------------------------------------------------------
|
#------------------------------------------------------------------
|
||||||
#------------------------------------------------------------------
|
#------------------------------------------------------------------
|
||||||
@@ -182,12 +182,12 @@ while quit!=True:
|
|||||||
if settings["alarmdisabled"]==0:
|
if settings["alarmdisabled"]==0:
|
||||||
settings["alarmdisabled"]=1
|
settings["alarmdisabled"]=1
|
||||||
stamp()
|
stamp()
|
||||||
print "Alarm disabled"
|
print("Alarm disabled")
|
||||||
writesettings()
|
writesettings()
|
||||||
else:
|
else:
|
||||||
settings["alarmdisabled"]=0
|
settings["alarmdisabled"]=0
|
||||||
stamp()
|
stamp()
|
||||||
print "Alarm enabled"
|
print("Alarm enabled")
|
||||||
writesettings()
|
writesettings()
|
||||||
looptype=1
|
looptype=1
|
||||||
else:
|
else:
|
||||||
@@ -204,12 +204,12 @@ while quit!=True:
|
|||||||
if settings["alarmdisabled"]==0:
|
if settings["alarmdisabled"]==0:
|
||||||
settings["alarmdisabled"]=1
|
settings["alarmdisabled"]=1
|
||||||
stamp()
|
stamp()
|
||||||
print "Alarm disabled"
|
print("Alarm disabled")
|
||||||
writesettings()
|
writesettings()
|
||||||
else:
|
else:
|
||||||
settings["alarmdisabled"]=0
|
settings["alarmdisabled"]=0
|
||||||
stamp()
|
stamp()
|
||||||
print "Alarm enabled"
|
print("Alarm enabled")
|
||||||
writesettings()
|
writesettings()
|
||||||
looptype=1
|
looptype=1
|
||||||
|
|
||||||
@@ -320,11 +320,11 @@ while quit!=True:
|
|||||||
if settings["alarmdisabled"]==1:
|
if settings["alarmdisabled"]==1:
|
||||||
settings["alarmdisabled"]=0
|
settings["alarmdisabled"]=0
|
||||||
stamp()
|
stamp()
|
||||||
print "Alarm enabled"
|
print("Alarm enabled")
|
||||||
settings['alarmtime']=dh*600+sh*60+dm*10+sm
|
settings['alarmtime']=dh*600+sh*60+dm*10+sm
|
||||||
writesettings()
|
writesettings()
|
||||||
stamp()
|
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()
|
sys.stdout.flush()
|
||||||
digit=1
|
digit=1
|
||||||
else:
|
else:
|
||||||
@@ -485,7 +485,7 @@ while quit!=True:
|
|||||||
if not alarmstarted:
|
if not alarmstarted:
|
||||||
track=settings['starttrack'];
|
track=settings['starttrack'];
|
||||||
stamp()
|
stamp()
|
||||||
print "[T%02dM%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])))
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
if not pygame.mixer.get_init():
|
if not pygame.mixer.get_init():
|
||||||
pygame.mixer.init()
|
pygame.mixer.init()
|
||||||
@@ -506,7 +506,7 @@ while quit!=True:
|
|||||||
if playtime < o_timetomaxvol:
|
if playtime < o_timetomaxvol:
|
||||||
brightness=int((playtime / o_timetomaxvol)*255)
|
brightness=int((playtime / o_timetomaxvol)*255)
|
||||||
volume = float(int((playtime*100) / o_timetomaxvol))/100
|
volume = float(int((playtime*100) / o_timetomaxvol))/100
|
||||||
# print "------> %d / %d => %f - %f [%d]"%(playtime,o_timetomaxvol,tvol,volume,brightness)
|
# print("------> %d / %d => %f - %f [%d]"%(playtime,o_timetomaxvol,tvol,volume,brightness))
|
||||||
else:
|
else:
|
||||||
volume=1
|
volume=1
|
||||||
|
|
||||||
@@ -519,7 +519,7 @@ while quit!=True:
|
|||||||
if not pygame.mixer.music.get_busy():
|
if not pygame.mixer.music.get_busy():
|
||||||
track=settings['starttrack']
|
track=settings['starttrack']
|
||||||
stamp()
|
stamp()
|
||||||
print "[T%02dM%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])))
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1])
|
pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1])
|
||||||
pygame.mixer.music.play()
|
pygame.mixer.music.play()
|
||||||
@@ -541,7 +541,7 @@ while quit!=True:
|
|||||||
display.SetBrightnessRaw(o_defaultbrightness)
|
display.SetBrightnessRaw(o_defaultbrightness)
|
||||||
alarmstarted=0
|
alarmstarted=0
|
||||||
stamp()
|
stamp()
|
||||||
print "Alarmtime expired"
|
print("Alarmtime expired")
|
||||||
|
|
||||||
if but1.WasPressed():
|
if but1.WasPressed():
|
||||||
if but1.WasLong(): # Finished
|
if but1.WasLong(): # Finished
|
||||||
@@ -561,7 +561,7 @@ while quit!=True:
|
|||||||
alarmstarted=0
|
alarmstarted=0
|
||||||
silencedalarm=1
|
silencedalarm=1
|
||||||
stamp()
|
stamp()
|
||||||
print "Silenced alarm"
|
print("Silenced alarm")
|
||||||
|
|
||||||
|
|
||||||
if but2.WasPressed():
|
if but2.WasPressed():
|
||||||
@@ -572,7 +572,7 @@ while quit!=True:
|
|||||||
|
|
||||||
track=settings['starttrack']
|
track=settings['starttrack']
|
||||||
stamp()
|
stamp()
|
||||||
print "[T%02dM%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])))
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
if pygame.mixer.get_init():
|
if pygame.mixer.get_init():
|
||||||
pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1])
|
pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1])
|
||||||
@@ -594,4 +594,4 @@ if pygame.mixer.get_init():
|
|||||||
display.cleanup()
|
display.cleanup()
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
stamp()
|
stamp()
|
||||||
print "Shutdown!"
|
print("Shutdown!")
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ class TM1637:
|
|||||||
def SetBrightnessRaw(self, briteness):
|
def SetBrightnessRaw(self, briteness):
|
||||||
"""Accepts raw brightness from 0 - 255"""
|
"""Accepts raw brightness from 0 - 255"""
|
||||||
brightness=briteness>>5
|
brightness=briteness>>5
|
||||||
print "BR=",brightness
|
# print("BR=%d",brightness)
|
||||||
if(self.__brightness != brightness):
|
if(self.__brightness != brightness):
|
||||||
self.__brightness = brightness
|
self.__brightness = brightness
|
||||||
# self.Show(self.__currentData)
|
# self.Show(self.__currentData)
|
||||||
|
|||||||
Reference in New Issue
Block a user