add mouse function.
[tmk_keyboard.git] / mykey.c
1 /* 2010/08/23 noname
2 * keyboard firmware based on PJRC USB keyboard example
3 */
4 /* Keyboard example with debug channel, for Teensy USB Development Board
5 * http://www.pjrc.com/teensy/usb_keyboard.html
6 * Copyright (c) 2008 PJRC.COM, LLC
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include <stdbool.h>
28 #include <avr/io.h>
29 #include <avr/pgmspace.h>
30 #include <avr/interrupt.h>
31 #include <util/delay.h>
32
33 #include "usb.h"
34 #include "usb_keyboard.h"
35 #include "usb_mouse.h"
36 #include "print.h"
37 #include "matrix.h"
38 #include "keymap.h"
39 #include "jump_bootloader.h"
40
41 #define LED_CONFIG (DDRD |= (1<<6))
42 #define LED_ON (PORTD &= ~(1<<6))
43 #define LED_OFF (PORTD |= (1<<6))
44 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
45
46 static void print_matrix(void);
47
48
49 uint16_t idle_count=0;
50
51
52
53 int main(void)
54 {
55 // set for 16 MHz clock
56 CPU_PRESCALE(0);
57
58 // Initialize the USB, and then wait for the host to set configuration.
59 // If the Teensy is powered without a PC connected to the USB port,
60 // this will wait forever.
61 usb_init();
62 while (!usb_configured()) /* wait */ ;
63
64 // Wait an extra second for the PC's operating system to load drivers
65 // and do whatever it does to actually be ready for input
66 _delay_ms(1000);
67
68 // Configure timer 0 to generate a timer overflow interrupt every
69 // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
70 // This demonstrates how to use interrupts to implement a simple
71 // inactivity timeout.
72 TCCR0A = 0x00;
73 TCCR0B = 0x05;
74 TIMSK0 = (1<<TOIE0);
75
76
77 matrix_init();
78 print("firmware 0.2 for t.m.k.\n");
79
80 bool modified = false;
81 bool has_ghost = false;
82 int key_index = 0;
83 int loop_count = 0;
84 int layer = 0;
85 while (1) {
86 matrix_scan();
87 layer = get_layer();
88 modified = matrix_is_modified();
89 has_ghost = matrix_has_ghost();
90
91 // print matrix state for debug
92 if (modified) {
93 print_matrix();
94
95 // LED flush
96 DDRD |= 1<<PD6;
97 PORTD |= 1<<PD6;
98 }
99
100 // set matrix state to keyboard_keys, keyboard_modifier_keys
101 key_index = 0;
102 keyboard_modifier_keys = 0;
103 for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
104 for (int row = 0; row < MATRIX_ROWS; row++) {
105 for (int col = 0; col < MATRIX_COLS; col++) {
106 if (matrix[row] & 1<<col) continue;
107
108 uint8_t code = get_keycode(layer, row, col);
109 if (code == KB_NO) {
110 continue;
111 } else if (KB_LCTRL <= code && code <= KB_RGUI) {
112 // modifier keycode: 0xE0-0xE7
113 keyboard_modifier_keys |= 1<<(code & 0x07);
114 } else {
115 if (key_index < 6)
116 keyboard_keys[key_index] = code;
117 key_index++;
118 }
119 }
120 }
121
122 if (!has_ghost) {
123 // when 4 left modifier keys down
124 if (keyboard_modifier_keys == (MOD_LCTRL | MOD_LSHIFT | MOD_LALT | MOD_LGUI)) {
125 // cancel all keys
126 keyboard_modifier_keys = 0;
127 for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
128 usb_keyboard_send();
129
130 /*
131 print("jump to bootloader...\n");
132 _delay_ms(1000);
133 jump_bootloader();
134 */
135
136 // mouse
137 print("usb_mouse_move\n");
138 usb_mouse_move(10, 0, 0);
139
140 _delay_ms(100);
141 continue;
142 }
143
144
145 // send keys to host
146 if (modified) {
147 if (key_index > 6) {
148 //Rollover
149 }
150 usb_keyboard_send();
151 }
152 }
153 loop_count++;
154 _delay_ms(2);
155 }
156 }
157
158 static void print_matrix(void) {
159 print("\nr/c 01234567\n");
160 for (int row = 0; row < MATRIX_ROWS; row++) {
161 phex(row); print(": ");
162 pbin_reverse(matrix[row]);
163 if (matrix_has_ghost_in_row(row)) {
164 print(" <ghost");
165 }
166 print("\n");
167 }
168 print("keys: ");
169 for (int i = 0; i < 6; i++) { phex(keyboard_keys[i]); print(" "); }
170 print("\n");
171 print("mod: "); phex(keyboard_modifier_keys); print("\n");
172 }
173
174 // This interrupt routine is run approx 61 times per second.
175 // A very simple inactivity timeout is implemented, where we
176 // will send a space character and print a message to the
177 // hid_listen debug message window.
178 ISR(TIMER0_OVF_vect)
179 {
180 idle_count++;
181 }
Imprint / Impressum