/************************************************************************/ /* */ /* Debouncing 8 Keys */ /* Sampling 4 Times */ /* With Repeat Function */ /* */ /* Author: Peter Dannegger */ /* danni@specs.de */ /* */ /************************************************************************/ #include "tast.h" uint32_t key_state; // debounced and inverted key state: // bit = 1: key pressed uint32_t key_press; // key press detect uint32_t key_rpt; // key long press and repeat void checkKeys() { static uint32_t ct0, ct1, rpt; uint32_t i; // ATTENTION! if your pins from button are inverted, change KEY_PIN to ~KEY_PIN i = key_state ^ KEY_PIN; // key changed ? ct0 = ~( ct0 & i ); // reset or count ct0 ct1 = ct0 ^ (ct1 & i); // reset or count ct1 i &= ct0 & ct1; // count until roll over ? key_state ^= i; // then toggle debounced state key_press |= key_state & i; // 0->1: key press detect if( (key_state & REPEAT_MASK) == 0 ) // check repeat function rpt = REPEAT_START; // start delay if( --rpt == 0 ){ rpt = REPEAT_NEXT; // repeat delay key_rpt |= key_state & REPEAT_MASK; } } uint32_t get_key_press( uint32_t key_mask ) { // cli(); // read and clear atomic ! key_mask &= key_press; // read key(s) key_press ^= key_mask; // clear key(s) // sei(); return key_mask; } uint32_t get_key_rpt( uint32_t key_mask ) { // cli(); // read and clear atomic ! key_mask &= key_rpt; // read key(s) key_rpt ^= key_mask; // clear key(s) // sei(); return key_mask; } uint32_t get_key_short( uint32_t key_mask ) { // cli(); // read key state and key press atomic ! return get_key_press( ~key_state & key_mask ); } uint32_t get_key_long( uint32_t key_mask ) { return get_key_press( get_key_rpt( key_mask )); } void clear_input(void) { key_state = 0; key_press = 0; key_rpt = 0; } //void initTast() //{ // key1_CTRL = 0x18; // Enable pull-up on // key2_CTRL = 0x18; // Enable pull-up on // key3_CTRL = 0x18; // Enable pull-up on // key4_CTRL = 0x18; // Enable pull-up on // key5_CTRL = 0x18; // Enable pull-up on //}