From 28e198ee7219f492cc3b3d4a033acbc675d214b5 Mon Sep 17 00:00:00 2001 From: Ben Blazak Date: Thu, 14 Jun 2012 22:02:57 -0700 Subject: [PATCH] added set layer function; more lib/keyfunctions* changes --- src/lib/key-functions.c | 80 ++++++++++++++++++++++++++++++++--------- src/lib/key-functions.h | 7 ++-- src/main.c | 6 ++-- 3 files changed, 72 insertions(+), 21 deletions(-) diff --git a/src/lib/key-functions.c b/src/lib/key-functions.c index 48517b6..2210948 100644 --- a/src/lib/key-functions.c +++ b/src/lib/key-functions.c @@ -16,8 +16,12 @@ #include "lib/usb/usage-page/keyboard.h" #include "keyboard.h" -#include "key-functions.h" +#include "key-functions.h" // includes the appropriate keyboard 'matrix.h' +#include "key-functions--private.h" + +// ---------------------------------------------------------------------------- +// private functions // ---------------------------------------------------------------------------- /* @@ -29,9 +33,10 @@ * * Note * - Because of the way USB does things, what this actually does is either add - * or remove 'keycode' from the list of currently pressed keys + * or remove 'keycode' from the list of currently pressed keys, to be sent at + * the end of the current cycle (see main.c) */ -static void _press_release(uint8_t keycode, bool pressed) { +void _press_release(uint8_t keycode, bool pressed) { // no-op if (keycode == 0) return; @@ -88,6 +93,44 @@ static void _press_release(uint8_t keycode, bool pressed) { } } +/* + * Set current layer + * - Sets any keys currently set to the overall current layer to the new layer, + * and then sets the overall current layer + * + * Arguments + * - value: the new layer value + * - current_layer: (a pointer to) the overall current layer (see main.c) + * - current_layers_: (a pointer to a matrix of) the current layer for each key + * (see main.c and lib/key-functions.h) + * + * Note + * - Leaving all non-current layer values alone allows changing layers while + * maintaining a possibly enabled layer mask (as might be used to implement + * firmware enabled numlock) + */ +void _layer_set_current( + uint8_t value, + uint8_t * current_layer, + uint8_t * current_layers_[KB_ROWS][KB_COLUMNS] ) { + + // don't switch to out-of-bounds layers + if (!( (0 <= *current_layer) && (*current_layer < KB_LAYERS) )) + return; + + for (uint8_t row=0; row 0) - ((*current_layers_)[row][col])--; - // else do nothing + _layer_set_current( + (*current_layer_)-1, + current_layer_, + current_layers_ ); } /* diff --git a/src/lib/key-functions.h b/src/lib/key-functions.h index 7b80874..db6e548 100644 --- a/src/lib/key-functions.h +++ b/src/lib/key-functions.h @@ -35,10 +35,13 @@ #define KBFUN_FUNCTION_ARGS \ - uint8_t keycode_, bool pressed_, \ + uint8_t keycode_, \ + bool pressed_, \ + uint8_t * current_layer_, \ uint8_t (*current_layers_)[KB_ROWS][KB_COLUMNS], \ uint8_t (*pressed_layers_)[KB_ROWS][KB_COLUMNS], \ - uint8_t * row_, uint8_t * col_ + uint8_t * row_, \ + uint8_t * col_ typedef void (*kbfun_funptr_t)( KBFUN_FUNCTION_ARGS ); diff --git a/src/main.c b/src/main.c index 56ea6b5..8fcb09f 100644 --- a/src/main.c +++ b/src/main.c @@ -27,6 +27,8 @@ int main(void) { kb_led_state_ready(); for (;;) { + // the overall current layer + static uint8_t current layer; // the current layer for each key static uint8_t current_layers[KB_ROWS][KB_COLUMNS]; // the layer each key was on when it was last pressed @@ -64,7 +66,7 @@ int main(void) { if (press_function) { (*press_function)( kb_layout_get(current_layer, row, col), - true, + true, ¤t_layer, ¤t_layers, &pressed_layers, &row, &col ); } @@ -76,7 +78,7 @@ int main(void) { if (release_function) { (*release_function)( kb_layout_get(pressed_layer, row, col), - false, + false, ¤t_layer, ¤t_layers, &pressed_layers, &row, &col ); }