-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.h
330 lines (311 loc) · 9.17 KB
/
gpio.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#pragma once
#include <cJSON.h>
char* regTypeToStr(TAddress reg);
#define CFG_GPIO "/gpio.json"
#define GPIO_MAX_COUNT 16
#define GPIO_MAX_LOCAL_ISTS 16
extern ModbusIP *mb;
#define GPIO_CLICK 50, 0
#define GPIO_PRESS 250, 0
#define GPIO_DOUBLE_CLICK 50
struct gpio_t {
String name = "";
uint8_t mode = INPUT;
uint8_t pin = 0;
bool inverse = false;
TAddress reg;
uint16_t *flag = nullptr;
};
std::vector<gpio_t> gpios;
uint16_t click = 50;
uint16_t press = 250;
uint16_t longPress = 2000;
uint16_t veryLong = 10000;
enum swType {
DIRECT,
CLICK,
PRESS,
DOUBLE_CLICK,
LONG_PRESS,
TRIPLE_CLICK,
VERY_LONG_PRESS
};
struct intrPress {
uint16_t flag = 0;
uint32_t timeStamp = 0;
};
uint16_t ists[GPIO_MAX_LOCAL_ISTS];
/*
template <I>
void gpioHandler() {
uint8_t state = digitalRead(ists[I]);
if (state == LOW) {
ists[I].timeStamp = millis();
return;
}
timeStamp = millis() - timeStamp;
ists[I].flag++;
};
template <I>
void gpioDirect() {
uint8_t state = digitalRead(ists[I]);
if (state == LOW) {
ists[I].timeStamp = millis();
return;
}
timeStamp = millis() - timeStamp;
ists[I].flag++;
};
*/
uint16_t cbDigitalRead(TRegister *reg, uint16_t val) {
std::vector<gpio_t>::iterator it =
std::find_if(gpios.begin(), gpios.end(),
[reg](const gpio_t &d) { return d.reg == reg->address; });
if (it != gpios.end()) {
switch (it->reg.type) {
case TAddress::HREG:
return 0;
break;
case TAddress::IREG:
return 0;
break;
case TAddress::COIL:
return COIL_VAL(digitalRead(it->pin) == HIGH);
break;
case TAddress::ISTS:
return ISTS_VAL(digitalRead(it->pin) == HIGH);
break;
}
}
// return COIL_VAL(false);
return val;
}
uint16_t cbDigitalWrite(TRegister *reg, uint16_t val) {
std::vector<gpio_t>::iterator it =
std::find_if(gpios.begin(), gpios.end(),
[reg](const gpio_t &d) { return d.reg == reg->address; });
if (it != gpios.end()) {
switch (it->reg.type) {
case TAddress::HREG:
return 0;
break;
case TAddress::IREG:
return 0;
break;
case TAddress::COIL:
digitalWrite(it->pin, COIL_BOOL(val) ? HIGH : LOW);
break;
case TAddress::ISTS:
digitalWrite(it->pin, ISTS_BOOL(val) ? HIGH : LOW);
break;
}
}
// return COIL_VAL(false);
return val;
}
uint16_t addGpio(TAddress local, uint8_t pin, uint8_t mode) {
gpio_t reg;
reg.reg = local;
reg.pin = pin;
reg.mode = mode;
pinMode(pin, mode);
switch (local.type) {
case TAddress::HREG:
mb->addHreg(local.address);
break;
case TAddress::IREG:
mb->addIreg(local.address);
break;
case TAddress::COIL:
mb->addCoil(local.address);
mb->onSetCoil(local.address,
(mode == OUTPUT) ? cbDigitalWrite : cbDigitalRead);
mb->onGetCoil(local.address, cbDigitalRead);
break;
case TAddress::ISTS:
mb->addIsts(local.address);
mb->onSetIsts(local.address,
(mode == OUTPUT) ? cbDigitalWrite : cbDigitalRead);
mb->onGetIsts(local.address, cbDigitalRead);
break;
}
gpios.push_back(reg);
return true;
}
bool saveGpio() {
File configFile = SPIFFS.open(CFG_GPIO, "w");
if (configFile) {
cJSON *json = cJSON_CreateObject();
if (json) {
cJSON *arr = cJSON_CreateArray();
if (arr) {
for (gpio_t ® : gpios) {
cJSON *item;
cJSON *entry = cJSON_CreateObject();
if (!entry)
continue;
item = cJSON_CreateNumber(reg.reg.type);
if (!item)
continue;
cJSON_AddItemToObject(entry, "type", item);
item = cJSON_CreateNumber(reg.reg.address);
if (!item)
continue;
cJSON_AddItemToObject(entry, "address", item);
item = cJSON_CreateNumber(reg.pin);
if (!item)
continue;
cJSON_AddItemToObject(entry, "pin", item);
item = cJSON_CreateNumber(reg.mode);
if (!item)
continue;
cJSON_AddItemToObject(entry, "mode", item);
item = cJSON_CreateBool(reg.inverse);
if (!item)
continue;
cJSON_AddItemToObject(entry, "inverse", item);
item = cJSON_CreateString(reg.name.c_str());
if (item)
cJSON_AddItemToObject(entry, "name", item);
cJSON_AddItemToArray(arr, entry);
}
cJSON_AddItemToObject(json, "gpio", arr);
char *out = cJSON_Print(json);
configFile.write((uint8_t *)out, strlen(out));
cJSON_Delete(json);
free(out);
}
configFile.close();
return true;
}
configFile.close();
return false;
}
return false;
}
bool readGpio() {
int16_t i;
File configFile = SPIFFS.open(CFG_GPIO, "r");
if (configFile) {
char *data = (char *)malloc(configFile.size() + 1);
if (data) {
if (configFile.read((uint8_t *)data, configFile.size()) ==
configFile.size()) {
data[configFile.size()] = '/0';
TDEBUG("gpio: config %d bytes read\n", configFile.size());
cJSON *json = cJSON_Parse(data);
if (json) {
TDEBUG("gpio: JSON parsed\n");
cJSON *arr = NULL;
cJSON *entry = NULL;
cJSON *item;
i = 0;
if (cJSON_HasObjectItem(json, "gpio"))
arr = cJSON_GetObjectItemCaseSensitive(json, "gpio");
if (arr) {
TDEBUG("gpio: registers:\n");
cJSON_ArrayForEach(entry, arr) {
gpio_t reg;
if (i > GPIO_MAX_COUNT)
break;
i++;
TDEBUG("gpio: #%d\n", i);
cJSON *item = NULL;
if (!cJSON_HasObjectItem(entry, "type"))
continue;
item = cJSON_GetObjectItemCaseSensitive(entry, "type");
if (!item || !cJSON_IsNumber(item))
continue;
reg.reg.type = (TAddress::RegType)item->valueint;
if (!cJSON_HasObjectItem(entry, "address"))
continue;
item = cJSON_GetObjectItemCaseSensitive(entry, "address");
if (!item || !cJSON_IsNumber(item))
continue;
reg.reg.address = item->valueint;
if (!cJSON_HasObjectItem(entry, "inverse"))
continue;
item = cJSON_GetObjectItemCaseSensitive(entry, "inverse");
if (!item || !cJSON_IsBool(item))
continue;
reg.inverse = item->valueint;
if (!cJSON_HasObjectItem(entry, "pin"))
continue;
item = cJSON_GetObjectItemCaseSensitive(entry, "pin");
if (!item || !cJSON_IsNumber(item))
continue;
reg.pin = item->valueint;
if (!cJSON_HasObjectItem(entry, "mode"))
continue;
item = cJSON_GetObjectItemCaseSensitive(entry, "mode");
if (!item || !cJSON_IsNumber(item))
continue;
reg.mode = item->valueint;
if (cJSON_HasObjectItem(entry, "name")) {
item = cJSON_GetObjectItemCaseSensitive(entry, "name");
if (item && cJSON_IsString(item))
reg.name = item->valuestring;
}
addGpio(reg.reg, reg.pin, reg.mode);
}
// cJSON_Delete(arr);
}
cJSON_Delete(json);
}
free(data);
configFile.close();
return true;
}
configFile.close();
return false;
}
return false;
}
return false;
}
uint32_t gpioInit() {
readGpio();
return RUN_DELETE;
}
void cliGpio(Shell &shell, int argc, const ShellArguments &argv) {
if (argc > 2) {
digitalWrite(atoi(argv[1]), atoi(argv[2]));
}
if (argc > 1) {
shell.println(digitalRead(atoi(argv[1])));
}
}
ShellCommand(gpio, "<nr>[ <0|1>] - GPIO: Read/Write", cliGpio);
void cliGpioMode(Shell &shell, int argc, const ShellArguments &argv) {
if (argc > 2) {
pinMode(atoi(argv[1]), strcmp(argv[2], "input") == 0 ? INPUT : OUTPUT);
}
}
ShellCommand(gpiomode, "<nr> <input|output> - GPIO: Set mode", cliGpioMode);
void cliGpioMapIsts(Shell &shell, int argc, const ShellArguments &argv) {
if (argc > 2) {
addGpio(ISTS(atoi(argv[2])), atoi(argv[1]), INPUT);
}
}
ShellCommand(gpiomapists, "<pin> <local_ists> - GPIO: Map to Ists",
cliGpioMapIsts);
void cliGpioMapCoil(Shell &shell, int argc, const ShellArguments &argv) {
if (argc > 2) {
addGpio(COIL(atoi(argv[2])), atoi(argv[1]), OUTPUT);
}
}
ShellCommand(gpiomapcoil, "<pin> <local_coil> - GPIO: Map to Coil",
cliGpioMapCoil);
void cliGpioMapList(Shell &shell, int argc, const ShellArguments &argv) {
shell.printf_P(PSTR("GPIO\t\tReg\n"));
for (auto &s : gpios) {
shell.printf_P(PSTR("%d\t %s \t%s\t%d\t'%s\n"), s.pin,
(s.mode == OUTPUT) ? "<=" : "=>", regTypeToStr(s.reg),
s.reg.address, s.name.c_str());
}
}
ShellCommand(gpiomaplist, "- GPIO: List mappings", cliGpioMapList);
void cliGpioMapSave(Shell &shell, int argc, const ShellArguments &argv) {
shell.println(saveGpio() ? "Done" : "Error");
}
ShellCommand(gpiomapsave, "- GPIO: Save mappings", cliGpioMapSave);