-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
85 lines (66 loc) · 2.33 KB
/
main.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
#!/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 the display brightness, sleep mode and reset
"""
# system packages
from random import randint
import time
# custom packages
from nextion import NexCheckbox, NexButton, NexHardware, NexNumber
# 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()
# ============================================================================
# ============================ Brightness function ===========================
# decrease display brightness to 50%
display_brightness = 50
print('Decreasing display brightness to {}%...'.format(display_brightness))
nh.brightness(display_brightness)
print()
time.sleep(1)
# ============================================================================
# ============================== Sleep function ==============================
# activate sleep mode of display
print('Activating display sleep mode for 5 seconds')
nh.sleep(True)
time.sleep(5)
print('Wakeup display again')
nh.sleep(False)
time.sleep(1)
# ============================================================================
# ============================ Hide/show function ============================
# manipulate display content to demonstrate display reset afterwards
print('Manipulating display content a little bit ...')
b0 = NexButton(nh, 0, 1, "b0")
b0.setText("something")
n0 = NexNumber(nh, 0, 1, "n0")
n0.setValue(randint(1, 100))
c0 = NexCheckbox(nh, 0, 1, "c0")
c0.setValue(0)
print('Reset display to default in 5 seconds')
time.sleep(5)
nh.reset()
print('Fully brightness restored, all elements show the default value')
print()
time.sleep(1)
# hide button "b0" on screen
print('Hiding button "{}" ...'.format(b0.name))
b0.hide()
print()
time.sleep(5)
# show button "b0" again on screen
print('Showing button "{}" again ...'.format(b0.name))
b0.show()
print()
# ============================================================================
# ============================= 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)