forked from DanNixon/NeoNextion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINextionWidget.cpp
110 lines (100 loc) · 2.99 KB
/
INextionWidget.cpp
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
/*! \file */
#include "INextionWidget.h"
/*!
* \brief Create a new widget adapter.
* \param nex Reference to the Nextion driver
* \param page ID of page this widget is on
* \param component Component ID of this widget
* \param name Name of this widget
*/
INextionWidget::INextionWidget(Nextion &nex, uint8_t page, uint8_t component,
const char *name)
: m_nextion(nex)
, m_pageID(page)
, m_componentID(component)
, m_name(name)
{
}
/*!
* \brief Gets the ID of the page this widget resides on.
* \return Page ID
*/
uint8_t INextionWidget::getPageID()
{
return m_pageID;
}
/*!
* \brief Gets the component ID of this widget.
* \return Component ID
*/
uint8_t INextionWidget::getComponentID()
{
return m_componentID;
}
/*!
* \brief Sets the value of a numerical property of this widget.
* \param propertyName Name of the property
* \param value Value
* \return True if successful
*/
bool INextionWidget::setNumberProperty(char *propertyName, uint32_t value)
{
size_t commandLen = 8 + strlen(m_name) + strlen(propertyName);
char commandBuffer[commandLen];
snprintf(commandBuffer, commandLen, "%s.%s=%ld", m_name, propertyName, value);
return sendCommand(commandBuffer);
}
/*!
* \brief Gets the value of a numerical property of this widget.
* \param propertyName Name of the property
* \return Value (may also return 0 in case of error)
*/
uint32_t INextionWidget::getNumberProperty(char *propertyName)
{
size_t commandLen = 7 + strlen(m_name) + strlen(propertyName);
char commandBuffer[commandLen];
snprintf(commandBuffer, commandLen, "get %s.%s", m_name, propertyName);
sendCommand(commandBuffer, false);
uint32_t id;
if (m_nextion.receiveNumber(&id))
return id;
else
return 0;
}
/*!
* \brief Sets the value of a string property of this widget.
* \param propertyName Name of the property
* \param value Value
* \return True if successful
*/
bool INextionWidget::setStringProperty(char *propertyName, char *value)
{
size_t commandLen = 7 + strlen(m_name) + strlen(propertyName) + strlen(value);
char command[commandLen];
snprintf(command, commandLen, "%s.%s=\"%s\"", m_name, propertyName, value);
return sendCommand(command);
}
/*!
* \brief Gets the value of a string property of this widget.
* \param propertyName Name of the property
* \param value Pointer to char array to store result in
* \param len Maximum length of value
* \return Actual length of value
*/
size_t INextionWidget::getStringProperty(char *propertyName, char *value,
size_t len)
{
size_t commandLen = 6 + strlen(m_name) + strlen(propertyName);
char command[commandLen];
snprintf(command, commandLen, "get %s.%s", m_name, propertyName);
sendCommand(command, false);
return m_nextion.receiveString(value, len);
}
bool INextionWidget::sendCommand(char *commandStr, bool checkComplete)
{
m_nextion.sendCommand(commandStr);
if (checkComplete)
return m_nextion.checkCommandComplete();
else
return true;
}