Skip to content

Commit

Permalink
feat!: initialisation supports custom keycount
Browse files Browse the repository at this point in the history
Previously, SQUIRREL would allocate memory for each key statically - at
build-time, but now you can specify the amount of keys in your keyboard
in the call to squirrel_init, meaning you can use the same version of
the library for multiple keyboards - instead of recompiling if your
keycount changes.

TODO: test this on real hardware

BREAKING CHANGE: squirrel_init now takes the amount of keys as an
argument, instead of being declared at build-time
  • Loading branch information
headblockhead committed Dec 28, 2024
1 parent 3635680 commit d7754d7
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 36 deletions.
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ cmake_minimum_required(VERSION 3.12)

project(squirrel)

# target_compile_definitions(squirrel PUBLIC SQUIRREL_KEYCOUNT=your_keycount)
# Override this using the above line in your build of the library! Otherwise, it
# will default to 1 key.
add_compile_definitions(SQUIRREL_KEYCOUNT=1)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_compile_options(-Og)
Expand Down
2 changes: 0 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
gcovr
];
};
# Reminder! If you want to use SQUIRREL as a library in one of your projects, you MUST build it yourself by adding the directory to your project with CMAKE and changing SQUIRREL_KEYCOUNT, for example:
# add_compile_definitions(SQUIRREL_KEYCOUNT=100)
packages.squirrel = pkgs.stdenv.mkDerivation {
name = "squirrel";
src = ./.;
Expand Down
1 change: 1 addition & 0 deletions include/squirrel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

enum squirrel_error {
ERR_NONE = 0,
ERR_OOM,
ERR_PASSTHROUGH_ON_BOTTOM_LAYER,
};

Expand Down
4 changes: 2 additions & 2 deletions include/squirrel_init.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once

enum squirrel_error
squirrel_init(void); // Initialize the keyboard with the total number of keys.
enum squirrel_error squirrel_init(
int keys); // Initialize the keyboard with the total number of keys.
2 changes: 1 addition & 1 deletion include/squirrel_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ release_key(uint8_t key_index); // Release the key at the index in the

// key_states is an array of booleans that represent the state of each key. Used
// by check_key to determine if a key is pressed or released.
extern bool key_states[SQUIRREL_KEYCOUNT];
extern bool *key_states;

// check_key compares the state of the key at the index to the key_states array
// to determine if the key is pressed or released, and calls the appropriate
Expand Down
2 changes: 1 addition & 1 deletion include/squirrel_quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

struct layer {
bool active; // true if this layer is currently active
struct key keys[SQUIRREL_KEYCOUNT];
struct key *keys;
};

// layers is a list of all the layers in the keyboard. 0-15 are configured,
Expand Down
14 changes: 12 additions & 2 deletions src/squirrel_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,27 @@
#include <stdlib.h>
#include <string.h>

enum squirrel_error squirrel_init(void) {
enum squirrel_error squirrel_init(int keys) {
struct key passthrough_key = (struct key){
.pressed = quantum_passthrough_press,
.released = quantum_passthrough_release,
};
for (int j = 16; j >= 0; j--) {
layers[j].active = false;
for (int i = 0; i < SQUIRREL_KEYCOUNT; i++) {
layers[j].keys = malloc(sizeof(struct key) * keys);
if (layers[j].keys == NULL) {
return ERR_OOM;
}
for (int i = 0; i < keys; i++) {
copy_key(&passthrough_key, &layers[j].keys[i]);
}
}
layers[16].active = true;
key_states = malloc(sizeof(bool) * keys);

if (key_states == NULL) {
return ERR_OOM;
}

return ERR_NONE;
};
2 changes: 1 addition & 1 deletion src/squirrel_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ enum squirrel_error release_key(uint8_t key_index) {
return ERR_NONE;
}

bool key_states[SQUIRREL_KEYCOUNT];
bool *key_states;

enum squirrel_error check_key(uint8_t key_index, bool is_pressed) {
if (key_states[key_index] == is_pressed) {
Expand Down
39 changes: 24 additions & 15 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@ enable_testing()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules")
if(CMAKE_COMPILER_IS_GNUCXX)
include(CodeCoverage)
APPEND_COVERAGE_COMPILER_FLAGS()
setup_target_for_coverage_gcovr_html(squirrel squirrel_test coverage)
list(APPEND GCOVR_EXCLUDES "tests/")
include(CodeCoverage)
append_coverage_compiler_flags()
setup_target_for_coverage_gcovr_html(squirrel squirrel_test coverage)
list(APPEND GCOVR_EXCLUDES "tests/")
endif()

add_executable(keyboard_press_release tests/keyboard_press_release.c)
target_link_libraries(keyboard_press_release squirrel)
add_test(NAME keyboard_press_release COMMAND keyboard_press_release)

add_executable(keyboard_modifier_press_release tests/keyboard_modifier_press_release.c)
add_executable(keyboard_modifier_press_release
tests/keyboard_modifier_press_release.c)
target_link_libraries(keyboard_modifier_press_release squirrel)
add_test(NAME keyboard_modifier_press_release COMMAND keyboard_modifier_press_release)
add_test(NAME keyboard_modifier_press_release
COMMAND keyboard_modifier_press_release)

add_executable(quantum_passthrough_press_release tests/quantum_passthrough_press_release.c)
add_executable(quantum_passthrough_press_release
tests/quantum_passthrough_press_release.c)
target_link_libraries(quantum_passthrough_press_release squirrel)
add_test(NAME quantum_passthrough_press_release COMMAND quantum_passthrough_press_release)
add_test(NAME quantum_passthrough_press_release
COMMAND quantum_passthrough_press_release)

add_executable(keyboard_get_keycodes tests/keyboard_get_keycodes.c)
target_link_libraries(keyboard_get_keycodes squirrel)
Expand All @@ -38,19 +42,24 @@ add_executable(layer_press_release tests/layer_press_release.c)
target_link_libraries(layer_press_release squirrel)
add_test(NAME layer_press_release COMMAND layer_press_release)

add_executable(consumer_activate_deactivate_get_consumer_code tests/consumer_activate_deactivate_get_consumer_code.c)
add_executable(consumer_activate_deactivate_get_consumer_code
tests/consumer_activate_deactivate_get_consumer_code.c)
target_link_libraries(consumer_activate_deactivate_get_consumer_code squirrel)
add_test(NAME consumer_activate_deactivate_get_consumer_code COMMAND consumer_activate_deactivate_get_consumer_code)
add_test(NAME consumer_activate_deactivate_get_consumer_code
COMMAND consumer_activate_deactivate_get_consumer_code)

add_executable(keyboard_activate_deactivate_keycode tests/keyboard_activate_deactivate_keycode.c)
add_executable(keyboard_activate_deactivate_keycode
tests/keyboard_activate_deactivate_keycode.c)
target_link_libraries(keyboard_activate_deactivate_keycode squirrel)
add_test(NAME keyboard_activate_deactivate_keycode COMMAND keyboard_activate_deactivate_keycode)
add_test(NAME keyboard_activate_deactivate_keycode
COMMAND keyboard_activate_deactivate_keycode)

add_executable(keyboard_activate_deactivate_get_modifier tests/keyboard_activate_deactivate_get_modifier.c)
add_executable(keyboard_activate_deactivate_get_modifier
tests/keyboard_activate_deactivate_get_modifier.c)
target_link_libraries(keyboard_activate_deactivate_get_modifier squirrel)
add_test(NAME keyboard_activate_deactivate_get_modifier COMMAND keyboard_activate_deactivate_get_modifier)
add_test(NAME keyboard_activate_deactivate_get_modifier
COMMAND keyboard_activate_deactivate_get_modifier)

add_executable(keymap tests/keymap.c)
target_link_libraries(keymap squirrel)
target_compile_definitions(keymap PRIVATE SQUIRREL_KEYCOUNT=2)
add_test(NAME keymap COMMAND keymap)
4 changes: 1 addition & 3 deletions tests/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ enum squirrel_error test_release(uint8_t layer, uint8_t key_index, void *arg) {
}

// test: press_key, release_key + check_key - in squirrel_key.c
#define SQUIRREL_KEYCOUNT 1

int main() {
squirrel_init();
squirrel_init(1);

// press_key + release_key
uint8_t code = 0xF0;
Expand Down
3 changes: 1 addition & 2 deletions tests/layer_press_release.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
// layer_solo_press
// layer_solo_release
// in squirrel_quantum.c
#define SQUIRREL_KEYCOUNT 1
int main() {
squirrel_init();
squirrel_init(1);
enum squirrel_error err;
struct key test_key;

Expand Down
3 changes: 1 addition & 2 deletions tests/quantum_passthrough_press_release.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ enum squirrel_error bad_test_release(uint8_t layer, uint8_t key_index,

// test: quantum_passthrough_press + quantum_passthrough_release test - in
// squirrel_quantum.c
#define SQUIRREL_KEYCOUNT 1
int main() {
squirrel_init();
squirrel_init(1);

struct key testkey;
testkey.pressed = test_press;
Expand Down

0 comments on commit d7754d7

Please sign in to comment.