-
Notifications
You must be signed in to change notification settings - Fork 0
/
syna.py
207 lines (149 loc) · 5.83 KB
/
syna.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
##################################################
## syna.py
## Menu assistant for SSD1306 Displays
## on ESP32 and ESP8266
##################################################
## Author: Dustin Kröger
## Copyright: Copyright 2021, Lipurd
## License: MIT License
## Version: 0.2.0
##################################################
from micropython import const
HEADLINE_MARGIN = const(16)
class SynaInterface:
def __init__(self, display, headline = None, parent = None):
self.headline = headline
self.parent = parent
self.display = display
# set headline
if self.headline:
self.topmargin = HEADLINE_MARGIN
else:
self.topmargin = 0
pass
def show(self):
pass
def up(self):
pass
def down(self):
pass
def click(self):
pass
class Syna(SynaInterface):
def __init__(self, display):
self.display = display
self.views = {}
self.view = None
def addMenu(self, identifier, items, headline = None, parent = None):
self.views[identifier] = Menu(self.display, items, headline, parent)
self.views[identifier].identifier = identifier
def addView(self, identifier, view, headline = None, parent = None):
self.views[identifier] = view(self.display, headline, parent)
self.views[identifier].identifier = identifier
def show(self, identifier):
self.view = self.views[identifier]
self.view.show()
def click(self):
# check if menu with list as item
if isinstance(self.view, Menu):
if isinstance(self.view.items[self.view.selected], list):
itemstr = self.view.items[self.view.selected][1]
if itemstr[:1] == '@':
self.show(itemstr[1:])
else:
self.view.click()
def down(self):
self.view.down()
def up(self):
self.view.up()
class Menu(SynaInterface):
def __init__(self, display, items, headline = None, parent = None):
super().__init__(display, headline, parent)
self.items = items
# add back to parent
if self.parent:
self.items.append(['back', '@%s' % self.parent])
# set maximum number of items per page
self.pagebreak = int((self.display.height - self.topmargin) / 10 )
self.selected = 0
self.page = 0
self.nextpagestr = None
def show(self):
"""Display the menu items on the current page"""
# clear screen
self.display.fill(0)
# print headline if set (margin gets set by init)
# also reset margin if value changed after init
if self.headline:
self.topmargin = HEADLINE_MARGIN
self.display.text(self.headline, 0, 0)
else:
self.topmargin = 0
# add menu items per page
for pos in range(self.page * self.pagebreak, len(self.items)):
# print menu item with the current "on page position" + 1 + headline margin
self.display.text(self._itemtext(pos), 1, self.topmargin + (pos - (self.page * self.pagebreak))*10 + 1)
# check if next item would be out of bound
if (pos - (self.page * self.pagebreak) + 1) >= self.pagebreak:
# print dots or other str in last row
if self.nextpagestr:
self.display.text(self.nextpagestr, 1, self.topmargin + self.pagebreak*10 + 1)
break
# highlight current item
self._select()
self.display.show()
def _select(self):
"""Highlight the current 'on page position' menu item"""
self.display.framebuf.fill_rect(0, self.topmargin + (self.selected - (self.page * self.pagebreak))*10, self.display.width, 9, 1)
self.display.text(self._itemtext(self.selected), 1, self.topmargin + (self.selected - (self.page * self.pagebreak))*10 + 1, 0)
def _deslect(self):
"""Reset the current 'on page position' menu item"""
self.display.framebuf.fill_rect(0, self.topmargin + (self.selected - (self.page * self.pagebreak))*10, self.display.width, 9, 0)
self.display.text(self._itemtext(self.selected), 1, self.topmargin + (self.selected - (self.page * self.pagebreak))*10 + 1, 1)
def _itemtext(self, pos):
if isinstance(self.items[pos], list):
return str(self.items[pos][0])
return str(self.items[pos])
def click(self):
"""Click on a menu item"""
# abort if no call specified
if not isinstance(self.items[self.selected], list) or len(self.items[self.selected]) != 2:
return
eval(self.items[self.selected][1])
def down(self):
"""Navigate down in the menu"""
self._deslect()
if (self.selected + 1) == len(self.items):
self.selected = 0
self.page = 0
self.show()
return
else:
self.selected += 1
if self.selected == ((self.page + 1) * self.pagebreak):
self.page += 1
self.show()
return
self._select()
self.display.show()
def up(self):
"""Navigate up in the menu"""
self._deslect()
# check if already the top item, if yes, set to last
if self.selected is 0:
# set to last page
self.page = int((len(self.items) -1) / self.pagebreak)
# set to last item
self.selected = (len(self.items) - 1)
self.show()
return
# check if current item is top on page and is changing to the prior page
elif self.selected == (self.page * self.pagebreak):
self.page -= 1
self.selected -= 1
self.show()
return
else:
self.selected -= 1
self._select()
self.display.show()