Skip to content

Commit 6ae239b

Browse files
committed
Fix: Prevent sample array (values) from overflowing.
1 parent 7976a7d commit 6ae239b

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

examples/Braccio_Learn_and_Repeat/Braccio_Learn_and_Repeat.ino

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ enum states {
1616

1717
int state = ZERO_POSITION;
1818

19-
float values[10000];
19+
static int const MAX_SAMPLES = 6*1000*2; /* 20 seconds. */
20+
21+
float values[MAX_SAMPLES];
2022
float* idx = values;
2123
float* final_idx = 0;
24+
int sample_cnt = 0;
2225
float homePos[6] = {157.5, 157.5, 157.5, 157.5, 157.5, 90.0};
2326

2427
static lv_obj_t * counter;
@@ -41,6 +44,7 @@ static void eventHandlerMenu(lv_event_t * e) {
4144
}
4245

4346
idx = values;
47+
sample_cnt = 0;
4448

4549
switch (id) {
4650
case 0: // if the button pressed is the first one
@@ -140,25 +144,32 @@ void setup() {
140144

141145
void loop() {
142146
if (state == RECORD) {
147+
148+
/* Check if we still have space for samples. */
149+
if (sample_cnt >= MAX_SAMPLES) {
150+
state = ZERO_POSITION;
151+
Serial.println("ZERO_POSITION");
152+
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
153+
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
154+
}
155+
/* Capture those samples. */
143156
Braccio.positions(idx);
144157
idx += 6;
158+
sample_cnt += 6;
145159
}
146160
if (state == REPLAY) {
147161
Braccio.moveTo(idx[0], idx[1], idx[2], idx[3], idx[4], idx[5]);
148162
idx += 6;
163+
sample_cnt += 6;
149164
if (idx >= final_idx) {
150165
Serial.println("REPLAY done");
151166
state = ZERO_POSITION;
152167
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
153168
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
154169
}
155170
}
156-
if (idx - values >= sizeof(values)) {
157-
Serial.println("ZERO_POSITION");
158-
state = ZERO_POSITION;
159-
}
160171
delay(100);
161172
if (state != ZERO_POSITION) {
162-
lv_label_set_text_fmt(counter, "Counter: %d" , idx - values);
173+
lv_label_set_text_fmt(counter, "Counter: %d" , sample_cnt);
163174
}
164175
}

0 commit comments

Comments
 (0)