Changed code to python3, add README.md

This commit is contained in:
Jan
2019-09-26 13:41:04 +02:00
parent 52a0af1d91
commit 77c06ee493
5 changed files with 140 additions and 91 deletions

48
README.md Normal file
View 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

View File

@@ -9,54 +9,54 @@ GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering
#GPIO.setup(20, GPIO.IN, pull_up_down = GPIO.PUD_UP)
class Button:
'Handle button stuff'
starttime=0
ispushed=0
waspushed=0
presstime=0
ignore=0
"""Handle clocky button stuff"""
starttime=0
ispushed=0
waspushed=0
presstime=0
ignore=0
def __init__(self, gpio):
print "initialize button on gpio %d"%gpio
self.gpio = gpio
GPIO.setup(self.gpio, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(self.gpio, GPIO.BOTH, callback=self.my_callback)
self.presstime=0
self.starttime=0
self.presstime=0
self.waslong=0
def my_callback(self,channel):
if not GPIO.input(channel): # button is pressed
# print "[press] on %d"%self.gpio
self.starttime=time.time()
self.ispushed=1
else: # button released
self.presstime=time.time()-self.starttime
# print "[release] on %d / <pressed> for=%f sconds"%(self.gpio,self.presstime)
if self.ispushed:
self.waspushed=1
self.ispushed=0
def __init__(self, gpio):
print("initialize button on gpio %d" %gpio)
self.gpio = gpio
GPIO.setup(self.gpio, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(self.gpio, GPIO.BOTH, callback=self.my_callback)
self.presstime=0
self.starttime=0
self.presstime=0
self.waslong=0
def my_callback(self,channel):
if not GPIO.input(channel): # button is pressed
# print "[press] on %d"%self.gpio
self.starttime=time.time()
self.ispushed=1
else: # button released
self.presstime=time.time()-self.starttime
# print "[release] on %d / <pressed> for=%f sconds"%(self.gpio,self.presstime)
if self.ispushed:
self.waspushed=1
self.ispushed=0
def WasPressed(self):
if self.waspushed:
self.waspushed=0
return 1
elif self.ispushed:
self.presstime=time.time()-self.starttime
if self.presstime > 2:
self.ispushed=0
self.waspushed=1
self.waslong=1
else:
return 0
def WasPressed(self):
if self.waspushed:
self.waspushed=0
return 1
elif self.ispushed:
self.presstime=time.time()-self.starttime
if self.presstime > 2:
self.ispushed=0
self.waspushed=1
self.waslong=1
else:
return 0
def WasLong(self):
if self.presstime > 2:
self.waslong=0
self.ignore=1
return 1
else:
return 0
def WasLong(self):
if self.presstime > 2:
self.waslong=0
self.ignore=1
return 1
else:
return 0

View File

@@ -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
cat clocky-font.py >>/usr/local/lib/python2.7/dist-packages/luma/core/legacy/font.py
to use the 6x8 font,
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:
python
>>> import from luma.core.legacy.font
python3
>>> import 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'>

View File

@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3
import pickle
import os.path
@@ -17,10 +17,10 @@ looptype=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_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_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
@@ -39,7 +39,7 @@ track=0
def signal_handler(signal,frame):
global quit
print ""
print("")
quit=1
def stamp():
@@ -92,28 +92,28 @@ display=max7219.MAX7219(brightness=1)
display.Clear()
display.SetBrightnessRaw(o_defaultbrightness)
print ""
print "reading mp3s from directory:"
print("")
print("reading mp3s from directory:")
allmp3s=readmp3list()
if allmp3s==0:
print "ERROR: No mp3's found!"
display.showclock(" E rr")
sleep(30)
print("ERROR: No mp3's found!")
display.showclock(" E rr")
sleep(30)
sys.exit()
c=0
for m in allmp3s:
print " %2d : %s"%(c+1,m)
print(" %2d : %s"%(c+1,m))
c+=1
print ""
print("")
but1=button.Button(21)
but2=button.Button(20)
but3=button.Button(26)
print ""
print("")
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['alarmtime']=o_defaultalarmtime;
@@ -125,7 +125,7 @@ if not os.path.isfile(o_configfilename):
handle.close()
else:
print "Configfile: \""+o_configfilename+"\":"
print("Configfile: \""+o_configfilename+"\":")
with open(o_configfilename, 'rb') as handle:
settings = pickle.load(handle)
@@ -133,26 +133,26 @@ else:
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'])
print("Alarmtime : %02d:%02d"%(a_hour,a_mins))
print("Alarmdisable: %d"%(settings['alarmdisabled']))
print("Starttrack : %d"%(settings['starttrack']))
for i in range(0,10):
if i==settings['starttrack']:
print "=>",
print("=>",end='')
else:
print " ",
print "[T%02dM%02d]"%(i,settings["track%d"%i]),
print(" ",end='')
print(" [T%01dM%02d] "%(i,settings["track%d"%i]),end='')
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:
print ""
print("")
settings["track%d"%i]=0
writesettings()
print ""
print("")
stamp()
print "Started"
print("Started")
sys.stdout.flush()
#------------------------------------------------------------------
#------------------------------------------------------------------
@@ -182,12 +182,12 @@ while quit!=True:
if settings["alarmdisabled"]==0:
settings["alarmdisabled"]=1
stamp()
print "Alarm disabled"
print("Alarm disabled")
writesettings()
else:
settings["alarmdisabled"]=0
stamp()
print "Alarm enabled"
print("Alarm enabled")
writesettings()
looptype=1
else:
@@ -204,12 +204,12 @@ while quit!=True:
if settings["alarmdisabled"]==0:
settings["alarmdisabled"]=1
stamp()
print "Alarm disabled"
print("Alarm disabled")
writesettings()
else:
settings["alarmdisabled"]=0
stamp()
print "Alarm enabled"
print("Alarm enabled")
writesettings()
looptype=1
@@ -320,11 +320,11 @@ while quit!=True:
if settings["alarmdisabled"]==1:
settings["alarmdisabled"]=0
stamp()
print "Alarm enabled"
print("Alarm enabled")
settings['alarmtime']=dh*600+sh*60+dm*10+sm
writesettings()
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:
@@ -485,7 +485,7 @@ while quit!=True:
if not alarmstarted:
track=settings['starttrack'];
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()
if not pygame.mixer.get_init():
pygame.mixer.init()
@@ -506,7 +506,7 @@ while quit!=True:
if playtime < o_timetomaxvol:
brightness=int((playtime / o_timetomaxvol)*255)
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:
volume=1
@@ -519,7 +519,7 @@ while quit!=True:
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]))
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()
@@ -541,7 +541,7 @@ while quit!=True:
display.SetBrightnessRaw(o_defaultbrightness)
alarmstarted=0
stamp()
print "Alarmtime expired"
print("Alarmtime expired")
if but1.WasPressed():
if but1.WasLong(): # Finished
@@ -561,7 +561,7 @@ while quit!=True:
alarmstarted=0
silencedalarm=1
stamp()
print "Silenced alarm"
print("Silenced alarm")
if but2.WasPressed():
@@ -572,7 +572,7 @@ while quit!=True:
track=settings['starttrack']
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()
if pygame.mixer.get_init():
pygame.mixer.music.load(allmp3s[settings["track%d"%track]-1])
@@ -594,4 +594,4 @@ if pygame.mixer.get_init():
display.cleanup()
sys.stdout.flush()
stamp()
print "Shutdown!"
print("Shutdown!")

View File

@@ -136,7 +136,7 @@ class TM1637:
def SetBrightnessRaw(self, briteness):
"""Accepts raw brightness from 0 - 255"""
brightness=briteness>>5
print "BR=",brightness
# print("BR=%d",brightness)
if(self.__brightness != brightness):
self.__brightness = brightness
# self.Show(self.__currentData)