forked from 20after4/micropython-esp32-wrover-lcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.py
154 lines (122 loc) · 3.84 KB
/
widgets.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import colors
import framebuf
from framebuf import FrameBuffer
from array import array
class Area(object):
def __init__(self, display, rows, cols, fg=colors.RGB_WHITE, bg=colors.RGB_BLACK, border=colors.RGB_RED, padding=4, x=0, y=0): # noqa: E501
self.buffer = None
self.fb = None
self.display = display
self.padding = padding
self.bg = bg
self.fg = fg
self.border = border
self.move(x, y)
self.resize(cols, rows)
if hasattr(self, 'init'):
self.init()
def init_fb(self):
if self.buffer is None:
self.buffer = bytearray(self.width * self.height * 2)
if self.fb is None:
self.fb = FrameBuffer(self.buffer, self.width, self.height, framebuf.RGB565) # noqa: E501
return self.fb
def free_fb(self):
self.buffer = None
self.fb = None
def move(self, x, y):
self.x = x
self.y = y
def resize(self, cols, rows):
pad = 1 + self.padding * 2
self.rows = rows
self.cols = cols
self.width = cols * 8 + pad
self.height = (rows * 8) + pad
self.dirty = True
def paint(self):
fb = self.init_fb()
fb.fill(self.bg)
fb.rect(0, 0, self.width, self.height, self.border)
def blit(self):
self.fb = None
self.display.blit_buffer(self.buffer, self.x, self.y, self.width, self.height) # noqa: E501
self.buffer = None
class Point(object):
x = 0
y = 0
v = 0
def __init__(self, val):
self.v = val
class Graph(Area):
def init(self):
self.scale_x = 8
self.scale_y = 8
self.points = array('B', [])
def point(self, v):
content_width = len(self.points) * self.scale_x
window_width = self.width - 2 - self.padding * 2
if (content_width >= window_width):
self.points = self.points[1:]
if v > self.scale_y:
self.scale_y = int(v) + 1
self.points.append(v)
self.dirty = True
def paint(self):
if not self.dirty:
return
super(Graph, self).paint()
prev = None
pad = 1 + self.padding
x = 0
for v in self.points:
point = Point(v)
point.x = pad + x * self.scale_x
v = point.v if point.v >= 1 else 1
point.y = (self.height / self.scale_y) * (self.scale_y / v)
point.y = round(self.height - point.y)
point.y += self.padding + 1
x += 1
if (prev):
self.fb.line(prev.x, prev.y, point.x, point.y, self.fg)
prev = point
self.blit()
self.dirty = False
class TextArea(Area):
def init(self):
self.lines = []
def text(self, txt):
self.lines = txt.splitlines()
self.dirty = True
self.paint()
def append(self, text):
self.dirty = True
if isinstance(text, str):
lines = text.splitlines()
elif isinstance(text, list):
lines = text
else:
lines = str(text).splitlines()
for line in lines:
self.lines.append(line)
scrollback = self.rows * 2
if len(self.lines) > scrollback:
self.lines = self.lines[-scrollback:]
def paint(self, skip_lines=None):
if not self.dirty:
return
super(TextArea, self).paint()
pad = 1 + self.padding
y = pad
lines = self.lines
if skip_lines:
lines = lines[skip_lines - 1:]
if len(lines) > self.rows:
lines = lines[-self.rows:]
for line in lines:
if len(line) > self.cols:
line = line[0:self.cols - 1]
self.fb.text(line, pad, y, self.fg)
y += 8
self.blit()
self.dirty = False