Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a37cfdf

Browse files
committedApr 28, 2022
Align examples.
1 parent 48ed9b5 commit a37cfdf

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed
 

‎examples/Platform_Tutorials/projects/p03-learning-mode/01_Braccio_learning_mode/01_Braccio_learning_mode.ino

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ enum states {
1313

1414
int state = ZERO_POSITION;
1515

16-
float values[10000];
16+
static int const MAX_SAMPLES = 6*1000*2; /* 20 seconds. */
17+
18+
float values[MAX_SAMPLES];
1719
float* idx = values;
1820
float* final_idx = 0;
21+
int sample_cnt = 0;
1922
float homePos[6] = {157.5, 157.5, 157.5, 157.5, 157.5, 90.0};
2023

2124
static lv_obj_t * counter;
@@ -25,10 +28,12 @@ static const char * btnm_map[] = { "RECORD", "\n", "REPLAY", "\n", "ZERO_POSITIO
2528

2629

2730
static void eventHandlerMenu(lv_event_t * e) {
31+
Braccio.lvgl_lock();
2832
lv_event_code_t code = lv_event_get_code(e);
2933
lv_obj_t * obj = lv_event_get_target(e);
3034

3135
if (code == LV_EVENT_KEY && lv_indev_get_key(lv_indev_get_act()) == LV_KEY_HOME) {
36+
Braccio.lvgl_unlock();
3237
state = ZERO_POSITION;
3338
return;
3439
}
@@ -42,6 +47,7 @@ static void eventHandlerMenu(lv_event_t * e) {
4247
}
4348

4449
idx = values;
50+
sample_cnt = 0;
4551

4652
switch (id) {
4753
case 0: // if the button pressed is the first one
@@ -90,9 +96,12 @@ static void eventHandlerMenu(lv_event_t * e) {
9096
break;
9197
}
9298
}
99+
Braccio.lvgl_unlock();
93100
}
94101

95-
void mainMenu() {
102+
void mainMenu()
103+
{
104+
Braccio.lvgl_lock();
96105
static lv_style_t style_focus;
97106
lv_style_init(&style_focus);
98107
lv_style_set_outline_color(&style_focus, lv_color_hex(COLOR_ORANGE));
@@ -124,6 +133,7 @@ void mainMenu() {
124133
lv_obj_align(counter, LV_ALIGN_CENTER, 0, 80);
125134

126135
lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_ALL, NULL);
136+
Braccio.lvgl_unlock();
127137

128138
Braccio.connectJoystickTo(btnm);
129139
}
@@ -141,25 +151,38 @@ void setup() {
141151

142152
void loop() {
143153
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. */
144165
Braccio.positions(idx);
145166
idx += 6;
167+
sample_cnt += 6;
146168
}
147169
if (state == REPLAY) {
148170
Braccio.moveTo(idx[0], idx[1], idx[2], idx[3], idx[4], idx[5]);
149171
idx += 6;
172+
sample_cnt += 6;
150173
if (idx >= final_idx) {
151174
Serial.println("REPLAY done");
152175
state = ZERO_POSITION;
176+
Braccio.lvgl_lock();
153177
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
154178
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
179+
Braccio.lvgl_unlock();
155180
}
156181
}
157-
if (idx - values >= sizeof(values)) {
158-
Serial.println("ZERO_POSITION");
159-
state = ZERO_POSITION;
160-
}
161182
delay(100);
162183
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();
164187
}
165188
}

0 commit comments

Comments
 (0)
Please sign in to comment.