Implement key debouncer.

This commit is contained in:
László Monda
2017-11-14 22:54:53 +01:00
parent 9e18caa0ba
commit a0afdcf476
7 changed files with 79 additions and 10 deletions

View File

@@ -14,6 +14,7 @@
#include "eeprom.h"
#include "microseconds/microseconds_pit.c"
#include "timer.h"
#include "key_debouncer.h"
void InitInterruptPriorities(void)
{
@@ -122,6 +123,7 @@ void InitPeripherals(void)
#ifdef I2C_WATCHDOG
InitI2cWatchdog();
#endif
InitKeyDebouncer();
EEPROM_Init();
//microseconds_init();
}

32
right/src/key_debouncer.c Normal file
View File

@@ -0,0 +1,32 @@
#include "key_debouncer.h"
#include "fsl_pit.h"
#include "slot.h"
#include "module.h"
#include "key_states.h"
#include "peripherals/test_led.h"
void PIT_KEY_DEBOUNCER_HANDLER(void)
{
TEST_LED_TOGGLE();
for (uint8_t slotId=0; slotId<SLOT_COUNT; slotId++) {
for (uint8_t keyId=0; keyId<MAX_KEY_COUNT_PER_MODULE; keyId++) {
uint8_t *debounceCounter = &KeyStates[slotId][keyId].debounceCounter;
if (*debounceCounter < 0xff) {
(*debounceCounter)++;
}
}
}
PIT_ClearStatusFlags(PIT, PIT_KEY_DEBOUNCER_CHANNEL, PIT_TFLG_TIF_MASK);
}
void InitKeyDebouncer(void)
{
pit_config_t pitConfig;
PIT_GetDefaultConfig(&pitConfig);
PIT_Init(PIT, &pitConfig);
PIT_SetTimerPeriod(PIT, PIT_KEY_DEBOUNCER_CHANNEL, MSEC_TO_COUNT(KEY_DEBOUNCER_INTERVAL_MSEC, PIT_SOURCE_CLOCK));
PIT_EnableInterrupts(PIT, PIT_KEY_DEBOUNCER_CHANNEL, kPIT_TimerInterruptEnable);
EnableIRQ(PIT_KEY_DEBOUNCER_IRQ_ID);
PIT_StartTimer(PIT, PIT_KEY_DEBOUNCER_CHANNEL);
}

18
right/src/key_debouncer.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef __KEY_DEBOUNCER_H__
#define __KEY_DEBOUNCER_H__
// Includes:
#include "peripherals/pit.h"
#include "fsl_common.h"
// Macros:
#define KEY_DEBOUNCER_INTERVAL_MSEC 1
#define KEY_DEBOUNCER_TIMEOUT_MSEC 10
// Functions:
void InitKeyDebouncer(void);
#endif

View File

@@ -1,5 +1,5 @@
#ifndef __KEY_SCANNER_H__
#define __TIMER_H__
#define __KEY_SCANNER_H__
// Includes:

View File

@@ -13,6 +13,7 @@
bool previous;
bool current;
bool suppressed;
uint8_t debounceCounter;
} key_state_t;
// Variables:

View File

@@ -1,20 +1,28 @@
#ifndef __PIT_H__
#define __PIT_H__
// Includes:
#include "peripherals/pit.h"
// Macros:
#define PIT_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_BusClk)
#define PIT_I2C_WATCHDOG_HANDLER PIT0_IRQHandler
#define PIT_I2C_WATCHDOG_IRQ_ID PIT0_IRQn
#define PIT_I2C_WATCHDOG_CHANNEL kPIT_Chnl_0
#define PIT_I2C_WATCHDOG_HANDLER PIT0_IRQHandler
#define PIT_I2C_WATCHDOG_IRQ_ID PIT0_IRQn
#define PIT_I2C_WATCHDOG_CHANNEL kPIT_Chnl_0
#define PIT_TIMER_HANDLER PIT1_IRQHandler
#define PIT_TIMER_IRQ_ID PIT1_IRQn
#define PIT_TIMER_CHANNEL kPIT_Chnl_1
#define PIT_TIMER_HANDLER PIT1_IRQHandler
#define PIT_TIMER_IRQ_ID PIT1_IRQn
#define PIT_TIMER_CHANNEL kPIT_Chnl_1
#define PIT_KEY_SCANNER_HANDLER PIT2_IRQHandler
#define PIT_KEY_SCANNER_IRQ_ID PIT2_IRQn
#define PIT_KEY_SCANNER_CHANNEL kPIT_Chnl_2
#define PIT_KEY_SCANNER_HANDLER PIT2_IRQHandler
#define PIT_KEY_SCANNER_IRQ_ID PIT2_IRQn
#define PIT_KEY_SCANNER_CHANNEL kPIT_Chnl_2
#define PIT_KEY_DEBOUNCER_HANDLER PIT3_IRQHandler
#define PIT_KEY_DEBOUNCER_IRQ_ID PIT3_IRQn
#define PIT_KEY_DEBOUNCER_CHANNEL kPIT_Chnl_3
#endif

View File

@@ -12,6 +12,7 @@
#include "layer.h"
#include "usb_report_updater.h"
#include "timer.h"
#include "key_debouncer.h"
uint32_t UsbReportUpdateTime = 0;
@@ -168,6 +169,13 @@ void UpdateActiveUsbReports(void)
key_state_t *keyState = &KeyStates[slotId][keyId];
key_action_t *action = &CurrentKeymap[activeLayer][slotId][keyId];
if (keyState->debounceCounter < KEY_DEBOUNCER_TIMEOUT_MSEC) {
keyState->current = keyState->previous;
} else if (!keyState->previous && keyState->current) {
keyState->debounceCounter = 0;
}
if (keyState->current) {
if (suppressKeys) {
keyState->suppressed = true;