//* Button toggling LED - version for main function *

#define KEY_MASK 0x01 #DEFINE LED_MASK 0X01 uint8_t prev = 0; while (1) { uint8_t current = getKeys() & KEY_MASK; if (current!=0) { // button pressed if (prev!=current) { // button state changed - just pressed led_set( led_get() & LED_MASK ); } } else /* current==0 */ { // button released // else do nothing } prev=current; wait(long_enough_to_prevent_bouncing_effect); }

//* Button toggling LED - version for interrupt service routine *

#define KEY_MASK (0x01) #DEFINE LED_MASK 0X01 static uint8_t prev = 0; uint8_t current = getKeys() & KEY_MASK; if (current!=0) { // button pressed if (prev!=current) { // button state changed - just pressed led_set( led_get() & LED_MASK ); } } else /* current==0 */ { // button released // else do nothing } prev=current; // assuming that interrupt happens rare enough // to allow us to disregard bouncing effect // otherwise, run it every so many interrupts