-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_8x8.py
75 lines (62 loc) · 2.35 KB
/
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
#!/usr/bin/python
import time
import datetime
from Adafruit_LEDBackpack import LEDBackpack
# ===========================================================================
# 8x8 Pixel Display
# ===========================================================================
class 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 = LEDBackpack(address=address, debug=debug)
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"
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))
def clear(self):
"Clears the entire display"
self.disp.clear()
class ColorEightByEight(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)) )