I'm writing a simple button event listener for the XK-1A board. I want to fire a single up/down event, when the button is pressed/released.
Without the timerafter between the select-case and the if, it sometimes happens, that the code in the if-block is executed on button-release
Code: Select all
void button_listener(in port b0, in port b1, chanend led_chan) {
unsigned b0status, b1status, time;
timer t;
b0 :> b0status;
b1 :> b1status;
led_chan <: servo_state;
while (1) {
select {
case b0 when pinsneq(b0status) :> b0status:
t :> time;
t when timerafter(time + 500000) :> void; // sleep 5ms because the if fails on button release, if not
if (b0status == 0 ) {
next_servo_state(); // static function to change static var servo_state
led_chan <: servo_state;
}
break;
}
}
}
Thanks for your help!
Greetings,
Chris