5
5
#define COLOR_LIGHT_TEAL 0x62AEB2
6
6
#define COLOR_ORANGE 0xE47128
7
7
8
+ // ENTER button
9
+ #define BUTTON_ENTER 6
10
+
8
11
enum states {
9
12
RECORD,
10
13
REPLAY,
@@ -13,9 +16,12 @@ enum states {
13
16
14
17
int state = ZERO_POSITION;
15
18
16
- float values[10000 ];
19
+ static int const MAX_SAMPLES = 6 *1000 *2 ; /* 20 seconds. */
20
+
21
+ float values[MAX_SAMPLES];
17
22
float * idx = values;
18
23
float * final_idx = 0 ;
24
+ int sample_cnt = 0 ;
19
25
float homePos[6 ] = {157.5 , 157.5 , 157.5 , 157.5 , 157.5 , 90.0 };
20
26
21
27
static lv_obj_t * counter;
@@ -25,15 +31,11 @@ static const char * btnm_map[] = { "RECORD", "\n", "REPLAY", "\n", "ZERO_POSITIO
25
31
26
32
27
33
static void eventHandlerMenu (lv_event_t * e) {
34
+ Braccio.lvgl_lock ();
28
35
lv_event_code_t code = lv_event_get_code (e);
29
36
lv_obj_t * obj = lv_event_get_target (e);
30
37
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)) {
37
39
uint32_t id = lv_btnmatrix_get_selected_btn (obj);
38
40
const char * txt = lv_btnmatrix_get_btn_text (obj, id);
39
41
@@ -42,6 +44,7 @@ static void eventHandlerMenu(lv_event_t * e) {
42
44
}
43
45
44
46
idx = values;
47
+ sample_cnt = 0 ;
45
48
46
49
switch (id) {
47
50
case 0 : // if the button pressed is the first one
@@ -90,9 +93,12 @@ static void eventHandlerMenu(lv_event_t * e) {
90
93
break ;
91
94
}
92
95
}
96
+ Braccio.lvgl_unlock ();
93
97
}
94
98
95
- void mainMenu () {
99
+ void mainMenu ()
100
+ {
101
+ Braccio.lvgl_lock ();
96
102
static lv_style_t style_focus;
97
103
lv_style_init (&style_focus);
98
104
lv_style_set_outline_color (&style_focus, lv_color_hex (COLOR_ORANGE));
@@ -124,6 +130,7 @@ void mainMenu() {
124
130
lv_obj_align (counter, LV_ALIGN_CENTER, 0 , 80 );
125
131
126
132
lv_obj_add_event_cb (btnm, eventHandlerMenu, LV_EVENT_ALL, NULL );
133
+ Braccio.lvgl_unlock ();
127
134
128
135
Braccio.connectJoystickTo (btnm);
129
136
}
@@ -141,25 +148,38 @@ void setup() {
141
148
142
149
void loop () {
143
150
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. */
144
162
Braccio.positions (idx);
145
163
idx += 6 ;
164
+ sample_cnt += 6 ;
146
165
}
147
166
if (state == REPLAY) {
148
167
Braccio.moveTo (idx[0 ], idx[1 ], idx[2 ], idx[3 ], idx[4 ], idx[5 ]);
149
168
idx += 6 ;
169
+ sample_cnt += 6 ;
150
170
if (idx >= final_idx) {
151
171
Serial.println (" REPLAY done" );
152
172
state = ZERO_POSITION;
173
+ Braccio.lvgl_lock ();
153
174
btnm_map[2 ] = " REPLAY" ; // reset the label of the first button back to "REPLAY"
154
175
lv_btnmatrix_set_btn_ctrl (btnm, 2 , LV_BTNMATRIX_CTRL_CHECKED);
176
+ Braccio.lvgl_unlock ();
155
177
}
156
178
}
157
- if (idx - values >= sizeof (values)) {
158
- Serial.println (" ZERO_POSITION" );
159
- state = ZERO_POSITION;
160
- }
161
179
delay (100 );
162
180
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 ();
164
184
}
165
185
}
0 commit comments