2012-04-10 10:44:27 +02:00
|
|
|
// vim: ts=4 sw=4 sts=4
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* main()
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* Copyright (c) 2012 Ben Blazak <benblazak.dev@gmail.com>
|
|
|
|
* Released under The MIT License (MIT) (see "license.md")
|
|
|
|
* Project located at <https://github.com/benblazak/ergodox-firmware>
|
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
#include <util/delay.h>
|
2012-04-29 08:40:00 +02:00
|
|
|
#include "lib-other/pjrc/usb_keyboard/usb_keyboard.h"
|
|
|
|
#include "lib/data-types.h"
|
2012-04-10 10:44:27 +02:00
|
|
|
|
|
|
|
#include "keyboard.h"
|
|
|
|
|
|
|
|
|
2012-04-12 06:05:45 +02:00
|
|
|
// note:
|
|
|
|
// - see your keyswitch specification for the necessary value. for cherry mx
|
|
|
|
// switches, bounce time should be <= 5ms. it looks like most switches are
|
2012-05-07 03:36:51 +02:00
|
|
|
// speced between 5 and 8 ms.
|
2012-04-12 06:05:45 +02:00
|
|
|
// - if timing is important, balance this value with the main() loop run time
|
|
|
|
// (~5ms, last i checked, nearly all of it in the i2c update() function)
|
|
|
|
#define DEBOUNCE_TIME 5 // in ms
|
2012-04-10 10:44:27 +02:00
|
|
|
|
2012-04-11 03:58:26 +02:00
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
int main(void) {
|
|
|
|
kb_init(); // does controller initialization too
|
|
|
|
|
2012-05-07 03:36:51 +02:00
|
|
|
kb_led1_on();
|
|
|
|
kb_led2_on();
|
|
|
|
kb_led3_on();
|
2012-04-12 06:05:45 +02:00
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
usb_init();
|
|
|
|
while (!usb_configured());
|
|
|
|
_delay_ms(1000); // make sure the OS has had time to load drivers, etc.
|
|
|
|
|
2012-05-07 03:36:51 +02:00
|
|
|
kb_led1_off();
|
|
|
|
kb_led2_off();
|
|
|
|
kb_led3_off();
|
2012-04-12 06:05:45 +02:00
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
for (;;) {
|
|
|
|
static uint8_t current_layer = 0;
|
|
|
|
|
|
|
|
// swap `kb_is_pressed` and `kb_was_pressed`, then update
|
|
|
|
bool (*temp)[KB_ROWS][KB_COLUMNS] = kb_was_pressed;
|
|
|
|
kb_was_pressed = kb_is_pressed;
|
|
|
|
kb_is_pressed = temp;
|
|
|
|
|
|
|
|
kb_update_matrix(*kb_is_pressed);
|
|
|
|
|
2012-04-29 07:39:23 +02:00
|
|
|
// call the appropriate function for each key, then send the usb report
|
|
|
|
// if necessary
|
2012-04-12 06:05:45 +02:00
|
|
|
// - everything else is the key function's responsibility; see the
|
2012-05-07 03:36:51 +02:00
|
|
|
// keyboard layout file ("keyboard/ergodox/layout/*.c") for which key
|
2012-04-29 07:39:23 +02:00
|
|
|
// is assigned which function (per layer), and "lib/key-functions.c"
|
|
|
|
// for their definitions
|
2012-04-11 03:58:26 +02:00
|
|
|
for (uint8_t row=0; row<KB_ROWS; row++) {
|
|
|
|
for (uint8_t col=0; col<KB_COLUMNS; col++) {
|
|
|
|
bool is_pressed = (*kb_is_pressed)[row][col];
|
|
|
|
bool was_pressed = (*kb_was_pressed)[row][col];
|
|
|
|
if (is_pressed != was_pressed) {
|
|
|
|
if (is_pressed) {
|
|
|
|
kbfun_funptr_t press_function =
|
2012-04-29 07:39:23 +02:00
|
|
|
kb_layout_press_get(current_layer, row, col);
|
2012-04-11 03:58:26 +02:00
|
|
|
if (press_function) {
|
|
|
|
(*press_function)(
|
2012-04-29 09:17:17 +02:00
|
|
|
kb_layout_get(current_layer, row, col),
|
2012-04-10 10:44:27 +02:00
|
|
|
¤t_layer, &row, &col );
|
2012-04-11 03:58:26 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kbfun_funptr_t release_function =
|
2012-04-29 07:39:23 +02:00
|
|
|
kb_layout_release_get(current_layer, row, col);
|
2012-04-11 03:58:26 +02:00
|
|
|
if (release_function) {
|
|
|
|
(*release_function)(
|
2012-04-29 09:17:17 +02:00
|
|
|
kb_layout_get(current_layer, row, col),
|
2012-04-10 10:44:27 +02:00
|
|
|
¤t_layer, &row, &col );
|
2012-04-11 03:58:26 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-10 10:44:27 +02:00
|
|
|
|
2012-04-11 03:58:26 +02:00
|
|
|
usb_keyboard_send();
|
|
|
|
_delay_ms(DEBOUNCE_TIME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-10 10:44:27 +02:00
|
|
|
|
|
|
|
// update LEDs
|
2012-05-07 03:36:51 +02:00
|
|
|
(keyboard_leds & (1<<0)) ? kb_led1_on() : kb_led1_off(); // num lock
|
|
|
|
(keyboard_leds & (1<<1)) ? kb_led2_on() : kb_led2_off(); // caps lock
|
|
|
|
(keyboard_leds & (1<<2)) ? kb_led3_on() : kb_led3_off(); // scroll lock
|
2012-04-11 03:58:26 +02:00
|
|
|
#if 0 // not implemented right now
|
2012-05-07 03:36:51 +02:00
|
|
|
(keyboard_leds & (1<<3)) ? kb_led4_on() : kb_led4_off(); // compose
|
|
|
|
(keyboard_leds & (1<<4)) ? kb_led5_on() : kb_led5_off(); // kana
|
2012-04-11 03:58:26 +02:00
|
|
|
#endif
|
2012-04-10 10:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|