Skip to content

Commit b1364eb

Browse files
author
K. Townsend
committed
first commit
1 parent 6ce7ddc commit b1364eb

File tree

6 files changed

+240
-0
lines changed

6 files changed

+240
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/python
2+
3+
import time
4+
import datetime
5+
from Adafruit_LEDBackpack import LEDBackpack
6+
7+
# ===========================================================================
8+
# 7-Segment Display
9+
# ===========================================================================
10+
11+
# This class is meant to be used with the four-character, seven segment
12+
# displays available from Adafruit
13+
14+
class SevenSegment:
15+
disp = None
16+
17+
# Hexadecimal character lookup table (row 1 = 0..9, row 2 = A..F)
18+
digits = [ 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, \
19+
0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 ]
20+
21+
# Constructor
22+
def __init__(self, address=0x70, debug=False):
23+
if (debug):
24+
print "Initializing a new instance of LEDBackpack at 0x%02X" % address
25+
self.disp = LEDBackpack(address=address, debug=debug)
26+
27+
def writeDigitRaw(self, charNumber, value):
28+
"Sets a digit using the raw 16-bit value"
29+
if (charNumber > 7):
30+
return
31+
# Set the appropriate digit
32+
self.disp.setBufferRow(charNumber, value)
33+
34+
def writeDigit(self, charNumber, value, dot=False):
35+
"Sets a single decimal or hexademical value (0..9 and A..F)"
36+
if (charNumber > 7):
37+
return
38+
if (value > 0xF):
39+
return
40+
# Set the appropriate digit
41+
self.disp.setBufferRow(charNumber, self.digits[value] | (dot << 7))
42+
43+
def setColon(self, state=True):
44+
"Enables or disables the colon character"
45+
# Warning: This function assumes that the colon is character '2',
46+
# which is the case on 4 char displays, but may need to be modified
47+
# if another display type is used
48+
if (state):
49+
self.disp.setBufferRow(2, 0xFFFF)
50+
else:
51+
self.disp.setBufferRow(2, 0)
52+

Adafruit_LEDBackpack/Adafruit_8x8.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/python
2+
3+
import time
4+
import datetime
5+
from Adafruit_LEDBackpack import LEDBackpack
6+
7+
# ===========================================================================
8+
# 8x8 Pixel Display
9+
# ===========================================================================
10+
11+
class EightByEight:
12+
disp = None
13+
14+
# Constructor
15+
def __init__(self, address=0x70, debug=False):
16+
if (debug):
17+
print "Initializing a new instance of LEDBackpack at 0x%02X" % address
18+
self.disp = LEDBackpack(address=address, debug=debug)
19+
20+
def writeRowRaw(self, charNumber, value):
21+
"Sets a row of pixels using a raw 16-bit value"
22+
if (charNumber > 7):
23+
return
24+
# Set the appropriate row
25+
self.disp.setBufferRow(charNumber, value)
26+
27+
def clearPixel(self, x, y):
28+
"A wrapper function to clear pixels (purely cosmetic)"
29+
self.setPixel(x, y, 0)
30+
31+
def setPixel(self, x, y, color=1):
32+
"Sets a single pixel"
33+
if (x >= 8):
34+
return
35+
if (y >= 8):
36+
return
37+
x += 7
38+
x %= 8
39+
# Set the appropriate pixel
40+
buffer = self.disp.getBuffer()
41+
if (color):
42+
self.disp.setBufferRow(y, buffer[y] | 1 << x)
43+
else:
44+
self.disp.setBufferRow(y, buffer[y] & ~(1 << x))
45+
46+
def clear(self):
47+
"Clears the entire display"
48+
self.disp.clear()
49+

Adafruit_LEDBackpack/Adafruit_I2C.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Adafruit_I2C/Adafruit_I2C.py
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/python
2+
3+
import time
4+
from copy import copy
5+
from Adafruit_I2C import Adafruit_I2C
6+
7+
# ============================================================================
8+
# LEDBackpack Class
9+
# ============================================================================
10+
11+
class LEDBackpack:
12+
i2c = None
13+
14+
# Registers
15+
__HT16K33_REGISTER_DISPLAY_SETUP = 0x80
16+
__HT16K33_REGISTER_SYSTEM_SETUP = 0x20
17+
__HT16K33_REGISTER_DIMMING = 0xE0
18+
19+
# Blink rate
20+
__HT16K33_BLINKRATE_OFF = 0x00
21+
__HT16K33_BLINKRATE_2HZ = 0x01
22+
__HT16K33_BLINKRATE_1HZ = 0x02
23+
__HT16K33_BLINKRATE_HALFHZ = 0x03
24+
25+
# Display buffer (8x16-bits)
26+
__buffer = [0x0000, 0x0000, 0x0000, 0x0000, \
27+
0x0000, 0x0000, 0x0000, 0x0000 ]
28+
29+
# Constructor
30+
def __init__(self, address=0x70, debug=False):
31+
self.i2c = Adafruit_I2C(address)
32+
self.address = address
33+
self.debug = debug
34+
35+
# Turn the oscillator on
36+
self.i2c.write8(self.__HT16K33_REGISTER_SYSTEM_SETUP | 0x01, 0x00)
37+
38+
# Turn blink off
39+
self.setBlinkRate(self.__HT16K33_BLINKRATE_OFF)
40+
41+
# Set maximum brightness
42+
self.setBrightness(15)
43+
44+
# Clear the screen
45+
self.clear()
46+
47+
def setBrightness(self, brightness):
48+
"Sets the brightness level from 0..15"
49+
if (brightness > 15):
50+
brightness = 15
51+
self.i2c.write8(self.__HT16K33_REGISTER_DIMMING | brightness, 0x00)
52+
53+
def setBlinkRate(self, blinkRate):
54+
"Sets the blink rate"
55+
if (blinkRate > self.__HT16K33_BLINKRATE_HALFHZ):
56+
blinkRate = self.__HT16K33_BLINKRATE_OFF
57+
self.i2c.write8(self.__HT16K33_REGISTER_DISPLAY_SETUP | 0x01 | (blinkRate << 1), 0x00)
58+
59+
def setBufferRow(self, row, value, update=True):
60+
"Updates a single 16-bit entry in the 8*16-bit buffer"
61+
if (row > 7):
62+
return # Prevent buffer overflow
63+
self.__buffer[row] = value # value # & 0xFFFF
64+
if (update):
65+
self.writeDisplay() # Update the display
66+
67+
def getBuffer(self):
68+
"Returns a copy of the raw buffer contents"
69+
bufferCopy = copy(self.__buffer)
70+
return bufferCopy
71+
72+
def writeDisplay(self):
73+
"Updates the display memory"
74+
bytes = []
75+
for item in self.__buffer:
76+
bytes.append(item & 0xFF)
77+
bytes.append((item >> 8) & 0xFF)
78+
self.i2c.writeList(0x00, bytes)
79+
80+
def clear(self, update=True):
81+
"Clears the display memory"
82+
self.__buffer = [ 0, 0, 0, 0, 0, 0, 0, 0 ]
83+
if (update):
84+
self.writeDisplay()
85+
86+
led = LEDBackpack(0x70)
87+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/python
2+
3+
import time
4+
import datetime
5+
from Adafruit_7Segment import SevenSegment
6+
7+
# ===========================================================================
8+
# Clock Example
9+
# ===========================================================================
10+
segment = SevenSegment(address=0x70)
11+
12+
print "Press CTRL+Z to exit"
13+
14+
# Continually update the time on a 4 char, 7-segment display
15+
while(True):
16+
now = datetime.datetime.now()
17+
hour = now.hour
18+
minute = now.minute
19+
second = now.second
20+
# Set hours
21+
segment.writeDigit(0, int(hour / 10)) # Tens
22+
segment.writeDigit(1, hour % 10) # Ones
23+
# Set minutes
24+
segment.writeDigit(3, int(minute / 10)) # Tens
25+
segment.writeDigit(4, minute % 10) # Ones
26+
# Toggle color
27+
segment.setColon(second % 2) # Toggle colon at 1Hz
28+
# Wait one second
29+
time.sleep(1)

Adafruit_LEDBackpack/ex_8x8_pixels.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python
2+
3+
import time
4+
import datetime
5+
from Adafruit_8x8 import EightByEight
6+
7+
# ===========================================================================
8+
# 8x8 Pixel Example
9+
# ===========================================================================
10+
grid = EightByEight(address=0x70)
11+
12+
print "Press CTRL+Z to exit"
13+
14+
# Continually update the 8x8 display one pixel at a time
15+
while(True):
16+
for x in range(0, 8):
17+
for y in range(0, 8):
18+
grid.setPixel(x, y)
19+
time.sleep(0.05)
20+
time.sleep(0.5)
21+
grid.clear()
22+
time.sleep(0.5)

0 commit comments

Comments
 (0)