-
Notifications
You must be signed in to change notification settings - Fork 0
/
neodraw.py
64 lines (56 loc) · 2.4 KB
/
neodraw.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
from coordinates import Cooridnates
from coordinates import PositionOutOfRangeError
from font import Font
from logutil import get_logger
class NeoDraw:
def __init__(self, neopixel, coordiantes : Cooridnates, font:Font = None) -> None: # type: ignore
self.neopixel = neopixel
self.coordiantes = coordiantes
self.font = font
self.logger = get_logger(__name__)
def line(self, x1 : int, y1 : int, x2 : int, y2 : int, rgb) -> None:
deltaX = x2-x1
deltaY = y2-y1
delta = max(abs(deltaX), abs(deltaY))
stepX = deltaX / delta
stepY = deltaY / delta
x = x1
y = y1
self.neopixel.set_pixel(self.coordiantes.cartesianToPostion(x, y), rgb)
for i in range(0, delta):
x += stepX
y += stepY
self.logger.debug('x={0}, y={0}'.format(x,y))
self.neopixel.set_pixel(self.coordiantes.cartesianToPostion(int(x), int(y)), rgb)
def letter(self, x : int, y : int, letter :str, rgb, font: Font = None): # type: ignore
if font==None:
localFont = self.font
else:
localFont = font
if localFont==None:
raise BaseException('font not defined')
self.logger.debug('showing letter '+letter)
topDownBitMapList = localFont[letter]
bottomUpBitMapList = topDownBitMapList[::-1] # reverse
for bitMapRow in range(0, len(bottomUpBitMapList)):
binaryString = localFont.toBinaryString(bottomUpBitMapList[bitMapRow])
self.logger.debug('{}: {}'.format(bitMapRow, binaryString))
for bitPosition in range(0, len(binaryString)):
if (binaryString[bitPosition]=='1'):
try:
xPos = x + bitPosition
yPos = y + bitMapRow
self.neopixel.set_pixel(self.coordiantes.cartesianToPostion(xPos, yPos), rgb)
except PositionOutOfRangeError:
pass
else:
try:
xPos = x + bitPosition
yPos = y + bitMapRow
self.neopixel.set_pixel(self.coordiantes.cartesianToPostion(xPos, yPos), (0,0,0))
except PositionOutOfRangeError:
pass
def clear(self):
self.neopixel.clear()
def show(self):
self.neopixel.show()