-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnextion_gpio.py
132 lines (105 loc) · 3.88 KB
/
nextion_gpio.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
NexGpio
Functions to interact with Nextion GPIOs
"""
# system packages
from time import sleep
# custom packages
from .common import Common
class NexGpioError(Exception):
"""Base class for exceptions in this module."""
pass
class NexGpio(Common):
"""docstring for NexGpio"""
INPUT_PULL_UP = 0 # set pin as pull up input
OUTPUT = 2 # set pin as push pull output
INPUT_BINDING = 1 # bind Nextion element to falling edge of signal
PWM = 3 # set pin as PWM output (only GPIO4 and GPIO7)
def __init__(self, nh) -> None:
"""
Init GPIO
:param nh: The Nextion hardware interface object
:type nh: NexHardware
"""
super().__init__(nh, pid=-1, cid=-1, name="gpio")
def pin_mode(self, port: int, mode: int, control_id: int = 0) -> bool:
"""
Set GPIO mode
:param port: The GPIO port number
:type port: int
:param mode: The GPIO port mode
0 - Pull on the input
1 - the control input binding
2 - Push-pull output
3 - pwm output
4 - open mode leakage
:type mode: int
:param control_id: The bound CID of other nextion element
:type control_id: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "cfgpio {},{},{}".format(port, mode, control_id)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
def digital_write(self, port: int, value: int) -> bool:
"""
Write a HIGH or LOW value to a digital pin
:param port: The GPIO port number
:type port: int
:param value: The value (0 or 1)
:type value: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "pio{}={}".format(port, value)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
def digital_read(self, port: int) -> int:
"""
Read a HIGH or a LOW value of a digital pin
:param port: The GPIO port number
:type port: int
:returns: Value of specified digital pin, either 1 or 0
:rtype: int
"""
cmd = "get pio{}".format(port)
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetNumber()
def analog_write(self, port: int, value: int) -> bool:
"""
Set analog value (PWM wave) to a pin
:param port: The GPIO port number
:type port: int
:param value: The duty cycle value (0-100)
:type value: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "pwm{}={}".format(port, value)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
def set_pwmfreq(self, value: int) -> bool:
"""
Set the PWM output frequency for all pins
:param value: The PWM frequency (1-65535)
:type value: int
:returns: True on success, false otherwise
:rtype: bool
"""
cmd = "pwmf={}".format(value)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
def get_pwmfreq(self) -> int:
"""
Get the PWM output frequency
:returns: The PWM frequency.
:rtype: int
"""
cmd = "get pwmf"
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetNumber()