forked from sfeakes/AqualinkD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpda_menu.c
328 lines (295 loc) · 9.97 KB
/
pda_menu.c
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
/*
* Copyright (c) 2017 Shaun Feakes - All rights reserved
*
* You may use redistribute and/or modify this code under the terms of
* the GNU General Public License version 2 as published by the
* Free Software Foundation. For the terms of this license,
* see <http://www.gnu.org/licenses/>.
*
* You are free to use this software under the terms of the GNU General
* Public License, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* https://github.com/sfeakes/aqualinkd
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "pda_menu.h"
#include "aq_serial.h"
#include "utils.h"
int _hlightindex = -1;
char _menu[PDA_LINES][AQ_MSGLEN+1];
void print_menu()
{
int i;
for (i=0; i < PDA_LINES; i++) {
//printf("PDA Line %d = %s\n",i,_menu[i]);
LOG(PDA_LOG,LOG_DEBUG, "PDA Menu Line %d = %s\n",i,_menu[i]);
}
if (_hlightindex > -1) {
//printf("PDA highlighted line = %d = %s\n",_hlightindex,_menu[_hlightindex]);
LOG(PDA_LOG,LOG_DEBUG, "PDA Menu highlighted line = %d = %s\n",_hlightindex,_menu[_hlightindex]);
}
}
int pda_m_hlightindex()
{
return _hlightindex;
}
char *pda_m_hlight()
{
return pda_m_line(_hlightindex);
}
char *pda_m_line(int index)
{
if (index >= 0 && index < PDA_LINES)
return _menu[index];
else
return "-"; // Just return something bad so I can use string comparison with no null check
// return NULL;
}
// Find exact menu item
int pda_find_m_index(char *text)
{
int i;
for (i = 0; i < PDA_LINES; i++) {
if (strncmp(pda_m_line(i), text, strlen(text)) == 0)
return i;
}
return -1;
}
// Fine menu item case insensative
int pda_find_m_index_case(char *text, int limit)
{
int i;
for (i = 0; i < PDA_LINES; i++) {
//printf ("+++ Compare '%s' to '%s' index %d\n",text,pda_m_line(i),i);
if (strncasecmp(pda_m_line(i), text, limit) == 0)
return i;
}
return -1;
}
// Find menu item very loose
int pda_find_m_index_loose(char *text)
{
int i;
for (i = 0; i < PDA_LINES; i++) {
//printf ("+++ Compare '%s' to '%s' index %d\n",text,pda_m_line(i),i);
if (strstr(pda_m_line(i), text) != NULL)
return i;
}
return -1;
}
/*
// Same as above but strip whitespace from menu item (NOT text parameter)
int pda_find_m_index_swcase(char *text, int limit)
{
int i;
char *mi;
for (i = 0; i < PDA_LINES; i++) {
//printf ("+++ Compare '%s' to '%s' index %d\n",text,pda_m_line(i),i);
mi = trimwhitespace(pda_m_line(i));
if (strncasecmp(mi, text, limit) == 0)
return i;
}
return -1;
}
*/
pda_menu_type pda_m_type()
{
if (strncasecmp(_menu[1],"AIR ", 5) == 0) {
return PM_HOME;
} else if (strncasecmp(_menu[0],"EQUIPMENT STATUS", 16) == 0) {
return PM_EQUIPTMENT_STATUS;
} else if (strncasecmp(_menu[0]," EQUIPMENT ", 16) == 0) {
return PM_EQUIPTMENT_CONTROL;
} else if (strncasecmp(_menu[0]," MAIN MENU ", 16) == 0) {
return PM_MAIN;
//else if ((_menu[0] == '\0' && _hlightindex == -1) || strncmp(_menu[4], "POOL MODE", 9) == 0 )// IF we are building the main menu this may be valid
} else if (strncasecmp(_menu[4], "POOL MODE", 9) == 0 || // Will not see POOL MODE if single device config (pool vs pool&spa)
strncasecmp(_menu[9], "EQUIPMENT ON/OFF", 16) == 0) {
if (pda_m_hlightindex() == -1) {
return PM_BUILDING_HOME;
} else {
return PM_HOME;
}
} else if (strncasecmp(_menu[0]," SET TEMP ", 16) == 0) {
return PM_SET_TEMP;
} else if (strncasecmp(_menu[0]," SET TIME ", 16) == 0) {
return PM_SET_TIME;
} else if (strncasecmp(_menu[0]," SET AquaPure ", 16) == 0) {
return PM_AQUAPURE;
} else if (strncasecmp(_menu[0]," SPA HEAT ", 16) == 0) {
return PM_SPA_HEAT;
} else if (strncasecmp(_menu[0]," POOL HEAT ", 16) == 0) {
return PM_POOL_HEAT;
} else if (strncasecmp(_menu[0]," SYSTEM SETUP ", 16) == 0) {
return PM_SYSTEM_SETUP;
} else if (strncasecmp(_menu[6],"Use ARROW KEYS ", 16) == 0 &&
strncasecmp(_menu[0]," FREEZE PROTECT ", 16) == 0) {
return PM_FREEZE_PROTECT;
} else if (strncasecmp(_menu[1]," DEVICES ", 16) == 0 &&
strncasecmp(_menu[0]," FREEZE PROTECT ", 16) == 0) {
return PM_FREEZE_PROTECT_DEVICES;
} else if (strncasecmp(_menu[3],"Firmware Version", 16) == 0 ||
strncasecmp(_menu[1]," AquaPalm", 12) == 0 ||
strncasecmp(_menu[1]," PDA-P", 6) == 0) { // PDA-P4 Only -or- PDA-PS4 Combo
return PM_FW_VERSION;
} else if (strncasecmp(_menu[0]," LABEL AUX ", 16) == 0) {// Catch AUX and not AUX4
return PM_AUX_LABEL;
} else if (strncasecmp(_menu[0]," LABEL AUX", 12) == 0 && // Will have number ie AUX4
strncasecmp(_menu[2]," CURRENT LABEL ", 16) == 0) {
return PM_AUX_LABEL_DEVICE;
} else if (strstr(_menu[0],"BOOST")) { // This is bad check, but PDA menus are BOOST | BOOST POOL | BOOST SPA, need to do better.
return PM_BOOST;
}
return PM_UNKNOWN;
}
/*
--- Main Menu ---
Line 0 =
Line 1 = AIR
(Line 4 first, Line 2 last, Highligh when finished)
--- Equiptment Status ---
Line 0 = EQUIPMENT STATUS
(Line 0 is first. No Highlight, everything in list is on)
--- Equiptment on/off menu --
Line 0 = EQUIPMENT
(Line 0 is first. Highlight when complete)
*/
bool process_pda_menu_packet(unsigned char* packet, int length)
{
bool rtn = true;
signed char first_line;
signed char last_line;
signed char line_shift;
signed char i;
switch (packet[PKT_CMD]) {
case CMD_PDA_CLEAR:
_hlightindex = -1;
memset(_menu, 0, PDA_LINES * (AQ_MSGLEN+1));
break;
case CMD_MSG_LONG:
if (packet[PKT_DATA] < 10) {
memset(_menu[packet[PKT_DATA]], 0, AQ_MSGLEN);
strncpy(_menu[packet[PKT_DATA]], (char*)packet+PKT_DATA+1, AQ_MSGLEN);
_menu[packet[PKT_DATA]][AQ_MSGLEN] = '\0';
}
if (getLogLevel(PDA_LOG) >= LOG_DEBUG){print_menu();}
break;
case CMD_PDA_HIGHLIGHT:
// when switching from hlight to hlightchars index 255 is sent to turn off hlight
if (packet[4] <= PDA_LINES) {
_hlightindex = packet[4];
} else {
_hlightindex = -1;
}
if (getLogLevel(PDA_LOG) >= LOG_DEBUG){print_menu();}
break;
case CMD_PDA_HIGHLIGHTCHARS:
if (packet[4] <= PDA_LINES) {
_hlightindex = packet[4];
} else {
_hlightindex = -1;
}
if (getLogLevel(PDA_LOG) >= LOG_DEBUG){print_menu();}
break;
case CMD_PDA_SHIFTLINES:
/// press up from top - shift menu down by 1
// PDA Shif | HEX: 0x10|0x02|0x62|0x0f|0x01|0x08|0x01|0x8d|0x10|0x03|
// press down from bottom - shift menu up by 1
// PDA Shif | HEX: 0x10|0x02|0x62|0x0f|0x01|0x08|0xff|0x8b|0x10|0x03|
first_line = (signed char)(packet[4]);
last_line = (signed char)(packet[5]);
line_shift = (signed char)(packet[6]);
LOG(PDA_LOG,LOG_DEBUG, "\n");
if (line_shift < 0) {
for (i = first_line-line_shift; i <= last_line; i++) {
memcpy(_menu[i+line_shift], _menu[i], AQ_MSGLEN+1);
}
_menu[last_line][0] = '\0';
} else {
for (i = last_line; i >= first_line+line_shift; i--) {
memcpy(_menu[i], _menu[i-line_shift], AQ_MSGLEN+1);
}
_menu[first_line][0] = '\0';
}
if (getLogLevel(PDA_LOG) >= LOG_DEBUG){print_menu();}
break;
}
return rtn;
}
#ifdef DO_NOT_COMPILE
bool NEW_process_pda_menu_packet_NEW(unsigned char* packet, int length)
{
bool rtn = true;
signed char first_line;
signed char last_line;
signed char line_shift;
signed char i;
pthread_mutex_lock(&_pda_menu_mutex);
switch (packet[PKT_CMD]) {
case CMD_STATUS:
pthread_cond_signal(&_pda_menu_update_complete_cond);
break;
case CMD_PDA_CLEAR:
rtn = pda_m_clear();
break;
case CMD_MSG_LONG:
if (packet[PKT_DATA] < 10) {
memset(_menu[packet[PKT_DATA]], 0, AQ_MSGLEN);
strncpy(_menu[packet[PKT_DATA]], (char*)packet+PKT_DATA+1, AQ_MSGLEN);
_menu[packet[PKT_DATA]][AQ_MSGLEN] = '\0';
}
if (packet[PKT_DATA] == _hlightindex) {
LOG(PDA_LOG,LOG_DEBUG, "process_pda_menu_packet: hlight changed from shift or up/down value\n");
pthread_cond_signal(&_pda_menu_hlight_change_cond);
}
if (getLogLevel(PDA_LOG) >= LOG_DEBUG){print_menu();}
update_pda_menu_type();
break;
case CMD_PDA_HIGHLIGHT:
// when switching from hlight to hlightchars index 255 is sent to turn off hlight
if (packet[4] <= PDA_LINES) {
_hlightindex = packet[4];
} else {
_hlightindex = -1;
}
pthread_cond_signal(&_pda_menu_hlight_change_cond);
if (getLogLevel(PDA_LOG) >= LOG_DEBUG){print_menu();}
break;
case CMD_PDA_HIGHLIGHTCHARS:
if (packet[4] <= PDA_LINES) {
_hlightindex = packet[4];
} else {
_hlightindex = -1;
}
pthread_cond_signal(&_pda_menu_hlight_change_cond);
if (getLogLevel(PDA_LOG) >= LOG_DEBUG){print_menu();}
break;
case CMD_PDA_SHIFTLINES:
// press up from top - shift menu down by 1
// PDA Shif | HEX: 0x10|0x02|0x62|0x0f|0x01|0x08|0x01|0x8d|0x10|0x03|
// press down from bottom - shift menu up by 1
// PDA Shif | HEX: 0x10|0x02|0x62|0x0f|0x01|0x08|0xff|0x8b|0x10|0x03|
first_line = (signed char)(packet[4]);
last_line = (signed char)(packet[5]);
line_shift = (signed char)(packet[6]);
LOG(PDA_LOG,LOG_DEBUG, "\n");
if (line_shift < 0) {
for (i = first_line-line_shift; i <= last_line; i++) {
memcpy(_menu[i+line_shift], _menu[i], AQ_MSGLEN+1);
}
} else {
for (i = last_line; i >= first_line+line_shift; i--) {
memcpy(_menu[i], _menu[i-line_shift], AQ_MSGLEN+1);
}
}
if (getLogLevel(PDA_LOG) >= LOG_DEBUG){print_menu();}
break;
}
pthread_mutex_unlock(&_pda_menu_mutex);
return rtn;
}
#endif