Skip to content

Commit

Permalink
Merge branch 'master' of github.com:UltimateHackingKeyboard/firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
mondalaci committed Dec 14, 2023
2 parents 9e10c5b + 699483d commit d11c8cb
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 16 deletions.
2 changes: 1 addition & 1 deletion doc-dev/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ setVar qActive 1
// toggle fn layer and keep it active for 5 seconds or until some recordKey instance starts recording a macro
toggleLayer fn
ifNotRecording ifNotPlaytime 5000 if ($qActive) goTo @0
ifNotRecording ifNotPlaytime 5000 if ($qActive) goTo $currentAddress
untoggleLayer
setVar qActive 0
```
Expand Down
43 changes: 40 additions & 3 deletions right/src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@

#ifdef WATCHES

#include "led_display.h"
#include "timer.h"
#include "key_states.h"
#include <limits.h>
#include "usb_interfaces/usb_interface_basic_keyboard.h"
#include "macros/core.h"
#include "macros/status_buffer.h"
#include "keymap.h"
#include "segment_display.h"

uint8_t CurrentWatch = 0;

Expand Down Expand Up @@ -140,4 +138,43 @@ void WatchValueMax(int v, uint8_t n)
}


void WatchFloatValue(float v, uint8_t n)
{
if (CurrentTime - lastWatch > watchInterval) {
SegmentDisplay_SetFloat(v, SegmentDisplaySlot_Debug);
lastWatch = CurrentTime;
}
}

void WatchFloatValueMin(float v, uint8_t n)
{
static float m = 0;

if (v < m) {
m = v;
}

if (CurrentTime - lastWatch > watchInterval) {
SegmentDisplay_SetFloat(m, SegmentDisplaySlot_Debug);
lastWatch = CurrentTime;
m = (float)INT_MAX;
}
}

void WatchFloatValueMax(float v, uint8_t n)
{
static float m = 0;

if (v > m) {
m = v;
}

if (CurrentTime - lastWatch > watchInterval) {
SegmentDisplay_SetFloat(m, SegmentDisplaySlot_Debug);
lastWatch = CurrentTime;
m = (float)INT_MIN;
}
}


#endif
16 changes: 16 additions & 0 deletions right/src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define WATCHES
#endif


#ifdef WATCHES
#ifndef SRC_UTILS_DBG_H_
#define SRC_UTILS_DBG_H_
Expand Down Expand Up @@ -41,6 +42,15 @@
// Watches value V in slot N.
#define WATCH_VALUE_MAX(V, N) if(CurrentWatch == N) { WatchValueMax(V, N); }

// Watches value V in slot N.
#define WATCH_FLOAT_VALUE(V, N) if(CurrentWatch == N) { WatchFloatValue(V, N); }

// Watches value V in slot N.
#define WATCH_FLOAT_VALUE_MIN(V, N) if(CurrentWatch == N) { WatchFloatValueMin(V, N); }

// Watches value V in slot N.
#define WATCH_FLOAT_VALUE_MAX(V, N) if(CurrentWatch == N) { WatchFloatValueMax(V, N); }

// Watches string V in slot N.
#define WATCH_STRING(V, N) if(CurrentWatch == N) { WatchString(V, N); }

Expand Down Expand Up @@ -71,6 +81,9 @@
void WatchValue(int v, uint8_t n);
void WatchValueMin(int v, uint8_t n);
void WatchValueMax(int v, uint8_t n);
void WatchFloatValue(float v, uint8_t n);
void WatchFloatValueMin(float, uint8_t n);
void WatchFloatValueMax(float, uint8_t n);
void WatchString(char const * v, uint8_t n);
void ShowValue(int v, uint8_t n);
void ShowString(char const * v, uint8_t n);
Expand All @@ -89,6 +102,9 @@
#define WATCH_VALUE(V, N)
#define WATCH_VALUE_MIN(V, N)
#define WATCH_VALUE_MAX(V, N)
#define WATCH_FLOAT_VALUE(V, N)
#define WATCH_FLOAT_VALUE_MIN(V, N)
#define WATCH_FLOAT_VALUE_MAX(V, N)
#define WATCH_STRING(V, N)
#define SHOW_STRING(V, N)
#define SHOW_VALUE(V, N)
Expand Down
11 changes: 2 additions & 9 deletions right/src/mouse_controller.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
#include <math.h>
#include "key_action.h"
#include "led_display.h"
#include "layer.h"
#include "slave_protocol.h"
#include "usb_interfaces/usb_interface_mouse.h"
#include "peripherals/test_led.h"
#include "slave_drivers/is31fl3xxx_driver.h"
#include "slave_drivers/uhk_module_driver.h"
#include "timer.h"
#include "config_parser/parse_keymap.h"
#include "usb_commands/usb_command_get_debug_buffer.h"
#include "arduino_hid/ConsumerAPI.h"
#include "secondary_role_driver.h"
#include "slave_drivers/touchpad_driver.h"
#include "mouse_controller.h"
#include "mouse_keys.h"
#include "slave_scheduler.h"
#include "layer_switcher.h"
#include "usb_report_updater.h"
#include "caret_config.h"
#include "keymap.h"
#include "macros/core.h"
#include "debug.h"
#include "postponer.h"
#include "layer.h"
#include "secondary_role_driver.h"

typedef struct {
float x;
Expand Down Expand Up @@ -709,6 +701,7 @@ void MouseController_ProcessMouseActions()

for (uint8_t moduleSlotId=0; moduleSlotId<UHK_MODULE_MAX_SLOT_COUNT; moduleSlotId++) {
uhk_module_state_t *moduleState = UhkModuleStates + moduleSlotId;

if (moduleState->moduleId == ModuleId_Unavailable || moduleState->pointerCount == 0) {
continue;
}
Expand Down
10 changes: 9 additions & 1 deletion right/src/segment_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ void SegmentDisplay_SerializeFloat(char* buffer, float num)
{
if (num <= -10.0f || 10.0f <= num ) {
SegmentDisplay_SerializeInt(buffer, num);
return;
}

int mag = 0;
Expand All @@ -127,7 +128,7 @@ void SegmentDisplay_SerializeFloat(char* buffer, float num)
negative = true;
}

while (num < 10.0f) {
while (num < 10.0f && mag < 10) {
mag++;
num *= 10;
}
Expand Down Expand Up @@ -178,6 +179,13 @@ void SegmentDisplay_SetInt(int32_t a, segment_display_slot_t slot)
SegmentDisplay_SetText(3, b, slot);
}

void SegmentDisplay_SetFloat(float a, segment_display_slot_t slot)
{
char b[3];
SegmentDisplay_SerializeFloat(b, a);
SegmentDisplay_SetText(3, b, slot);
}

bool SegmentDisplay_SlotIsActive(segment_display_slot_t slot)
{
return slots[slot].active;
Expand Down
2 changes: 1 addition & 1 deletion right/src/segment_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <stdint.h>
#include <stdbool.h>
#include "timer.h"
#include "macros/vars.h"

// Macros:
Expand Down Expand Up @@ -36,6 +35,7 @@
void SegmentDisplay_Update();
void SegmentDisplay_SetText(uint8_t len, const char* text, segment_display_slot_t slot);
void SegmentDisplay_SetInt(int32_t a, segment_display_slot_t slot);
void SegmentDisplay_SetFloat(float a, segment_display_slot_t slot);
void SegmentDisplay_SerializeInt(char* buffer, int32_t a);
void SegmentDisplay_SerializeFloat(char* buffer, float f);
void SegmentDisplay_SerializeVar(char* buffer, macro_variable_t var);
Expand Down
1 change: 1 addition & 0 deletions right/src/slave_drivers/uhk_module_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ slave_result_t UhkModuleSlaveDriver_Update(uint8_t uhkModuleDriverId)
pointer_delta_t *pointerDelta = (pointer_delta_t*)(rxMessage->data + keyStatesLength);
uhkModuleState->pointerDelta.x += pointerDelta->x;
uhkModuleState->pointerDelta.y += pointerDelta->y;
uhkModuleState->pointerDelta.debugInfo = pointerDelta->debugInfo;
}
}
res.status = kStatus_Uhk_IdleCycle;
Expand Down
1 change: 1 addition & 0 deletions shared/module/slave_protocol_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ void SlaveTxHandler(void)
// (This handler can be interrupted by sensor interrupts.)
pointerDelta->x = PointerDelta.x;
pointerDelta->y = PointerDelta.y;
pointerDelta->debugInfo = PointerDelta.debugInfo;
PointerDelta.x = 0;
PointerDelta.y = 0;
__enable_irq();
Expand Down
4 changes: 4 additions & 0 deletions shared/slave_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@
uint8_t data[I2C_MESSAGE_MAX_PAYLOAD_LENGTH];
} ATTR_PACKED i2c_message_t;

typedef struct {
} ATTR_PACKED pointer_debug_info_t;

typedef struct {
int16_t x;
int16_t y;
pointer_debug_info_t debugInfo;
} ATTR_PACKED pointer_delta_t;

// Variables:
Expand Down
62 changes: 61 additions & 1 deletion trackpoint/src/module.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "fsl_gpio.h"
#include "module.h"
#include <stdint.h>

pointer_delta_t PointerDelta;

Expand Down Expand Up @@ -147,7 +148,62 @@ static bool readByte()
return false;
}

void PS2_CLOCK_IRQ_HANDLER(void) {
#define AXIS_COUNT 2
#define WINDOW_LENGTH 16
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define DRIFT_RESET_PERIOD 2000
#define TRACKPOINT_UPDATE_PERIOD 10
#define DRIFT_TOLERANCE 1

static uint16_t window[AXIS_COUNT][WINDOW_LENGTH];
static uint8_t windowIndex = 0;
static uint16_t windowSum[AXIS_COUNT];

void recognizeDrifts(int16_t x, int16_t y)
{
uint16_t deltas[AXIS_COUNT] = {ABS(x), ABS(y)};

// compute average speed across the window
for (uint8_t axis=0; axis<AXIS_COUNT; axis++) {
windowSum[axis] -= window[axis][windowIndex];
window[axis][windowIndex] = deltas[axis];
windowSum[axis] += window[axis][windowIndex];
}

windowIndex = (windowIndex + 1) % WINDOW_LENGTH;

// check whether current speed matches remembered "drift" speed
static uint16_t supposedDrift[AXIS_COUNT] = {0, 0};
static uint16_t driftLength = 0;
bool drifting = true;
for (uint8_t axis=0; axis<AXIS_COUNT; axis++) {
if (ABS(windowSum[axis] - supposedDrift[axis]) > DRIFT_TOLERANCE + 1) {
drifting = false;
}
}

if (windowSum[0] < 1 && windowSum[1] < 1) {
drifting = false;
}

// handle drift detection logic
if (drifting) {
driftLength++;
if (driftLength > DRIFT_RESET_PERIOD / TRACKPOINT_UPDATE_PERIOD) {
driftLength = 0;
shouldReset = true;
}
} else {
driftLength = 0;
for (uint8_t axis=0; axis<AXIS_COUNT; axis++) {
supposedDrift[axis] = windowSum[axis];
}
}
}


void PS2_CLOCK_IRQ_HANDLER(void)
{
static uint8_t byte1 = 0;
static uint16_t deltaX = 0;
static uint16_t deltaY = 0;
Expand Down Expand Up @@ -260,7 +316,11 @@ void PS2_CLOCK_IRQ_HANDLER(void) {
PointerDelta.y -= deltaY;
lastX = deltaX;
lastY = deltaY;

recognizeDrifts(deltaX, deltaY);
} else {
// If there was error then current data is not relevant.
// We use the last delta as an approximation.
PointerDelta.x -= lastX;
PointerDelta.y -= lastY;
}
Expand Down

0 comments on commit d11c8cb

Please sign in to comment.