Hi babazaroni. Have not reviewed your logic flow but an important comment is that you are working with mechanical push button switches which are prone to bouncing switch contacts. For this reason, care must be used to offer software to debounce the switch - often logic chips will perform this task in hardware through timed delays but XMOS is able to handle the same task in software.
Take for example the application note AN00184 for the xCore-200 Explorer kit (working with this example this morning) and has the following code snippet for dealing with the pushbuttons:
/* Check for a change in button 1 - Detects 1->0 transition */
if(is_button_pressed(0)) {
if(button_1_valid) {
button_1_valid = 0;
length = sprintf(tmp_string, "\r\nButton 1 Pressed!\r\n");
cdc.write(tmp_string, length);
}
} else {
button_1_valid = 1;
}
/* Check for a change in button 2 - Detects 1->0 transition */
if(is_button_pressed(1)) {
if(button_2_valid) {
button_2_valid = 0;
length = sprintf(tmp_string, "\r\nButton 2 Pressed!\r\n");
cdc.write(tmp_string, length);
}
} else {
button_2_valid = 1;
}
where is_button_pressed() is defined as:
/* Checks if a button is pressed */
int is_button_pressed(int button_id)
{
if(get_button_state(button_id) == BUTTON_PRESSED) {
/* Wait for debounce and check again */
delay_ticks(DEBOUNCE_TIME);
if(get_button_state(button_id) == BUTTON_PRESSED) {
return 1; /* Yes button is pressed */
}
}
/* No button press */
return 0;
}
and
#define DEBOUNCE_TIME (XS1_TIMER_HZ/50)
Here is a complete example on how to deal with mechanical pushbuttons with debounce code support:
https://www.xmos.com/published/an10006- ... ion=latest
You will need to extend your code to offer similar delays and then sample again to confirm that the pushbutton is indeed in the correct position before making a selection.