minor cleanup

master
Stefan Dorn 2016-02-04 04:38:18 +00:00
parent e71b8fbeb0
commit 2159af0050
1 changed files with 117 additions and 121 deletions

View File

@ -1,4 +1,3 @@
// vim: ts=4 sw=4 sts=4
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* main() * main()
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
@ -20,7 +19,8 @@
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#define MAX_ACTIVE_LAYERS 20 // TODO remove this; for now we'll just limit it to the number of layers
#define MAX_ACTIVE_LAYERS KB_LAYERS
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -34,9 +34,6 @@ static bool main_kb_was_transparent[KB_ROWS][KB_COLUMNS];
uint8_t main_layers_pressed[KB_ROWS][KB_COLUMNS]; uint8_t main_layers_pressed[KB_ROWS][KB_COLUMNS];
uint8_t main_loop_row;
uint8_t main_loop_col;
uint8_t main_arg_layer; uint8_t main_arg_layer;
uint8_t main_arg_layer_offset; uint8_t main_arg_layer_offset;
uint8_t main_arg_row; uint8_t main_arg_row;
@ -52,78 +49,73 @@ bool main_arg_trans_key_pressed;
* main() * main()
*/ */
int main(void) { int main(void) {
kb_init(); // does controller initialization too kb_init(); // does controller initialization too
kb_led_state_power_on(); kb_led_state_power_on();
usb_init(); usb_init();
while (!usb_configured()); while (!usb_configured());
kb_led_delay_usb_init(); // give the OS time to load drivers, etc. kb_led_delay_usb_init(); // give the OS time to load drivers, etc.
kb_led_state_ready(); kb_led_state_ready();
for (;;) { for (;;) {
// swap `main_kb_is_pressed` and `main_kb_was_pressed`, then update // swap `main_kb_is_pressed` and `main_kb_was_pressed`, then update
bool (*temp)[KB_ROWS][KB_COLUMNS] = main_kb_was_pressed; bool (*temp)[KB_ROWS][KB_COLUMNS] = main_kb_was_pressed;
main_kb_was_pressed = main_kb_is_pressed; main_kb_was_pressed = main_kb_is_pressed;
main_kb_is_pressed = temp; main_kb_is_pressed = temp;
kb_update_matrix(*main_kb_is_pressed); kb_update_matrix(*main_kb_is_pressed);
// this loop is responsible to // this loop is responsible to
// - "execute" keys when they change state // - "execute" keys when they change state
// - keep track of which layers the keys were on when they were pressed // - keep track of which layers the keys were on when they were pressed
// (so they can be released using the function from that layer) // (so they can be released using the function from that layer)
// //
// note // note
// - everything else is the key function's responsibility // - everything else is the key function's responsibility
// - see the keyboard layout file ("keyboard/ergodox/layout/*.c") for // - see the keyboard layout file ("keyboard/ergodox/layout/*.c") for
// which key is assigned which function (per layer) // which key is assigned which function (per layer)
// - see "lib/key-functions/public/*.c" for the function definitions // - see "lib/key-functions/public/*.c" for the function definitions
for (int row=0; row<KB_ROWS; row++) { for (uint8_t row=0; row<KB_ROWS; row++) {
for (int col=0; col<KB_COLUMNS; col++) { for (uint8_t col=0; col<KB_COLUMNS; col++) {
main_arg_is_pressed = (*main_kb_is_pressed)[row][col]; main_arg_is_pressed = (*main_kb_is_pressed)[row][col];
main_arg_was_pressed = (*main_kb_was_pressed)[row][col]; main_arg_was_pressed = (*main_kb_was_pressed)[row][col];
if (main_arg_is_pressed != main_arg_was_pressed) { if (main_arg_is_pressed != main_arg_was_pressed) {
if (main_arg_is_pressed) { if (main_arg_is_pressed) {
main_arg_layer = main_layers_peek(0); main_arg_layer = main_layers_peek(0);
main_layers_pressed[row][col] = main_arg_layer; main_layers_pressed[row][col] = main_arg_layer;
main_arg_trans_key_pressed = false; main_arg_trans_key_pressed = false;
} else { } else {
main_arg_layer = main_layers_pressed[row][col]; main_arg_layer = main_layers_pressed[row][col];
main_arg_trans_key_pressed = main_kb_was_transparent[row][col]; main_arg_trans_key_pressed = main_kb_was_transparent[row][col];
} }
// set remaining vars, and "execute" key // set remaining vars, and "execute" key
main_arg_row = row; main_arg_row = row;
main_arg_col = col; main_arg_col = col;
main_arg_layer_offset = 0; main_arg_layer_offset = 0;
main_exec_key(); main_exec_key();
main_kb_was_transparent[row][col] = main_arg_trans_key_pressed; main_kb_was_transparent[row][col] = main_arg_trans_key_pressed;
} }
} }
} }
// send the USB report (even if nothing's changed) // send the USB report (even if nothing's changed)
usb_keyboard_send(); usb_keyboard_send();
usb_extra_consumer_send(); usb_extra_consumer_send();
_delay_ms(MAKEFILE_DEBOUNCE_TIME); _delay_ms(MAKEFILE_DEBOUNCE_TIME);
// update LEDs // update LEDs
if (keyboard_leds & (1<<0)) { kb_led_num_on(); } if (keyboard_leds & (1<<0)) { kb_led_num_on(); } else { kb_led_num_off(); }
else { kb_led_num_off(); } if (keyboard_leds & (1<<1)) { kb_led_caps_on(); } else { kb_led_caps_off(); }
if (keyboard_leds & (1<<1)) { kb_led_caps_on(); } if (keyboard_leds & (1<<2)) { kb_led_scroll_on(); } else { kb_led_scroll_off(); }
else { kb_led_caps_off(); } if (keyboard_leds & (1<<3)) { kb_led_compose_on(); } else { kb_led_compose_off(); }
if (keyboard_leds & (1<<2)) { kb_led_scroll_on(); } if (keyboard_leds & (1<<4)) { kb_led_kana_on(); } else { kb_led_kana_off(); }
else { kb_led_scroll_off(); } }
if (keyboard_leds & (1<<3)) { kb_led_compose_on(); }
else { kb_led_compose_off(); }
if (keyboard_leds & (1<<4)) { kb_led_kana_on(); }
else { kb_led_kana_off(); }
}
return 0; return 0;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -141,9 +133,9 @@ int main(void) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
struct layers { struct layers {
uint8_t layer; uint8_t layer;
uint8_t id; uint8_t id;
uint8_t sticky; uint8_t sticky;
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -158,18 +150,18 @@ uint8_t layers_ids_in_use[MAX_ACTIVE_LAYERS] = {true};
* the current possition. * the current possition.
*/ */
void main_exec_key(void) { void main_exec_key(void) {
void (*key_function)(void) = void (*key_function)(void) =
( (main_arg_is_pressed) ( (main_arg_is_pressed)
? kb_layout_press_get(main_arg_layer, main_arg_row, main_arg_col) ? kb_layout_press_get(main_arg_layer, main_arg_row, main_arg_col)
: kb_layout_release_get(main_arg_layer, main_arg_row, main_arg_col) ); : kb_layout_release_get(main_arg_layer, main_arg_row, main_arg_col) );
if (key_function) if (key_function)
(*key_function)(); (*key_function)();
// If the current layer is in the sticky once up state and a key defined // If the current layer is in the sticky once up state and a key defined
// for this layer (a non-transparent key) was pressed, pop the layer // for this layer (a non-transparent key) was pressed, pop the layer
if (layers[layers_head].sticky == eStickyOnceUp && main_arg_any_non_trans_key_pressed) if (layers[layers_head].sticky == eStickyOnceUp && main_arg_any_non_trans_key_pressed)
main_layers_pop_id(layers_head); main_layers_pop_id(layers_head);
} }
/* /*
@ -183,17 +175,19 @@ void main_exec_key(void) {
* - failure: 0 (default) (out of bounds) * - failure: 0 (default) (out of bounds)
*/ */
uint8_t main_layers_peek(uint8_t offset) { uint8_t main_layers_peek(uint8_t offset) {
if (offset <= layers_head) if (offset <= layers_head) {
return layers[layers_head - offset].layer; return layers[layers_head - offset].layer;
}
return 0; // default, or error return 0; // default, or error
} }
uint8_t main_layers_peek_sticky(uint8_t offset) { uint8_t main_layers_peek_sticky(uint8_t offset) {
if (offset <= layers_head) if (offset <= layers_head) {
return layers[layers_head - offset].sticky; return layers[layers_head - offset].sticky;
}
return 0; // default, or error return 0; // default, or error
} }
/* /*
@ -207,20 +201,20 @@ uint8_t main_layers_peek_sticky(uint8_t offset) {
* - failure: 0 (the stack was already full) * - failure: 0 (the stack was already full)
*/ */
uint8_t main_layers_push(uint8_t layer, uint8_t sticky) { uint8_t main_layers_push(uint8_t layer, uint8_t sticky) {
// look for an available id // look for an available id
for (uint8_t id=1; id<MAX_ACTIVE_LAYERS; id++) { for (uint8_t id=1; id<MAX_ACTIVE_LAYERS; id++) {
// if one is found // if one is found
if (layers_ids_in_use[id] == false) { if (layers_ids_in_use[id] == false) {
layers_ids_in_use[id] = true; layers_ids_in_use[id] = true;
layers_head++; layers_head++;
layers[layers_head].layer = layer; layers[layers_head].layer = layer;
layers[layers_head].id = id; layers[layers_head].id = id;
layers[layers_head].sticky = sticky; layers[layers_head].sticky = sticky;
return id; return id;
} }
} }
return 0; // default, or error return 0; // default, or error
} }
/* /*
@ -230,22 +224,23 @@ uint8_t main_layers_push(uint8_t layer, uint8_t sticky) {
* - 'id': the id of the element to pop from the stack * - 'id': the id of the element to pop from the stack
*/ */
void main_layers_pop_id(uint8_t id) { void main_layers_pop_id(uint8_t id) {
// look for the element with the id we want to pop // look for the element with the id we want to pop
for (uint8_t element=1; element<=layers_head; element++) for (uint8_t element=1; element<=layers_head; element++) {
// if we find it // if we find it
if (layers[element].id == id) { if (layers[element].id == id) {
// move all layers above it down one // move all layers above it down one
for (; element<layers_head; element++) { for (; element<layers_head; element++) {
layers[element].layer = layers[element+1].layer; layers[element].layer = layers[element+1].layer;
layers[element].id = layers[element+1].id; layers[element].id = layers[element+1].id;
} }
// reinitialize the topmost (now unused) slot // reinitialize the topmost (now unused) slot
layers[layers_head].layer = 0; layers[layers_head].layer = 0;
layers[layers_head].id = 0; layers[layers_head].id = 0;
// record keeping // record keeping
layers_ids_in_use[id] = false; layers_ids_in_use[id] = false;
layers_head--; layers_head--;
} }
}
} }
/* /*
@ -260,14 +255,15 @@ void main_layers_pop_id(uint8_t id) {
* - failure: 0 (default) (id unassigned) * - failure: 0 (default) (id unassigned)
*/ */
uint8_t main_layers_get_offset_id(uint8_t id) { uint8_t main_layers_get_offset_id(uint8_t id) {
// look for the element with the id we want to get the offset of // look for the element with the id we want to get the offset of
for (uint8_t element=1; element<=layers_head; element++) for (uint8_t element=1; element<=layers_head; element++) {
// if we find it // if we find it
if (layers[element].id == id) if (layers[element].id == id) {
return (layers_head - element); return (layers_head - element);
}
return 0; // default, or error }
return 0; // default, or error
} }
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------