-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dwin2.h
251 lines (215 loc) · 7.89 KB
/
Dwin2.h
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
//***************************************************
//* Library to simplify working with DWIN Displays *
//* Lib use FreeRTOS, so for ESP32 only *
//* Copyright (C) 2024 Pavel Pervushkin. Ver.1.0.2 *
//* Released under the MIT license. *
//***************************************************
#ifndef Dwin2_h
#define Dwin2_h
#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include "vector"
#include <codecvt>
#include <locale>
#include <HardwareSerial.h>
#define BUFSIZE 256
#define HW_SERIAL_NUM 2
typedef enum {
INT,
DOUBLE,
UTF,
ASCII,
ICON
} uitype_t;
typedef struct {
size_t size;
uint8_t *cmd;
} cmdtype_t;
typedef enum {
RED,
BLUE,
GREEN,
ORANGE,
PURPLE,
TURQUOISE,
BROWN,
PINK,
DARK_GREEN,
YELLOW_GREEN,
ROSE_RED,
DEEP_PURPLE,
SKY_BLUE,
GRAY,
BLACK,
DARK_BLUE,
WHITE
} uicolor_t;
//***********************************************************************************************************************
//************* DWIN2 main class ****************************************************************************************
//***********************************************************************************************************************
class DWIN2
{
private:
// Increment/decrement text or numeric integer data by delta
void increment(const double &delta);
// Send a command to read the numeric value of the UI element
void sendReadUiNumCmd();
// Send a command to read the text value of the UI element
// The larger the maxTextSize value, the longer the buffer, so it is not recommended to
// set the value large, if the length of the intended text does not require it
void sendReadUiTextCmd(const uint8_t &maxTextSize);
// Response processing after sending sendReadUiNumCmd() or sendReadUiTextCmd() commands
int hexBufIntProcessing(const std::vector<uint8_t> &buffer);
double hexBufDblProcessing(const std::vector<uint8_t> &buffer);
String hexBufUtfProcessing(const std::vector<uint8_t> &buffer);
String hexBufAsciiProcessing(const std::vector<uint8_t> &buffer);
// Send command to send over UART, with mutex
void sendUart(const uint8_t *command, const uint8_t &cmdLength);
static String _dwinEcho; // Storing the response from the display
// Data from the response of the sent command
String _uiData = "";
bool _echo; // Listen to the response from the display
// Handle blinking of UI elements
esp_timer_handle_t _blinkTimerHandle;
static void blinkTmr(void *arg);
uint64_t _blinkPeriod;
// Blink status
static bool _isBlink;
void blinkUI(bool state);
// Communication with the display via uart
static HardwareSerial *_uart;
uint16_t _spHexAddr;
uint16_t _vpHexAddr;
uitype_t _uitype;
uint8_t _id;
// Buffer for storing the command
static std::vector<uint8_t> _uartCmdBuffer;
// Read UART
static std::vector<uint8_t> _uartRxBuf;
SemaphoreHandle_t _uartUiReadSem; // Binary semaphore
// Mutex for UART
static SemaphoreHandle_t _uartMutex;
// Mutex for buffer
static SemaphoreHandle_t _bufferMutex;
// Limits and delta
int _minVal;
int _maxVal;
double _delta;
// Current value for UI element
double _currentVal;
// Storing a text array for the UI element
std::vector<String> _listStrVal;
// Rotation direction (for Encoder knob)
bool _rightDir;
bool _loopRotation;
void swapBytes(uint8_t* bytes, size_t size) {
for (size_t i = 0; i < size / 2; ++i) {
uint8_t temp = bytes[i];
bytes[i] = bytes[size - i - 1];
bytes[size - i - 1] = temp;
}
}
String utf16_to_utf8(const uint16_t* utf16, size_t utf16_len);
// Output array in HEX format
template<typename Type>
static String printHex(Type c, int arrSize)
{
static const char hex_digits[] = "0123456789ABCDEF";
uint8_t hexCharArr[arrSize] = {};
String hexStr;
hexStr.reserve(arrSize*4);
for (int i=0; i < arrSize; i++)
{
hexStr.concat(hex_digits[c[i] >> 4]);
hexStr.concat(hex_digits[c[i] & 15]);
hexStr.concat(" ");
}
return hexStr;
};
protected:
typedef std::function<void(DWIN2 &uartcb)> CallbackFunction;
CallbackFunction uartEcho_cb = NULL;
void _handleEchoUart();
// Clearing the display buffer DWIN
void clearRxBuf();
// Processing the response from sent commands to DWIN Display
static void uartTask(void* parameter); // Static method to be run in the thread
TaskHandle_t _taskHandleUart = nullptr; // FreeRTOS task descriptor
SemaphoreHandle_t _uartWriteSem; // Bynary counting semaphore
public:
DWIN2();
~DWIN2();
void begin(const uint16_t &spHexAddr = 0, const uint16_t &vpHexAddr = 0, const uint8_t &rxPin = 16, const uint8_t &txPin = 17);
// Common methods
// Set page number
void setPage(const uint8_t &pageNum);
// Get page number
uint8_t getPage();
// Set diaplay brightness
void setBrightness(const uint8_t &brightness);
// Get diaplay brightness
uint8_t getBrightness();
// Restart display
void restartHMI();
// Methods for ui elements
// Set the id of the object to be created
void setId(const uint8_t &id);
// Setting a new address for the UI element
void setAddress(const uint16_t &spHexAddr, const uint16_t &vpHexAddr);
// Select the UI type of the display element
void setUiType(const uitype_t &uitype);
// Set the min. and max. values,
// delta to increase/decrease the value
void setLimits(const uint32_t &minVal, const uint32_t &maxVal, const bool &loopRotation = false);
// For text data, limits can be calculated automatically
void setLimits(const bool& loopRotation = true);
// Setting the initial value
void setStartVal(const double ¤tVal);
// Set a text list of values for UI elements
void setStrListVal(const std::vector<String> listStrVal);
// Set blink rate in milliseconds
void setBlinkPeriod(const uint64_t &blinkPeriodMs);
// Setting the called colbeck function
void setUartCbHandler(CallbackFunction f);
// Enable/disable echo mode
void setEcho(const bool &echo);
// Set color
// Overloaded function
void setColor(uint16_t colorHex);
void setColor(uicolor_t color);
// Dwin answer
String getDwinEcho();
// Blink ui element.
void blink(const bool &isBlink);
// Hide/unhide UI element
void showUi();
void hideUi();
// Sending numeric/text values to the display
// Overloaded function
void sendData(const int &data);
void sendData(const double &data);
void sendData(const String &data);
// Set Variables Icon
void setVarIcon(const int &icoNum);
// Set UI-element position
void setPos(const int &x, const int &y);
// Send the command to the display in Hex format
void sendRawCommand(const uint8_t *cmd, const size_t &cmdLength);
// Increment/decrement the value by a specified delta depending on the direction when calling the method
void update(const double &delta = 1.0, const bool &rightDir = true);
// Clearing the text field
void clearText(uint8_t length = 10);
// Get blink status
bool getBlinkStatus();
// Get the current value (as a number for int and dbl values and as an index for text values)
double getCurrentVal();
// Get object ID
uint8_t getId();
// Read data from UI element
String getUiData(const uint8_t &textSize = 10);
//
uint8_t getVarIconIndex();
};
#endif