-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMenuItems.cpp
More file actions
executable file
·371 lines (313 loc) · 11.6 KB
/
MenuItems.cpp
File metadata and controls
executable file
·371 lines (313 loc) · 11.6 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
*/
#include "tcMenu.h"
#include "MenuItems.h"
#include "RuntimeMenuItem.h"
MenuItem::MenuItem(MenuType menuType, const AnyMenuInfo* menuInfo, MenuItem* next, bool infoProgmem) {
this->flags = 0;
this->menuType = menuType;
bitWrite(flags, MENUITEM_INFO_STRUCT_PGM, infoProgmem);
if(menuInfo != nullptr) this->info = menuInfo;
this->next = next;
this->setChanged(true); // items always start out needing redrawing.
this->setVisible(true); // always start out visible.
}
bool MenuItem::isSendRemoteNeeded(uint8_t remoteNo) const {
return bitRead(flags, (remoteNo + (int)MENUITEM_REMOTE_SEND0));
}
void MenuItem::setSendRemoteNeeded(uint8_t remoteNo, bool needed) {
bitWrite(flags, (remoteNo + (int)MENUITEM_REMOTE_SEND0), (needed && !isLocalOnly()));
}
void MenuItem::setEditing(bool active) {
bool isEditOnEntry = isEditing();
bitWrite(flags, MENUITEM_EDITING, active);
setChanged(true);
if (isMenuRuntimeMultiEdit(this) && !active && isEditOnEntry) {
auto* item = reinterpret_cast<EditableMultiPartMenuItem*>(this);
item->stopMultiEdit();
}
}
void MenuItem::setSendRemoteNeededAll() {
// make sure local only fields are never marked for sending.
if(isLocalOnly()) clearSendRemoteNeededAll();
flags = flags | MENUITEM_ALL_REMOTES;
}
void MenuItem::clearSendRemoteNeededAll() {
flags = flags & (~MENUITEM_ALL_REMOTES);
}
void MenuItem::triggerCallback() const {
if (isMenuRuntime(this)) {
return asRuntimeItem(this)->runCallback();
}
auto* cb = (isInfoProgMem()) ? get_info_callback(&info->callback) : info->callback;
if(cb) cb(getId());
}
uint8_t MenuItem::copyNameToBuffer(char* buf, int offset, int size) const {
if (isMenuRuntime(this)) {
asRuntimeItem(this)->copyRuntimeName(buf + offset, size - offset);
return strlen(buf + offset) + offset;
}
else if(isInfoProgMem()) {
const char* name = info->name;
uint8_t ret = safeProgCpy(buf + offset, name, size - offset);
return ret + offset;
}
else {
strncpy(buf + offset, info->name, size - offset);
return strlen(buf + offset) + offset;
}
}
uint16_t MenuItem::getId() const
{
if (isMenuRuntime(this)) {
return asRuntimeItem(this)->getRuntimeId();
}
return (isInfoProgMem()) ? get_info_uint(&info->id) : info->id;
}
uint16_t MenuItem::getMinimumValue() const
{
return (isInfoProgMem()) ? get_info_uint(&info->minValue) : info->minValue;
}
uint16_t MenuItem::getMaximumValue() const
{
if (isMenuRuntime(this)) {
return asRuntimeItem(this)->getNumberOfParts();
}
return (isInfoProgMem()) ? get_info_uint(&info->maxValue) : info->maxValue;
}
void MenuItem::changeOccurred(bool silent) {
setChanged(true);
setSendRemoteNeededAll();
if(!silent) triggerCallback();
}
uint16_t MenuItem::getEepromPosition() const
{
if (isMenuRuntime(this)) return asRuntimeItem(this)->getRuntimeEeprom();
return isInfoProgMem() ? get_info_uint(&info->eepromAddr) : info->eepromAddr;
}
void MenuItem::setActive(bool active) {
bitWrite(flags, MENUITEM_ACTIVE, active);
setChanged(true);
}
// on avr boards we store all info structures in progmem, so we need this code to
// pull the enum structures out of progmem. Otherwise we just read it out normally
#ifdef __AVR__
void EnumMenuItem::copyEnumStrToBuffer(char* buffer, int size, int idx) const {
if(!isInfoProgMem()) {
auto* enumInfo = reinterpret_cast<const EnumMenuInfo*>(info);
strncpy(buffer, enumInfo->menuItems[idx], size);
}
char** itemPtr = ((char**)pgm_read_ptr_near(&((EnumMenuInfo*)info)->menuItems) + idx);
char* itemLoc = (char *)pgm_read_ptr_near(itemPtr);
strncpy_P(buffer, itemLoc, size);
buffer[size - 1] = 0;
}
int EnumMenuItem::getLengthOfEnumStr(int idx) const {
if(!isInfoProgMem()) {
auto* enumInfo = reinterpret_cast<const EnumMenuInfo*>(info);
return strlen(enumInfo->menuItems[idx]);
}
char** itemPtr = ((char**)pgm_read_ptr_near(&((EnumMenuInfo*)info)->menuItems) + idx);
char* itemLoc = (char *)pgm_read_ptr_near(itemPtr);
return strlen_P(itemLoc);
}
#else
void EnumMenuItem::copyEnumStrToBuffer(char* buffer, int size, int idx) const {
EnumMenuInfo* enumInfo = (EnumMenuInfo*)info;
const char * const* choices = enumInfo->menuItems;
const char * choice = choices[idx];
strncpy(buffer, choice, size);
buffer[size - 1] = 0;
}
int EnumMenuItem::getLengthOfEnumStr(int idx) const {
EnumMenuInfo* enumInfo = (EnumMenuInfo*)info;
const char * const* choices = enumInfo->menuItems;
const char * choice = choices[idx];
return strlen(choice);
}
#endif
bool isSame(float d1, float d2) {
float result = tcFltAbs(d1 - d2);
return result < 0.0000001;
}
void AnalogMenuItem::copyValue(char * buffer, uint8_t bufferSize) const {
uint8_t dpNeeded = getDecimalPlacesForDivisor();
WholeAndFraction wf = getWholeAndFraction();
buffer[0]=0;
if(wf.negative) appendChar(buffer, '-', bufferSize);
fastltoa(buffer, wf.whole, 5, NOT_PADDED, bufferSize);
if (dpNeeded != 0) {
appendChar(buffer, '.', sizeof bufferSize);
fastltoa(buffer, wf.fraction, dpNeeded, '0', bufferSize);
}
uint8_t numLen = strlen(buffer);
if(numLen < bufferSize) copyUnitToBuffer(buffer + numLen, bufferSize - numLen);
}
float AnalogMenuItem::getAsFloatingPointValue() const {
WholeAndFraction wf = getWholeAndFraction();
serdebugF4("getasF ", wf.whole, wf.fraction, wf.negative);
float fract = (float(wf.fraction) / float(getActualDecimalDivisor()));
float whole = (wf.negative) ? -float(wf.whole) : float(wf.whole);
serdebugF3("whole, fract ", whole, fract);
return (wf.negative) ? (whole - fract) : (whole + fract);
}
uint16_t AnalogMenuItem::getActualDecimalDivisor() const {
uint16_t divisor = getDivisor();
if (divisor < 2) return 1;
return (divisor > 1000) ? 10000 : (divisor > 100) ? 1000 : (divisor > 10) ? 100 : 10;
}
WholeAndFraction AnalogMenuItem::getWholeAndFraction() const {
WholeAndFraction wf;
int32_t calcVal = int32_t(getCurrentValue()) + int32_t(getOffset());
int32_t divisor = getDivisor();
wf.negative = (calcVal < 0);
if (divisor < 2) {
wf.whole = calcVal;
wf.fraction = 0;
}
else {
wf.whole = abs(calcVal) / int16_t(divisor);
int fractMax = getActualDecimalDivisor();
wf.fraction = abs((calcVal % divisor)) * (fractMax / divisor);
}
return wf;
}
void AnalogMenuItem::setFromWholeAndFraction(WholeAndFraction wf) {
int fractMax = getActualDecimalDivisor();
uint16_t divisor = getDivisor();
int correctedFraction = wf.fraction / (fractMax / divisor);
auto val = (wf.whole * getDivisor());
if(wf.negative) {
val = -val;
val = val - correctedFraction;
}
else {
val = val + correctedFraction;
}
setCurrentValue(val - getOffset());
serdebugF4("setWF ", wf.whole, wf.fraction, getCurrentValue());
}
void AnalogMenuItem::setFromFloatingPointValue(float value) {
WholeAndFraction wf;
if(value < 0.0F) {
wf.negative = true;
value = tcFltAbs(value);
}
wf.whole = static_cast<uint32_t>(value);
wf.fraction = static_cast<uint32_t>((value - float(wf.whole)) * float(getActualDecimalDivisor()));
setFromWholeAndFraction(wf);
}
uint8_t AnalogMenuItem::getDecimalPlacesForDivisor() const {
uint16_t divisor = getDivisor();
if (divisor < 2) return 0;
return (divisor > 1000) ? 4 : (divisor > 100) ? 3 : (divisor > 10) ? 2 : 1;
}
int AnalogMenuItem::getOffset() const {
auto* anInfo = reinterpret_cast<const AnalogMenuInfo*>(info);
return isInfoProgMem() ? get_info_int(&(anInfo->offset)) : anInfo->offset;
}
uint16_t AnalogMenuItem::getDivisor() const {
auto* anInfo = reinterpret_cast<const AnalogMenuInfo*>(info);
return isInfoProgMem() ? get_info_uint(&(anInfo->divisor)) : anInfo->divisor;
}
int AnalogMenuItem::unitNameLength() const {
auto* anInfo = reinterpret_cast<const AnalogMenuInfo*>(info);
return isInfoProgMem() ? ((int) strlen_P(anInfo->unitName)) : strlen(anInfo->unitName);
}
void AnalogMenuItem::copyUnitToBuffer(char *unitBuff, uint8_t size) const {
auto* anInfo = reinterpret_cast<const AnalogMenuInfo*>(info);
if(isInfoProgMem()) {
safeProgCpy(unitBuff, anInfo->unitName, size);
}
else {
strncpy(unitBuff, anInfo->unitName, size);
unitBuff[size - 1] = 0;
}
}
BooleanNaming BooleanMenuItem::getBooleanNaming() const {
auto* enumInfo = reinterpret_cast<const BooleanMenuInfo*>(info);
return isInfoProgMem() ? static_cast<BooleanNaming>(get_info_char(&(enumInfo->naming))) : enumInfo->naming;
}
void FloatMenuItem::setFloatValue(float newVal, bool silent) {
if(isSame(newVal, currValue)) return;
this->currValue = newVal;
changeOccurred(silent);
}
int FloatMenuItem::getDecimalPlaces() const {
auto *fltInfo = reinterpret_cast<const FloatMenuInfo*>(info);
return isInfoProgMem() ? get_info_int(&fltInfo->numDecimalPlaces) : fltInfo->numDecimalPlaces;
}
void ValueMenuItem::setCurrentValue(uint16_t val, bool silent) {
if (val == currentValue || val > getMaximumValue()) {
return;
}
if (val < getMinimumValue()) {
val = getMinimumValue();
}
currentValue = val;
changeOccurred(silent);
}
const char ON_STR[] PGM_TCM = "ON";
const char OFF_STR[] PGM_TCM = "OFF";
const char YES_STR[] PGM_TCM = "YES";
const char NO_STR[] PGM_TCM = " NO";
const char TRUE_STR[] PGM_TCM = " TRUE";
const char FALSE_STR[] PGM_TCM= "FALSE";
void copyMenuItemNameAndValue(const MenuItem* item, char* buffer, size_t bufferSize, char additionalSep) {
item->copyNameToBuffer(buffer, bufferSize);
if(additionalSep != 0) appendChar(buffer, additionalSep, bufferSize);
appendChar(buffer, ' ', bufferSize);
int pos = strlen(buffer);
copyMenuItemValue(item, buffer + pos, bufferSize - pos);
}
void copyMenuItemValue(const MenuItem* item, char* buffer, size_t bufferSize) {
buffer[0] = 0;
if(item->getMenuType() == MENUTYPE_ENUM_VALUE) {
auto* enItem = reinterpret_cast<const EnumMenuItem*>(item);
enItem->copyEnumStrToBuffer(buffer, bufferSize, enItem->getCurrentValue());
}
else if(item->getMenuType() == MENUTYPE_BOOLEAN_VALUE) {
auto* boolItem = reinterpret_cast<const BooleanMenuItem*>(item);
BooleanNaming naming = boolItem->getBooleanNaming();
const char* val;
switch(naming) {
case NAMING_ON_OFF:
val = boolItem->getBoolean() ? ON_STR : OFF_STR;
break;
case NAMING_YES_NO:
val = boolItem->getBoolean() ? YES_STR : NO_STR;
break;
default:
val = boolItem->getBoolean() ? TRUE_STR : FALSE_STR;
break;
}
safeProgCpy(buffer, val, bufferSize);
}
else if(item->getMenuType() == MENUTYPE_FLOAT_VALUE) {
auto* flItem = reinterpret_cast<const FloatMenuItem*>(item);
fastftoa(buffer, flItem->getFloatValue(), flItem->getDecimalPlaces(), bufferSize);
}
else if(item->getMenuType() == MENUTYPE_INT_VALUE) {
auto* anItem = reinterpret_cast<const AnalogMenuItem*>(item);
anItem->copyValue(buffer, bufferSize);
}
else if(item->getMenuType() == MENUTYPE_ACTION_VALUE || item->getMenuType() == MENUTYPE_SUB_VALUE) {
appendChar(buffer, '>', bufferSize);
appendChar(buffer, '>', bufferSize);
}
else if(item->getMenuType() == MENUTYPE_BACK_VALUE) {
buffer[0]=0;
if(item->isActive()) {
strncpy(buffer, "[..]", bufferSize);
}
}
else if(item->getMenuType() == MENUTYPE_TITLE_ITEM) {
buffer[0] = 0;
}
else if(isMenuRuntime(item)) {
auto* rtItem = reinterpret_cast<const RuntimeMenuItem*>(item);
rtItem->copyValue(buffer, bufferSize);
}
}