-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.ino
1529 lines (1278 loc) · 50.3 KB
/
main.ino
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
/*
Ergware
Copyright (C) David W. Vernooy
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//#define PROD
#undef PROD
#include "nil.h"
#include <avr/pgmspace.h>
#include <stdio.h>
#include "lcd.h"
#include <string.h>
#include <avr/io.h>
#include "avr_heap.h"
#include "usart.h"
#include "time.h"
#include <math.h>
#include "st_lld.h"
#define DEBOUNCE_TIME 275 /* time to wait while "de-bouncing" button */
#define DELTA 0.0001
#define DOUBLE_SIXTY 60.0
#define ZERO 0.0
/********************************************************************************
Defines - erg
********************************************************************************/
#define LEFT bit_is_clear(PIND, PD5)
#define MIDDLE bit_is_clear(PIND, PD6)
#define RIGHT bit_is_clear(PIND, PD7)
/*number of constants for averaging*/
#define MAX_N 5
/********************************************************************************
Constants - erg
********************************************************************************/
static double J_moment = 0.16; //kg*m^2 - set this to the moment of inertia of your flywheel.
//look at documentation for ways to measure it. Its easy to do.
static double d_omega_div_omega2 = 0.0031;//erg_constant - to get this for your erg:
// Uncomment the code below that writes out the constant K_damp_esimator_vector[0]. Then run the erg,
//run that version of the software, get the number and add it to this line, then re-compile.
//It should be close to value above. I decided just to hard code it instead of dynamically updating
//it during the running of the software, though that would be easy and is presumably what other ergs do.
static double K_damp = ZERO; //related by J_moment*omega_dot = K_damp * omega^2 during non-power part of stroke
//I calcualate this below.
static double cal_factor = ZERO; //distance per rev ... calculated later
static double magic_factor = 2.8; //a heuristic constant people use to relate revs to distance-rowed
static double pi = 3.1415926;
static double seconds_per_tick = 0.000016; //16us per tick with 256 divider in timer1 & 8MHz clock ... thread7
/********************************************************************************
Global Variables - NIL
********************************************************************************/
FILE lcd_out;
FILE usart_out;
volatile uint8_t lcd_context = 0; //which thread has LCD_context
volatile uint8_t lcd_context_count[6] = {1,1,1,1,1,1}; //start screen within thread
volatile uint8_t lcd_context_count_MAX[6] = {1,1,1,3,3,2}; //max screens within thread
static thread_reference_t trp_button_detect = NULL; //thread reference object ... thread_reference_t = thread_t*
static thread_reference_t trp_chopper = NULL; //thread reference object ... thread_reference_t = thread_t*
static thread_reference_t trp_menu = NULL; //thread reference object ... thread_reference_t = thread_t*
static thread_reference_t trp_start = NULL; //thread reference object ... thread_reference_t = thread_t*
static thread_t *tp[4] = {NULL, NULL, NULL, NULL}; //array of pointers to threads
double minute_counter;
/* Declare a semaphore */
semaphore_t lcdUSE; /*semaphore to protect the LCD*/
// Global menu data
#define maxMenus 3
#define maxItems 4
/********************************************************************************
Global Variables - menu & buttons
********************************************************************************/
static uint8_t curMenu = 0; // menu currently shown
static uint8_t curMenu_old = 0; // menu currently shown
static uint8_t curItem = 0; // item currently marked (line 0 for menutitle, so starts on one). NB! Item marked may not correspond to line marked (see cursorCount)
static uint8_t menuCount = 0; // keeps track of what line of menu to print
volatile uint8_t switchtype = 0;
static char menutitle[maxMenus][5];
static char menuitem[maxMenus][maxItems][5];
static uint8_t menulink[maxMenus][maxItems]; // a link to a submenu OR
static uint8_t menuactn[maxMenus][maxItems]; // menu actions - turn on LED or whatever
static uint8_t menuitemcount[maxMenus]; // holds number of menu items in each menu
static uint8_t squelched = 0;
static uint16_t squelch_count = 0;
static uint8_t trapped = 0;
/********************************************************************************
Global Variables - erg
********************************************************************************/
static uint8_t elapsed_hours = 0;
static uint8_t elapsed_mins = 0;
static uint8_t elapsed_secs = 0;
static uint8_t split_hours = 0;
static uint8_t split_mins = 0;
static uint8_t split_secs = 0;
static double split_time = 0.0;
static uint8_t chop_ticks = 0;
static double average_SPM = ZERO;
static double volatile distance_rowed = ZERO;
static uint16_t volatile chop_counter[2];
static double stroke_vector[MAX_N];
static double stroke_vector_avg;
static double power_vector[MAX_N];
static double power_vector_avg;
static double calorie_tot = ZERO;
static double K_power = ZERO;
static double J_power = ZERO;
static double power_ratio_vector[MAX_N];
static double K_damp_estimator_vector[MAX_N];
static double K_damp_estimator_vector_avg;
static double K_damp_estimator = ZERO;
static double speed_vector[MAX_N];
static double speed_vector_avg = ZERO;
static double power_ratio_vector_avg = ZERO;
static double stroke_elapsed = ZERO;
static double power_elapsed = ZERO;
static double stroke_distance = ZERO;
static double stroke_distance_old = ZERO;
static double omega_vector[2];
static double omega_dot_vector[2];
static double omega_dot_dot = ZERO;
static double current_dt = ZERO;
static double omega_vector_avg = ZERO;
static double omega_vector_avg_curr = 0.0;
static uint8_t omega_dot_screen = 0;
static uint8_t omega_dot_dot_screen = 0;
static uint8_t power_stroke_screen[2];
static uint16_t stroke = 0;
static uint8_t j = 0;
static TIME t_stroke, t_power;
/*********************************************************************************
time parser
********************************************************************************/
static void parse_time(double time_in_min, uint8_t *hours, uint8_t *mins, uint8_t *secs) {
double temp_min;
double temp_secs;
*hours = (uint8_t) (time_in_min/60);
temp_min = time_in_min - (double) DOUBLE_SIXTY * (*hours);
*mins = (uint8_t) temp_min;
temp_secs = DOUBLE_SIXTY*temp_min - DOUBLE_SIXTY *(double) (*mins);
*secs = (uint8_t) temp_secs;
}
/*********************************************************************************
weighted_avg_function
********************************************************************************/
static double weighted_avg(double *vector) {
double temp_weight, temp_sum;
uint8_t j;
temp_sum = ZERO;
temp_weight = ZERO;
for (j =0;j<MAX_N; j++) {
temp_weight += pow(2,MAX_N-j-1);
temp_sum += pow(2,MAX_N-j-1) * vector[j];
}
temp_sum = temp_sum/temp_weight;
return temp_sum;
}
/*********************************************************************************
reshuffle_function
********************************************************************************/
/*
static void reshuffle(double *vector) {
uint8_t j;
for (j = MAX_N;j>0; j--) {
vector[j] = vector[j-1];
}
}
*/
/*********************************************************************************
Menu Setup
********************************************************************************/
static void initMenu(void) {
sprintf_P(menutitle[0], PSTR("Ergo"));
sprintf_P(menuitem[0][0], PSTR("Row!"));
menulink[0][0] = 0; // no sub menu, just action
menuactn[0][0] = 1;
sprintf_P(menuitem[0][1], PSTR("Prog"));
menulink[0][1] = 1;
menuactn[0][1] = 0;
sprintf_P(menuitem[0][2], PSTR("Info"));
menulink[0][2] = 2;
menuactn[0][2] = 0;
sprintf_P(menutitle[1], PSTR("Prog"));
sprintf_P(menuitem[1][0], PSTR("D/T"));
menulink[1][0] = 0; // no sub menu, just action
menuactn[1][0] = 2;
sprintf_P(menuitem[1][1], PSTR("Pace"));
menulink[1][1] = 0;
menuactn[1][1] = 3;
sprintf_P(menuitem[1][2], PSTR("Burn"));
menulink[1][2] = 0;
menuactn[1][2] = 4;
sprintf_P(menuitem[1][3], PSTR("//"));
menulink[1][3] = 0; // code to trigger return to main menu. Can't use 0 as that is considered no action here
menuactn[1][3] = 99;
sprintf_P(menutitle[2], PSTR("Info"));
sprintf_P(menuitem[2][0], PSTR("Data"));
menulink[2][0] = 0;
menuactn[2][0] = 5;
sprintf_P(menuitem[2][1], PSTR("Heap"));
menulink[2][1] = 0;
menuactn[2][1] = 6;
sprintf_P(menuitem[2][2], PSTR("//"));
menulink[2][2] = 0; // code to trigger return to main menu. Can't use 0 as that is considered no action here
menuactn[2][2] = 99;
menuitemcount[0] = 3;
menuitemcount[1] = 4;
menuitemcount[2] = 3;
// END of menu data
}
/*********************************************************************************
Menu show routine
********************************************************************************/
static void showMenu(void) {
lcd_clear();
lcd_goto_xy(1,1);
invert = 1;
fprintf_P(&lcd_out, PSTR("%s"), menutitle[curMenu]);
invert = 0;
//print menu content
while (menuCount < menuitemcount[curMenu]) {
lcd_goto_xy(1, menuCount+2); // +1 to leave first line for menu title
if (curItem == menuCount) {
// item currently indicated by cursor
fprintf_P(&lcd_out, PSTR(">%s"), menuitem[curMenu][menuCount]);
} else {
fprintf_P(&lcd_out, PSTR(" %s"), menuitem[curMenu][menuCount]);
}
menuCount++;
}
}
/*********************************************************************************
Interrupt handler for the CD chopper disk
********************************************************************************/
CH_IRQ_HANDLER(INT0_vect)
{
CH_IRQ_PROLOGUE();
chSysLockFromISR();
chop_ticks++;
if ((chop_ticks % 8 ) == 0) {
chop_ticks = 0;
chop_counter[1] = chop_counter[0];
chop_counter[0] = TCNT1;
TCNT1 = 0;
distance_rowed += cal_factor;
chThdResumeI(&trp_chopper, (msg_t)0x55);
}
chSysUnlockFromISR();
CH_IRQ_EPILOGUE();
}
/*********************************************************************************
* Button Handler 100Hz (10ms) timer interrupt generated by OCR2A (128 us per tick, 80 ticks)
*********************************************************************************/
CH_IRQ_HANDLER(TIMER2_COMPA_vect)
{
CH_IRQ_PROLOGUE();
chSysLockFromISR();
/* Wakes up a thread waiting on a thread reference object*/
/* Resuming the thread with message, synchronous*/
/* Synchronizes with thread that is suspended with &trp*/
/* can also pass the message to that thread */
// squelch code
if (squelched == 1) {
if (squelch_count < 40) {
squelch_count++;
}
else {
squelch_count = 0;
squelched = 0;
}
}
if ((LEFT | MIDDLE | RIGHT) & (squelched == 0)) {
trapped++;
}
if (trapped > 5) {
// button detected code here
trapped = 0;
squelched = 1;
if (LEFT) {
//it is button 1 ... generally scrolling or escaping
switchtype = 1;
}
else if (MIDDLE) {
//it is button 2 ... selecting
switchtype = 2;
}
else if (RIGHT) {
//it is button 1 ... generally scrolling or escaping
switchtype = 3;
}
//Send a button_detect signal to handler THREAD1 because a button has been pressed
chThdResumeI(&trp_button_detect, (msg_t)switchtype);
}
chSysUnlockFromISR();
CH_IRQ_EPILOGUE();
}
/*********************************************************************************
Thread1 - handles all the button context, gets woken up from button press timer hander
********************************************************************************/
THD_FUNCTION(Thread1, arg) {
(void)arg;
msg_t msg;
/************************************************
Set switch inputs
*************************************************/
DDRD &= ~((1 << PD7) |(1 << PD6) |(1 << PD5)); //set PD7/6/5 as input
PORTD |= ((1 << PD7) |(1 << PD6) |(1 << PD5)); //turn on pull-ups
/************************************************
Timer/counter 2, 8 bit ... 10 ms timer for button push
*************************************************/
TCNT2=0; /* set counter to zero*/
// Mode 4 table 18-8 page 157. CTC mode and top in OCR1A
TCCR2A=(1<<WGM21);//0b00000010, Normal port operation, CTC mode
TCCR2B= (1<<CS22)|(1<<CS21)|(1<<CS20); // c/ clock/1024 = 0.064 ms Tick ... * 156 counts = 10ms
OCR2A=0x9D;//157 counts, then reset (8 bit means 255 max), since 0 is included, count to 157
TIMSK2 = (1 << OCIE2A);//0b00000010; Output compare match A interrupt enable mask bit
/* using a variable called lcd_context allows buttons to take */
/* on different context depending on which screens */
/* were alive when they were used */
/* this thread does the interpretation after the IRQ traps the button press */
chThdSleepMilliseconds(100);
while (true) {
chSysLock();
/* Waiting for button push & IRQ to happen */
/* Current thread put to sleep & sets up the reference variable trp for the trigger to reference */
/* Will resume with a chThdResumeI referencing trp from interrupt*/
msg = chThdSuspendTimeoutS(&trp_button_detect, TIME_INFINITE);
chSysUnlock();
switch (lcd_context) {
case 0:
/* we were in the splash screenb & a button was pushed & handled by interrupt*/
/* the button context was set in interrupt, need to signal thread 2 */
//next 3 should be atomic
chSysLock();
switchtype = 0; //set artificially though any button could have been pushed
lcd_context = 1;
/*Send a resume signal to menu to continue - right now it is suspended */
chThdResumeI(&trp_start, (msg_t)switchtype);
chSysUnlock();
break;
case 1:
/* we were in the main menu & a button was pushed & handled by interrupt*/
/* the button context was set in interrupt, need to signal thread 2 */
//next 3 should be atomic
chSysLock();
/*Send a resume signal to menu to continue - right now it is suspended */
chThdResumeI(&trp_menu, (msg_t)switchtype);
chSysUnlock();
break;
case 3: //we were in thread3
//next 3 should be atomic
chSysLock();
lcd_context =1; //going back to thread2
/*returning from thread4 ... any button did it ... so we will go back to the */
/* main menu thread 2 reset the switchtype to 0, we are neither going up or */
/* down, nor selecting*/
switchtype = 0;
/*Send an event signal to thread 4 to go back up to the main entry point*/
/*Since the button was pressed to get out of that thread */
/* Note - event masks should be thought of as or'd binary places. So you should use */
/*Send a resume signal to menu to continue - right now it is suspended */
chThdResumeI(&trp_menu, (msg_t)switchtype);
/* 1, 2, 4, 8, 16 etc.. as distinct masks when signaling a specific thread*/
/* the same mask # can be used when signaling other distinct threads, if you are */
/* directly signaling that thread anyways */
chEvtSignalI(tp[0], (eventmask_t)2);
chSysUnlock();
break;
case 4:
//we were in thread4
//next 3 should be atomic
chSysLock();
if (switchtype == 1) { //returning from thread9 back to this thread .. thread 1
lcd_context =1; //need to returning to thread2
/*returning from thread5 ... button 1 did it ... so we will go back to the main menu */
/* reset the switchtype to 0*/
switchtype = 0;
/*Send a resume signal to menu to continue - right now it is suspended */
chThdResumeI(&trp_menu, (msg_t)switchtype);
/*Send an event signal to thread 5 to go back up to the main entry point*/
/*Since the button was pressed to get out of that thread */
chEvtSignalI(tp[1], (eventmask_t)2);
chSysUnlock();
break;
}
if (switchtype ==2) {
/*reset timer*/
chEvtSignalI(tp[1], (eventmask_t)4);
chSysUnlock();
break;
}
if (switchtype ==3) {
/*start timer*/
chEvtSignalI(tp[1], (eventmask_t)8);
chSysUnlock();
break;
}
case 5:
//we were in thread5
//next 3 should be atomic
chSysLock();
if (switchtype == 1) { //returning from thread5 back to this thread .. thread 1
lcd_context =1; //need to returning to thread2
/*returning from thread5 ... button 1 did it ... so we will go back to the main menu */
/* reset the switchtype to 0*/
switchtype = 0;
/*Send a resume signal to menu to continue - right now it is suspended */
chThdResumeI(&trp_menu, (msg_t)switchtype);
/*Send an event signal to thread 5 to go back up to the main entry point*/
/*Since the button was pressed to get out of that thread */
chEvtSignalI(tp[2], (eventmask_t)2);
chSysUnlock();
}
else {
/*switchtype ==2, we are staying in thread5*/
/*don't change lcd_context, we are staying in thread5*/
/*button pressed to cycle through subscreens ... update the subscreen count*/
lcd_context_count[4] = lcd_context_count[4]+1;
if (lcd_context_count[4] > lcd_context_count_MAX[4]) {lcd_context_count[4] = 1;}
/*staying in thread 5, signal thread5 to re-enter submenu with the new lcd context*/
/* note, skip eventmask 3 because that is binary 11, which would signal 2 events*/
chEvtSignalI(tp[2], (eventmask_t)4); //stay in thread
chSysUnlock();
}
break;
case 6: //we were in thread 6 ... any button returns
chSysLock();
if (switchtype == 1) { //returning from thread5 back to this thread .. thread 1
lcd_context =1; //need to returning to thread2
/*returning from thread5 ... button 1 did it ... so we will go back to the main menu */
/* reset the switchtype to 0*/
switchtype = 0;
/*Send a resume signal to menu to continue - right now it is suspended */
chThdResumeI(&trp_menu, (msg_t)switchtype);
/*Send an event signal to thread 5 to go back up to the main entry point*/
/*Since the button was pressed to get out of that thread */
chEvtSignalI(tp[3], (eventmask_t)2);
chSysUnlock();
}
else {
/*button pressed to cycle through subscreens ... update the subscreen count*/
lcd_context_count[5] = lcd_context_count[5]+1;
if (lcd_context_count[5] > lcd_context_count_MAX[5]) {lcd_context_count[5] = 1;}
chEvtSignalI(tp[3], (eventmask_t)4);
chSysUnlock();
}
break;
}
}/*while (true) */
}//THREAD1
/*********************************************************************************
Thread 2 - Main thread for LCD menu + signalling of other actions, signaled from
Thread1 for LCD use and for re-update when button pressed
********************************************************************************/
THD_FUNCTION(Thread2, arg) {
/*this thread will update the menu on the lcd */
/*it will take a semaphore for use of the lcd */
/*it will also take a semaphore for button pressed*/
(void)arg;
msg_t msg;
initMenu(); // initialize menu by adding menu data to menu globals
/*Wait for the lcdUSe semaphore to be free & take it */
chSemWait(&lcdUSE);
lcd_init();
#ifdef PROD
lcd_contrast(0x28);//0x28 for production application
#else //PROD
lcd_contrast(0x3E);//0x3E for STK500 board
#endif //PROD
lcd_clear();
lcd_goto_xy(1,1);
fprintf_P(&lcd_out,PSTR(" Sarah's Ergo "));
lcd_goto_xy(3,5);
print_splash();
/*give back lcd semaphore */
chSemSignal(&lcdUSE);
chThdSleepMilliseconds(100);
chSysLock();
/* Waiting for button push & IRQ to happen */
/* Current thread put to sleep & sets up the reference variable trp for the trigger to reference */
/* Will resume with a chThdResumeI referencing trp from interrupt*/
msg = chThdSuspendTimeoutS(&trp_start, TIME_INFINITE);
chSysUnlock();
while(true) {
/*********************************************************************************
Update LCD when triggered, then wait
********************************************************************************/
/*Wait for the lcdUSe semaphore to be free & take it again */
chSemWait(&lcdUSE);
/*show the menu*/
showMenu();
/*give back the lcd semaphore */
chSemSignal(&lcdUSE);
chSysLock();
/* Waiting for button push & IRQ to happen */
/* Current thread put to sleep & sets up the reference variable trp for the trigger to reference */
/* Will resume with a chThdResumeI referencing trp from interrupt*/
msg = chThdSuspendTimeoutS(&trp_menu, TIME_INFINITE);
chSysUnlock();
/*********************************************************************************
No action
********************************************************************************/
if ((switchtype == 0)|(switchtype == 3)) { //we are returning from a sub-menu action
if (curItem == menuitemcount[curMenu]) {
curItem = 0;
}
menuCount = 0;
} //switchtype 0
/*********************************************************************************
Scrolling
********************************************************************************/
if (switchtype == 1) { //we are scrolling
curItem++; // add one to curr item
if (curItem == menuitemcount[curMenu]) {
curItem = 0;
}
menuCount = 0;
} //switchtype 1
/*********************************************************************************
Selecting and triggering
********************************************************************************/
if (switchtype == 2) { //we are selecting
// handle user input
if (menuactn[curMenu][curItem]) {
// has an action
switch (menuactn[curMenu][curItem]) {
case 1: // action 0.1
chEvtSignalI(tp[0], (eventmask_t)1); // signal thread3
break;
case 2:
/*signals thread4, which is sitting there waiting */
/*with chEvtWaitAnyTimeout. Can also send flags */
lcd_context_count[3] = 1;
chEvtSignalI(tp[1], (eventmask_t)1); // signal thread9
break;
case 3:
/*signals thread4, which is sitting there waiting */
/*with chEvtWaitAnyTimeout. Can also send flags */
lcd_context_count[3] = 2;
chEvtSignalI(tp[1], (eventmask_t)1); // signal thread9
break;
case 4:
/*signals thread4, which is sitting there waiting */
/*with chEvtWaitAnyTimeout. Can also send flags */
lcd_context_count[3] = 3;
chEvtSignalI(tp[1], (eventmask_t)1); // signal thread4
break;
case 5: //
chEvtSignalI(tp[2], (eventmask_t)1); // signal thread5
break;
case 6: //
chEvtSignalI(tp[3], (eventmask_t)1); // signal thread5
break;
case 99:
curMenu_old = curMenu;
curMenu = menulink[curMenu][curItem]; // return to main menu
/* enable this if want to return to top of menu*/
curItem=curMenu_old; // reset menu item to which cursor point
break;
}//switch
menuCount = 0;
} //menuaction
else {
curMenu = menulink[curMenu][curItem]; // set to menu selected by cursor
curItem = 0; // reset menu item to which cursor point
menuCount = 0; // reprint from first line of this page
}//end if menuaction
} // switchtype = 2
} // end while (true)
}//THREAD2
/*********************************************************************************
Thread 3 - LCD write out all the erg data
********************************************************************************/
THD_FUNCTION(Thread3, arg) {
(void)arg;
tp[0] = chThdGetSelfX(); //returns a pointer to current thread
while (true) {
/* it starts in off state, or gets here when turned off. Eventmask1 turns it on*/
chEvtWaitAnyTimeout((eventmask_t)1, TIME_INFINITE);
/*take control of LCD semaphore*/
chSemWait(&lcdUSE);
/*switch lcd_context to thread 4*/
lcd_context = 3;
// Print on first line
lcd_clear();
lcd_goto_xy(1,1);
invert = 1;
fprintf_P(&lcd_out,PSTR(" Sarah's Ergo "));
invert = 0;
while(true) {
/* it is inside this loop and toggling. If we get an */
/* Eventmask2, it turns off */
if(chEvtWaitAnyTimeout((eventmask_t)2, TIME_IMMEDIATE) == 2){
/*thread3 gives back the lcd */
chSemSignal(&lcdUSE);
break;
}
/***********************************************
update time
************************************************/
lcd_goto_xy(3,6);
parse_time((double) minute_counter, &elapsed_hours, &elapsed_mins, &elapsed_secs);
fprintf_P(&lcd_out,PSTR("%2d:%02d"),elapsed_mins, elapsed_secs);
/***********************************************
update distance
************************************************/
lcd_goto_xy(3,5);
fprintf_P(&lcd_out,PSTR("%1.0f meters "), distance_rowed);
/***********************************************
update stroke rate
************************************************/
lcd_goto_xy(1,2);
if (stroke < 5) {
fprintf_P(&lcd_out,PSTR("SPM -- "));
}
else {
fprintf_P(&lcd_out,PSTR("SPM %2.0f "), DOUBLE_SIXTY/stroke_vector_avg);
}
lcd_goto_xy(9,2);
if ((stroke < 2) || ((elapsed_hours ==0) && (elapsed_mins ==0) && (elapsed_secs < 30))) {
fprintf_P(&lcd_out,PSTR("avg --"));
}
else {
average_SPM = (double) stroke/((double)elapsed_hours*DOUBLE_SIXTY+(double)elapsed_mins+(double)elapsed_secs/DOUBLE_SIXTY);
fprintf_P(&lcd_out,PSTR("avg %2.0f"), average_SPM);
}
/***********************************************
update stroke count & ratio
************************************************/
lcd_goto_xy(1,3);
fprintf_P(&lcd_out,PSTR("S# %d "), stroke);
lcd_goto_xy(11,3);
fprintf_P(&lcd_out,PSTR("%1.2f "), power_ratio_vector_avg);
/***********************************************
update average power
************************************************/
//lcd_goto_xy(1,4);
lcd_goto_xy(4,4);
if (stroke < 5) {
fprintf_P(&lcd_out,PSTR("--- W"));
}
else {
fprintf_P(&lcd_out,PSTR("%1.0f W "), power_vector_avg);
}
/***********************************************
update 500m split
************************************************/
lcd_goto_xy(10,6);
if (stroke < 10) {
fprintf_P(&lcd_out,PSTR("--:--"));
}
else {
fprintf_P(&lcd_out,PSTR("%2d:%02d"), split_mins, split_secs);
}
chThdSleepMilliseconds(350);
}//while(true) inner
} //while(true) outer
}//THREAD3
/*********************************************************************************
Thread 4 - self contained action task, escapable, takes LCD semaphore
********************************************************************************/
THD_FUNCTION(Thread4, arg) {
(void)arg;
TIME internal_timer;
double internal_timer_elapsed = ZERO;
double internal_distance_rowed = ZERO;
double internal_distance_temp_old = ZERO;
double internal_distance_temp =ZERO;
double internal_distance_temp_paused =ZERO;
double internal_timer_elapsed_paused =ZERO;
double internal_average_split =ZERO;
uint8_t internal_timer_started;
uint8_t paused;
uint8_t internal_split_hours = 0;
uint8_t internal_split_mins = 0;
uint8_t internal_split_secs = 0;
uint8_t ias_count = 0;
uint16_t internal_stroke_count_ref = 0;
uint16_t internal_stroke_count = 0;
//uint8_t entry_flag;
tp[1] = chThdGetSelfX();//returns a pointer to current thread
while (true) {
/* wait for signal coming from main menu thread 2*/
chEvtWaitAnyTimeout((eventmask_t)1, TIME_INFINITE);
/*take control of LCD semaphore*/
chSemWait(&lcdUSE);
/*switch lcd_context to thread 4*/
lcd_context = 4;
lcd_clear();
internal_timer_started = 0;
paused = 1;
while(true) {
/*now inside loop, wait for signal from thread1 button handler to get out*/
if(chEvtWaitAnyTimeout((eventmask_t)2, TIME_IMMEDIATE) == 2){
/*thread4 gives back the lcd */
chSemSignal(&lcdUSE);
break;
}
switch (lcd_context_count[3]) {
case 1:
if (internal_timer_started == 0) {
lcd_goto_xy(1,1);
fprintf_P(&lcd_out,PSTR("--:--"));
lcd_goto_xy(11,1);
fprintf_P(&lcd_out,PSTR("---"));
lcd_goto_xy(3,3);
bigfont = 1;
fprintf_P(&lcd_out,PSTR("--:-- "));
lcd_goto_xy(1,5);
fprintf_P(&lcd_out,PSTR("----"));
bigfont = 0;
lcd_go_up_one();
fprintf_P(&lcd_out,PSTR(" m "));
lcd_goto_xy(10,6);
fprintf_P(&lcd_out,PSTR("--:--"));
internal_distance_rowed = 0;
}
/*now inside loop, wait for signal from thread1 button handler start internal timer*/
if(chEvtWaitAnyTimeout((eventmask_t)4, TIME_IMMEDIATE) == 4){
if (internal_timer_started ==0) {//we were paused, then unpause
/*starts internal timer */
GetTime (&internal_timer);
internal_timer_started = 1;
internal_distance_temp = distance_rowed;
internal_distance_rowed = 0;
internal_distance_temp_paused = 0;
internal_timer_elapsed_paused = 0;
internal_timer_elapsed = 0;
paused = 0;
internal_stroke_count_ref = stroke;
}
else {
if ((paused ==0) & (internal_timer_started ==1)) {//pause
paused =1;
internal_distance_temp_paused = internal_distance_rowed;
internal_timer_elapsed_paused = internal_timer_elapsed;
}
else {
if ((paused ==1) & (internal_timer_started ==1)) { //unpause
paused = 0;
GetTime (&internal_timer);
internal_distance_temp = distance_rowed;
internal_stroke_count_ref = stroke;
}
}
}
}
/*now inside loop, wait for signal from thread1 button handler to get out*/
if(chEvtWaitAnyTimeout((eventmask_t)8, TIME_IMMEDIATE) == 8){
/*thread4 gives back the lcd */
internal_timer_started = 0;
break;
}
/* not paused */
if (internal_timer_started == 1) {
lcd_goto_xy(1,6);
if (paused ==0) {
fprintf_P(&lcd_out,PSTR(" "));
}
else {
invert = 1;
fprintf_P(&lcd_out,PSTR(" * "));
invert = 0;
}
lcd_goto_xy(1,1);
if (paused ==0) {
internal_timer_elapsed = internal_timer_elapsed_paused+(double)Get_elapsed_s(&internal_timer);
}
else {
internal_timer_elapsed = internal_timer_elapsed_paused ;
}
parse_time((double) internal_timer_elapsed/60.0, &elapsed_hours, &elapsed_mins, &elapsed_secs);
fprintf_P(&lcd_out,PSTR("%2d:%02d"),elapsed_mins, elapsed_secs);
if (paused ==0) {
internal_distance_temp_old = internal_distance_temp;
internal_distance_temp = distance_rowed;
internal_distance_rowed += (internal_distance_temp - internal_distance_temp_old);
}
else {
internal_distance_rowed = internal_distance_temp_paused;
}
lcd_goto_xy(1,5);
bigfont = 1;
fprintf_P(&lcd_out,PSTR("%4.0f"), internal_distance_rowed);
bigfont = 0;
lcd_go_up_one();
fprintf_P(&lcd_out,PSTR(" m "));
if (paused == 0) {
/*
calculate
internal stroke count ... resets on pause, references external, accounted internal
internal stroke_rate ... resets on pause, uses external, updates on internal stroke count
internal split ... uses external, updates on internal stroke count
internal average split ... uses external, updated internally
*/
internal_stroke_count = stroke - internal_stroke_count_ref;
if (internal_stroke_count < 7) {//don't update
}
else { //update stuff
//stroke rate
lcd_goto_xy(11,1);
fprintf_P(&lcd_out,PSTR("%3.0f"), DOUBLE_SIXTY/stroke_vector_avg);
//internal split
lcd_goto_xy(3,3);
bigfont = 1;
fprintf_P(&lcd_out,PSTR("%2d:%02d"), split_mins, split_secs);
bigfont = 0;
//average split
ias_count++;
internal_average_split += 500.0*internal_timer_elapsed/(internal_distance_rowed+DELTA);
if (ias_count %50 ==0) {
lcd_goto_xy(10,6);
internal_average_split = internal_average_split/50;
parse_time((internal_average_split/DOUBLE_SIXTY), &internal_split_hours, &internal_split_mins, &internal_split_secs);
fprintf_P(&lcd_out,PSTR("%2d:%02d"), internal_split_mins, internal_split_secs);
internal_average_split = 0;
ias_count = 0;
}
}
}
}
break;
case 2:
/* this case is the distance counter */
lcd_goto_xy(3,2);
bigfont = 1;
if (stroke < 5) {
fprintf_P(&lcd_out,PSTR("---"));
}
else {
fprintf_P(&lcd_out,PSTR("%3.0f"), DOUBLE_SIXTY/stroke_vector_avg);
}
bigfont = 0;
lcd_go_up_one();
fprintf_P(&lcd_out,PSTR(" SPM "));
lcd_goto_xy(3,4);
bigfont = 1;
if (stroke < 5) {