-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
220 lines (170 loc) · 6.84 KB
/
main.py
File metadata and controls
220 lines (170 loc) · 6.84 KB
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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
Main script
Do your stuff here, this file is similar to the loop() function on Arduino
Example on how to interact with a button element
"""
# system packages
import time
# custom packages
from nextion import NexButton, NexHardware
# define communication pins for Nextion display
tx_pin = 21
rx_pin = 22
# create Nextion hardware interface
nh = NexHardware(rx_pin=rx_pin, tx_pin=tx_pin)
# init nextion communication interface
nh.nexInit()
# create a button instance
b0 = NexButton(nh, 0, 1, "b0")
# ============================================================================
# ============================== Example values ==============================
# new values of button
button_text = "btn txt"
background_color_value = 63488 # red
font_color_value = 31 # blue
pressed_background_color_value = 64480 # orange
pressed_font_color_value = 2047 # cyan
x_offset = 20
y_offset = 20
# ============================================================================
# ============================== Text functions ==============================
# request the text of button "b0"
print('Requesting button "{}" text ...'.format(b0.name))
response = b0.getText()
print('Button "{}" text is: "{}"'.format(b0.name, response))
print()
time.sleep(1)
# modify button "b0" showing "newtxt" by default
print('Set button "{}" to "{}"'.format(b0.name, button_text))
b0.setText(button_text)
print()
time.sleep(1)
# request the text of button "b0" again
print('Requesting button "{}" text ...'.format(b0.name))
response = b0.getText()
print('Button "{}" text is: "{}"'.format(b0.name, response))
print()
# sanity check
if response != button_text:
print('WARNING: GET value did not match SET value')
time.sleep(1)
# ============================================================================
# =========================== Background functions ===========================
# request the background color of button "b0"
print('Requesting background color of button "{}" ...'.format(b0.name))
response = b0.Get_background_color_bco()
print('Background color of button "{}" is: "{}"'.format(b0.name, response))
print()
time.sleep(1)
# modify the background color of button "b0" to "red"
# search for RGB565 Colors. Red is "63488" at 65k colors
print('Set background color of button "{}" to "{}"'.
format(b0.name, background_color_value))
b0.Set_background_color_bco(background_color_value)
print()
time.sleep(1)
# request the background color of button "b0" again
print('Requesting background color of button "{}" ...'.format(b0.name))
response = b0.Get_background_color_bco()
print('Background color of button "{}" is: "{}"'.format(b0.name, response))
print()
# sanity check
if response != background_color_value:
print('WARNING: GET value did not match SET value')
time.sleep(1)
# ============================================================================
# ============================== Font functions ==============================
# request the font color of button "b0"
print('Requesting font color of button "{}" ...'.format(b0.name))
response = b0.Get_font_color_pco()
print('Font color of button "{}" is: "{}"'.format(b0.name, response))
print()
time.sleep(1)
# modify the font color of button "b0" to "blue"
# search for RGB565 Colors. Blue is "31" at 65k colors
print('Set font color of button "{}" to "{}"'.
format(b0.name, font_color_value))
b0.Set_font_color_pco(font_color_value)
print()
time.sleep(1)
# request the font color of button "b0" again
print('Requesting font color of button "{}" ...'.format(b0.name))
response = b0.Get_font_color_pco()
print('Font color of button "{}" is: "{}"'.format(b0.name, response))
print()
# sanity check
if response != font_color_value:
print('WARNING: GET value did not match SET value')
time.sleep(1)
# ============================================================================
# ============================ Position functions ============================
# request the x/y position of button "b0" again
print('Requesting x/y position of button "{}" ...'.format(b0.name))
x_position = b0.Get_place_xcen()
y_position = b0.Get_place_ycen()
print('Position of button "{}" is: "x={}", "y={}"'.
format(b0.name, x_position, y_position))
print()
x_position += x_offset
y_position += y_offset
# modify the x/y position of button "b0"
print('Set x/y position of button "{}" to "x={}", "y={}"'.
format(b0.name, x_position, y_position))
b0.Set_place_xcen(x_position)
b0.Set_place_ycen(y_position)
print()
time.sleep(1)
# ============================================================================
# ============================ Special functions =============================
# request the pressed font color of button "b0"
print('Requesting pressed font color of button "{}" ...'.format(b0.name))
response = b0.Get_press_font_color_pco2()
print('Pressed font color of button "{}" is: "{}"'.format(b0.name, response))
print()
time.sleep(1)
# modify the pressed font color of button "b0" to "cyan"
# search for RGB565 Colors. Cyan is "2047" at 65k colors
print('Set font color of button "{}" to "{}"'.
format(b0.name, pressed_font_color_value))
b0.Set_press_font_color_pco2(pressed_font_color_value)
print()
time.sleep(1)
# request the pressed font color of button "b0" again
print('Requesting pressed font color of button "{}" ...'.format(b0.name))
response = b0.Get_press_font_color_pco2()
print('Pressed font color of button "{}" is: "{}"'.format(b0.name, response))
print()
# sanity check
if response != pressed_font_color_value:
print('WARNING: GET value did not match SET value')
time.sleep(1)
# request the pressed background color of button "b0"
print('Requesting pressed background color of button "{}" ...'.format(b0.name))
response = b0.Get_press_background_color_bco2()
print('Pressed background color of button "{}" is: "{}"'.
format(b0.name, response))
print()
time.sleep(1)
# modify the pressed background color of button "b0" to "orange"
# search for RGB565 Colors. Red is "64480" at 65k colors
print('Set pressed background color of button "{}" to "{}"'.
format(b0.name, pressed_background_color_value))
b0.Set_press_background_color_bco2(pressed_background_color_value)
print()
time.sleep(1)
# request the pressed background color of button "b0" again
print('Requesting pressed background color of button "{}" ...'.format(b0.name))
response = b0.Get_press_background_color_bco2()
print('Pressed background color of button "{}" is: "{}"'.
format(b0.name, response))
print()
# sanity check
if response != pressed_background_color_value:
print('WARNING: GET value did not match SET value')
# ============================================================================
# ============================= End of example ===============================
print('Returning to REPL in 5 seconds')
# wait for 5 more seconds to safely finish the may still running threads
time.sleep(5)