-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdumbdisplay.cpp
More file actions
5302 lines (5071 loc) · 172 KB
/
dumbdisplay.cpp
File metadata and controls
5302 lines (5071 loc) · 172 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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Arduino.h>
//#include <math.h>
#include "dumbdisplay.h"
#include "__dd_cpp_include.h"
// #ifndef DD_INIT_LAYER_COUNT
// #define DD_INIT_LAYER_COUNT 5
// #endif
// #ifndef DD_LAYER_COUNT_INC
// #define DD_LAYER_COUNT_INC 3
// #endif
// #if DD_INIT_LAYER_COUNT < 1
// #error "DD_INIT_LAYER_COUNT must be at least 1"
// #endif
// #if DD_LAYER_COUNT_INC < 2
// #error "DD_LAYER_COUNT_INC must be at least 2"
// #endif
// #ifdef DD_NO_PASSIVE_CONNECT
// #warning ??? DD_NO_PASSIVE_CONNECT set ???
// #else
// #define SUPPORT_PASSIVE
// #define SUPPORT_MASTER_RESET
// #endif
// #ifdef DD_NO_FEEDBACK
// #warning ??? DD_NO_FEEDBACK set ???
// #else
// #define ENABLE_FEEDBACK
// #endif
// #ifdef ENABLE_FEEDBACK
// //#define FEEDBACK_BUFFER_SIZE 4
// #define HANDLE_FEEDBACK_DURING_DELAY
// #define READ_BUFFER_USE_BUFFER
// #ifndef DD_NO_FEEDBACK_BYTES
// #define FEEDBACK_SUPPORT_BYTES
// #endif
// #endif
// HAND_SHAKE_GAP changed from 1000 to 500 since 2024-06-22
#define HAND_SHAKE_GAP 500
//#define VALIDATE_GAP 2000
#define STORE_LAYERS
#define MORE_KEEP_ALIVE
#define SUPPORT_ENCODE_OPER
// #if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO)
// #define PGM_READ_BYTERS
// #endif
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO)
#define TL_BUFFER_DATA_LEN 16
#else
#define TL_BUFFER_DATA_LEN 128
#define SUPPORT_USE_WOIO
#endif
#define SUPPORT_CONTAINER
#define CONTAINER_LAYER_ID -9
#define CONTAINER_LAYER_ID_STR "-9"
#define SUPPORT_MY_STRTOK
#define TO_BOOL(val) (val ? "1" : "0")
//#define TO_EDIAN() String(DDCheckEndian()) since 2024-09-14, use the following
#define TO_EDIAN() TO_BOOL(DDCheckEndian())
//#define DD_DEBUG_BASIC
//#define DD_DEBUG_HS
//#define DD_DEBUG_SEND_COMMAND
//#define DEBUG_ECHO_COMMAND
//#define DEBUG_VALIDATE_CONNECTION
//#define DEBUG_RECEIVE_FEEDBACK
//#define DEBUG_ECHO_FEEDBACK
//#define DEBUG_SHOW_FEEDBACK
//#define DEBUG_TUNNEL_RESPONSE
//#define DEBUG_MISSING_ENDPOINT_C
//#define DEBUG_TUNNEL_RESPONSE_C
//#define DEBUG_SHOW_IN_LINE_CHARS
//#define DEBUG_READ_FEEDBACK_BYTES
//#define DEBUG_READ_PIXEL_IMAGE
//#define DEBUG_SUPPORT_MY_STRTOK
//#define SUPPORT_LONG_PRESS_FEEDBACK
#ifdef DD_NO_IDLE_CALLBACK
#warning ??? DD_NO_IDLE_CALLBACK set ???
#else
#define SUPPORT_IDLE_CALLBACK
#endif
#ifdef DD_NO_CONNECT_VERSION_CHANGED_CALLBACK
#warning ??? DD_NO_CONNECT_VERSION_CHANGED_CALLBACK set ???
#else
#define SUPPORT_CONNECT_VERSION_CHANGED_CALLBACK
#define FIRST_CONNECT_VERSION_CONSIDER_CHANGED
#endif
#ifdef DD_NO_TUNNEL
#warning ??? DD_NO_TUNNEL set ???
#else
#define SUPPORT_TUNNEL
#endif
//#define TUNNEL_TIMEOUT_MILLIS 30000
#define VALIDATE_CONNECTION
//#define DEBUG_WITH_LED
#define MASTER_RESET_KEEP_CONNECTED
#ifdef DD_NO_RECONNECT
#warning ??? DD_NO_RECONNECT set ???
#else
#define SUPPORT_RECONNECT
//#define RECONNECT_NO_KEEP_ALIVE_MILLIS 5000
#endif
#define VALIDATE_GAP 1000
#define RECONNECTING_VALIDATE_GAP 500
//#define SHOW_KEEP_ALIVE
//#define DEBUG_RECONNECT_WITH_COMMENT
//#define RECONNECTED_RESET_KEEP_ALIVE
// not flush seems to be a bit better for Serial (lost data) ... BUT ... seems require to flush for STM32 ... PASSIVE/blink/blink.ino
#define FLUSH_AFTER_SENT_COMMAND true
#define YIELD_AFTER_SEND_COMMAND false
#define YIELD_AFTER_HANDLE_FEEDBACK true
#ifdef DD_NO_DEBUG_INTERFACE
#warning ??? DD_NO_DEBUG_INTERFACE set ???
#else
#define SUPPORT_DEBUG_INTERFACE
#endif
// #ifdef DD_DEBUG_LESS_MEMORY_FOOTPRINT
// #warning ??? DD_DEBUG_LESS_MEMORY_FOOTPRINT set ???
// #undef SUPPORT_USE_WOIO
// // #undef SUPPORT_IDLE_CALLBACK
// // #undef SUPPORT_RECONNECT
// #undef SUPPORT_CONNECT_VERSION_CHANGED_CALLBACK
// #undef FIRST_CONNECT_VERSION_CONSIDER_CHANGED
// #undef SUPPORT_TUNNEL
// #undef SUPPORT_DEBUG_INTERFACE
// #endif
#define USE_MALLOC_FOR_LAYER_ARRAY
//#define DD_SID "Arduino-c9" // DD library version (compatibility)
//#define DD_SID "Arduino-c10" // DD library version (EXPECTED_DD_LIB_COMPATIBILITY) ... since v0.9.9-v30
//#define DD_SID "Arduino-c11" // DD library version (EXPECTED_DD_LIB_COMPATIBILITY) ... since v0.9.9-v31
//#define DD_SID "Arduino-c12" // DD library version (EXPECTED_DD_LIB_COMPATIBILITY) ... since v0.9.9-v34
//#define DD_SID "Arduino-c13" // DD library version (EXPECTED_DD_LIB_COMPATIBILITY) ... since v0.9.9-v40
//#define DD_SID "Arduino-c14" // DD library version (EXPECTED_DD_LIB_COMPATIBILITY) ... since v0.9.9-v50
//#define DD_SID "Arduino-c15" // DD library version (EXPECTED_DD_LIB_COMPATIBILITY) ... since v0.9.9-v53
#define DD_SID "Arduino-c16" // DD library version (EXPECTED_DD_LIB_COMPATIBILITY) ... since v0.9.9-v56 .. pin layers landscape feature not yet used
#include "_dd_commands.h"
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) || defined(ARDUINO_AVR_MEGA2560)
// void display_freeram() {
// Serial.print(F("- SRAM left: "));
// Serial.println(freeRam());
// }
int freeRam() { // https://docs.arduino.cc/learn/programming/memory-guide/?_gl=1*1tn4gcs*_gcl_au*MTQwODU3MTY5MS4xNzE4Mzc0MjIy*FPAU*MTQwODU3MTY5MS4xNzE4Mzc0MjIy*_ga*NDU2NzU2Mzg0LjE3MDk2NDIyMTk.*_ga_NEXN8H46L5*MTcyMzcyMzQzNi40NC4xLjE3MjM3MjQyNzUuMC4wLjUyNDIxMTk0MQ..*_fplc*RUtpd2ZNeFBsZXQ5bzhWamxXemRJTFclMkYzRWwzVE1zayUyQko3Nmg1U1htVXJCb3NuS2xLd05pR3h1cGxxZUZPNEZzN1JSeThtSWdBa21SWkVTbG1YcktZY0JTZ0xrRVlWdW1ZakszVjl1OTBZbVRFelFVQkVFY1BiaEZPUEsyQSUzRCUzRA..
extern int __heap_start,*__brkval;
int v;
return (int)&v - (__brkval == 0
? (int)&__heap_start : (int) __brkval);
}
#endif
long _DDIdleTimeoutMillis = DD_DEF_IDLE_TIMEOUT;
bool _DDDisableParamEncoding = false;
#ifdef SUPPORT_USE_WOIO
class DDWriteOnyIO: public DDInputOutput {
public:
DDWriteOnyIO(DDInputOutput* io, uint16_t bufferSize = 8/*256*/): io(io) {
this->bufferSize = bufferSize;
this->buffer = new uint8_t[bufferSize];
this->bufferedCount = 0;
this->keepBuffering = false;
}
~DDWriteOnyIO() {
delete this->buffer;
}
void print(const String &s) {
print(s.c_str());
}
void print(const char *p) {
int len = strlen(p);
write((uint8_t*) p, len);
// const char *c = p;
// while (true) {
// if (*c == 0) {
// break;
// }
// c++;
// }
// int count = c - p;
// write((uint8_t*) p, count);
}
void write(uint8_t b) {
write(&b, 1);
}
void write(const uint8_t *buf, size_t size) {
if ((bufferedCount + size) > bufferSize) {
_flush();
}
if (size > bufferSize) {
_flush();
io->write(buf, size);
} else {
const uint8_t *s = buf;
uint8_t *t = buffer + bufferedCount;
bool flushAfterward = false;
for (size_t i = 0; i < size; i++) {
if (!this->keepBuffering) {
if (*s == '\n') {
flushAfterward = true;
}
}
*t = *s;
s++;
t++;
bufferedCount++;
}
if (flushAfterward) {
_flush();
}
}
}
inline void flush() {
if (this->keepBuffering) {
return;
}
_flush();
}
public:
void setKeepBuffering(bool keep) {
this->keepBuffering = keep;
}
private:
void _flush() {
if (bufferedCount > 0) {
if (false) {
Serial.print(". flush ");
Serial.print(bufferedCount);
if (this->keepBuffering) {
Serial.print(" ... KEEP");
}
Serial.println();
}
io->write(buffer, bufferedCount);
bufferedCount = 0;
}
}
private:
DDInputOutput* io;
uint8_t bufferSize;
uint8_t* buffer;
uint8_t bufferedCount;
bool keepBuffering;
};
#endif
#define YIELD() delay(1)
#ifdef DDIO_USE_DD_SERIAL
DDSerial* _The_DD_Serial = NULL;
#endif
namespace DDImpl {
bool _CanLogToSerial();
class IOProxy {
public:
IOProxy(DDInputOutput *pIO) {
this->pIO = pIO;
#ifdef SUPPORT_RECONNECT
this->reconnectKeepAliveMillis = 0;
this->lastKeepAliveMillis = 0;
this->reconnectEnabled = false;
#endif
this->reconnecting = false;
}
const char* getWhat() {
return pIO->getWhat();
}
bool available();
inline const String& get() {
return data;
}
inline void clear() {
data = "";
}
inline void print(const String &s) {
pIO->print(s);
}
inline void print(const char *p) {
pIO->print(p);
}
inline void write(uint8_t b) {
pIO->write(b);
}
void keepAlive();
void validConnection();
void setReconnectRCId(const String& rcId) {
#ifdef SUPPORT_RECONNECT
this->reconnectRCId = rcId;
this->reconnectEnabled = true;
this->reconnectKeepAliveMillis = 0;
#endif
}
bool isReconnecting() {
return this->reconnecting;
}
#ifdef FEEDBACK_SUPPORT_BYTES
inline bool charAvailable() {
return pIO->available();
}
char getChar() {
return pIO->read();
}
#endif
private:
DDInputOutput *pIO;
bool fromSerial;
String data;
#if defined (SUPPORT_IDLE_CALLBACK) || defined (SUPPORT_RECONNECT)
unsigned long lastKeepAliveMillis;
#endif
#ifdef SUPPORT_RECONNECT
bool reconnectEnabled;
String reconnectRCId;
long reconnectKeepAliveMillis;
#endif
bool reconnecting;
};
/*volatile */bool _Connected = false;
/*volatile */int _ConnectVersion = 0;
//bool _Reconnecting = false;
#ifdef SUPPORT_IDLE_CALLBACK
/*volatile */DDIdleCallback _IdleCallback = NULL;
#endif
#ifdef SUPPORT_CONNECT_VERSION_CHANGED_CALLBACK
/*volatile */DDConnectVersionChangedCallback _ConnectVersionChangedCallback = NULL;
#endif
#ifdef SUPPORT_DEBUG_INTERFACE
DDDebugInterface *_DebugInterface;
#endif
bool IOProxy::available() {
bool done = false;
while (!done && pIO->available()) {
char c = pIO->read();
if (c == '\n') {
done = true;
} else {
#ifdef DEBUG_SHOW_IN_LINE_CHARS
int dataLen = data.length();
if ((dataLen == 0 && c != '<') || (dataLen > 0 && dataLen < 30)) {
Serial.print(". in:[");
Serial.print(c);
Serial.println("]");
}
#endif
data += c;
// data = data + c;
}
}
return done;
}
// const String& IOProxy::get() {
// return data;
// }
// void IOProxy::clear() {
// //data.remove(0, data.length());
// data = "";
// }
// void IOProxy::print(const String &s) {
// pIO->print(s);
// }
// void IOProxy::print(const char *p) {
// pIO->print(p);
// }
// void IOProxy::write(uint8_t b) {
// pIO->write(b);
// }
void IOProxy::keepAlive() {
#if defined (SHOW_KEEP_ALIVE) || defined(DEBUG_RECONNECT_WITH_COMMENT)
this->print("// KEEP ALIVE\n");
#endif
#ifdef SUPPORT_RECONNECT
this->lastKeepAliveMillis = millis();
#endif
pIO->keepAlive();
}
void IOProxy::validConnection() {
#ifdef DEBUG_VALIDATE_CONNECTION
Serial.print("... validate connection ... ");
Serial.print(lastKeepAliveMillis);
if (reconnectEnabled) {
Serial.print(" ... RE enabled ");
}
Serial.println(" ...");
#endif
pIO->validConnection();
#if defined(SUPPORT_MASTER_RESET)
if (lastKeepAliveMillis == 0) { // since 2023-06-02
lastKeepAliveMillis = millis(); // don't wait for keep alive
}
#endif
#if defined (SUPPORT_IDLE_CALLBACK) || defined (SUPPORT_RECONNECT)
bool needReconnect = false;
if (this->lastKeepAliveMillis > 0) {
long now = millis();
long notKeptAliveMillis = now - this->lastKeepAliveMillis;
if (notKeptAliveMillis > _DDIdleTimeoutMillis) {
needReconnect = true;
#ifdef SUPPORT_IDLE_CALLBACK
if (_IdleCallback != NULL) {
long idleForMillis = notKeptAliveMillis - _DDIdleTimeoutMillis;
_IdleCallback(idleForMillis, DDIdleConnectionState::IDLE_RECONNECTING);
}
#endif
#ifdef DD_DEBUG_BASIC
if (_CanLogToSerial()) Serial.println("!!! 'keep alive' message not received for too long ==> reconnect");
#endif
}
#ifdef SUPPORT_MASTER_RESET
if (needReconnect) {
this->reconnecting = true;
}
#endif
#ifdef SUPPORT_RECONNECT
if (this->reconnectEnabled && needReconnect) {
this->reconnecting = true;
#ifdef SUPPORT_DEBUG_INTERFACE
if (_DebugInterface != NULL) {
_DebugInterface->logConnectionState(DDDebugConnectionState::DEBUG_RECONNECTING);
}
#endif
YIELD();
#ifdef DEBUG_RECONNECT_WITH_COMMENT
this->print("// NEED TO RECONNECT\n");
#endif
#ifdef DEBUG_VALIDATE_CONNECTION
Serial.print("=== reconnect: ");
Serial.println(this->reconnectRCId);
#endif
this->print("%%>RECON>");
this->print(DD_SID);
this->print(":");
this->print(this->reconnectRCId);
// if (!_EnableDoubleClick) {
// this->print(",dblclk=0");
// }
this->print("\n");
this->reconnectKeepAliveMillis = this->lastKeepAliveMillis;
} else if (this->reconnectKeepAliveMillis > 0) {
this->reconnecting = false;
_ConnectVersion = _ConnectVersion + 1;
#ifdef SUPPORT_CONNECT_VERSION_CHANGED_CALLBACK
if (_ConnectVersionChangedCallback != NULL) {
_ConnectVersionChangedCallback(_ConnectVersion);
}
#endif
#ifdef DEBUG_RECONNECT_WITH_COMMENT
this->print("// DETECTED RECONNECTION\n");
#endif
#ifdef DEBUG_VALIDATE_CONNECTION
Serial.print("*** reconnected: ");
Serial.println(_ConnectVersion);
#endif
this->reconnectKeepAliveMillis = 0;
#ifdef RECONNECTED_RESET_KEEP_ALIVE
this->lastKeepAliveMillis = millis();
#endif
#ifdef SUPPORT_DEBUG_INTERFACE
if (_DebugInterface != NULL) {
_DebugInterface->logConnectionState(DDDebugConnectionState::DEBUG_RECONNECTED);
}
#endif
}
#endif
}
#endif
}
//volatile bool _Preconneced = false;
//volatile bool _Connected = false;
/*volatile */short _DDCompatibility = 0; // see DD_APP_COMPATIBILITY
/*volatile */int _NextLid = 0;
/*volatile*/int _NextImgId = 0;
/*volatile*/int _NextBytesId = 0;
DDObject** _DDLayerArray = NULL;
int _MaxDDLayerCount = 0;
DDInputOutput* /*volatile */_DDIO = NULL;
#ifdef SUPPORT_USE_WOIO
DDInputOutput* /*volatile */_WODDIO = NULL;
/*volatile */uint16_t _SendBufferSize = 0;//DD_DEF_SEND_BUFFER_SIZE;
#else
#define _WODDIO _DDIO
#endif
#ifdef SUPPORT_CONTAINER
GraphicalDDLayer* __RootLayer = NULL;
#endif
inline void _SetIO(DDInputOutput* io, uint16_t sendBufferSize, long idleTimeout) {
_DDIO = io;
#ifdef SUPPORT_USE_WOIO
if (_WODDIO != NULL) delete _WODDIO;
if (sendBufferSize > 0 && io->canUseBuffer()) {
_SendBufferSize = sendBufferSize;
_WODDIO = new DDWriteOnyIO(io, sendBufferSize);
} else {
_SendBufferSize = 0;
_WODDIO = io;
}
#endif
_DDIdleTimeoutMillis = idleTimeout;
}
IOProxy* /*volatile */_ConnectedIOProxy = NULL;
/*volatile */bool _ConnectedFromSerial = false;
bool _CanLogToSerial() {
if (_ConnectedIOProxy != NULL) {
return !_ConnectedFromSerial;
} else {
return _DDIO != NULL && !(_DDIO->isForSerial() || _DDIO->isBackupBySerial());
}
}
// #ifdef DEBUG_WITH_LED
// /*volatile */int _DebugLedPin = -1;
// #endif
#ifdef DEBUG_ECHO_FEEDBACK
/*volatile */bool _DebugEnableEchoFeedback = false;
#endif
// #ifdef DD_CAN_TURN_OFF_CONDENSE_COMMAND
// volatile bool _NoEncodeInt = false;
// #endif
#define IS_FLOAT_ZERO(f) ((((f)<0?-(f):(f)) - 0.0) < 0.001)
#define IS_FLOAT_WHOLE(f) IS_FLOAT_ZERO((f) - (int) (f))
#define DD_FLOAT_DP 3
#ifdef DD_CONDENSE_COMMAND
#define TO_C_INT(i) (DDIntEncoder(i).encoded())
// #ifdef DD_CAN_TURN_OFF_CONDENSE_COMMAND
// #define TO_C_INT(i) (_NoEncodeInt ? String(i) : DDIntEncoder(i).encoded())
// #else
// #define TO_C_INT(i) (DDIntEncoder(i).encoded())
// #endif
#define TO_NUM(num) IS_FLOAT_WHOLE(num) ? String((int) num) : String(num, DD_FLOAT_DP)
#else
#define TO_C_INT(i) String(i)
#define TO_NUM(num) String(num)
#endif
#define TO_C_NUM(num) IS_FLOAT_WHOLE(num) ? TO_C_INT((int) num) : String(num, DD_FLOAT_DP)
void __SendErrorComment(const char* comment);
volatile bool _SendingCommand = false;
volatile bool _HandlingFeedback = false;
#ifdef SUPPORT_PASSIVE
#define _C_PRECONNECTING 1
#define _C_PRECONNECTED 3
#define _C_IOPROXY_SET 4
#define _C_HANDSHAKE 5
//#define _C_MASTER_RESET 6
struct _ConnectState {
_ConnectState(): step(0) {}
short step;
long startMillis;
long lastCallMillis;
bool firstCall;
long hsStartMillis;
long hsNextMillis;
IOProxy* pIOProxy;
IOProxy* pBUSerialIOProxy;
DDInputOutput *pBUSIO;
short compatibility;
};
_ConnectState _C_state;
bool __Connect(/*bool calledPassive = false*/) {
// #if defined(MASTER_RESET_KEEP_CONNECTED)
// if (_C_state.step == _C_MASTER_RESET) {
// _C_state.step = 0;
// return false;
// }
// #endif
bool mustLoop = !_DDIO->canConnectPassive();
if (_C_state.step > 0 && _C_state.hsStartMillis > 0) {
long diffMillis = millis() - _C_state.hsStartMillis;
if (diffMillis > _DDIdleTimeoutMillis) {
// start over
_C_state.step = 0;
return false;
}
}
// Serial.print(">");
// Serial.print(_C_state.step);
if (_C_state.step == 0) {
_C_state.startMillis = millis();
_C_state.lastCallMillis = _C_state.startMillis;
_C_state.hsStartMillis = 0;
_C_state.firstCall = true;
_C_state.step = _C_PRECONNECTING/*1*/;
#ifdef SUPPORT_DEBUG_INTERFACE
if (_DebugInterface != NULL) {
_DebugInterface->logConnectionState(DDDebugConnectionState::DEBUG_NOT_CONNECTED);
}
#endif
}
if (_C_state.step == _C_PRECONNECTING/*1*/) {
if (!_DDIO->preConnect(_C_state.firstCall)) {
#ifdef SUPPORT_IDLE_CALLBACK
bool checkIdle = _IdleCallback != NULL;
if (checkIdle/*!_IsInPassiveMode && _IdleCallback != NULL*/) {
long now = millis();
long diffMillis = now - _C_state.lastCallMillis;
if (diffMillis >= HAND_SHAKE_GAP) {
long idleForMillis = now - _C_state.startMillis;
_IdleCallback(idleForMillis, DDIdleConnectionState::IDLE_NOT_CONNECTED);
_C_state.lastCallMillis = now;
}
}
#endif
_C_state.firstCall = false;
return false;
}
_C_state.step = _C_PRECONNECTED/*3*//*2*/;
#ifdef SUPPORT_DEBUG_INTERFACE
if (_DebugInterface != NULL) {
_DebugInterface->logConnectionState(DDDebugConnectionState::DEBUG_CONNECTING);
}
#endif
}
if (_C_state.step == _C_PRECONNECTED/*3*/) {
_C_state.hsNextMillis = millis();
//IOProxy ioProxy(_DDIO);
if (_C_state.pIOProxy != NULL) {
delete _C_state.pIOProxy;
}
if (_C_state.pBUSerialIOProxy != NULL) {
delete _C_state.pBUSerialIOProxy;
}
if (_C_state.pBUSIO != NULL) {
delete _C_state.pBUSIO;
}
_C_state.pIOProxy = new IOProxy(_DDIO);
_C_state.pBUSerialIOProxy = NULL;
_C_state.pBUSIO = NULL;
if (_DDIO->isBackupBySerial()) {
//pSIO = new DDInputOutput(_DDIO);
_C_state.pBUSIO = _DDIO->newForSerialConnection();
_C_state.pBUSerialIOProxy = new IOProxy(_C_state.pBUSIO);
}
_C_state.hsStartMillis = millis();
_C_state.step = _C_IOPROXY_SET/*4*/;
// faster
//return false;
}
if (_C_state.step == _C_IOPROXY_SET/*4*/) {
while (true) {
if (mustLoop) YIELD();
long now = millis();
if (now > _C_state.hsNextMillis) {
#ifdef SUPPORT_DEBUG_INTERFACE
if (_DebugInterface != NULL) {
_DebugInterface->logConnectionState(DDDebugConnectionState::DEBUG_CONNECTING);
}
#endif
_C_state.pIOProxy->print("ddhello\n");
if (_C_state.pBUSerialIOProxy != NULL) {
#if defined(DD_EXPERIMENTAL)
_C_state.pBUSerialIOProxy->print("ddhello");
const char* viaWhat = _C_state.pIOProxy->getWhat();
if (viaWhat != NULL) {
_C_state.pBUSerialIOProxy->print("__via:");
_C_state.pBUSerialIOProxy->print(viaWhat);
}
_C_state.pBUSerialIOProxy->print("\n");
#else
_C_state.pBUSerialIOProxy->print("ddhello\n");
#endif
}
#ifdef DD_DEBUG_HS
Serial.println("handshake:ddhello");
#endif
#ifdef SUPPORT_IDLE_CALLBACK
bool checkIdle = _IdleCallback != NULL;
if (checkIdle/*!_IsInPassiveMode && _IdleCallback != NULL*/) {
long idleForMillis = now - _C_state.startMillis;
_IdleCallback(idleForMillis, DDIdleConnectionState::IDLE_CONNECTING);
}
#endif
_C_state.hsNextMillis = now + HAND_SHAKE_GAP;
}
bool fromBUSerial = false;
bool available = _C_state.pIOProxy->available();
if (!available && _C_state.pBUSerialIOProxy != NULL) {
if (_C_state.pBUSerialIOProxy->available()) {
available = true;
fromBUSerial = true;
}
}
if (available) {
const String& data = fromBUSerial ? _C_state.pBUSerialIOProxy->get() : _C_state.pIOProxy->get();
#ifdef DD_DEBUG_HS
Serial.println("handshake:data-[" + data + "]");
#endif
if (data == "ddhello") {
if (fromBUSerial) {
_SetIO(_C_state.pBUSIO, DD_DEF_SEND_BUFFER_SIZE, DD_DEF_IDLE_TIMEOUT);
//_DDIO = pSIO;
_C_state.pBUSIO = NULL;
}
if (_ConnectedIOProxy != NULL) {
delete _ConnectedIOProxy;
}
_ConnectedIOProxy = new IOProxy(_DDIO);
// _ConnectedFromSerial = fromSerial;
//_ConnectedFromSerial = _DDIO->isSerial();
_ConnectedFromSerial = fromBUSerial || _DDIO->isSerial();
#ifdef DD_DEBUG_BASIC
if (_CanLogToSerial()) Serial.println("--- connection established");
#endif
if (_CanLogToSerial()) {
Serial.println("**********");
#ifdef DD_DEBUG_BASIC
Serial.print("* _DDIO.isSerial()=");
Serial.println(_DDIO->isSerial());
Serial.print("* _DDIO.isForSerial()=");
Serial.println(_DDIO->isForSerial());
Serial.print("* _DDIO.isBackupBySerial()=");
Serial.println(_DDIO->isBackupBySerial());
Serial.print("* _DDIO.canConnectPassive()=");
Serial.println(_DDIO->canConnectPassive());
Serial.print("* _DDIO.canUseBuffer()=");
Serial.println(_DDIO->canUseBuffer());
#endif
#ifdef SUPPORT_USE_WOIO
Serial.print("* _SendBufferSize=");
Serial.println(_SendBufferSize);
#endif
//Serial.print("* _EnableDoubleClick=");
//Serial.println(_EnableDoubleClick ? "yes" : "no");
Serial.println("**********");
Serial.flush();
}
_C_state.step = _C_HANDSHAKE/*5*/;
// if (mustLoop) {
// break;
// }
// return false;
break; // faster
}
#ifdef DD_DEBUG_HS
if (_ConnectedIOProxy != NULL) {
Serial.println("handshake:DONE");
} else {
Serial.println("handshake:... got=[" + data + "]");
}
#endif
if (fromBUSerial)
_C_state.pBUSerialIOProxy->clear();
else
_C_state.pIOProxy->clear();
}
if (!mustLoop) {
break;
}
}
// moved up
// if (pSerialIOProxy != NULL)
// delete pSerialIOProxy;
// if (pSIO != NULL)
// delete pSIO;
if (!mustLoop) {
return false;
}
//Serial.print("$$$$$$$$$$ "); Serial.println(_C_state.step);
}
//int compatibility = 0;
if (_C_state.step == _C_HANDSHAKE/*5*/) {
//_C_state.compatibility = 0;
if (true) {
// since 2024-11-08 ... faster sent >init>
_C_state.hsNextMillis = millis() + HAND_SHAKE_GAP;
} else {
_C_state.hsNextMillis = millis();
}
_C_state.step = 6;
}
if (_C_state.step == 6) {
//IOProxy ioProxy(_DDIO);
while (true) {
if (mustLoop) YIELD();
long now = millis();
if (now > _C_state.hsNextMillis) {
// #ifdef DEBUG_WITH_LED
// if (debugLedPin != -1) {
// debugLedOn = !debugLedOn;
// digitalWrite(debugLedPin, debugLedOn ? HIGH : LOW);
// }
// #endif
//Serial.println((_ConnectedFromSerial ? "SERIAL" : "NON-SERIAL"));
//ioProxy.print(">init>:Arduino-c1\n");
_ConnectedIOProxy->/*ioProxy.*/print(">init>:");
_ConnectedIOProxy->/*ioProxy.*/print(DD_SID);
// if (!_EnableDoubleClick) {
// ioProxy.print(",dblclk=0");
// }
_ConnectedIOProxy->/*ioProxy.*/print("\n");
_C_state.hsNextMillis = now + HAND_SHAKE_GAP;
#ifdef DD_DEBUG_HS
Serial.println("handshake:sent-ddinit");
#endif
}
if (_ConnectedIOProxy->/*ioProxy.*/available()) {
const String& data = _ConnectedIOProxy->/*ioProxy.*/get();
short compatibility = -1;
if (data == "<init<") {
//break;
compatibility = 0;
//_C_state.step = 7;
//return false;
} else if (data.startsWith("<init<:")) {
compatibility = data.substring(7).toInt();
//break;
//_C_state.step = 7;
//return false;
}
if (compatibility != -1) {
_C_state.compatibility = compatibility;
_C_state.step = 7;
if (true) {
// since 2023-06-17
_ConnectedIOProxy->/*ioProxy.*/clear();
}
if (mustLoop) {
break;
}
return false;
}
_ConnectedIOProxy->/*ioProxy.*/clear();
}
if (!mustLoop) {
break;
}
}
if (!mustLoop) {
return false;
}
}
_Connected = true;
_ConnectVersion = 1;
// _ConnectedIOProxy = new IOProxy(_DDIO);
_DDCompatibility = _C_state.compatibility;
if (false) {
// ignore any input in 1000ms window
delay(1000);
while (_DDIO->available()) {
_DDIO->read();
}
}
if (true) {
_DDIO->print("// Connected to DD c" + String(_C_state.compatibility) + "\n"/*.c_str()*/);
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) || defined(ARDUINO_AVR_MEGA2560)
_DDIO->print("// $ Free SRAM: " + String(freeRam() / 1024.0) + "KB" + "\n");
#elif defined(ESP32)
_DDIO->print(String("// $ Sketch: ") + String(ESP.getSketchSize() / 1024.0) + "KB" + " / Free: " + String(ESP.getFreeSketchSpace() / 1024.0) + "KB" + "\n");
_DDIO->print(String("// $ Heap: " + String(ESP.getHeapSize() / 1024.0) + "KB") + " / Free: " + String(ESP.getFreeHeap() / 1024.0) + "KB" + "\n");
_DDIO->print(String("// $ PSRAM: " + String(ESP.getPsramSize() / 1024.0) + "KB") + " / Free: " + String(ESP.getFreePsram() / 1024.0) + "KB" + "\n");
#endif
//_DDIO->flush();
if (false) {
// *** debug code
for (int i = 0; i < 10; i++) {
delay(500);
_DDIO->print("// connected to DD c" + String(_C_state.compatibility) + "\n"/*.c_str()*/);
}
}
#ifdef DD_DEBUG_HS
Serial.println("// *** CONNECTED");
#endif
_C_state.step = 0;
}
// #ifdef DEBUG_WITH_LED
// if (debugLedPin != -1) {
// digitalWrite(debugLedPin, LOW);
// }
// #endif
// if (false) {
// // *** debug code
// _DDIO->print("// connection to DD made\n");
// _sendCommand0("", "// *** connection made ***");
// }
#ifdef DD_DEBUG_HS
Serial.println("// *** DONE MAKE CONNECTION");
#endif
#if defined(SUPPORT_CONNECT_VERSION_CHANGED_CALLBACK) && defined(FIRST_CONNECT_VERSION_CONSIDER_CHANGED)
if (_ConnectVersionChangedCallback != NULL) {
_ConnectVersionChangedCallback(_ConnectVersion); // will be 1
}
#endif
return true;
}
bool _Connect(bool calledPassive = false) {
if (_Connected)
return true;
if (!calledPassive) {
_C_state.step = 0;
}
while (true) {
YIELD();
if (__Connect()) {
#ifdef SUPPORT_DEBUG_INTERFACE
if (_DebugInterface != NULL) {
_DebugInterface->logConnectionState(DDDebugConnectionState::DEBUG_CONNECTED);
}
#endif
return true;
}
if (calledPassive) {
return false;
}
}
}
#else
void _Connect(/*long maxWaitMillis = -1, bool calledPassive = false*/) {
if (_Connected)
return;
{