-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
560 lines (442 loc) · 14.5 KB
/
util.cpp
File metadata and controls
560 lines (442 loc) · 14.5 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include "device.h"
#include "util.h"
#include "errors.h"
#include <Adafruit_RGBLCDShield.h>
#include <Arduino.h>
#include <EEPROM.h>
extern char *__brkval;
SmartHomeState::SmartHomeState() {
this->num_devices = 0;
memset(this->devices_slots_free, true, MAX_CAPACITY);
this-> current_device_index = 0;
};
NUMBER SmartHomeState::next_free_index() {
for (NUMBER i=0; i < MAX_CAPACITY; i++) {
if (devices_slots_free[i]) {
return i;
}
}
return -1;
};
NUMBER SmartHomeState::get_device_index_by_id(char id[4]) {
for (NUMBER i = 0; i < MAX_CAPACITY; i++) {
if (!this->devices_slots_free[i] && strcmp(this->devices[i].id, id) == 0) {
return i;
};
};
return -1; // not found
};
HRESULT SmartHomeState::add_device(Device device) {
if (this->get_device_index_by_id(device.id) != -1) {
return E_STATE_CONFLICTING_DEVICE;
};
if (this->num_devices >= MAX_CAPACITY) {
return E_STATE_CAPACITY_REACHED;
};
NUMBER i = this->insert(device.id);
if (!this->devices_slots_free[i]) {
// if slot isnt free shuffle up
if (!shuffle_up(i)) {
// if we cant shuffle up shuffle down
if (!shuffle_down(i)) {
// if we cant shuffle down panic
return E_STATE_PANIC;
}
}
}
this->devices[i] = device;
this->devices_slots_free[i] = false;
this->is_current = false;
this-> num_devices += 1;
return S_OK;
};
NUMBER SmartHomeState::insert(char id[4]){
if (this->num_devices == 0) {
return 0;
};
NUMBER last_taken_spot;
for (NUMBER i = 0; i < MAX_CAPACITY; i ++) {
if (this->devices_slots_free[i]) {
continue;
};
last_taken_spot = i;
// ID IS LOWER THAN DEVice at index i
if (strcmp(this->devices[i].id, id) > 0) {
return i;
};
};
// we got to the end of the array so we
// should insert after the last device
if (last_taken_spot == MAX_CAPACITY-1) {
return last_taken_spot; // we are going to have to shuffle down
} else {
return last_taken_spot+1;
}
}
bool SmartHomeState::shuffle_up(NUMBER after) {
NUMBER free_index = after + 1;
while (free_index < MAX_CAPACITY) {
if (this->devices_slots_free[free_index]) {
break;
};
free_index ++;
};
if (free_index >= MAX_CAPACITY) {
return false; // no space to shuffle up so we will have to shuffle down
};
// shuffle up between after-free_index both exclusive
// we start from the top and move the element below
// up into the free place
for (NUMBER i = free_index; i > after; i--) {
this->devices[i] = this->devices[i-1];
this->devices_slots_free[i] = false;
this->devices_slots_free[i-1] = true;
};
return true;
};
bool SmartHomeState::shuffle_down(NUMBER before) {
NUMBER free_index = before - 1;
while (free_index >= 0) {
if (this->devices_slots_free[free_index]) {
break;
};
free_index--;
};
// catch an overflow (0-1 is greated than max cap)
if (free_index > MAX_CAPACITY) {
return false; // no space to shuffle down so we will have to shuffle up
};
// shuffle down between free_index-before both exclusive
// we start from the bottom and move the element above
// down into the free place
for (NUMBER i = free_index; i < before; i++) {
this->devices[i] = this->devices[i+1];
this->devices_slots_free[i] = false;
this->devices_slots_free[i+1] = true;
};
return true;
}
HRESULT SmartHomeState::remove_device(char id[4]) {
NUMBER i = get_device_index_by_id(id);
if (i != -1) {
this->devices_slots_free[i] = true;
this->num_devices -=1;
this->is_current = false;
if (this->current_device_index==i) {
this->current_device_index++;
}
return S_OK;
};
return E_STATE_NO_KNOWN_DEVICE;
}
HRESULT SmartHomeState::overwrite_device(Device device) {
NUMBER index = this->get_device_index_by_id(device.id);
if (index == -1) {
return E_STATE_NO_KNOWN_DEVICE;
};
this->devices[index] = device;
this->is_current = false;
return S_OK;
}
NUMBER SmartHomeState::device_count() {
return this->num_devices;
};
bool SmartHomeState::device_meets_state_criteria(NUMBER device_index) {
switch (this->display_mode) {
case ON_DEVICES:
return this->devices[device_index].state == true;
case OFF_DEVICES:
return this->devices[device_index].state == false;
default:
return true;
};
}
DisplayFlags SmartHomeState::current_device(Device* device) {
// we can cheat here by getting the device before the current device+1
// instead of recalculating extra state
this->current_device_index += 1;
DisplayFlags flags = this->prev_device(device);
if (flags != NO_DEVICES) {
return flags;
} else {
this->current_device_index = 0;
return this->next_device(device);
}
};
DisplayFlags SmartHomeState::next_device(Device* device) {
if (this->num_devices == 0) {
return NO_DEVICES;
};
NUMBER flags = AT_TOP | AT_BOTTOM;
bool found_device = false;
// scan for device after current pointer
// then scan for a next device incase we arent at the top or bottom
for (NUMBER i = 1 + this->current_device_index; i < MAX_CAPACITY; i++) {
if (!this->devices_slots_free[i] && this->device_meets_state_criteria(i)) {
if (!found_device) {
*device = this->devices[i];
this->current_device_index = i;
found_device = true;
} else {
flags = flags & ~AT_BOTTOM;
break; // no need to search more than 1 device below
};
};
};
if (!found_device) {
return NO_DEVICES;
};
// index 0 always at top
// prevents overflow
if (this->current_device_index != 0) {
for (NUMBER i = this->current_device_index-1; i >= 0; i--) {
if (!this->devices_slots_free[i] && this->device_meets_state_criteria(i)) {
flags = flags & ~AT_TOP;
break;
};
};
} else {
flags = flags & ~AT_TOP;
}
switch (device->type) {
case Thermostat:
case Light:
case Speaker:
flags = flags | DISPLAY_POWER;
};
return flags;
};
DisplayFlags SmartHomeState::prev_device(Device* device) {
if (this->num_devices == 0) {
return NO_DEVICES;
};
NUMBER flags = AT_TOP | AT_BOTTOM;
bool found_device = false;
// scan for a device before the current index
// then again for another device to see if we are at the bottom
for (NUMBER i = this->current_device_index-1; i >= 0; i--) {
if (!this->devices_slots_free[i] && this->device_meets_state_criteria(i)) {
if (!found_device) {
*device = this->devices[i];
this->current_device_index = i;
found_device = true;
} else {
flags = flags & ~AT_TOP;
break; // no need to search more than 1 device below
};
};
};
if (!found_device) {
return NO_DEVICES;
};
// scan
for (NUMBER i = this->current_device_index+1; i < MAX_CAPACITY; i++) {
if (!this->devices_slots_free[i] && this->device_meets_state_criteria(i)) {
flags = flags & ~AT_BOTTOM;
break;
};
};
switch (device->type) {
case Thermostat:
case Light:
case Speaker:
flags = flags | DISPLAY_POWER;
};
return flags;
}
HRESULT SmartHomeState::set_device_state(char id[4], bool state) {
NUMBER index = this->get_device_index_by_id(id);
if (index == -1) {
return E_STATE_NO_KNOWN_DEVICE;
};
this->devices[index].state = state;
this->is_current = false;
return S_OK;
};
HRESULT SmartHomeState::set_device_power(char id[4], NUMBER power) {
NUMBER index = this->get_device_index_by_id(id);
if (index == -1) {
return E_STATE_NO_KNOWN_DEVICE;
};
switch (this->devices[index].type) {
case Thermostat:
if (power < 9 || power > 32) {
return E_COMMAND_VALUE_OUT_OF_RANGE;
};
break;
case Speaker:
case Light:
if (power < 0 || power > 100) {
return E_COMMAND_VALUE_OUT_OF_RANGE;
};
break;
default:
return E_COMMAND_DEVICE_FEATURE_MISMATCH;
};
this->devices[index].power = power;
this->is_current = false;
return S_OK;
};
void SmartHomeState::update_pressed_buttons(int state) {
unsigned long current_timestamp = millis();
const int buttons[NUM_BUTTONS] = {
BUTTON_UP,
BUTTON_DOWN,
BUTTON_LEFT,
BUTTON_RIGHT,
BUTTON_SELECT,
};
for (NUMBER i = 0; i < NUM_BUTTONS; i++) {
bool stored_as_down = (this->buttons_down_since[i] != 0);
bool button_is_down = (state & buttons[i]);
if (button_is_down && !stored_as_down) {
this->buttons_down_since[i] = current_timestamp;
} else if (!button_is_down && stored_as_down) {
this->buttons_down_since[i] = 0;
};
};
};
// This function may not be correct when a button is pressed down
// at one of every EXACTLY maxsize(unsigned long) ms
// but this is 1ms every ~50 days and is probably accceptable
bool SmartHomeState::button_down_for(int button, unsigned long time) {
unsigned long current_timestamp = millis();
const int buttons[NUM_BUTTONS] = {
BUTTON_UP,
BUTTON_DOWN,
BUTTON_LEFT,
BUTTON_RIGHT,
BUTTON_SELECT,
};
for (NUMBER i = 0; i < NUM_BUTTONS; i++) {
//Select the button
// this could be done with a map
// but that allocates on the heap
if (buttons[i] & button) {
unsigned long stored_timestamp = this->buttons_down_since[i];
return (stored_timestamp && ((current_timestamp - stored_timestamp) > time));
};
};
return false;
};
// writes over ANYTHING in the eeprom
HRESULT SmartHomeState::write_devices_to_eeprom() {
unsigned int eeprom_pointer = 0;
unsigned int eeprom_length = EEPROM.length()-1;
for (NUMBER i = 0; i < MAX_CAPACITY; i++) {
if (this->devices_slots_free[i]) {
continue;
}
unsigned char size = sizeof this->devices[i];
if ((eeprom_pointer + size + 4) > eeprom_length) {
return E_STATE_EEPROM_FULL;
};
// [SIG1, SIG2, SIZE, DEVICE... , TERMIATOR]
EEPROM.put(eeprom_pointer++, EEPROM_SIG_BYTE_1);
EEPROM.put(eeprom_pointer++, EEPROM_SIG_BYTE_2);
EEPROM.put(eeprom_pointer++, size);
EEPROM.put(eeprom_pointer, this->devices[i]);
eeprom_pointer += size;
EEPROM.put(eeprom_pointer++, EEPROM_TERMINATING_BYTE);
};
return S_OK;
}
NUMBER SmartHomeState::read_devices_from_eeprom() {
NUMBER devices_read = 0;
for (unsigned int eeprom_pointer = 0; eeprom_pointer < EEPROM.length(); eeprom_pointer++) {
if (EEPROM.read(eeprom_pointer) != EEPROM_SIG_BYTE_1) {
continue;
};
if (EEPROM.read(eeprom_pointer+1) != EEPROM_SIG_BYTE_2) {
continue;
};
unsigned char size = EEPROM.read(eeprom_pointer+2);
// this should remove almost every false positive
if (EEPROM.read(eeprom_pointer+3+size) != EEPROM_TERMINATING_BYTE) {
continue;
};
Device device;
EEPROM.get(eeprom_pointer+3, device);
if (this->add_device(device) == S_OK) {
devices_read++;
};
// set the pointer to the terminating byte
// because the next iteration will add 1 to the pointer
eeprom_pointer += 3+size;
};
return devices_read;
}
// UNSAFE - len must be appropriate
// len (may) include the null terminator
bool is_supported_char(char str[], int len, bool allow_lower) {
for (int i = 0; i < len; i++) {
int c = int(str[i]);
if (c == 0) { // No point checking past null terminator
return true;
};
// Could apply some boolean logic to normalise/simplify
// but it is humanly understandable this way
// if NOT (65 < c < 90 OR 96 < c < 123)
// basically if c is not A-Z or not a-z respectively
if (allow_lower) {
if (96 < c && c < 123) {
continue;
};
};
if ( (c < 65 || c > 90) ) {
return false;
};
};
return true;
};
// as log functions are often VERY expensive (can be up to 30x more cycles)
// for all circumstances using an approximate guess based on
// the fast log 2 using the leading zeros method would be better
// but considering our integers are (at most) 100
// this cheaper is function to count
// if we only need a few instructions to check 1, 10, 100
// compared to significantly more instructions for a constant time variant
unsigned int fast_int_log_10(unsigned int x) {
if (x < 10) {
return 1;
} else if (x < 100) {
return 2;
} else {
return 3;
}
}
void fill_char_with_int(char str[], int x, int digits) {
// we use digits as an array indexer
// to avoid having to reverse the array
// we can only do this because we know how
// many digits we want to display/pad
unsigned NUMBER digit_index = digits - 1;
unsigned NUMBER leading_zeros = digits - fast_int_log_10(x);
for (unsigned NUMBER i = 0; i < digits; i++) {
unsigned NUMBER remainder = x % 10;
x = x / 10;
str[digit_index] = char(remainder) + '0';
digit_index -=1;
};
for (unsigned NUMBER i = 0; i < leading_zeros; i++) {
str[i] = ' ';
};
};
// the code on learn didnt work for me (idk why)
// maybe because i have not allocated on the heap (i hope..)
uintptr_t calculate_free_memory() {
// ref to stack var can be treated
// as the top of stack pointer
char sp;
if ((uintptr_t) __brkval) {
// BOUNDRY BETWEEN STACK AND HEAP
return &sp - __brkval;
} else {
// start of the heap allocation
//return &sp - __malloc_heap_start;
// work out ourselves
uintptr_t ptr = malloc(1);
free((void *) ptr);
return &sp - ptr;;
};
};