Skip to content

Commit 87bed18

Browse files
committed
Refine 7-segment backpack to use inverted digits, and filled out clock example
1 parent 33e530e commit 87bed18

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Adafruit_LEDBackpack/Adafruit_7Segment.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313

1414
class SevenSegment:
1515
disp = None
16-
16+
invert = False
17+
1718
# Hexadecimal character lookup table (row 1 = 0..9, row 2 = A..F)
1819
digits = [ 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, \
1920
0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 ]
2021

22+
# The same, but upside-down
23+
idigits = [ 0x3F, 0x30, 0x5B, 0x79, 0x74, 0x6D, 0x6F, 0x38, 0x7F, 0x7D, \
24+
0x7E, 0x67, 0x0F, 0x73, 0x4F, 0x4E ]
25+
2126
# Constructor
2227
def __init__(self, address=0x70, debug=False):
2328
if (debug):
@@ -37,14 +42,23 @@ def writeDigit(self, charNumber, value, dot=False):
3742
return
3843
if (value > 0xF):
3944
return
45+
46+
# Decide which digit set to use: check self.invert
47+
d = self.idigits[value] if self.invert else self.digits[value]
48+
49+
# If inverted, also reverse the character positions
50+
c = (4-charNumber) if self.invert else charNumber
51+
4052
# Set the appropriate digit
41-
self.disp.setBufferRow(charNumber, self.digits[value] | (dot << 7))
53+
self.disp.setBufferRow(c, d | (dot << 7))
4254

4355
def setColon(self, state=True):
4456
"Enables or disables the colon character"
4557
# Warning: This function assumes that the colon is character '2',
4658
# which is the case on 4 char displays, but may need to be modified
4759
# if another display type is used
60+
# This sets all non-digit LEDs. Change (2,0xFFFF) to (2,2) to just
61+
# set colon on the 1.2" 7-Segment display.
4862
if (state):
4963
self.disp.setBufferRow(2, 0xFFFF)
5064
else:

Adafruit_LEDBackpack/ex_7segment_clock.py

100644100755
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
# ===========================================================================
1010
segment = SevenSegment(address=0x70)
1111

12+
# Set segment.invert = True if unit is upside-down
13+
segment.invert = False
14+
15+
# Set brightness between 0 (off) and 15 (full) if you value your retinae
16+
segment.disp.setBrightness(1)
17+
1218
print "Press CTRL+Z to exit"
1319

1420
# Continually update the time on a 4 char, 7-segment display

0 commit comments

Comments
 (0)