-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.py
299 lines (240 loc) · 7.98 KB
/
common.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
Common
Common functions shared accross all Nextion elements
Check the description of Mixin
https://realpython.com/lessons/multiple-inheritance-python/
"""
# system packages
from time import sleep
class CommonError(Exception):
"""Base class for exceptions in this module."""
pass
class Common(object):
"""docstring for Common"""
def __init__(self, nh, pid: int, cid: int, name: str) -> None:
"""
Init gauge
:param nh: The Nextion hardware interface object
:type nh: NexHardware
:param pid: The page ID
:type pid: int
:param cid: The component ID
:type cid: int
:param name: The component name
:type name: str
"""
self._pid = pid
self._cid = cid
self._name = name
self._nh = nh
@property
def pid(self) -> int:
"""
Get page ID of element
:returns: Page ID
:rtype: int
"""
return self._pid
@property
def cid(self) -> int:
"""
Get component ID of element
:returns: Component ID
:rtype: int
"""
return self._cid
@property
def name(self) -> str:
"""
Get name of element
:returns: Name of element
:rtype: str
"""
return self._name
def hide(self) -> bool:
"""
Hide component on screen
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "vis {},0".format(self.name)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
def show(self) -> bool:
"""
Show component on screen
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "vis {},1".format(self.name)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
class CommonBackgroundColorMixin(object):
"""docstring for CommonBackgroundColorMixin"""
def Get_background_color_bco(self) -> int:
"""
Get background color attribute of component
:returns: The background color
:rtype: int
"""
cmd = "get {}.bco".format(self.name)
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetNumber()
def Set_background_color_bco(self, number: int) -> bool:
"""
Set background color attribute of component
:param number: The background color number
:type number: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "{}.bco={}".format(self.name, number)
self._nh.sendCommand(cmd)
cmd = "ref {}".format(self.name)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
class CommonFontMixin(object):
"""docstring for CommonFontMixin"""
def Get_font_color_pco(self) -> int:
"""
Get font color attribute of component
:returns: The font color
:rtype: int
"""
cmd = "get {}.pco".format(self.name)
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetNumber()
def Set_font_color_pco(self, number: int) -> bool:
"""
Set font color attribute of component
:param number: The font color number
:type number: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "{}.pco={}".format(self.name, number)
self._nh.sendCommand(cmd)
cmd = "ref {}".format(self.name)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
def getFont(self) -> int:
"""
Get font attribute of component
:returns: The font color
:rtype: int
"""
cmd = "get {}.font".format(self.name)
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetNumber()
def setFont(self, number: int) -> bool:
"""
Set font attribute of component
:param number: The font number
:type number: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "{}.font={}".format(self.name, number)
self._nh.sendCommand(cmd)
cmd = "ref {}".format(self.name)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
class CommonPositionMixin(object):
"""docstring for CommonPositionMixin"""
def Get_place_xcen(self) -> int:
"""
Get xcen attribute of component
:returns: The x position
:rtype: int
"""
cmd = "get {}.xcen".format(self.name)
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetNumber()
def Set_place_xcen(self, number: int) -> bool:
"""
Get xcen attribute of component
:param number: The new x position
:type number: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "{}.xcen={}".format(self.name, number)
self._nh.sendCommand(cmd)
cmd = "ref {}".format(self.name)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
def Get_place_ycen(self) -> int:
"""
Get ycen attribute of component
:returns: The y position
:rtype: int
"""
cmd = "get {}.ycen".format(self.name)
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetNumber()
def Set_place_ycen(self, number: int) -> bool:
"""
Get ycen attribute of component
:param number: The new y position
:type number: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "{}.ycen={}".format(self.name, number)
self._nh.sendCommand(cmd)
cmd = "ref {}".format(self.name)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
class CommonTextMixin(object):
"""docstring for CommonTextMixin"""
def getText(self) -> str:
"""
Get text attribute of component
:returns: The text.
:rtype: str
"""
cmd = "get {}.txt".format(self.name)
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetString()
def setText(self, text: str) -> bool:
"""
Set text attribute of component
:param text: The text
:type text: str
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = '{}.txt="{}"'.format(self.name, text)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
class CommonValueMixin(object):
"""docstring for CommonValueMixin"""
def getValue(self) -> int:
"""
Get value attribute of component
:returns: The value.
:rtype: int
"""
cmd = "get {}.val".format(self.name)
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetNumber()
def setValue(self, value: int) -> bool:
"""
Set value attribute of component
:param text: The value
:type text: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "{}.val={}".format(self.name, value)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()