]> git.friedersdorff.com Git - max/tmk_keyboard.git/blobdiff - mykey.c
add jump_bootloader.
[max/tmk_keyboard.git] / mykey.c
diff --git a/mykey.c b/mykey.c
index 93f753f01f1f07e136a73520aa2daad69f53b270..dcb0852ec43d0a6739986268a683c6767bb3c83b 100644 (file)
--- a/mykey.c
+++ b/mykey.c
  * THE SOFTWARE.
  */
 
+#include <stdbool.h>
 #include <avr/io.h>
 #include <avr/pgmspace.h>
 #include <avr/interrupt.h>
 #include <util/delay.h>
-#include "usb_keyboard_debug.h"
+
+#include "usb_device.h"
 #include "print.h"
 #include "matrix.h"
 #include "keymap.h"
+#include "jump_bootloader.h"
 
 #define LED_CONFIG    (DDRD |= (1<<6))
 #define LED_ON        (PORTD &= ~(1<<6))
@@ -45,7 +48,8 @@ uint16_t idle_count=0;
 
 int main(void)
 {
-    uint8_t modified = 0;
+    bool modified = false;
+    bool has_ghost = false;
     uint8_t key_index = 0;
 
     // set for 16 MHz clock
@@ -75,57 +79,53 @@ int main(void)
     print("keyboard firmware 0.1 for t.m.k.\n");
 
     while (1) {
+        int layer = 0;
         uint8_t row, col, code;
 
-        modified = 0;
-
         matrix_scan();
+        layer = get_layer();
 
-        keyboard_modifier_keys = 0;
-        for (int i = 0; i < 6; i++)
-            keyboard_keys[i] = KB_NO;
-        key_index = 0;
+        modified = matrix_is_modified();
+        has_ghost = matrix_has_ghost();
 
-        for (row = 0; row < MATRIX_ROWS; row++) {
-            if (matrix[row] != prev_matrix[row]) {
-                modified = 1;
-            }
+        // doesnt send keys during ghost occurs
+        if (modified && !has_ghost) {
+            key_index = 0;
+            keyboard_modifier_keys = 0;
+            for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
 
-            for (col = 0; col < MATRIX_COLS; col++) {
-                if (matrix[row] & 1<<col) continue;
-                code = get_keycode(row, col);
-                
-                // Modifier keycode: 0xE0-0xE7
-                if (KB_LCTRL <= code && code <= KB_RGUI) {
-                    keyboard_modifier_keys |= 1<<(code&0x07);
-                } else {
-                    if (key_index < 6) {
-                        keyboard_keys[key_index] = code;
+            for (row = 0; row < MATRIX_ROWS; row++) {
+                for (col = 0; col < MATRIX_COLS; col++) {
+                    if (matrix[row] & 1<<col) continue;
+
+                    code = get_keycode(layer, row, col);
+                    if (code == KB_NO) {
+                        continue;
+                    } else if (KB_LCTRL <= code && code <= KB_RGUI) {
+                        // modifier keycode: 0xE0-0xE7
+                        keyboard_modifier_keys |= 1<<(code & 0x07);
+                    } else {
+                        if (key_index < 6)
+                            keyboard_keys[key_index] = code;
+                        key_index++;
                     }
-                    key_index++;
                 }
-
             }
-        }
-
-        if (key_index > 6) {
-            //Rollover
-        }
-                
 
+            // run bootloader when 4 left modifier keys down
+            if (keyboard_modifier_keys == (MOD_LCTRL | MOD_LSHIFT | MOD_LALT | MOD_LGUI)) {
+                print("jump to bootloader...\n");
+                _delay_ms(1000);
+                jump_bootloader();
+            }
 
-        // if any keypresses were detected, reset the idle counter
-        if (modified) {
-            print("    01234567\n");
-            for (row = 0; row < MATRIX_ROWS; row++) {
-                phex(row); print(": "); pbin_reverse(matrix[row]); print("\n");
+            if (key_index > 6) {
+                //Rollover
             }
-            print("keys: ");
-            for (int i = 0; i < 6; i++) { phex(keyboard_keys[i]); print(" "); }
-            print("\n");
-            print("mod: "); phex(keyboard_modifier_keys); print("\n");
+
             usb_keyboard_send();
 
+
             // variables shared with interrupt routines must be
             // accessed carefully so the interrupt routine doesn't
             // try to use the variable in the middle of our access
@@ -134,6 +134,23 @@ int main(void)
             sei();
         }
 
+        // print matrix state for debug
+        if (modified) {
+            print("\nr/c 01234567\n");
+            for (row = 0; row < MATRIX_ROWS; row++) {
+                phex(row); print(": ");
+                pbin_reverse(matrix[row]);
+                if (matrix_has_ghost_in_row(row)) {
+                    print(" <ghost");
+                }
+                print("\n");
+            }
+            print("keys: ");
+            for (int i = 0; i < 6; i++) { phex(keyboard_keys[i]); print(" "); }
+            print("\n");
+            print("mod: "); phex(keyboard_modifier_keys); print("\n");
+        }
+
         // now the current pins will be the previous, and
         // wait a short delay so we're not highly sensitive
         // to mechanical "bounce".
@@ -150,7 +167,6 @@ ISR(TIMER0_OVF_vect)
     idle_count++;
     if (idle_count > 61 * 8) {
         idle_count = 0;
-        //print("Timer Event :)\n");
-        //usb_keyboard_press(KEY_SPACE, 0);
+        print(".");
     }
 }