v0.1.0: added max7219 support, added signal handling

This commit is contained in:
Jan
2019-09-26 10:17:02 +02:00
parent 930a517982
commit 52a0af1d91
6 changed files with 716 additions and 192 deletions

181
tm1637.py
View File

@@ -2,22 +2,21 @@ import math
import RPi.GPIO as IO
import threading
from time import sleep, localtime
# from tqdm import tqdm
# IO.setwarnings(False)
IO.setmode(IO.BCM)
HexDigits = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d,
0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x49, 0x50 ]
0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x49, 0x50, 0x40]
# 0 1 2 3 4 5 6
# 7 8 9 A B C D E F = r
# 10 11 12 13 14 15 16 17
# 7 8 9 A B C D E F = r -
# 10 11 12 13 14 15 16 17 18
#
# 0
# ---
# | |
# 5 | 7 | 1
# 5 | 6 | 1
# ---
# | |
# 4 | | 2
@@ -31,6 +30,36 @@ STARTADDR = 0xC0
# DEBUG = False
# return array value of a certain character
def s2c(s):
if s>='0' and s<='9':
return(int(s))
elif s=='A':
return(10)
elif s=='B':
return(11)
elif s=='C':
return(12)
elif s=='D':
return(13)
elif s=='E':
return(14)
elif s=='F':
return(15)
elif s=='=':
return(16)
elif s=='r':
return(17)
elif s=='-':
return(18)
elif s=='O':
return(0)
else:
return(0x7f)
class TM1637:
__doublePoint = False
__Clkpin = 0
@@ -47,6 +76,7 @@ class TM1637:
def cleanup(self):
"""Stop updating clock, turn off display, and cleanup GPIO"""
self.showclock(" ")
IO.cleanup()
def Clear(self):
@@ -60,11 +90,18 @@ class TM1637:
self.__brightness = b
self.__doublePoint = point
def ShowInt(self, i):
s = str(i)
self.Clear()
for i in range(0, len(s)):
self.Show1(i, int(s[i]))
def showclock(self,tstr):
"Show da clock"
if tstr[2]==':':
self.ShowDoublepoint(True)
else:
self.ShowDoublepoint(False)
self.Show1(0,s2c(tstr[0]))
self.Show1(1,s2c(tstr[1]))
self.Show1(2,s2c(tstr[3]))
self.Show1(3,s2c(tstr[4]))
def Show(self, data):
for i in range(0, 4):
@@ -96,21 +133,13 @@ class TM1637:
self.writeByte(0x88 + int(self.__brightness))
self.stop()
def SetBrightness(self, percent):
"""Accepts percent brightness from 0 - 1"""
max_brightness = 7.0
brightness = math.ceil(max_brightness * percent)
if (brightness < 0):
brightness = 0
def SetBrightnessRaw(self, briteness):
"""Accepts raw brightness from 0 - 255"""
brightness=briteness>>5
print "BR=",brightness
if(self.__brightness != brightness):
self.__brightness = brightness
self.Show(self.__currentData)
def SetBrightnessRaw(self, brightness):
"""Accepts raw brightness from 0 - 7"""
if(self.__brightness != brightness):
self.__brightness = brightness
self.Show(self.__currentData)
# self.Show(self.__currentData)
def ShowDoublepoint(self, on):
"""Show or hide double point divider"""
@@ -171,109 +200,3 @@ class TM1637:
else:
data = HexDigits[data] + pointData
return data
def clock(self, military_time):
"""Clock script modified from:
https://github.com/johnlr/raspberrypi-tm1637"""
self.ShowDoublepoint(True)
while (not self.__stop_event.is_set()):
t = localtime()
hour = t.tm_hour
if not military_time:
hour = 12 if (t.tm_hour % 12) == 0 else t.tm_hour % 12
d0 = hour // 10 if hour // 10 else 0
d1 = hour % 10
d2 = t.tm_min // 10
d3 = t.tm_min % 10
digits = [d0, d1, d2, d3]
self.Show(digits)
# # Optional visual feedback of running alarm:
# print digits
# for i in tqdm(range(60 - t.tm_sec)):
for i in range(60 - t.tm_sec):
if (not self.__stop_event.is_set()):
sleep(1)
def StartClock(self, military_time=True):
# Stop event based on: http://stackoverflow.com/a/6524542/3219667
self.__stop_event = threading.Event()
self.__clock_thread = threading.Thread(
target=self.clock, args=(military_time,))
self.__clock_thread.start()
def StopClock(self):
try:
print 'Attempting to stop live clock'
self.__stop_event.set()
except:
print 'No clock to close'
if __name__ == "__main__":
"""Confirm the display operation"""
display = TM1637(CLK=14, DIO=4, brightness=1.0)
display.Clear()
digits = [1, 2, 3, 4]
display.Show(digits)
print "1234 - Working? (Press Key)"
scrap = raw_input()
print "Updating one digit at a time:"
display.Clear()
display.Show1(1, 3)
sleep(0.5)
display.Show1(2, 2)
sleep(0.5)
display.Show1(3, 1)
sleep(0.5)
display.Show1(0, 4)
print "4321 - (Press Key)"
scrap = raw_input()
print "Add double point\n"
display.ShowDoublepoint(True)
sleep(0.2)
print "Brightness Off"
display.SetBrightness(0)
sleep(0.5)
print "Full Brightness"
display.SetBrightness(1)
sleep(0.5)
print "30% Brightness"
display.SetBrightness(0.3)
sleep(0.3)
display.ShowDoublepoint(False)
display.Show1(0,0x7f);
display.Show1(1,14);
display.Show1(2,17);
display.Show1(3,17);
# 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()