Skip to content

Commit 87ee676

Browse files
committed
Extract code for REPLAY state into separate function.
1 parent 0fac2d6 commit 87ee676

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

examples/Braccio_Learn_and_Repeat/Braccio_Learn_and_Repeat.ino

+31-25
Original file line numberDiff line numberDiff line change
@@ -183,21 +183,7 @@ void loop()
183183

184184
if (state == State::REPLAY)
185185
{
186-
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]);
187-
replay_cnt += 6;
188-
189-
if (replay_cnt >= sample_cnt)
190-
{
191-
state = State::ZERO_POSITION;
192-
Braccio.lvgl_lock();
193-
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
194-
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
195-
Braccio.lvgl_unlock();
196-
}
197-
198-
Braccio.lvgl_lock();
199-
lv_label_set_text_fmt(counter, "Counter: %d" , (replay_cnt / 6));
200-
Braccio.lvgl_unlock();
186+
state = handle_REPLAY();
201187
}
202188

203189
if (state == State::ZERO_POSITION)
@@ -220,23 +206,43 @@ State handle_RECORD()
220206
replay_cnt = 0;
221207

222208
Braccio.lvgl_lock();
223-
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
209+
btnm_map[0] = "RECORD";
224210
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
225211
Braccio.lvgl_unlock();
226212

227213
return State::ZERO_POSITION;
228214
}
229-
else
230-
{
231-
/* Capture those samples. */
232-
Braccio.positions(values + sample_cnt);
233-
sample_cnt += 6;
234215

235-
/* Update sample counter. */
216+
/* Capture those samples. */
217+
Braccio.positions(values + sample_cnt);
218+
sample_cnt += 6;
219+
220+
/* Update sample counter. */
221+
Braccio.lvgl_lock();
222+
lv_label_set_text_fmt(counter, "Counter: %d" , (sample_cnt / 6));
223+
Braccio.lvgl_unlock();
224+
225+
return State::RECORD;
226+
}
227+
228+
State handle_REPLAY()
229+
{
230+
if (replay_cnt >= sample_cnt)
231+
{
236232
Braccio.lvgl_lock();
237-
lv_label_set_text_fmt(counter, "Counter: %d" , (sample_cnt / 6));
233+
btnm_map[2] = "REPLAY";
234+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
238235
Braccio.lvgl_unlock();
239236

240-
return State::RECORD;
237+
return State::ZERO_POSITION;
241238
}
242-
}
239+
240+
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]);
241+
replay_cnt += 6;
242+
243+
Braccio.lvgl_lock();
244+
lv_label_set_text_fmt(counter, "Counter: %d" , (replay_cnt / 6));
245+
Braccio.lvgl_unlock();
246+
247+
return State::REPLAY;
248+
}

0 commit comments

Comments
 (0)