This is my first pass so I'd appreciate any suggestions for cleaning up the code or places where I might not have found the inevitable bugs.
These should be put in the Adafruit_LEDBackpack directory
I called this 'DigitWriteRaw.py'
Code: Select all
#!/usr/bin/python
from Adafruit_7Segment import SevenSegment
# Class to allow writing of Raw characters to 7 Segment backpack without having to hardcode values
# List of allowable characters. Case matters
# A b C d E F G H h I i J L n O o P q r S t u U y _ - : . 0 1 2 3 4 5 6 7 8 9
# Invalid chars should cause the display to show 'Err'
# To this function pass - I2C Address , string , colon flag (1=on , 0=off)
# If you want the "dot" on after a char pass it in the string after the number e.g 'hEL.o' will fill in the dot next to L
# There is no checking for too long strings yet, but sure to pass only 4 chars (not including the dots)
class DigitRawWrite:
sevseg_rawdict = {'A': 119, 'b': 124, 'C': 57, 'd': 94, 'E': 121, 'F': 113, 'G': 125, 'H': 118, 'h': 116, 'I': 6, 'i': 4, 'J': 30, 'L': 56, 'n': 84, 'O': 63, 'o': 92, 'P': 115, 'q': 103, 'r': 80, 'S': 109, 't': 70, 'u': 28, 'U': 62, 'y': 110, '-': 64, '_': 8, '-': 1, ':': 255, '.': 128, ' ': 0, 0: 63, 1: 6, 2: 91, 3: 79, 4: 102, 5: 109, 6: 125, 7: 7, 8: 127, 9: 103}
def Output_7seg(self, addr, d_str, colon):
segment = SevenSegment(address=addr)
ctr = 0
dict_error = 0
seglist = list()
for c in d_str:
if c == '.':
seglist[ctr - 1] = seglist[ctr - 1] + 128
else:
try:
index_c = int(c)
except ValueError:
index_c = c
if index_c not in self.sevseg_rawdict:
dict_error = 1
else:
seglist.append(self.sevseg_rawdict[index_c])
ctr = ctr + 1
if dict_error:
seglist = (121,80,80,0)
ctr = 4
offset = 5 - len(seglist)
while ctr >= 0:
if ctr == 2:
ctr = ctr - 1
offset = offset - 1
else:
segment.writeDigitRaw(ctr, seglist[ctr - offset])
ctr = ctr - 1
segment.setColon(colon)
Code: Select all
#!/usr/bin/python
import time
import datetime
from DigitRawWrite import DigitRawWrite
drw = DigitRawWrite()
print "Press CTRL+Z to exit"
# Continually update the display with "hello" and time
while(True):
o_str = 'hEL.o'
drw.Output_7seg(0x72, o_str, 1)
time.sleep(2)
now = datetime.datetime.now()
hour = now.hour
minute = now.minute
timeseg = str(int(hour/10)) + str(hour % 10) + str(int(minute / 10)) + str(minute % 10)
drw.Output_7seg(0x72, timeseg, 1)
time.sleep(2)