Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 205 additions & 0 deletions Adafruit_LEDBackpack/Adafruit_14Segment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#!/usr/bin/python

import time
import datetime
from Adafruit_LEDBackpack import LEDBackpack

# ===========================================================================
# 14-Segment Display
# ===========================================================================

# 11111111111111
# 32 1 2
# 32 2 5 0 2
# 32 5 1 2 2
# 32 6 2 4 2
# 64 128
# 16 2 4 8 4
# 16 0 0 1 4
# 16 4 9 9 4
# 16 8 6 2 4
# 88888888888888 16384



# This class is meant to be used with the four-character, fourteen segment
# displays available from Adafruit

class FourteenSegment:
disp = None

# Hexadecimal character lookup table (row 1 = 0..9, row 2 = A..F)
digits = [ 0x3F, 0x06, 0xDB, 0xCF, 0xE6, 0xED, 0xFD, 0x07, 0xFF, 0xEF, \
0xF7, 0xFC, 0x39, 0xDE, 0x79, 0x71 ]

chars = [
0x1, #
0x2, #
0x4, #
0x8, #
0x10, #
0x20, #
0x40, #
0x80, #
0x100, #
0x200, #
0x400, #
0x800, #
0x1000, #
0x2000, #
0x4000, #
0x8000, #
0x0, #
0x0, #
0x0, #
0x0, #
0x0, #
0x0, #
0x0, #
0x0, #
0x12c9, #
0x15c0, #
0x12f9, #
0xe3, #
0x530, #
0x12c8, #
0x3a00, #
0x1700, #
0x0, #
0x6, # !
0x220, # "
0x12ce, # #
0x12ed, # $
0xc24, # %
0x235d, # &
0x400, # '
0x2400, # (
0x900, # )
0x3fc0, # *
0x12c0, # +
0x800, # ,
0xc0, # -
0x0, # .
0xc00, # /
0xc3f, # 0
0x6, # 1
0xdb, # 2
0x8f, # 3
0xe6, # 4
0x2069, # 5
0xfd, # 6
0x7, # 7
0xff, # 8
0xef, # 9
0x1200, # :
0xa00, # ;
0x2400, # <
0xc8, # =
0x900, # >
0x1083, # ?
0x2bb, # @
0xf7, # A
0x128f, # B
0x39, # C
0x120f, # D
0xf9, # E
0x71, # F
0xbd, # G
0xf6, # H
0x1200, # I
0x1e, # J
0x2470, # K
0x38, # L
0x536, # M
0x2136, # N
0x3f, # O
0xf3, # P
0x203f, # Q
0x20f3, # R
0xed, # S
0x1201, # T
0x3e, # U
0xc30, # V
0x2836, # W
0x2d00, # X
0x1500, # Y
0xc09, # Z
0x39, # [
0x2100, #
0xf, # ]
0xc03, # ^
0x8, # _
0x100, # `
0x1058, # a
0x2078, # b
0xd8, # c
0x88e, # d
0x858, # e
0x71, # f
0x48e, # g
0x1070, # h
0x1000, # i
0xe, # j
0x3600, # k
0x30, # l
0x10d4, # m
0x1050, # n
0xdc, # o
0x170, # p
0x486, # q
0x50, # r
0x2088, # s
0x78, # t
0x1c, # u
0x2004, # v
0x2814, # w
0x28c0, # x
0x200c, # y
0x848, # z
0x949, # {
0x1200, # |
0x2489, # }
0x520, # ~
0x3fff #
]

# Constructor
def __init__(self, address=0x70, debug=False):
if (debug):
print "Initializing a new instance of LEDBackpack at 0x%02X" % address
self.disp = LEDBackpack(address=address, debug=debug)

def writeDigitRaw(self, charNumber, value):
"Sets a digit using the raw 16-bit value"
if (charNumber > 7):
return
# Set the appropriate digit
self.disp.setBufferRow(charNumber, value)

def writeDigit(self, charNumber, value, dot=False):
"Sets a single decimal or hexademical value (0..9 and A..F)"
if (charNumber > 7):
return
if (value > 0xF):
return
# Set the appropriate digit
self.disp.setBufferRow(charNumber, self.digits[value] | (dot << 14))

def writeChar(self, charNumber, value, dot=False):
"Writes an ascii character on the screen"
if (charNumber > 7):
return
# Set the appropriate character
self.disp.setBufferRow(charNumber, self.chars[ord(value)] | (dot << 14))


def setColon(self, state=True):
"Enables or disables the colon character"
# Warning: This function assumes that the colon is character '2',
# which is the case on 4 char displays, but may need to be modified
# if another display type is used
if (state):
self.disp.setBufferRow(2, 0x4000)
else:
self.disp.setBufferRow(2, 0)

43 changes: 43 additions & 0 deletions Adafruit_LEDBackpack/ex_14segment_asciichars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/python

import time
import datetime
import sys
from Adafruit_14Segment import FourteenSegment
from Adafruit_LEDBackpack import LEDBackpack

# ===========================================================================
# Ascii characters enumeration - tests all segments and LCD dimming
# ===========================================================================
lcd = LEDBackpack(address=0x70)
segment = FourteenSegment(address=0x70)
print "Press CTRL+Z to exit"

for pos in range(127):
segment.writeChar(0,chr(pos))
segment.writeChar(1,chr(pos))
segment.writeChar(2,chr(pos))
segment.writeChar(3,chr(pos))
time.sleep(0.3)

byeword="Done "
for pos in range(4):
segment.writeChar(pos,byeword[pos])

# demoes dimming function
for level in range(16):
lcd.setBrightness(level)
time.sleep(0.3)

for level in reversed(range(16)):
lcd.setBrightness(level)
time.sleep(0.3)

# make byeword scroll away
for c in range(5):
segment.writeChar(0,byeword[0])
segment.writeChar(1,byeword[1])
segment.writeChar(2,byeword[2])
segment.writeChar(3,byeword[3])
byeword=byeword[1:]
time.sleep(0.3)
31 changes: 31 additions & 0 deletions Adafruit_LEDBackpack/ex_14segment_clock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/python

import time
import datetime
from Adafruit_14Segment import FourteenSegment

# ===========================================================================
# Clock Example
# ===========================================================================
segment = FourteenSegment(address=0x70)

print "Press CTRL+Z to exit"

# Continually update the time on a 4 char, 14-segment display
while(True):
now = datetime.datetime.now()
hour = now.hour
minute = now.minute
second = now.second
# Set hours
segment.writeDigit(0, int(hour / 10)) # Tens
# Set minutes
segment.writeDigit(2, int(minute / 10)) # Tens
segment.writeDigit(3, minute % 10) # Ones
# Toggle colon
if second % 2:
segment.writeDigit(1, hour % 10, True) # Ones, dot on
else:
segment.writeDigit(1, hour % 10) # Ones, dot off
# Wait one second
time.sleep(1)
99 changes: 99 additions & 0 deletions Adafruit_LEDBackpack/ex_14segment_one_digit_clock.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash
# uses one 14-segment character on LCD BackPack from Adafruit
# to display current system time. accuracy is +-3.75 minutes
#
# time display is implemented as follows:
# inner segments indicates direction of a longer hand on an analog clock
# each outer segment lit counts for 2 hours, if a decimal dot is lit it adds another one hour
# furthermore outer segments are enabled in such a way to resemble the shape of a respective number
#
# example:
#
# all segments off
# :-------:
# : : : : :
# : ::: :
# --- ---
# : ::: :
# : : : : :
# :-------: :
#
# 7 hours (am or pm) and around 7.5 minutes
# :ooooooo:
# : : : o o
# : ::o o
# --- ---
# : ::: o
# : : : : o
# :-------: o
#
# around quarter to 1
# :-------:
# : : : : :
# : ::: :
# ooo ---
# : ::: :
# : : : : :
# :-------: :
#
# when started without arguments it displays the current time. argument "demo" starts a continous cycle
# where in each iteration additional minute is added to time and shown on both stdout and LCD.
# this can give you an better idea how indication of time works.


#trap 'echo -e "\e0;$BASH_COMMAND\007"' DEBUG
address=0x70
bus=0
# which character to use (counted from 0)
pos=0

digitsmin=(512 1536 1024 1152 128 8320 8192 12288 4096 6144 2048 2112 64 320 256 768 768)
digitshrs=(0 16384 1 16385 36 16417 56 16391 51 16423 59 16443 63)

# set brightness to low
sudo i2cset -y ${bus} "${address}" 0xe0

demo=0
min=1
if [ "$1" = "demo" ]
then
echo "demo mode set"
demo=1
fi

echo "Clock invoked"
while :
do
if [ "${demo}" -eq 1 ]
then
DATEARGS="-d now + ${min} minutes"
read hours minutes seconds < <(date "$DATEARGS" '+%I %M %S' | sed 's/\b0//g')
echo "displaying $hours:$minutes:$seconds"
min=$((min+1))
else
read hours minutes seconds < <(date '+%I %M %S' | sed 's/\b0//g')
fi

if [ ${minutes} -gt 58 ]
then
minutes=0
fi

# better accuracy now
minutes=$(perl -e 'print '$minutes'+'$seconds'/60')
indexm=$(perl -e 'print int((('${minutes}'-1.875)/3.75+1))')

maskm=${digitsmin[$indexm]}
maskh=${digitshrs[$hours]}
mask=$((maskm | maskh))
if [ "${mask}" != "${lastmask}" ]
then
sudo i2cset -y ${bus} "${address}" $((${pos}*2)) ${mask} w
lastmask=$mask
fi

if [ "${demo}" -ne 1 ]
then
break
fi
done
Loading