-
Notifications
You must be signed in to change notification settings - Fork 23
/
sgh_Adafruit_8x8.py
executable file
·108 lines (89 loc) · 3.4 KB
/
sgh_Adafruit_8x8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python
import time
import datetime
from sgh_Adafruit_LEDBackpack import sgh_LEDBackpack
# ===========================================================================
# 8x8 Pixel Display
# Modified by Simon Walters as interface to ScratchGPIO
# ===========================================================================
class sgh_EightByEight:
disp = None
# Constructor
def __init__(self, address=0x70, debug=False):
if (debug):
print "Initializing a new instance of LEDBackpack at 0x%02X" % address
self.disp = sgh_LEDBackpack(address=address, debug=debug)
self.matrix = [0] * 64
def writeRowRaw(self, charNumber, value):
"Sets a row of pixels using a raw 16-bit value"
if (charNumber > 7):
return
# Set the appropriate row
self.disp.setBufferRow(charNumber, value)
def clearPixel(self, x, y):
"A wrapper function to clear pixels (purely cosmetic)"
self.setPixel(x, y, 0)
def setPixel(self, x, y, color=1):
"Sets a single pixel"
self.matrix[(7-x)+(y*8)] = color
if (x >= 8):
return
if (y >= 8):
return
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.
x %= 8
# Set the appropriate pixel
buffer = self.disp.getBuffer()
if (color):
self.disp.setBufferRow(y, buffer[y] | 1 << x)
else:
self.disp.setBufferRow(y, buffer[y] & ~(1 << x))
#print self.matrix
def clear(self):
"Clears the entire display"
self.disp.clear()
self.matrix = [0] * 64
def setBrightness(self, brightness):
"Clears the entire display"
self.disp.setBrightness(brightness)
def scroll(self, direction = "left"):
#print direction
if direction == "left":
for y in range(0,8):
for x in range (0,7):
#print (x+1+(y*8))
self.setPixel(7-x,y,self.matrix[x+1+(y*8)])
x=7
for y in range(0,8):
self.setPixel(7-x,y,0)
if direction == "right":
print "right"
for y in range(0,8):
for x in range (7,0,-1):
#print (x+1+(y*8))
self.setPixel(7-x,y,self.matrix[x-1+(y*8)])
x=0
for y in range(0,8):
self.setPixel(7-x,y,0)
class ColorEightByEight(sgh_EightByEight):
def setPixel(self, x, y, color=1):
"Sets a single pixel"
if (x >= 8):
return
if (y >= 8):
return
x %= 8
# Set the appropriate pixel
buffer = self.disp.getBuffer()
# TODO : Named color constants?
# ATNN : This code was mostly taken from the arduino code, but with the addition of clearing the other bit when setting red or green.
# 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.
# The bug doesn't show up in the examples because it's always clearing.
if (color == 1):
self.disp.setBufferRow(y, (buffer[y] | (1 << x)) & ~(1 << (x+8)) )
elif (color == 2):
self.disp.setBufferRow(y, (buffer[y] | 1 << (x+8)) & ~(1 << x) )
elif (color == 3):
self.disp.setBufferRow(y, buffer[y] | (1 << (x+8)) | (1 << x) )
else:
self.disp.setBufferRow(y, buffer[y] & ~(1 << x) & ~(1 << (x+8)) )