Skip to content

Commit

Permalink
Fix issues with sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
malcx95 committed May 13, 2024
1 parent 1864ea0 commit a8ba1fe
Show file tree
Hide file tree
Showing 12 changed files with 1,936 additions and 1,657 deletions.
2 changes: 1 addition & 1 deletion common/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace common::constants

const int MAX_JUST_PRESSED_KEYS = TOTAL_NUM_KEYS;

const int MAX_QUEUED_KEY_EVENTS = 20;
const int MAX_QUEUED_KEY_EVENTS = 100;

const int MAX_KEYREPORT_KEYS = 6;

Expand Down
21 changes: 19 additions & 2 deletions common/custom_keycodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,30 @@ namespace common::constants

const uint16_t LAYER_HOLD_MODIFIER = 0xC000;
const uint16_t LAYER_TOGGLE_MODIFIER = 0xB000;
const uint16_t MOUSE_BASE_CODE = 0xA000;
const uint16_t CONTROL_BASE_CODE = 0xD000;

const uint16_t LAYER_HOLD_0 = (0 | LAYER_HOLD_MODIFIER);
const uint16_t LAYER_HOLD_1 = (1 | LAYER_HOLD_MODIFIER);
const uint16_t LAYER_HOLD_2 = (2 | LAYER_HOLD_MODIFIER);

const uint16_t LAYER_TOGGLE_0 = (0 | LAYER_TOGGLE_MODIFIER);
const uint16_t LAYER_TOGGLE_1 = (1 | LAYER_TOGGLE_MODIFIER);
const uint16_t LAYER_TOGGLE_2 = (2 | LAYER_TOGGLE_MODIFIER);

const uint16_t MOUSE_LEFT_CLICK = (0 | MOUSE_BASE_CODE);
const uint16_t MOUSE_RIGHT_CLICK = (1 | MOUSE_BASE_CODE);
const uint16_t MOUSE_MIDDLE_CLICK = (2 | MOUSE_BASE_CODE);
const uint16_t MOUSE_SCROLL_UP = (3 | MOUSE_BASE_CODE);
const uint16_t MOUSE_SCROLL_DOWN = (4 | MOUSE_BASE_CODE);
const uint16_t MOUSE_MOVE_LEFT = (5 | MOUSE_BASE_CODE);
const uint16_t MOUSE_MOVE_RIGHT = (6 | MOUSE_BASE_CODE);
const uint16_t MOUSE_MOVE_UP = (7 | MOUSE_BASE_CODE);
const uint16_t MOUSE_MOVE_DOWN = (8 | MOUSE_BASE_CODE);
const uint16_t MOUSE_MOVE_ACCELERATE = (9 | MOUSE_BASE_CODE);
const uint16_t MAX_MOUSE_CODE = 9;

const uint16_t CONTROL_BRIGHTNESS_INC = (0 | CONTROL_BASE_CODE);
const uint16_t CONTROL_BRIGHTNESS_DEC = (1 | CONTROL_BASE_CODE);
const uint16_t CONTROL_CONFIG_MODE_TOGGLE = (2 | CONTROL_BASE_CODE);
const uint16_t MAX_CONTROL_CODE = 2;

}
3,424 changes: 1,812 additions & 1,612 deletions compile_commands.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/backlight/schemes/wave.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace core::backlight::schemes
{

const float SPEED = 0.0005f;
const float SPEED = 0.005f;
const float X_SPEED = 0.8f;
const float Y_SPEED = 1.1f;
const float MAX_PHASE_DIFFERENCE = 0.3f;
Expand Down
7 changes: 3 additions & 4 deletions core/firmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ void Firmware::update()
loaded_keymap = true;
}
device.start_timer();
keyboard::KeyboardScanResult result;
key_scanner.scan(result);
keymap.translate_keyboard_scan_result(result, key_queue);
key_scanner.scan(keyboard_scan_result);
keymap.translate_keyboard_scan_result(keyboard_scan_result, key_queue);
keyboard::communication::send_key_report(key_queue, device);
backlight.update(result);
backlight.update(keyboard_scan_result);

const uint32_t elapsed = device.get_timer_micros();
if (elapsed < CYCLE_TIME_MICROS)
Expand Down
1 change: 1 addition & 0 deletions core/firmware.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Firmware
keyboard::KeyScanner key_scanner;
keyboard::KeyQueue key_queue;
keyboard::KeyMap keymap;
keyboard::KeyboardScanResult keyboard_scan_result;

bool loaded_keymap = false;

Expand Down
85 changes: 52 additions & 33 deletions core/keyboard/keymap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,28 @@ void KeyMap::update_current_layer(const KeyboardScanResult& scan_result)
}


bool KeyMap::extract_single_key(const Action* action, KeyReport& single_key_report)
{
if (action->is_single_key())
{
const auto code = action->sequence[0].key;
if (util::key_is_valid_standard_key(code))
{
// this check is necessary since the key might be a layer modifier
single_key_report.add_key(code);
}

const auto modifier = action->sequence[0].modifier;
single_key_report.add_key(modifier);

const auto media = action->sequence[0].media;
single_key_report.add_key(media);
return true;
}
return false;
}


void KeyMap::translate_keyboard_scan_result(const KeyboardScanResult& scan_result, KeyQueue& key_queue)
{
/*
Expand All @@ -170,40 +192,39 @@ void KeyMap::translate_keyboard_scan_result(const KeyboardScanResult& scan_resul
const auto action = get_action(layer_to_use, key->row, key->col);
if (action != nullptr)
{
if (action->is_single_key())
{
const auto code = action->sequence[0].key;
if (util::key_is_valid_standard_key(code))
{
// this check is necessary since the key might be a layer modifier
single_key_report.add_key(code);
}

const auto modifier = action->sequence[0].modifier;
single_key_report.add_key(modifier);

const auto media = action->sequence[0].media;
single_key_report.add_key(media);
single_key_pressed = true;
}
else
single_key_pressed |= extract_single_key(action, single_key_report);
}
}


// handle sequences, these should start only once
for (int i = 0; i < scan_result.num_just_pressed; ++i)
{
const auto key = scan_result.just_pressed[i];
const auto action = get_action(layer_to_use, key->row, key->col);
if (action != nullptr && !action->is_single_key())
{
for (int j = 0; j < action->sequence_length; ++j)
{
for (int j = 0; j < action->sequence_length; ++j)
{
KeyReport report;
const auto code = action->sequence[j].key;
report.add_key(code);

const auto modifier = action->sequence[j].modifier;
report.add_key(modifier);

const auto media = action->sequence[j].media;
report.add_key(media);
key_queue.push(report);
}
KeyReport report;
const auto code = action->sequence[j].key;
report.add_key(code);

const auto modifier = action->sequence[j].modifier;
report.add_key(modifier);

const auto media = action->sequence[j].media;
report.add_key(media);
key_queue.push(report);

// Add an empty report between each key, to ensure that the computer
// interprets them as separate keypresses. Otherwise double letters will
// be interpreted as a single letters.
key_queue.push({});
}
}
}

if (single_key_pressed && single_key_report.num_keys > 0)
{
key_queue.push(single_key_report);
Expand Down Expand Up @@ -258,9 +279,7 @@ bool KeyMap::deserialize_keymap(const uint16_t* data, int size)
for (int j = 0; j < sequence_length; ++j)
{
const uint16_t key = data[i++];
if (!util::key_is_valid_standard_key(key)
&& !util::key_is_layer_hold_modifier(key)
&& !util::key_is_layer_toggle_modifier(key))
if (!util::key_is_valid_non_modifier_and_non_media(key))
{
return false;
}
Expand Down
6 changes: 6 additions & 0 deletions core/keyboard/keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class KeyMap
* Updates the current layer based on whether the scan result contains a toggle layer key.
*/
void update_current_layer(const KeyboardScanResult& scan_result);

/**
* Extracts the key from the action and adds it to the key report.
* Returns true if the action is a single key press.
*/
bool extract_single_key(const Action* action, KeyReport& single_key_report);
};

}
22 changes: 21 additions & 1 deletion core/keyboard/keyutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace core::keyboard::util
{

/**
* This array is used to check if a keycode is valid.
* This array is used to check if a standard keycode is valid.
*/
const bool IS_KEYCODE_VALID[116]
{
Expand Down Expand Up @@ -183,4 +183,24 @@ inline bool key_is_layer_toggle_modifier(uint16_t key)
return get_layer_modifier_layer(key) < common::constants::MAX_NUM_LAYERS;
}

inline bool key_is_mouse_key(uint16_t key)
{
return (key & 0xFF00) == common::constants::MOUSE_BASE_CODE;
}

inline bool key_is_control_key(uint16_t key)
{
return (key & 0xFF00) == common::constants::CONTROL_BASE_CODE;
}

inline bool key_is_valid_non_modifier_and_non_media(uint16_t key)
{
return
key_is_valid_standard_key(key) ||
key_is_layer_toggle_modifier(key) ||
key_is_layer_hold_modifier(key) ||
key_is_mouse_key(key) ||
key_is_control_key(key);
}

}
2 changes: 2 additions & 0 deletions core/keyboard/test/test_keymap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ TEST_CASE("Test translate scan result", "[KeyMap]")
core::keyboard::KeyboardScanResult scan_result;
scan_result.num_pressed = 1;
scan_result.pressed[0] = &descriptions[1];
scan_result.num_just_pressed = 1;
scan_result.just_pressed[0] = &descriptions[1];

keymap.translate_keyboard_scan_result(scan_result, queue);

Expand Down
1 change: 0 additions & 1 deletion core/util/array_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ void array_subtract(const T a[], int a_size, const T b[], int b_size, T result[]
result[result_size++] = a[i];
}
}

}

}
20 changes: 18 additions & 2 deletions simulator/simulator_device.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "simulator_device.h"
#include "keycodes.h"
#include <chrono>
#include <mutex>
#include <thread>
Expand Down Expand Up @@ -54,7 +55,6 @@ void SimulatorDevice::gpio_setup(const uint8_t pin, const PinMode mode)
void SimulatorDevice::gpio_write(const uint8_t pin, const PinState value)
{
std::lock_guard<std::mutex> lock(mutex);
std::cout << "SimulatorDevice::gpio_write(" << (int)pin << ", " << (int)value << ")" << std::endl;
if (pin_to_row.count(pin))
{
row_state[pin_to_row[pin]] = (bool)value;
Expand Down Expand Up @@ -119,7 +119,23 @@ void SimulatorDevice::set_keyboard_media(const uint16_t media)
this->current_media = media;
}

void SimulatorDevice::keyboard_send() { }
void SimulatorDevice::keyboard_send()
{
bool any_keys_pressed = false;
for (int i = 0; i < 6; i++)
{
if (current_keys[i] != 0)
{
any_keys_pressed = true;
const auto full_code = 0xF000 | current_keys[i];
std::cout << "Key: " << KEY_CODES.at(full_code);
}
}
if (any_keys_pressed)
{
std::cout << std::endl;
}
}


bool SimulatorDevice::sd_init()
Expand Down

0 comments on commit a8ba1fe

Please sign in to comment.