From 332f2a69fa1136323c8f3348bd97fa1d6f35e552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Sun, 12 Nov 2017 17:33:23 +0100 Subject: [PATCH] Set up the timer properly and introduce CurrentTime. --- right/src/init_peripherals.c | 3 ++- right/src/timer.c | 18 ++++++++++++++---- right/src/timer.h | 8 ++++++-- .../usb_command_get_debug_buffer.c | 2 +- right/src/usb_report_updater.c | 17 ++++++++++++----- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/right/src/init_peripherals.c b/right/src/init_peripherals.c index f9b6ca1..bd84feb 100644 --- a/right/src/init_peripherals.c +++ b/right/src/init_peripherals.c @@ -77,7 +77,8 @@ void InitI2cMainBus(void) I2C_MasterGetDefaultConfig(&masterConfig); masterConfig.baudRate_Bps = I2C_MAIN_BUS_BAUD_RATE; uint32_t sourceClock = CLOCK_GetFreq(I2C_MASTER_BUS_CLK_SRC); - I2C_MasterInit(I2C_MAIN_BUS_BASEADDR, &masterConfig, sourceClock);} + I2C_MasterInit(I2C_MAIN_BUS_BASEADDR, &masterConfig, sourceClock); +} void initI2cEepromBus(void) { diff --git a/right/src/timer.c b/right/src/timer.c index 52f5f8b..149b471 100644 --- a/right/src/timer.c +++ b/right/src/timer.c @@ -1,18 +1,28 @@ #include "fsl_pit.h" #include "timer.h" +uint32_t CurrentTime = 1; + +void PIT_TIMER_HANDLER(void) +{ + CurrentTime++; + PIT_ClearStatusFlags(PIT, PIT_TIMER_CHANNEL, PIT_TFLG_TIF_MASK); +} + void Timer_Init(void) { pit_config_t pitConfig; PIT_GetDefaultConfig(&pitConfig); PIT_Init(PIT, &pitConfig); - PIT_SetTimerPeriod(PIT, PIT_TIMER_CHANNEL, USEC_TO_COUNT(TIMER_INTERVAL_USEC, PIT_SOURCE_CLOCK)); + PIT_SetTimerPeriod(PIT, PIT_TIMER_CHANNEL, MSEC_TO_COUNT(TIMER_INTERVAL_MSEC, PIT_SOURCE_CLOCK)); PIT_EnableInterrupts(PIT, PIT_TIMER_CHANNEL, kPIT_TimerInterruptEnable); -// EnableIRQ(PIT_TIMER_IRQ_ID); + EnableIRQ(PIT_TIMER_IRQ_ID); PIT_StartTimer(PIT, PIT_TIMER_CHANNEL); } -uint32_t Timer_GetTime(void) +uint32_t Timer_GetElapsedTime(uint32_t *time) { - return PIT_GetCurrentTimerCount(PIT, PIT_TIMER_CHANNEL); + uint32_t elapsedTime = CurrentTime - *time; + *time = CurrentTime; + return elapsedTime; } diff --git a/right/src/timer.h b/right/src/timer.h index 33e2afc..f44e242 100644 --- a/right/src/timer.h +++ b/right/src/timer.h @@ -7,11 +7,15 @@ // Macros: - #define TIMER_INTERVAL_USEC 1000 + #define TIMER_INTERVAL_MSEC 1 + +// Variables: + + extern uint32_t CurrentTime; // Functions: void Timer_Init(void); - uint32_t Timer_GetTime(void); + uint32_t Timer_GetElapsedTime(uint32_t *time); #endif diff --git a/right/src/usb_commands/usb_command_get_debug_buffer.c b/right/src/usb_commands/usb_command_get_debug_buffer.c index 32a2e16..3e1b60d 100644 --- a/right/src/usb_commands/usb_command_get_debug_buffer.c +++ b/right/src/usb_commands/usb_command_get_debug_buffer.c @@ -14,7 +14,7 @@ void UsbCommand_GetDebugBuffer(void) SetDebugBufferUint32(5, I2cSlaveScheduler_Counter); SetDebugBufferUint32(9, I2cWatchdog_WatchCounter); SetDebugBufferUint32(13, I2cWatchdog_RecoveryCounter); - SetDebugBufferUint32(40, Timer_GetTime()); + SetDebugBufferUint32(40, CurrentTime); memcpy(GenericHidOutBuffer, DebugBuffer, USB_GENERIC_HID_OUT_BUFFER_LENGTH); diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index 0f419e0..f4d0c03 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -11,15 +11,20 @@ #include "right_key_matrix.h" #include "layer.h" #include "usb_report_updater.h" +#include "timer.h" + +uint32_t UsbReportUpdateTime = 0; static uint8_t mouseWheelDivisorCounter = 0; static uint8_t mouseSpeedAccelDivisorCounter = 0; -static uint8_t mouseSpeed = 3; +static uint8_t mouseSpeed = 10; static bool wasPreviousMouseActionWheelAction = false; +static uint32_t elapsedTime; void processMouseAction(key_action_t *action) { bool isWheelAction = action->mouse.scrollActions && !action->mouse.moveActions && !action->mouse.buttonActions; + uint16_t distance = mouseSpeed * elapsedTime / 25; if (isWheelAction && wasPreviousMouseActionWheelAction) { mouseWheelDivisorCounter++; @@ -56,16 +61,16 @@ void processMouseAction(key_action_t *action) } } else if (action->mouse.moveActions) { if (action->mouse.moveActions & MouseMove_Left) { - ActiveUsbMouseReport->x = -mouseSpeed; + ActiveUsbMouseReport->x = -distance; } if (action->mouse.moveActions & MouseMove_Right) { - ActiveUsbMouseReport->x = mouseSpeed; + ActiveUsbMouseReport->x = distance; } if (action->mouse.moveActions & MouseMove_Up) { - ActiveUsbMouseReport->y = -mouseSpeed; + ActiveUsbMouseReport->y = -distance; } if (action->mouse.moveActions & MouseMove_Down) { - ActiveUsbMouseReport->y = mouseSpeed; + ActiveUsbMouseReport->y = distance; } } @@ -129,6 +134,8 @@ static secondary_role_t secondaryRole; void UpdateActiveUsbReports(void) { static uint8_t previousModifiers = 0; + elapsedTime = Timer_GetElapsedTime(&UsbReportUpdateTime); + basicScancodeIndex = 0; mediaScancodeIndex = 0; systemScancodeIndex = 0;