55#define COLOR_LIGHT_TEAL 0x62AEB2
66#define COLOR_ORANGE 0xE47128
77
8+ // ENTER button
9+ #define BUTTON_ENTER 6
10+
811enum states {
912 RECORD,
1013 REPLAY,
@@ -13,9 +16,12 @@ enum states {
1316
1417int state = ZERO_POSITION;
1518
16- float values[10000 ];
19+ static int const MAX_SAMPLES = 6 *1000 *2 ; /* 20 seconds. */
20+
21+ float values[MAX_SAMPLES];
1722float * idx = values;
1823float * final_idx = 0 ;
24+ int sample_cnt = 0 ;
1925float homePos[6 ] = {157.5 , 157.5 , 157.5 , 157.5 , 157.5 , 90.0 };
2026
2127static lv_obj_t * counter;
@@ -25,15 +31,11 @@ static const char * btnm_map[] = { "RECORD", "\n", "REPLAY", "\n", "ZERO_POSITIO
2531
2632
2733static void eventHandlerMenu (lv_event_t * e) {
34+ Braccio.lvgl_lock ();
2835 lv_event_code_t code = lv_event_get_code (e);
2936 lv_obj_t * obj = lv_event_get_target (e);
3037
31- if (code == LV_EVENT_KEY && lv_indev_get_key (lv_indev_get_act ()) == LV_KEY_HOME) {
32- state = ZERO_POSITION;
33- return ;
34- }
35-
36- if (code == LV_EVENT_CLICKED) {
38+ if (code == LV_EVENT_CLICKED || (code == LV_EVENT_KEY && Braccio.getKey () == BUTTON_ENTER)) {
3739 uint32_t id = lv_btnmatrix_get_selected_btn (obj);
3840 const char * txt = lv_btnmatrix_get_btn_text (obj, id);
3941
@@ -42,6 +44,7 @@ static void eventHandlerMenu(lv_event_t * e) {
4244 }
4345
4446 idx = values;
47+ sample_cnt = 0 ;
4548
4649 switch (id) {
4750 case 0 : // if the button pressed is the first one
@@ -90,9 +93,12 @@ static void eventHandlerMenu(lv_event_t * e) {
9093 break ;
9194 }
9295 }
96+ Braccio.lvgl_unlock ();
9397}
9498
95- void mainMenu () {
99+ void mainMenu ()
100+ {
101+ Braccio.lvgl_lock ();
96102 static lv_style_t style_focus;
97103 lv_style_init (&style_focus);
98104 lv_style_set_outline_color (&style_focus, lv_color_hex (COLOR_ORANGE));
@@ -124,6 +130,7 @@ void mainMenu() {
124130 lv_obj_align (counter, LV_ALIGN_CENTER, 0 , 80 );
125131
126132 lv_obj_add_event_cb (btnm, eventHandlerMenu, LV_EVENT_ALL, NULL );
133+ Braccio.lvgl_unlock ();
127134
128135 Braccio.connectJoystickTo (btnm);
129136}
@@ -141,25 +148,38 @@ void setup() {
141148
142149void loop () {
143150 if (state == RECORD) {
151+
152+ /* Check if we still have space for samples. */
153+ if (sample_cnt >= MAX_SAMPLES) {
154+ state = ZERO_POSITION;
155+ Serial.println (" ZERO_POSITION" );
156+ Braccio.lvgl_lock ();
157+ btnm_map[0 ] = " RECORD" ; // reset the label of the first button back to "RECORD"
158+ lv_btnmatrix_set_btn_ctrl (btnm, 0 , LV_BTNMATRIX_CTRL_CHECKABLE);
159+ Braccio.lvgl_unlock ();
160+ }
161+ /* Capture those samples. */
144162 Braccio.positions (idx);
145163 idx += 6 ;
164+ sample_cnt += 6 ;
146165 }
147166 if (state == REPLAY) {
148167 Braccio.moveTo (idx[0 ], idx[1 ], idx[2 ], idx[3 ], idx[4 ], idx[5 ]);
149168 idx += 6 ;
169+ sample_cnt += 6 ;
150170 if (idx >= final_idx) {
151171 Serial.println (" REPLAY done" );
152172 state = ZERO_POSITION;
173+ Braccio.lvgl_lock ();
153174 btnm_map[2 ] = " REPLAY" ; // reset the label of the first button back to "REPLAY"
154175 lv_btnmatrix_set_btn_ctrl (btnm, 2 , LV_BTNMATRIX_CTRL_CHECKED);
176+ Braccio.lvgl_unlock ();
155177 }
156178 }
157- if (idx - values >= sizeof (values)) {
158- Serial.println (" ZERO_POSITION" );
159- state = ZERO_POSITION;
160- }
161179 delay (100 );
162180 if (state != ZERO_POSITION) {
163- lv_label_set_text_fmt (counter, " Counter: %d" , idx - values);
181+ Braccio.lvgl_lock ();
182+ lv_label_set_text_fmt (counter, " Counter: %d" , (sample_cnt / 6 ));
183+ Braccio.lvgl_unlock ();
164184 }
165185}
0 commit comments