-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_new.cpp
682 lines (598 loc) · 17.2 KB
/
main_new.cpp
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
#include "Files.h"
#include "MicroBitConfig.h"
#include "MicroBit.h"
#include "MicroBitUARTService.h"
#include "Set.h"
#include "TwoDigitDisplay.h"
#include "repcount.h"
#define INCREMENT_A 10
#define INCREMENT_B 1
#define INPUTLAG 70
#define LONGTEXTDELAY 65
#define WAITFORINPUT 400
#define DISPLAYTIME 50
#define PAUSEMULTIPLIKATOR 2562
//#define QUICKTEST
#define BLETEST
MicroBit uBit;
MicroBitUARTService *uart;
TwoDigitDisplay twodigit;
ManagedString filename;
Set meinsatz;
ManagedString delimiter("\n");
ManagedString first_start;
ManagedString pausestring("pause");
//TODO : fiber that checks for uart input. or via eventservice. so app sends "send log"
// and microbit sends logfile to the app. independent of main thread.
// racecondition check if file is written in main_set_input_loop() or something
int pause_setting=PAUSEMULTIPLIKATOR;
int inputBuff = 0;
int inputterm = 0;
int terminator = 0;
int connected = 0;
int goback = 0;
void myFiber(){
int i =0;
while(true){
uBit.serial.printf("fiber: %d \n",i);
i++;
uBit.sleep(1000);
}
}
void nothing(MicroBitEvent) {}
int int_from_serial() {
return atoi(uBit.serial.readUntil(delimiter).toCharArray());
}
ManagedString string_from_serial() { return uBit.serial.readUntil(delimiter, SYNC_SPINWAIT); }
void inputButton(MicroBitEvent e) {
inputBuff = 0;
if (e.source == MICROBIT_ID_BUTTON_A) {
if (e.value == MICROBIT_BUTTON_EVT_CLICK)
inputBuff = inputBuff + INCREMENT_A;
if (e.value == MICROBIT_BUTTON_EVT_HOLD) {
inputBuff = inputBuff - INCREMENT_A;
uBit.sleep(300);
while (uBit.buttonA.isPressed()) {
uBit.sleep(150);
inputBuff = inputBuff - INCREMENT_A;
}
}
}
if (e.source == MICROBIT_ID_BUTTON_B) {
if (e.value == MICROBIT_BUTTON_EVT_CLICK)
inputBuff = inputBuff + INCREMENT_B;
if (e.value == MICROBIT_BUTTON_EVT_HOLD) {
inputBuff = inputBuff - INCREMENT_B;
uBit.sleep(300);
while (uBit.buttonB.isPressed()) {
uBit.sleep(150);
inputBuff = inputBuff - INCREMENT_B;
}
}
}
}
ManagedString string_from_ble() { return uart->readUntil(delimiter); }
int input() {
int ret = 0;
while (true) {
ret = ret + inputBuff;
inputBuff = 0;
// uBit.display.printAsync(ret);
uBit.display.printAsync(twodigit.createImage(ret));
uBit.sleep(INPUTLAG);
// uBit.display.clear();
if (inputterm == 1) {
inputterm = 0;
return ret;
}
}
}
int input(int previous_val) {
int ret = previous_val;
int i = 0;
while (true) {
if (inputBuff == 0 and i != 0) {
/*if (goback != 0) {
return ret;
}*/
if (inputterm == 1) {
inputterm = 0;
return ret;
}
if (i > DISPLAYTIME) {
uBit.display.clear();
}
}
else {
i = 0;
ret = ret + inputBuff;
ret = abs(ret);
inputBuff = 0;
uBit.display.printAsync(twodigit.createImage(ret));
}
uBit.sleep(INPUTLAG);
i++;
}
}
int input(const char *message) {
int ret = 0;
uBit.display.print(message);
while (0 == uBit.buttonA.isPressed() and 0 == uBit.buttonB.isPressed()) {
uBit.sleep(INPUTLAG);
}
int i = 0;
while ((true)) {
if (inputBuff == 0 and i != 0) {
if (goback != 0) {
return ret;
}
if (inputterm == 1) {
inputterm = 0;
return ret;
}
if (i > DISPLAYTIME) {
uBit.display.clear();
}
}
else {
i = 0;
ret = ret + inputBuff;
ret = abs(ret);
inputBuff = 0;
uBit.display.printAsync(twodigit.createImage(ret));
}
uBit.sleep(INPUTLAG);
i++;
}
}
int input(char message) {
int ret = 0;
uBit.display.printChar(message);
while (0 == uBit.buttonA.isPressed() and 0 == uBit.buttonB.isPressed()) {
uBit.sleep(INPUTLAG);
}
while (true) {
ret = ret + inputBuff;
inputBuff = 0;
// uBit.display.printAsync(ret);
uBit.display.printAsync(twodigit.createImage(ret));
uBit.sleep(INPUTLAG);
// uBit.display.clear();
if (inputterm == 1) {
inputterm = 0;
return ret;
}
}
}
///*time in 1/10 sekunden
int input(int time, int previous_val) {
int ret = previous_val;
int i = 0;
while ((i < time)) {
if (inputBuff == 0 and i != 0) {
if (goback != 0) {
return ret;
}
if (inputterm == 1) {
inputterm = 0;
return ret;
}
if (i > DISPLAYTIME) {
uBit.display.clear();
}
}
else {
i = 0;
ret = ret + inputBuff;
ret = abs(ret);
// uBit.display.printAsync(ret);
inputBuff = 0;
uBit.display.printAsync(twodigit.createImage(ret));
}
uBit.sleep(INPUTLAG);
//if (goback != 0) {
// return ret;
//}
//if (inputterm == 1) {
// inputterm = 0;
// return ret;
//}
i++;
}
// uBit.display.clear();
uBit.io.P1.setDigitalValue(0);
uBit.io.P8.setDigitalValue(0);
return ret;
}
void serial_input(MicroBitEvent e) {
ManagedString buffer;
buffer = string_from_serial();
uBit.serial.printf("input: %s", buffer.toCharArray());
// meinsatz.set_name(buffer);
}
void main_set_input_loop() {
uBit.serial.printf("main_set_input_loop()\n");
int loop_case = 0;
int oldname;
while (true) {
// prüfe ob Set Loop verlassen werden soll.
loop_case = loop_case % 3;
switch (loop_case)
{
case 0:
uBit.display.scroll("ID");
uBit.display.clear();
oldname = meinsatz.get_ID();
meinsatz.set_ID(input(WAITFORINPUT, meinsatz.get_ID()));
uBit.sleep(500);
if (oldname != meinsatz.get_ID()) {
meinsatz.set_name(read_to_string(ManagedString(meinsatz.get_ID())));
uBit.display.scroll(meinsatz.get_name(), LONGTEXTDELAY);
}
if (meinsatz.get_name() == ManagedString("")) {
meinsatz.set_name(ManagedString("-"));
}
uBit.sleep(500);
loop_case++;
break;
case 1:
uBit.display.scroll("Kg", LONGTEXTDELAY);
if (goback != 0) {
break;
}
meinsatz.set_weight(input(WAITFORINPUT, meinsatz.get_weight()));
uBit.sleep(500);
loop_case += 1 - goback;
break;
case 2:
uBit.display.scroll("WDH", LONGTEXTDELAY);
if (goback != 0) {
break;
}
meinsatz.set_reps(repcount(inputBuff, inputterm, goback, pause_setting));
uBit.sleep(500);
uBit.display.clear();
loop_case += 1 - goback;
break;
default:
break;
}
goback = 0;
if (terminator == 1) {
terminator = 0;
break;
}
}
uBit.serial.printf("writeSettoFile\n");
// meinsatz.write_to_file(filename); // schreibe den Satz ins file
uBit.sleep(500);
return;
}
/**
* TODO das muss gelöscht werden, code smell
*/
void catalog_read(MicroBitEvent) {
ManagedString exercisenumber;
ManagedString exercisename;
uBit.serial.printf("starte\n");
uBit.display.scroll("usb input");
do {
exercisenumber = string_from_serial();
uBit.sleep(5);
exercisename = string_from_serial();
uBit.sleep(5);
if (!remove_file(exercisenumber)) {
uBit.serial.printf("failed to remove file");
}
uBit.sleep(5);
appendLine(exercisenumber, exercisename); // jedes ID hat eig file in dem name steht
uBit.sleep(5);
} while (!(exercisenumber == ManagedString("ENDE")));
if (!remove_file(ManagedString("ENDE"))) {
uBit.serial.printf("failed to remove file");
uBit.serial.send(exercisenumber);
}
uBit.sleep(5);
uBit.serial.printf("microbit ende");
uBit.display.scrollAsync("mitte");
uBit.serial.printf("begin loop\n");
uBit.display.scroll("check 1:");
uBit.display.scroll(read_to_string(ManagedString("1")));
for (int i = 1; i <= 100; i++)
{
const int x = i;
uBit.serial.printf("%d: ", i);
readToSerial(ManagedString(x));
// uBit.display.scroll(read_to_string(x));
}
uBit.display.scrollAsync("ende");
}
void onConnected(MicroBitEvent) {
uBit.display.scroll("C");
connected = 1;
}
void onDisconnected(MicroBitEvent) {
uBit.display.scroll("D");
connected = 0;
}
void input_done(MicroBitEvent) {
uBit.display.printCharAsync('>', 200);
inputterm = 1;
}
void terminate_loop(MicroBitEvent e) {
terminator = 1;
uBit.display.printCharAsync('*', 200);
}
////// allocieren aus unklarem Grund 250-400 bei Aufruf durch listner. nicht wenn in der main
void printfile_toserial(MicroBitEvent e) { readToSerial(ManagedString(filename)); }
void ble_sendfile(MicroBitEvent e) { readtoBLE(filename); }
void go_back(MicroBitEvent e) {
goback = 2;
uBit.display.printCharAsync('<', 200);
}
void set_pause() {
uBit.display.scroll("Pause:");
uint8_t pause = (uint8_t)input(pause_setting);
uint8_t* ptr_pause = &pause;
uBit.storage.put(pausestring, ptr_pause, sizeof(pause));
}
void ble_id_name(MicroBitEvent e) {
uBit.sleep(300);
uart->send("gebe Nr. ein");
ManagedString exercisenumber = string_from_ble();
uart->send("gebe Name ein");
ManagedString exercisename = string_from_ble();
uBit.sleep(300);
remove_file(exercisenumber);
uBit.sleep(300);
appendLine(exercisenumber, exercisename);
uBit.sleep(300);
readtoBLE(exercisenumber);
uBit.sleep(300);
uBit.display.scroll(read_to_string(exercisenumber));
}
int main() {
// Initialise the micro:bit runtime.
// delimiter = "\n";
// TODO:
/*
automatisches zählen der reps
(Beschleunigungswerte speichern/senden)
Key Values speichern für Trainingsmittelkatalog
*/
uBit.init();
uBit.serial.baud(9600);
uart = new MicroBitUARTService(*uBit.ble, 64, 64); // ble service
//uBit.serial.attach(handler); if connected to serial do something: print log, read catalog, whatever
// uBit.io.P0.eventOn(MICROBIT_PIN_EVENT_ON_TOUCH);
// uBit.io.P1.eventOn(MICROBIT_PIN_EVENT_ON_TOUCH);
// uBit.io.P2.eventOn(MICROBIT_PIN_EVENT_ON_TOUCH);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY,
inputButton);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY,
inputButton);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_LONG_CLICK,
terminate_loop);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK,
input_done);
const uint8_t start[] = {
0, 0, 0, 0, 0,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
0, 0, 0, 0, 0,
};
uBit.display.print(MicroBitImage(5, 5, start));
initrepcount();
// for (int j = 0; j < 3; ++j) {
// int res=repcount(inputBuff, inputterm, goback, pause_setting);
// uart->send(ManagedString(res));
// }
#ifndef QUICKTEST
// storing the setting if microbit has started , Warning
// uninitialized
uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED,
onConnected); // entscheide ob eingabe über ble
uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED,
onDisconnected);
// info
uBit.display.scroll("b->");
ManagedString menu;
int menuswitch = 0;
while (true) {
if (uBit.buttonAB.isPressed()) {
menu = "AB";
menuswitch = 3;
break;
}
if (uBit.buttonA.isPressed()) {
menu = "A";
menuswitch = 1;
break;
}
if (uBit.buttonB.isPressed()) {
menu = "B";
menuswitch = 2;
break;
}
uBit.sleep(300);
}
// switch (menuswitch)
//// {
//// // ---------MENU A
//// case 1:
//// uBit.display.scrollAsync(menu);
//// uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, catalog_read);
//// uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, printfile_toserial);
//// uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, nothing);
//// while (terminator == 0) {
//// uBit.sleep(1000);
//// }
//// terminator = 0;
//// uBit.sleep(1000);
//// uBit.messageBus.ignore(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, catalog_read);
//// uBit.messageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, printfile_toserial);
//// uBit.messageBus.ignore(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, nothing);
////
//// break;
////
////
//// // --------MENU B
//// case 2:
////
////
////
//// uBit.display.scrollAsync(menu);
////
////// if (NULL == uBit.storage.get(pausestring)) {
////// uint8_t pause = (uint8_t)input("maximal Pause:");
////// pause_setting = pause * PAUSEMULTIPLIKATOR;
////// uint8_t* ptr_pause = &pause;
////// uBit.storage.put(pausestring, ptr_pause, sizeof(pause));
////// }
////// else {
////// uBit.display.scroll("Pause:");
////// uint8_t pause = (uint8_t)input(uBit.storage.get(pausestring)->value[0]);
////// pause_setting = pause * PAUSEMULTIPLIKATOR;
////// uint8_t* ptr_pause = &pause;
////// uBit.storage.put(pausestring, ptr_pause, sizeof(pause));
//////
////// }
////
////
//// break;
//// case 3:
//// uBit.display.scrollAsync(menu);
//// uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, ble_id_name);
//// uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, nothing);
//// uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, ble_sendfile);
//// while (terminator == 0) {
//// uBit.sleep(300);
////
//// }
//// terminator = 0;
//// uBit.sleep(1000);
//// uBit.messageBus.ignore(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, ble_id_name);
//// uBit.messageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, nothing);
//// uBit.messageBus.ignore(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, ble_sendfile);
//// break;
////
////
////
////
////
//// default:
//// break;
//// }
//uBit.messageBus.listen(MICROBIT_ID_SERIAL, MICROBIT_SERIAL_EVT_DELIM_MATCH,
//
// serial_input);
uint8_t storeee = 1;
uint8_t *stor = &storeee; // storing the setting if microbit has started , Warning
// uninitialized
first_start = "fileexist"; // name des settings,storage key value pair
if (NULL == uBit.storage.get(first_start)) {
uBit.storage.put(first_start, stor, sizeof(stor));
appendLine(filename, ManagedString("date,name,id,weight,reps"));
} // prueft ob der microbit zum ersten mal in das file schreibt.
// dann wird die header dafür geschrieben. (csv format header)
// warten auf Verbindung mit Smartphone
// uBit.serial.send(
// ManagedString("gebe die ID, das Gewicht, die Reps, ein! via "
// "buttonsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
// uBit.serial.send("datum:");
// wenn Verbunden lese das Datum ein (von Handy App)
#ifdef BLETEST
if (connected == 1)
{
uart->send("gebe Name ein");
ManagedString buffer = string_from_ble();
uBit.storage.put("1", (uint8_t*)buffer.toCharArray(), sizeof(buffer.toCharArray()));
KeyValuePair* testt = uBit.storage.get("1");
const char * stringname = (const char*)testt->value;
uBit.display.scroll(stringname);
}
//compiles!
/**
* so kann das aussehen wenn neuer satz kommt
*/
if (connected == 1) {
uart->send("gebe Name ein");
ManagedString exercisename = string_from_ble();
uBit.sleep(300);
uart->send("gebe Nr. ein");
ManagedString exercisenumber = string_from_ble();
uBit.sleep(300);
remove_file(exercisenumber);
uBit.sleep(300);
appendLine(exercisenumber, exercisename);
uBit.sleep(300);
readtoBLE(exercisenumber);
uBit.sleep(300);
uBit.display.scroll(read_to_string(exercisenumber));
}
if (connected == 1) {
uart->send(ManagedString("Datum: 3x T,M,J newline"));
meinsatz.set_date(atoi(string_from_ble().toCharArray()),
atoi(string_from_ble().toCharArray()),
atoi(string_from_ble().toCharArray()));
uart->send(ManagedString("ID, gewicht, reps"));
}
/**
* überlegen wie es gemacht werden soll mit rein manueller Datums eingabe. RTC zu schwer
* evtl. Datum nur am Handy ändern. einfach unterschiedliche Trainings mit Datum 2099
*/
else {
uBit.display.scroll("Datum:");
meinsatz.set_date(input('T'), input('M'), input('J'));
}
#endif // BLETEST
if (connected == 1) {
uBit.display.scrollAsync("Handy:");
uart->send(ManagedString("Datum: 3x T,M,J newline"));
meinsatz.set_date(atoi(string_from_ble().toCharArray()),
atoi(string_from_ble().toCharArray()),
atoi(string_from_ble().toCharArray()));
uart->send(ManagedString("fahre am Geraet fort"));
}
else {
// uBit.display.scroll("Datum:");
meinsatz.set_date(input("T"), input("M"), input("J"));
}
uBit.messageBus.listen(MICROBIT_ID_IO_P0, MICROBIT_EVT_ANY, go_back);
main_set_input_loop();
// ansonsten über Hand eingabe
// uBit.serial.printf("gebe datum ein");
// meinsatz.set_date(int_from_serial(), int_from_serial(),
// int_from_serial());
// automatischer / manueller Input der Sets
#endif
uBit.messageBus.ignore(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, inputButton);
uBit.messageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, inputButton);
// uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK,
// printfile_toserial); // gebe file via serial aus
// uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK,
// ble_sendfile); // gebe via BLE aus //probleme da ein neuer fiber
// jedesmal viel Speicher allociert.
// for (int k = 0; k < 20; ++k) {
// uBit.serial.printf("Starte test:");
// uBit.sleep(1000);
// readtoBLE(filename);
// readToSerial(filename);
// }
// uBit.display.scrollAsync("press reset for new session, i sleep", 80);
// create_fiber(myFiber);
// uBit.sleep(10000);
// readtoBLE(filename);
// uBit.messageBus.ignore(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK,
// printfile_toserial); // gebe file via serial aus
// uBit.messageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK,
// ble_sendfile);
// Insert your code here!
// If main exits, there may still be other fibers running or registered
// event handlers etc. Simply release this fiber, which will mean we enter
// the scheduler. Worse case, we then sit in the idle task forever, in a
// power efficient sleep.
// uBit.sleep(50000);
// release_fiber();
}