Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit d437263

Browse files
committed
Create Adafruit_8x8.py
1 parent 9f638e2 commit d437263

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

Adafruit_8x8.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/python
2+
# Updated to allow for scrolling text with the Adafruit_GFX.py library
3+
# ColorEightByEight not updated yet.
4+
5+
from Adafruit_LEDBackpack import LEDBackpack
6+
7+
# ===========================================================================
8+
# 8x8 Pixel Display
9+
# ===========================================================================
10+
11+
class EightByEight(LEDBackpack):
12+
#disp = None
13+
rotation = None
14+
15+
# Constructor
16+
def __init__(self, address=0x70, debug=False):
17+
if (debug):
18+
print "Initializing a new instance of LEDBackpack at 0x%02X" % address
19+
LEDBackpack.__init__(self, 8, 8, address, debug)
20+
self.rotation = 0
21+
22+
def writeRowRaw(self, charNumber, value):
23+
#"Sets a row of pixels using a raw 16-bit value"
24+
if (charNumber > 7):
25+
return
26+
# Set the appropriate row
27+
self.setBufferRow(charNumber, value)
28+
29+
def clearPixel(self, x, y):
30+
#"A wrapper function to clear pixels (purely cosmetic)"
31+
self.setPixel(x, y, 0)
32+
33+
def drawPixel(self, x, y, color=1):
34+
#"Sets a single pixel"
35+
if ((y < 0) or (y >= 8)):
36+
return
37+
if ((x < 0) or (x >= 8)):
38+
return
39+
40+
# Added rotation code 4/16/13 - RH
41+
if self.rotation == 1:
42+
x, y = self.swap(x, y)
43+
x = 8 - x - 1
44+
elif self.rotation == 2:
45+
x = 8 - x - 1
46+
y = 8 - y - 1
47+
elif self.rotation == 3:
48+
x, y = self.swap(x, y)
49+
y = 8 - y - 1
50+
51+
x += 7 # ATTN: This might be a bug? On the color matrix, this causes x=0 to draw on the last line instead of the first.
52+
x %= 8
53+
# Set the appropriate pixel
54+
buffer = self.getBuffer()
55+
if (color):
56+
self.setBufferRow(y, buffer[y] | 1 << x, False) # disable update option to determine if writeDisplay() is called when
57+
else: # writing a bunch of pixels. Speeds things up dramatically when scrolling
58+
self.setBufferRow(y, buffer[y] & ~(1 << x), False) # text on multiple matrices. Call writeDisplay() after updating buffer.
59+
60+
# Used to rotate display 4/16/13 - RH
61+
# Corrected for dual output 6/5/13 - RH
62+
def swap(self, x, y):
63+
temp = x
64+
x = y
65+
y = temp
66+
return (x, y)
67+
68+
# Added 6/3/13 - RH
69+
def printMessage(self, text):
70+
for letter in range(0, len(text)):
71+
self.write(text[letter])
72+
if (self.debug):
73+
print(text[letter])
74+
75+
class ColorEightByEight(EightByEight):
76+
def setPixel(self, x, y, color=1):
77+
#"Sets a single pixel"
78+
if (x >= 8):
79+
return
80+
if (y >= 8):
81+
return
82+
83+
x %= 8
84+
85+
# Set the appropriate pixel
86+
buffer = self.disp.getBuffer()
87+
88+
# TODO : Named color constants?
89+
# ATNN : This code was mostly taken from the arduino code, but with the addition of clearing the other bit when setting red or green.
90+
# The arduino code does not do that, and might have the bug where if you draw red or green, then the other color, it actually draws yellow.
91+
# The bug doesn't show up in the examples because it's always clearing.
92+
93+
if (color == 1):
94+
self.disp.setBufferRow(y, (buffer[y] | (1 << x)) & ~(1 << (x+8)) )
95+
elif (color == 2):
96+
self.disp.setBufferRow(y, (buffer[y] | 1 << (x+8)) & ~(1 << x) )
97+
elif (color == 3):
98+
self.disp.setBufferRow(y, buffer[y] | (1 << (x+8)) | (1 << x) )
99+
else:
100+
self.disp.setBufferRow(y, buffer[y] & ~(1 << x) & ~(1 << (x+8)) )

0 commit comments

Comments
 (0)