-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuitpython_terminalpixels.py
44 lines (35 loc) · 1.29 KB
/
circuitpython_terminalpixels.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
from math import ceil
import sys
class FakePixel:
def __init__(self, n=80, x=1, y=1, rows=1, auto_write=True):
self.n = n
self.bpp = 3
self._x = x
self._y = y
self._rows = rows
self._chars_per_row = n // rows
self.pixels = [(0, 0, 0)] * n
self.auto_write = auto_write
sys.stdout.write('%c[2J' % 27)
def show(self):
sys.stdout.write('%c[s%c[%d;%dH' % (27, 27, self._x, self._y))
for row in range(self._rows):
sys.stdout.write('%c[%d;%dH' % (27, self._y + row, self._x))
sys.stdout.write(
''.join(['%c[48;2;%d;%d;%dm ' % (27, item[0], item[1], item[2])
for item in self.pixels[row * self._chars_per_row:(row+1) * self._chars_per_row]]))
sys.stdout.write('%c[u' % 27)
sys.stdout.flush()
def __setitem__(self, key, value):
if isinstance(value, int):
value = (value >> 16 & 255, value >> 8 & 255, value & 255)
self.pixels[key] = value
if self.auto_write:
self.show()
def __getitem__(self, item):
return self.pixels[item]
def __len__(self):
return len(self.pixels)
def fill(self, color):
for n in range(self.n):
self.__setitem__(n, color)