/
workout.c
1674 lines (1505 loc) · 52.4 KB
/
workout.c
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
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: workout.c
*
* Copyright (C) 2010 Andrew Roth
*
* 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
/*
Welcome to the workout plugin.
http://github.com/andrewroth/rockbox_workout/wiki/brainstorming
Andrew Roth
*/
#include "stdio.h"
#include "plugin.h"
#include "string.h"
#include "time.h"
#include "lib/xlcd.h"
PLUGIN_HEADER
#if defined(SIMULATOR)
#define DEBUG_ENABLED true
#else
#define DEBUG_ENABLED false
#endif
#define debug_print(...) if (DEBUG_ENABLED) { rb->snprintf(debug_line_ptr, TXT_LEN, __VA_ARGS__); debug(debug_line_ptr); }
#define init_int(var, val) var = ((var) != 0 ? (var) : (val))
#define init(var, val) var = ((var) != NULL ? (var) : (val))
#define STR_LEN 48
#define TXT_LEN 1024
#define MAX_WORKOUTS 10
#define MAX_WORKOUT_DATES 30
#define MAX_EXERCISES_PER_WORKOUT 20
#define MAX_EXERCISES 120
#define MAX_SETS_PER_EXERCISE 5
#define MAX_EXERCISE_SETS MAX_EXERCISES*MAX_SETS_PER_EXERCISE
//#define MAX_EXERCISE_SETS 120*5 // 600
#define MAX_EXERCISE_LOGS MAX_WORKOUT_DATES*MAX_EXERCISES_PER_WORKOUT
//#define MAX_EXERCISE_LOGS 30*20 // 600
//#define MAX_SET_LOGS MAX_EXERCISE_LOGS*MAX_SETS_PER_EXERCISE
//#define MAX_SET_LOGS 30*20*5 // 3000
#define MAX_SET_LOGS 14*MAX_EXERCISES_PER_WORKOUT*MAX_SETS_PER_EXERCISE
#define MAX_BUFFERS 0
#define MAX_FUNCTIONS MAX_SET_LOGS*3 // weight, reps, rest
#define SAVE_LOGS_SECONDS 60
/* colors */
#define LCD_RED LCD_RGBPACK(255, 0, 0)
#define LCD_BLUE LCD_RGBPACK(0, 0, 255)
#define LCD_ORANGE LCD_RGBPACK(255, 165, 0)
#define LCD_YELLOW LCD_RGBPACK(255, 255, 0)
#define LCD_GREEN LCD_RGBPACK(0, 255, 0)
#define LCD_AQUA LCD_RGBPACK(127,255,212)
#define BACKGROUND_COLOR LCD_BLACK
/* choose a workout menu defines */
#define SCREEN_WIDTH LCD_WIDTH
#define SCREEN_HEIGHT LCD_HEIGHT
#define WORKOUT_MENU_WIDTH (LCD_WIDTH - WORKOUT_MENU_MARGIN*2)
//#define WORKOUT_MENU_HEIGHT (WORKOUT_MENU_ROW_HEIGHT * WORKOUT_MENU_ROWS) + WORKOUT_MENU_HEADER_HEIGHT*2
#define WORKOUT_MENU_HEIGHT (LCD_HEIGHT - WORKOUT_MENU_MARGIN*2)
#define WORKOUT_MENU_ROW_HEIGHT 32
#define WORKOUT_MENU_ROWS 4
#define WORKOUT_MENU_HEADER_HEIGHT 16
#define WORKOUT_MENU_MARGIN 5
#define WORKOUT_MENU_COLOR LCD_RED
#define WORKOUT_MENU_SEL_COLOR LCD_BLUE
/* workout menu itself */
#define WORKOUT_COLOR LCD_RED
#define WORKOUT_SETUP_COLOR LCD_ORANGE
#define WORKOUT_INPROGRESS_COLOR LCD_YELLOW
#define WORKOUT_RESTING_COLOR LCD_AQUA
#define WORKOUT_DONE_COLOR LCD_GREEN
#define WORKOUT_MARGIN 5
#define WORKOUT_ROW_HEIGHT 16
#define WORKOUT_TITLE_CHAR_WIDTH 8
#define WORKOUT_ROWS 8
#define WORKOUT_SET_WIDTH 160
#define WORKOUT_SET_MARGIN 5
#define WORKOUT_SETS_PER_ROW 1
#define WORKOUT_DASHBOARD_PERCENT 28.0
#define WORKOUT_DASHBOARD_TOP ((100.0-WORKOUT_DASHBOARD_PERCENT)/100.0)*SCREEN_HEIGHT
#define WORKOUT_DASHBOARD_PROG_HEIGHT 10
#define WORKOUT_DASHBOARD_PROG_LEFT 30
#define WORKOUT_DASHBOARD_PROG_TOP 10
/* exercise types */
#define EXERCISE_TYPE_WEIGHTS 1
#define EXERCISE_TYPE_CARDIO 2
#define EXERCISE_TYPE_STRETCH 3
/* exercise states */
#define NUM_EXERCISE_STATES 4
#define EXERCISE_SETTING_UP 0
#define EXERCISE_INPROGRESS 1
#define EXERCISE_RESTING 2
#define DONE_WORKOUT 3
/* menu more/less indicators - little triangle that designates more entries in a list */
#define MENU_MORE_WIDTH 8
#define MENU_MORE_HEIGHT 8
typedef struct workout_log_entry_s workout_log_entry;
typedef struct workout_date_s workout_date;
typedef struct workout_s workout;
typedef struct exercise_s exercise;
/* workout structures */
typedef struct {
int id;
char name[STR_LEN];
int position;
exercise *exercise_ref;
unsigned short int row;
unsigned short int reps;
int hold_for;
int weight;
} exercise_set;
struct workout_s {
int id;
char name[STR_LEN];
//time_t created_at;
//time_t updated_at;
int num_exercises;
exercise *exercises[MAX_EXERCISES_PER_WORKOUT];
};
struct exercise_s {
int id;
char name[STR_LEN];
char description[STR_LEN];
int n;
unsigned short int exercise_type_id;
//time_t created_at;
//time_t updated_at;
exercise_set *sets[MAX_SETS_PER_EXERCISE];
int num_sets;
unsigned short int row;
};
typedef struct {
int id;
int workout_id;
int exercise_id;
} workout_exercise;
struct workout_date_s {
int id;
char when[STR_LEN];
long when_int;
workout *workout;
//time_t created_at;
//time_t updated_at;
time_t started_at;
time_t finished_at;
int n;
};
typedef struct {
time_t created_at;
time_t started_at;
time_t last_completed_at;
time_t finished_at;
workout_date *workout_date;
exercise *exercise;
} exercise_log_entry;
typedef struct {
time_t created_at;
time_t started_at;
time_t completed_at;
time_t done_rested_at;
exercise_set *exercise_set;
exercise_log_entry *exercise_log_entry;
int n;
short int position;
} set_log_entry;
typedef struct {
int id;
exercise_set *exercise_set;
char variable[STR_LEN];
int base;
int inc;
int round_to_nearest;
int min_n;
int max_n;
int min_v;
int max_v;
} function;
/* method stubs */
void clear_screen(void);
void setup_fake_data(void);
void draw_workout_menu(void);
void workout_menu_fwd(void);
void workout_menu_back(void);
void workout_fwd(void);
void workout_back(void);
int read_csv(char *name, void (*callback)(char[STR_LEN], char[STR_LEN], char[STR_LEN]));
void draw_menu_more(int mid_x, int mid_y, bool filled, bool down);
void set_screen_to_workout_menu(void);
void set_screen_to_workout(void);
void draw_workout(void);
void draw_workout_buffer(void);
void init_debug(void);
void debug(char *s);
void copy_screen_to_buffer(void);
void load_csvs(void);
void load_workouts(void);
long now(void);
void tick(void);
void draw_workout_dashboard(void);
void set_playback_state(int state);
float calculate_function(char variable[STR_LEN], exercise_set *es, long n, float default_val);
void write_workout_dates(void);
void write_exercise_logs(void);
void write_set_log_entries(void);
void save_all_logs(void);
void morse_play_int(int i);
void morse_play(char c);
void morse_dot(void);
void morse_dash(void);
/* data allocations */
workout workouts[MAX_WORKOUTS];
workout_exercise workout_exercises[MAX_WORKOUTS * MAX_EXERCISES];
workout_date workout_dates[MAX_WORKOUT_DATES];
exercise exercises[MAX_EXERCISES];
exercise_set exercise_sets[MAX_EXERCISE_SETS];
exercise_log_entry exercise_log_entries[MAX_EXERCISE_LOGS];
set_log_entry set_log_entries[MAX_SET_LOGS];
function functions[MAX_FUNCTIONS];
int num_workouts = 0;
int num_workout_dates = 0;
int num_exercises = 0;
int num_workout_exercises = 0;
int num_exercise_sets = 0;
int num_workout_log_entries = 0;
int num_exercise_log_entries = 0;
int num_set_log_entries = 0;
int num_functions = 0;
/* finders */
exercise_log_entry *find_exercise_log_entry(workout_date *wd, exercise *e);
exercise_log_entry *find_exercise_log_entry_from_created_at(time_t created_at);
exercise_log_entry *find_or_create_exercise_log_entry(workout_date *wd, exercise *e);
set_log_entry *find_set_log_entry(exercise_log_entry *ele, exercise_set *s);
set_log_entry *find_or_create_set_log_entry(exercise_log_entry *ele, exercise_set *s);
workout *find_workout(long id);
exercise *find_exercise(long id);
workout_date *find_workout_date(long id);
exercise_set *find_exercise_set(int id);
/* screens */
#define WORKOUT_MENU 1
#define WORKOUT 2
/* state vars */
int app_current_screen = WORKOUT_MENU;
int playback_state;
time_t playback_last_state_change;
time_t last_save;
bool playback_is_last_set = false;
float playback_stay_seconds;
long int time_since_last_tick;
int state_seconds;
#define DEFAULT_SECONDS_ON_SETUP_FIRST_EXERCISE 60
int DEFAULT_SECONDS_ON_STATE[NUM_EXERCISE_STATES] = {
10.0 /* setup */,
30.0 /* inprogress */,
30.0 /* rest */,
0.0 /* done workout */
};
int DEFAULT_SECONDS_ON_STATE_FOR_STRETCH[NUM_EXERCISE_STATES] = {
5.0 /* setup */,
30.0 /* inprogress */,
5.0 /* rest */,
0.0 /* done workout */
};
char *STATE_STR[NUM_EXERCISE_STATES] = {
"setup weight" /* setup */,
"lift!" /* inprogress */,
"rest" /* rest */,
"done workout." /* done workout */
};
char *STATE_STR_FOR_STRETCH[NUM_EXERCISE_STATES] = {
"setup" /* setup */,
"hold" /* inprogress */,
"rest" /* rest */,
"done stretching." /* done workout */
};
char *STATE_WAIT_VAR[NUM_EXERCISE_STATES] = {
"setup" /* setup */,
"lift" /* inprogress */,
"rest" /* rest */,
"done" /* done workout */
};
char *STATE_WAIT_VAR_FOR_STRETCH[NUM_EXERCISE_STATES] = {
"setup" /* setup */,
"hold_for" /* inprogress */,
"rest" /* rest */,
"done" /* done workout */
};
int STATE_BEEP_FREQ[NUM_EXERCISE_STATES] = {
1000 /* setup */,
2000 /* inprogress */,
4000 /* rest */,
8000 /* done workout */
};
int STATE_BEEP_DURATION[NUM_EXERCISE_STATES] = {
100 /* setup */,
100 /* inprogress */,
100 /* rest */,
100 /* done workout */
};
int STATE_BEEP_AMPLITUDE[NUM_EXERCISE_STATES] = {
6000 /* setup */,
6000 /* inprogress */,
6000 /* rest */,
6000 /* done workout */
};
/* exercise playback */
exercise *playback_exercise = NULL;
exercise_log_entry *playback_exercise_log = NULL;
int playback_exercise_index;
exercise_set *playback_set = NULL;
int playback_set_index;
set_log_entry *playback_set_log = NULL;
/* globals */
#if MAX_BUFFERS > 0
char buffers[MAX_BUFFERS][LCD_WIDTH * LCD_HEIGHT * LCD_DEPTH];
#endif
int next_free_buffer = 0;
int workout_menu_selected_row = 0;
int workout_menu_top_item_index = 0;
int workout_top_item_index = 0;
workout *curr_workout;
workout_date *curr_workout_date;
int debug_fd;
char debug_line[TXT_LEN];
char *debug_line_ptr = debug_line;
exercise *workout_selected_exercise = NULL;
int workout_selected_exercise_index;
exercise_set *workout_selected_set = NULL;
int workout_selected_set_index;
bool workout_reset_cache = true;
int workout_top_row = 0;
enum plugin_status plugin_start(const void* parameter) {
(void)parameter;
int button;
bool scroll_fwd, scroll_back;
init_debug();
debug("=====================================================");
debug("Program startup");
debug_print("current time: %ld", now());
setup_fake_data();
load_csvs();
clear_screen();
/* viewport test */
//char store[LCD_WIDTH * LCD_HEIGHT * LCD_DEPTH];
//rb->lcd_putsxy(0, 0, "PUTS");
//rb->memcpy(store, rb->lcd_framebuffer, LCD_WIDTH * LCD_HEIGHT * LCD_DEPTH);
/* end viewport test */
scroll_fwd = scroll_back = false;
while (1) {
/* process buttons and draw current screen */
if (app_current_screen == WORKOUT_MENU) {
if (scroll_fwd) {
workout_menu_fwd();
} else if (scroll_back) {
workout_menu_back();
}
draw_workout_menu();
} else if (app_current_screen == WORKOUT) {
if (scroll_fwd) {
workout_fwd();
} else if (scroll_back) {
workout_back();
}
draw_workout();
}
//rb->memcpy(rb->lcd_framebuffer, store, LCD_WIDTH * LCD_HEIGHT * LCD_DEPTH);
rb->lcd_update();
scroll_fwd = scroll_back = false;
button = rb->button_get_w_tmo(25);
tick();
switch (button) {
case BUTTON_POWER:
debug("Program end");
return PLUGIN_OK;
case BUTTON_SELECT:
if (app_current_screen == WORKOUT_MENU) {
set_screen_to_workout();
} else if (app_current_screen == WORKOUT) {
if (workout_selected_exercise != NULL && workout_selected_set != NULL) {
playback_exercise = workout_selected_exercise;
playback_exercise_index = workout_selected_exercise_index;
playback_set = workout_selected_set;
playback_set_index = workout_selected_set_index;
playback_exercise_log = find_or_create_exercise_log_entry(curr_workout_date, playback_exercise);
playback_set_log = find_or_create_set_log_entry(playback_exercise_log, playback_set);
init_int(playback_set_log->started_at, now());
set_playback_state(EXERCISE_SETTING_UP);
}
}
break;
case BUTTON_SCROLL_FWD:
case BUTTON_SCROLL_FWD|BUTTON_REPEAT:
scroll_fwd = true;
break;
case BUTTON_SCROLL_BACK:
case BUTTON_SCROLL_BACK|BUTTON_REPEAT:
scroll_back = true;
break;
case BUTTON_LEFT:
if (app_current_screen == WORKOUT) {
set_screen_to_workout_menu();
}
}
}
debug("Program end");
}
void tick() {
long int diff;
if (app_current_screen == WORKOUT) {
time_since_last_tick = now() - playback_last_state_change;
diff = time_since_last_tick;
//debug_print("[%s] diff: %ld", STATE_STR[playback_state], diff);
if (diff >= playback_stay_seconds) {
debug_print("MOVE STATE before = %d [%s]", playback_state, STATE_STR[playback_state]);
switch (playback_state) {
case EXERCISE_SETTING_UP:
if (playback_set_log) {
playback_set_log->started_at = now();
}
if (playback_exercise_index == 0) {
curr_workout_date->started_at = now();
}
if (playback_exercise_log != NULL && playback_set_index == 0) {
playback_exercise_log->started_at = now();
}
//playback_last_state_change = now();
set_playback_state(EXERCISE_INPROGRESS);
break;
case EXERCISE_INPROGRESS:
if (playback_set_log) {
playback_set_log->completed_at = now();
}
if (playback_is_last_set) {
playback_exercise_log->last_completed_at = now();
}
//playback_last_state_change = now();
set_playback_state(EXERCISE_RESTING);
break;
case EXERCISE_RESTING:
/* fun part - move to the next set or exercise */
if (playback_set_log == NULL) return;
playback_set_log->done_rested_at = now();
debug_print("Done resting this set. Currently at playback_set_index %d and there are %d sets in this exercise", playback_set_index, playback_exercise->num_sets);
if (playback_set_index >= playback_exercise->num_sets - 1) {
playback_exercise_log->finished_at = now();
/* current exercise is done */
if (playback_exercise_index >= curr_workout->num_exercises - 1) {
/* done workout! */
curr_workout_date->finished_at = now();
set_playback_state(DONE_WORKOUT);
} else {
/* still have more exercises in the workout */
playback_exercise_index++;
playback_exercise = curr_workout->exercises[playback_exercise_index];
playback_exercise_log = find_or_create_exercise_log_entry(curr_workout_date, playback_exercise);
playback_set_index = 0;
playback_set = playback_exercise->sets[0]; /* TODO: what if there are no sets? */
playback_set_log = find_or_create_set_log_entry(playback_exercise_log, playback_set);
set_playback_state(EXERCISE_SETTING_UP);
}
} else {
/* still have more sets in exercise */
playback_set_index++;
playback_set++;
playback_set_log = find_or_create_set_log_entry(playback_exercise_log, playback_set);
playback_is_last_set = playback_set_index + 1 == playback_exercise->num_sets;
set_playback_state(EXERCISE_SETTING_UP);
}
//playback_last_state_change = now();
break;
}
debug_print(" after = %d [%s]", playback_state, STATE_STR[playback_state]);
}
/* save logs if necessary */
/*
if (now() - last_save > 10) {
debug_print("SAVE LOGS");
save_all_logs();
last_save = now();
}
*/
}
}
workout *find_workout(long id) {
int i;
for (i = 0; i < num_workouts; i++) {
if (workouts[i].id == id) {
return workouts + i;
}
}
return NULL;
}
exercise *find_exercise(long id) {
int i;
for (i = 0; i < num_exercises; i++) {
if (exercises[i].id == id) {
return exercises + i;
}
}
return NULL;
}
workout_date *find_workout_date(long id) {
int i;
for (i = 0; i < num_workout_dates; i++) {
if (workout_dates[i].id == id) {
return workout_dates + i;
}
}
return NULL;
}
void init_debug() {
if (DEBUG_ENABLED) {
debug_fd = rb->open("/workout.txt", O_RDWR | O_CREAT | O_APPEND, 0666);
}
}
void debug(char *s) {
if (DEBUG_ENABLED) {
if (s[rb->strlen(s)-1] != '\n') {
rb->fdprintf(debug_fd, "%s\n", s);
} else {
rb->fdprintf(debug_fd, "%s", s);
}
}
}
void clear_screen() {
rb->lcd_set_drawmode(DRMODE_SOLID);
rb->lcd_set_foreground(LCD_RED);
rb->lcd_set_background(LCD_BLACK);
rb->lcd_clear_display();
}
void set_screen_to_workout_menu() {
debug("Screen switch to workout menu");
app_current_screen = WORKOUT_MENU;
clear_screen();
}
void set_screen_to_workout() {
exercise *e;
exercise_set *s;
exercise_log_entry *ele;
set_log_entry *sle;
debug("Screen switch to workout view");
if (app_current_screen == WORKOUT_MENU) {
curr_workout_date = workout_dates + workout_menu_selected_row;
curr_workout = curr_workout_date->workout;
curr_workout_date->workout = curr_workout;
debug_print("Number of exercise in workout: %d\n", curr_workout->num_exercises);
if (curr_workout->num_exercises > 0) {
workout_selected_exercise = curr_workout->exercises[0];
workout_selected_exercise_index = 0;
// set exercise_log_entry of the first exercise
e = curr_workout->exercises[0];
ele = find_or_create_exercise_log_entry(curr_workout_date, e);
playback_exercise = e;
playback_exercise_index = 0;
playback_exercise_log = ele;
/* set the first set created */
debug_print("Checking for first set entry. Num sets: %d", e->num_sets);
if (e->num_sets > 0) {
s = e->sets[0];
sle = find_or_create_set_log_entry(ele, s);
playback_set = s;
playback_set_index = 0;
playback_set_log = sle;
}
set_playback_state(EXERCISE_SETTING_UP);
}
}
app_current_screen = WORKOUT;
//playback_last_state_change = now();
last_save = now();
workout_reset_cache = true;
workout_top_row = 0;
clear_screen();
}
void setup_fake_data(void) {
/* sets */
/*
exercise_sets[0].exercise_id = 0;
rb->strcpy(exercise_sets[0].name, "A1");
exercise_sets[0].position = 0;
exercise_sets[1].exercise_id = 0;
rb->strcpy(exercise_sets[1].name, "A2");
exercise_sets[1].position = 1;
exercise_sets[2].exercise_id = 0;
rb->strcpy(exercise_sets[2].name, "A3");
exercise_sets[2].position = 2;
*/
/* exercises */
/*
num_exercises = 3;
rb->strcpy(exercises[0].name, "ExA");
exercises[0].sets[0] = exercise_sets + 0;
exercises[0].sets[1] = exercise_sets + 1;
exercises[0].sets[2] = exercise_sets + 2;
exercises[0].num_sets = 3;
rb->strcpy(exercises[1].name, "ExB");
rb->strcpy(exercises[2].name, "ExC");
*/
/* workouts */
/*
rb->strcpy(workouts[0].name, "Upper Body 1");
workouts[0].id = 0;
workouts[0].num_exercises = 0;
rb->strcpy(workouts[1].name, "Legs 1");
workouts[1].id = 1;
workouts[1].num_exercises = 0;
rb->strcpy(workouts[2].name, "Stretches");
workouts[2].id = 2;
workouts[2].num_exercises = 0;
rb->strcpy(workouts[3].name, "W4");
workouts[3].id = 3;
workouts[3].num_exercises = 0;
rb->strcpy(workouts[4].name, "W5");
workouts[4].id = 4;
workouts[4].num_exercises = 3;
workouts[4].exercises[0] = exercises;
workouts[4].exercises[1] = exercises + 1;
workouts[4].exercises[2] = exercises + 2;
num_workouts = 5;
*/
}
void workout_menu_fwd() {
if (workout_menu_selected_row < num_workout_dates - 1) {
workout_menu_selected_row++;
}
workout_menu_top_item_index = MAX(0, workout_menu_selected_row - WORKOUT_MENU_ROWS + 1);
}
void workout_menu_back() {
if (workout_menu_selected_row > 0) {
workout_menu_selected_row--;
}
workout_menu_top_item_index = MIN(workout_menu_selected_row, workout_menu_top_item_index);
}
/* tries to move forward one set. return true if successful, false if at the last set of the current set */
bool workout_fwd_set(void) {
if (workout_selected_set_index < workout_selected_exercise->num_sets - 1) {
workout_selected_set++;
workout_selected_set_index++;
workout_selected_set = workout_selected_exercise->sets[workout_selected_set_index];
workout_top_row = MAX(workout_top_row, workout_selected_set->row - WORKOUT_ROWS + 1);
return true;
} else {
return false;
}
}
/* tries to move forward one exercise. returns true if successful, false if at the last exercise */
bool workout_fwd_exercise(void) {
if (workout_selected_exercise_index < curr_workout->num_exercises - 1) {
workout_selected_exercise_index++;
workout_selected_exercise = curr_workout->exercises[workout_selected_exercise_index];
workout_top_row = MAX(workout_top_row, workout_selected_exercise->row - WORKOUT_ROWS + 1);
return true;
} else {
return false;
}
}
/* tries to move backward one set. return true if successful, false if at the first set of the current exercise */
bool workout_back_set(void) {
if (workout_selected_set_index > 0) {
workout_selected_set_index--;
workout_selected_set = workout_selected_exercise->sets[workout_selected_set_index];
workout_top_row = MIN(workout_top_row, workout_selected_set->row);
return true;
} else {
return false;
}
}
/* tries to move backward one exercise. returns true if successful, false if at the last exercise */
bool workout_back_exercise(void) {
if (workout_selected_exercise_index > 0) {
workout_selected_exercise_index--;
workout_selected_exercise = curr_workout->exercises[workout_selected_exercise_index];
workout_top_row = MIN(workout_top_row, workout_selected_exercise->row);
return true;
} else {
return false;
}
}
void workout_fwd() {
/* if workout_selected_exercise isn't set by this point, we can safely assume there are no exercises */
if (workout_selected_exercise == NULL) {
return;
}
/* if no set is chosen, go to the first set, or go on to the next exercise if no sets */
if (workout_selected_exercise != NULL && workout_selected_set == NULL) {
if (workout_selected_exercise->num_sets > 0) {
workout_selected_set = workout_selected_exercise->sets[0];
workout_selected_set_index = 0;
workout_top_row = MAX(workout_top_row, workout_selected_set->row - WORKOUT_ROWS + 1);
} else if (workout_fwd_exercise()) {
workout_fwd();
}
return;
}
/* otherwise move the set forward one or jump to the next exercise. */
if (!workout_fwd_set() && workout_fwd_exercise()) {
workout_selected_set = NULL;
}
}
void workout_back() {
int ns;
/* if workout_selected_exercise isn't set by this point, we can safely assume there are no exercises */
if (workout_selected_exercise == NULL) {
return;
}
/* if no set is chosen but there is an exercise chosen, go backwards to the last set of the previous exercise */
if (workout_selected_exercise != NULL && workout_selected_set == NULL) {
if (workout_back_exercise()) {
ns = workout_selected_exercise->num_sets;
workout_selected_set = workout_selected_exercise->sets[ns-1];
workout_selected_set_index = ns-1;
workout_top_row = MIN(workout_top_row, workout_selected_set->row);
}
return;
}
/* otherwise move the set backward one, or jump to the exercise only */
if (!workout_back_set()) {
workout_selected_set = NULL;
workout_top_row = MIN(workout_top_row, workout_selected_exercise->row);
}
}
void draw_workout_menu() {
int i, x, y, half_row;
bool at_top, at_bottom;
rb->lcd_set_foreground(WORKOUT_MENU_COLOR);
x = (SCREEN_WIDTH - WORKOUT_MENU_WIDTH) / 2;
y = (SCREEN_HEIGHT - WORKOUT_MENU_HEIGHT) / 2;
//debug_print("x=%d y=%d", x, y);
half_row = WORKOUT_MENU_ROW_HEIGHT / 2;
/* border */
rb->lcd_drawrect(x, y, WORKOUT_MENU_WIDTH, WORKOUT_MENU_HEADER_HEIGHT);
rb->lcd_drawrect(x, y, WORKOUT_MENU_WIDTH, WORKOUT_MENU_HEIGHT);
rb->lcd_drawrect(x, y + WORKOUT_MENU_HEIGHT - WORKOUT_MENU_HEADER_HEIGHT,
WORKOUT_MENU_WIDTH, WORKOUT_MENU_HEADER_HEIGHT);
/* more scroll up/down triangle indicators */
if (num_workouts > WORKOUT_MENU_ROWS) {
at_top = workout_menu_top_item_index == 0;
at_top = false;
at_bottom = workout_menu_top_item_index == num_workouts - WORKOUT_MENU_ROWS;
draw_menu_more(SCREEN_WIDTH / 2, y + WORKOUT_MENU_HEADER_HEIGHT / 2, !at_top, false);
draw_menu_more(SCREEN_WIDTH / 2, y + WORKOUT_MENU_HEIGHT - WORKOUT_MENU_HEADER_HEIGHT / 2, !at_bottom, true);
}
/* menu */
y += WORKOUT_MENU_HEADER_HEIGHT + WORKOUT_MENU_MARGIN;
for (i = 0;
i < WORKOUT_MENU_ROWS && i + workout_menu_top_item_index < num_workout_dates; i++) {
// clear out the row with a solid color first
rb->lcd_set_foreground(BACKGROUND_COLOR);
rb->lcd_fillrect(x + WORKOUT_MENU_MARGIN, y, WORKOUT_MENU_WIDTH - WORKOUT_MENU_MARGIN*2, WORKOUT_MENU_ROW_HEIGHT);
// set different color if selected
if (i + workout_menu_top_item_index == workout_menu_selected_row) {
rb->lcd_set_foreground(WORKOUT_MENU_SEL_COLOR);
} else {
rb->lcd_set_foreground(WORKOUT_MENU_COLOR);
}
// row text
rb->lcd_putsxy(x + WORKOUT_MENU_MARGIN, y, workout_dates[i + workout_menu_top_item_index].workout->name);
// workout date
y += half_row;
rb->lcd_putsxy(LCD_WIDTH * 0.3, y, workout_dates[i + workout_menu_top_item_index].when);
y += half_row;
y += 2;
//y += WORKOUT_MENU_ROW_HEIGHT;
}
}
void draw_workout() {
draw_workout_buffer();
}
void draw_workout_buffer() {
int row, i, j, x, y, c, half_row, title_width;
//bool at_top, at_bottom;
char ex_line[STR_LEN], set_line[STR_LEN];
exercise* curr_exercise;
exercise_set *curr_set;
exercise_log_entry *exercise_log_entry;
set_log_entry *set_log_entry;
bool draw;
/* reset buffers, they will be rendered again from here */
next_free_buffer = 0;
/* start with a blank screen */
rb->lcd_set_foreground(LCD_BLACK);
rb->lcd_fillrect(0, 0, LCD_WIDTH, LCD_HEIGHT);
rb->lcd_set_foreground(WORKOUT_COLOR);
x = WORKOUT_MARGIN;
y = WORKOUT_MARGIN;
half_row = WORKOUT_ROW_HEIGHT / 2;
/* draw the workout title */
title_width = WORKOUT_TITLE_CHAR_WIDTH * rb->strlen(curr_workout->name);
rb->lcd_putsxy(x + SCREEN_WIDTH / 2 - title_width / 2, y, curr_workout->name);
/* more scroll up/down triangle indicators */
/* TODO this later
if (num_workouts > WORKOUT_MENU_ROWS) {
at_top = workout_top_item_index == 0;
at_bottom = workout_top_item_index == curr_workout->num_exercises - WORKOUT_MENU_ROWS;
draw_menu_more(SCREEN_WIDTH / 2, y, !at_top, false);
draw_menu_more(SCREEN_WIDTH / 2, y + WORKOUT_MENU_HEIGHT - WORKOUT_MENU_ROW_HEIGHT, !at_bottom, true);
}
*/
/* menu */
y += WORKOUT_ROW_HEIGHT + half_row;
//debug_print("In workout view render. Current workout: %s; num exc: %d", curr_workout->name, curr_workout->num_exercises);
row = 0;
for (i = 0; i < curr_workout->num_exercises; i++, row++, y += (draw ? WORKOUT_ROW_HEIGHT : 0)) {
draw = row >= workout_top_row && row < workout_top_row + WORKOUT_ROWS;
curr_exercise = curr_workout->exercises[i + workout_top_item_index];
// find exercise log
exercise_log_entry = find_exercise_log_entry(curr_workout_date, curr_exercise);
//debug_print("In workout view render loop. workout_top_row: %d row: %d draw: %d", workout_top_row, row, draw);
// clear out the row with a solid color first
if (draw) {
rb->lcd_set_foreground(BACKGROUND_COLOR);
rb->lcd_fillrect(0, y, SCREEN_WIDTH, WORKOUT_ROW_HEIGHT);
}
/* exercise text */
if (exercise_log_entry && exercise_log_entry->finished_at != 0) {
rb->lcd_set_foreground(WORKOUT_DONE_COLOR);
} else if (exercise_log_entry && exercise_log_entry->last_completed_at != 0) {
rb->lcd_set_foreground(WORKOUT_RESTING_COLOR);
} else if (exercise_log_entry && exercise_log_entry->started_at != 0) {
rb->lcd_set_foreground(WORKOUT_INPROGRESS_COLOR);
} else if (exercise_log_entry && exercise_log_entry->created_at != 0) {
rb->lcd_set_foreground(WORKOUT_SETUP_COLOR);
} else {
rb->lcd_set_foreground(WORKOUT_COLOR);
}
if (playback_exercise == curr_exercise && playback_set == NULL) {
rb->snprintf(ex_line, STR_LEN, "*%s", curr_exercise->name);
rb->lcd_putsxy(WORKOUT_MARGIN + 1, y, ex_line);
} else {
rb->lcd_putsxy(WORKOUT_MARGIN + 1, y, curr_exercise->name);
}
/* draw box around it if selected */
if (draw && workout_selected_set == NULL && workout_selected_exercise == curr_exercise) {
rb->lcd_drawrect(WORKOUT_MARGIN, y, SCREEN_WIDTH - WORKOUT_MARGIN * 2, WORKOUT_ROW_HEIGHT - 1);
}
if (workout_reset_cache) {
curr_exercise->row = row;
}
/* exercise sets */
row++;
if (draw) y += WORKOUT_ROW_HEIGHT;
c = 0;
for (j = 0; j < curr_workout->exercises[i + workout_top_item_index]->num_sets; j++) {
draw = row >= workout_top_row && row < workout_top_row + WORKOUT_ROWS;
curr_set = curr_workout->exercises[i + workout_top_item_index]->sets[j];
x = c * WORKOUT_SET_WIDTH + WORKOUT_MARGIN + WORKOUT_SET_MARGIN;
/* clear out space */
if (draw) {
rb->lcd_set_foreground(BACKGROUND_COLOR);
rb->lcd_fillrect(x, y, WORKOUT_SET_WIDTH, WORKOUT_ROW_HEIGHT);
}
/* set text */
set_log_entry = find_set_log_entry(exercise_log_entry, curr_set);
if (draw) {
if (set_log_entry && set_log_entry->done_rested_at != 0) {
rb->lcd_set_foreground(WORKOUT_DONE_COLOR);
} else if (set_log_entry && set_log_entry->completed_at != 0) {
rb->lcd_set_foreground(WORKOUT_RESTING_COLOR);
} else if (set_log_entry && set_log_entry->started_at != 0) {
rb->lcd_set_foreground(WORKOUT_INPROGRESS_COLOR);
} else if (set_log_entry && set_log_entry->created_at != 0) {
rb->lcd_set_foreground(WORKOUT_SETUP_COLOR);
} else {
rb->lcd_set_foreground(WORKOUT_COLOR);
}
}
if (workout_reset_cache) {
if (curr_exercise->exercise_type_id == EXERCISE_TYPE_WEIGHTS) {
curr_set->reps = calculate_function("reps", curr_set, curr_workout_date->n, 0);
curr_set->weight = calculate_function("weight", curr_set, curr_workout_date->n, 0);
} else if (curr_exercise->exercise_type_id == EXERCISE_TYPE_STRETCH) {
curr_set->hold_for = calculate_function("hold_for", curr_set, curr_workout_date->n, 30);
}
curr_set->row = row;
}
/* draw set */
if (draw) {
//debug_print(" exercise_type_id=%d", curr_exercise->exercise_type_id);
if (curr_exercise->exercise_type_id == EXERCISE_TYPE_WEIGHTS) {
if (curr_set == playback_set) {
rb->snprintf(set_line, STR_LEN, "*%s %dx%d", curr_set->name, (int)curr_set->reps, (int)curr_set->weight);
} else {
rb->snprintf(set_line, STR_LEN, "%s %dx%d", curr_set->name, (int)curr_set->reps, (int)curr_set->weight);
}
} else if (curr_exercise->exercise_type_id == EXERCISE_TYPE_STRETCH) {
if (curr_set == playback_set) {
rb->snprintf(set_line, STR_LEN, "*%s %ds", curr_set->name, (int)curr_set->hold_for);
} else {
rb->snprintf(set_line, STR_LEN, "%s %ds", curr_set->name, (int)curr_set->hold_for);
}
}
rb->lcd_putsxy(x + 1, y + 1, set_line);
}
/* draw box around it if selected */
if (draw && workout_selected_set == curr_set) {
//debug_print(" select set %s", curr_set->name);