@@ -19,9 +19,8 @@ int state = ZERO_POSITION;
19
19
static int const MAX_SAMPLES = 6 *100 *2 ; /* 20 seconds. */
20
20
21
21
float values[MAX_SAMPLES];
22
- float * idx = values;
23
- float * final_idx = 0 ;
24
22
int sample_cnt = 0 ;
23
+ int replay_cnt = 0 ;
25
24
float homePos[6 ] = {157.5 , 157.5 , 157.5 , 157.5 , 157.5 , 90.0 };
26
25
27
26
static lv_obj_t * counter;
@@ -30,33 +29,32 @@ static lv_obj_t * btnm;
30
29
static const char * btnm_map[] = { " RECORD" , " \n " , " REPLAY" , " \n " , " ZERO_POSITION" , " \n " , " \0 " };
31
30
32
31
33
- static void eventHandlerMenu (lv_event_t * e) {
32
+ static void eventHandlerMenu (lv_event_t * e)
33
+ {
34
34
Braccio.lvgl_lock ();
35
35
lv_event_code_t code = lv_event_get_code (e);
36
36
lv_obj_t * obj = lv_event_get_target (e);
37
37
38
- if (code == LV_EVENT_CLICKED || (code == LV_EVENT_KEY && Braccio.getKey () == BUTTON_ENTER)) {
38
+ if (code == LV_EVENT_CLICKED || (code == LV_EVENT_KEY && Braccio.getKey () == BUTTON_ENTER))
39
+ {
39
40
uint32_t id = lv_btnmatrix_get_selected_btn (obj);
40
41
const char * txt = lv_btnmatrix_get_btn_text (obj, id);
41
42
42
- if (state == RECORD) {
43
- final_idx = idx;
44
- }
45
-
46
- idx = values;
47
- sample_cnt = 0 ;
48
-
49
- switch (id) {
43
+ switch (id)
44
+ {
50
45
case 0 : // if the button pressed is the first one
51
- if (txt == " RECORD" ) {
46
+ if (txt == " RECORD" )
47
+ {
52
48
state = RECORD;
49
+ sample_cnt = 0 ;
53
50
Braccio.disengage (); // allow the user to freely move the braccio
54
51
lv_btnmatrix_set_btn_ctrl (btnm, 0 , LV_BTNMATRIX_CTRL_CHECKED);
55
52
Serial.println (" RECORD" );
56
53
lv_btnmatrix_clear_btn_ctrl (btnm, 1 , LV_BTNMATRIX_CTRL_DISABLED); // remove disabled state of the replay button
57
54
btnm_map[0 ] = " STOP" ; // change the label of the first button to "STOP"
58
55
}
59
- else if (txt == " STOP" ) {
56
+ else if (txt == " STOP" )
57
+ {
60
58
state = ZERO_POSITION;
61
59
Braccio.engage (); // enable the steppers so that the braccio stands still
62
60
lv_btnmatrix_set_btn_ctrl (btnm, 2 , LV_BTNMATRIX_CTRL_CHECKED);
@@ -65,14 +63,16 @@ static void eventHandlerMenu(lv_event_t * e) {
65
63
break ;
66
64
case 1 :
67
65
btnm_map[0 ] = " RECORD" ; // reset the label of the first button back to "RECORD"
68
- if (txt == " REPLAY" ){
69
- state = REPLAY;
70
- btnm_map[2 ] = " STOP" ; // change the label of the second button to "STOP"
71
- Braccio.engage ();
72
- lv_btnmatrix_set_btn_ctrl (btnm, 1 , LV_BTNMATRIX_CTRL_CHECKED);
73
- Serial.println (" REPLAY" );
66
+ if (txt == " REPLAY" )
67
+ {
68
+ state = REPLAY;
69
+ replay_cnt = 0 ;
70
+ btnm_map[2 ] = " STOP" ; // change the label of the second button to "STOP"
71
+ Braccio.engage ();
72
+ lv_btnmatrix_set_btn_ctrl (btnm, 1 , LV_BTNMATRIX_CTRL_CHECKED);
74
73
}
75
- else if (txt==" STOP" ){
74
+ else if (txt==" STOP" )
75
+ {
76
76
state = ZERO_POSITION;
77
77
Braccio.engage (); // enable the steppers so that the braccio stands still
78
78
lv_btnmatrix_set_btn_ctrl (btnm, 2 , LV_BTNMATRIX_CTRL_CHECKED);
@@ -89,7 +89,6 @@ static void eventHandlerMenu(lv_event_t * e) {
89
89
delay (500 );
90
90
Braccio.moveTo (homePos[0 ], homePos[1 ], homePos[2 ], homePos[3 ], homePos[4 ], homePos[5 ]);
91
91
lv_btnmatrix_set_btn_ctrl (btnm, 2 , LV_BTNMATRIX_CTRL_CHECKED);
92
- Serial.println (" ZERO_POSITION" );
93
92
break ;
94
93
}
95
94
}
@@ -158,9 +157,10 @@ void loop()
158
157
if (state == RECORD)
159
158
{
160
159
/* Check if we still have space for samples. */
161
- if (sample_cnt >= MAX_SAMPLES) {
160
+ if (sample_cnt >= MAX_SAMPLES)
161
+ {
162
162
state = ZERO_POSITION;
163
- Serial. println ( " ZERO_POSITION " ) ;
163
+ replay_cnt = 0 ;
164
164
Braccio.lvgl_lock ();
165
165
btnm_map[0 ] = " RECORD" ; // reset the label of the first button back to "RECORD"
166
166
lv_btnmatrix_set_btn_ctrl (btnm, 0 , LV_BTNMATRIX_CTRL_CHECKABLE);
@@ -169,32 +169,38 @@ void loop()
169
169
else
170
170
{
171
171
/* Capture those samples. */
172
- Braccio.positions (idx);
173
- idx += 6 ;
172
+ Braccio.positions (values + sample_cnt);
174
173
sample_cnt += 6 ;
175
174
}
175
+
176
+ Braccio.lvgl_lock ();
177
+ lv_label_set_text_fmt (counter, " Counter: %d" , (sample_cnt / 6 ));
178
+ Braccio.lvgl_unlock ();
176
179
}
177
180
178
181
if (state == REPLAY)
179
182
{
180
- Braccio.moveTo (idx[ 0 ], idx[ 1 ], idx[ 2 ], idx[ 3 ], idx[ 4 ], idx[ 5 ]);
181
- idx += 6 ;
182
- sample_cnt += 6 ;
183
- if (idx >= final_idx) {
184
- Serial. println ( " REPLAY done " );
183
+ Braccio.moveTo (values[replay_cnt + 0 ], values[replay_cnt + 1 ], values[replay_cnt + 2 ], values[replay_cnt + 3 ], values[replay_cnt + 4 ], values[replay_cnt + 5 ]);
184
+ replay_cnt += 6 ;
185
+
186
+ if (replay_cnt >= sample_cnt)
187
+ {
185
188
state = ZERO_POSITION;
186
189
Braccio.lvgl_lock ();
187
190
btnm_map[2 ] = " REPLAY" ; // reset the label of the first button back to "REPLAY"
188
191
lv_btnmatrix_set_btn_ctrl (btnm, 2 , LV_BTNMATRIX_CTRL_CHECKED);
189
192
Braccio.lvgl_unlock ();
190
193
}
191
- }
192
194
193
- if (state != ZERO_POSITION)
194
- {
195
195
Braccio.lvgl_lock ();
196
- lv_label_set_text_fmt (counter, " Counter: %d" , (sample_cnt / 6 ));
196
+ lv_label_set_text_fmt (counter, " Counter: %d" , (replay_cnt / 6 ));
197
197
Braccio.lvgl_unlock ();
198
198
}
199
+
200
+ if (state == ZERO_POSITION)
201
+ {
202
+ Braccio.engage ();
203
+ Braccio.moveTo (homePos[0 ], homePos[1 ], homePos[2 ], homePos[3 ], homePos[4 ], homePos[5 ]);
204
+ }
199
205
}
200
206
}
0 commit comments