@@ -13,9 +13,12 @@ enum states {
13
13
14
14
int state = ZERO_POSITION;
15
15
16
- float values[10000 ];
16
+ static int const MAX_SAMPLES = 6 *1000 *2 ; /* 20 seconds. */
17
+
18
+ float values[MAX_SAMPLES];
17
19
float * idx = values;
18
20
float * final_idx = 0 ;
21
+ int sample_cnt = 0 ;
19
22
float homePos[6 ] = {157.5 , 157.5 , 157.5 , 157.5 , 157.5 , 90.0 };
20
23
21
24
static lv_obj_t * counter;
@@ -25,10 +28,12 @@ static const char * btnm_map[] = { "RECORD", "\n", "REPLAY", "\n", "ZERO_POSITIO
25
28
26
29
27
30
static void eventHandlerMenu (lv_event_t * e) {
31
+ Braccio.lvgl_lock ();
28
32
lv_event_code_t code = lv_event_get_code (e);
29
33
lv_obj_t * obj = lv_event_get_target (e);
30
34
31
35
if (code == LV_EVENT_KEY && lv_indev_get_key (lv_indev_get_act ()) == LV_KEY_HOME) {
36
+ Braccio.lvgl_unlock ();
32
37
state = ZERO_POSITION;
33
38
return ;
34
39
}
@@ -42,6 +47,7 @@ static void eventHandlerMenu(lv_event_t * e) {
42
47
}
43
48
44
49
idx = values;
50
+ sample_cnt = 0 ;
45
51
46
52
switch (id) {
47
53
case 0 : // if the button pressed is the first one
@@ -90,9 +96,12 @@ static void eventHandlerMenu(lv_event_t * e) {
90
96
break ;
91
97
}
92
98
}
99
+ Braccio.lvgl_unlock ();
93
100
}
94
101
95
- void mainMenu () {
102
+ void mainMenu ()
103
+ {
104
+ Braccio.lvgl_lock ();
96
105
static lv_style_t style_focus;
97
106
lv_style_init (&style_focus);
98
107
lv_style_set_outline_color (&style_focus, lv_color_hex (COLOR_ORANGE));
@@ -124,6 +133,7 @@ void mainMenu() {
124
133
lv_obj_align (counter, LV_ALIGN_CENTER, 0 , 80 );
125
134
126
135
lv_obj_add_event_cb (btnm, eventHandlerMenu, LV_EVENT_ALL, NULL );
136
+ Braccio.lvgl_unlock ();
127
137
128
138
Braccio.connectJoystickTo (btnm);
129
139
}
@@ -141,25 +151,38 @@ void setup() {
141
151
142
152
void loop () {
143
153
if (state == RECORD) {
154
+
155
+ /* Check if we still have space for samples. */
156
+ if (sample_cnt >= MAX_SAMPLES) {
157
+ state = ZERO_POSITION;
158
+ Serial.println (" ZERO_POSITION" );
159
+ Braccio.lvgl_lock ();
160
+ btnm_map[0 ] = " RECORD" ; // reset the label of the first button back to "RECORD"
161
+ lv_btnmatrix_set_btn_ctrl (btnm, 0 , LV_BTNMATRIX_CTRL_CHECKABLE);
162
+ Braccio.lvgl_unlock ();
163
+ }
164
+ /* Capture those samples. */
144
165
Braccio.positions (idx);
145
166
idx += 6 ;
167
+ sample_cnt += 6 ;
146
168
}
147
169
if (state == REPLAY) {
148
170
Braccio.moveTo (idx[0 ], idx[1 ], idx[2 ], idx[3 ], idx[4 ], idx[5 ]);
149
171
idx += 6 ;
172
+ sample_cnt += 6 ;
150
173
if (idx >= final_idx) {
151
174
Serial.println (" REPLAY done" );
152
175
state = ZERO_POSITION;
176
+ Braccio.lvgl_lock ();
153
177
btnm_map[2 ] = " REPLAY" ; // reset the label of the first button back to "REPLAY"
154
178
lv_btnmatrix_set_btn_ctrl (btnm, 2 , LV_BTNMATRIX_CTRL_CHECKED);
179
+ Braccio.lvgl_unlock ();
155
180
}
156
181
}
157
- if (idx - values >= sizeof (values)) {
158
- Serial.println (" ZERO_POSITION" );
159
- state = ZERO_POSITION;
160
- }
161
182
delay (100 );
162
183
if (state != ZERO_POSITION) {
163
- lv_label_set_text_fmt (counter, " Counter: %d" , idx - values);
184
+ Braccio.lvgl_lock ();
185
+ lv_label_set_text_fmt (counter, " Counter: %d" , (sample_cnt / 6 ));
186
+ Braccio.lvgl_unlock ();
164
187
}
165
188
}
0 commit comments