aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2012-06-07 02:25:15 +0900
committertmk <nobody@nowhere>2012-06-07 02:47:33 +0900
commitf4125707399d11a7d80587659c464b9bcddb8c56 (patch)
tree1d2a02e30f8cd103e8f4dc36629c09f6a3d44fef /common
parent225de7a847a511d004bf909b1334e19497cf2f9d (diff)
downloadqmk_firmware-f4125707399d11a7d80587659c464b9bcddb8c56.tar.gz
qmk_firmware-f4125707399d11a7d80587659c464b9bcddb8c56.zip
Moved files to common, protocol and doc directory
Diffstat (limited to 'common')
-rw-r--r--common/bootloader.c22
-rw-r--r--common/bootloader.h25
-rw-r--r--common/command.c243
-rw-r--r--common/command.h25
-rw-r--r--common/controller_teensy.h27
-rw-r--r--common/debug.h36
-rw-r--r--common/host.c237
-rw-r--r--common/host.h57
-rw-r--r--common/host_driver.h33
-rw-r--r--common/keyboard.c203
-rw-r--r--common/keyboard.h28
-rw-r--r--common/keymap.h34
-rw-r--r--common/layer.c207
-rw-r--r--common/layer.h32
-rw-r--r--common/led.h33
-rw-r--r--common/matrix.h49
-rwxr-xr-xcommon/mousekey.c132
-rw-r--r--common/mousekey.h29
-rw-r--r--common/print.c93
-rw-r--r--common/print.h45
-rw-r--r--common/report.h96
-rw-r--r--common/sendchar.h27
-rw-r--r--common/sendchar_null.c23
-rw-r--r--common/sendchar_uart.c25
-rw-r--r--common/timer.c89
-rw-r--r--common/timer.h53
-rw-r--r--common/uart.c129
-rw-r--r--common/uart.h11
-rw-r--r--common/usb_keycodes.h423
-rw-r--r--common/util.c37
-rw-r--r--common/util.h34
31 files changed, 2537 insertions, 0 deletions
diff --git a/common/bootloader.c b/common/bootloader.c
new file mode 100644
index 000000000..5cbfc72e5
--- /dev/null
+++ b/common/bootloader.c
@@ -0,0 +1,22 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "bootloader.h"
19
20
21void bootloader_jump(void) __attribute__ ((weak));
22void bootloader_jump(void) {}
diff --git a/common/bootloader.h b/common/bootloader.h
new file mode 100644
index 000000000..44775039d
--- /dev/null
+++ b/common/bootloader.h
@@ -0,0 +1,25 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef BOOTLOADER_H
19#define BOOTLOADER_H
20
21
22/* give code for your bootloader to come up if needed */
23void bootloader_jump(void);
24
25#endif
diff --git a/common/command.c b/common/command.c
new file mode 100644
index 000000000..e325a5d84
--- /dev/null
+++ b/common/command.c
@@ -0,0 +1,243 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#include <stdint.h>
18#include <stdbool.h>
19#include <util/delay.h>
20#include "usb_keycodes.h"
21#include "host.h"
22#include "print.h"
23#include "debug.h"
24#include "util.h"
25#include "timer.h"
26#include "layer.h"
27#include "matrix.h"
28#include "bootloader.h"
29#include "command.h"
30
31#ifdef HOST_PJRC
32# include "usb_keyboard.h"
33# ifdef EXTRAKEY_ENABLE
34# include "usb_extra.h"
35# endif
36#endif
37
38#ifdef HOST_VUSB
39# include "usbdrv.h"
40#endif
41
42
43static uint8_t command_common(void);
44static void help(void);
45static void switch_layer(uint8_t layer);
46
47static bool last_print_enable;
48
49uint8_t command_proc(void)
50{
51 uint8_t processed = 0;
52 last_print_enable = print_enable;
53
54 if (!IS_COMMAND())
55 return 0;
56
57 print_enable = true;
58 if (command_extra() || command_common()) {
59 processed = 1;
60 _delay_ms(500);
61 }
62 print_enable = last_print_enable;
63 return processed;
64}
65
66/* This allows to define extra commands. return 0 when not processed. */
67uint8_t command_extra(void) __attribute__ ((weak));
68uint8_t command_extra(void)
69{
70 return 0;
71}
72
73
74static uint8_t command_common(void)
75{
76 switch (host_get_first_key()) {
77 case KB_H:
78 help();
79 break;
80 case KB_B:
81 host_clear_keyboard_report();
82 host_send_keyboard_report();
83 print("jump to bootloader... ");
84 _delay_ms(1000);
85 bootloader_jump(); // not return
86 print("not supported.\n");
87 break;
88 case KB_D:
89 debug_enable = !debug_enable;
90 if (debug_enable) {
91 last_print_enable = true;
92 print("debug enabled.\n");
93 debug_matrix = true;
94 debug_keyboard = true;
95 debug_mouse = true;
96 } else {
97 print("debug disabled.\n");
98 last_print_enable = false;
99 debug_matrix = false;
100 debug_keyboard = false;
101 debug_mouse = false;
102 }
103 break;
104 case KB_X: // debug matrix toggle
105 debug_matrix = !debug_matrix;
106 if (debug_matrix)
107 print("debug matrix enabled.\n");
108 else
109 print("debug matrix disabled.\n");
110 break;
111 case KB_K: // debug keyboard toggle
112 debug_keyboard = !debug_keyboard;
113 if (debug_keyboard)
114 print("debug keyboard enabled.\n");
115 else
116 print("debug keyboard disabled.\n");
117 break;
118 case KB_M: // debug mouse toggle
119 debug_mouse = !debug_mouse;
120 if (debug_mouse)
121 print("debug mouse enabled.\n");
122 else
123 print("debug mouse disabled.\n");
124 break;
125 case KB_V: // print version & information
126 print(STR(DESCRIPTION) "\n");
127 break;
128 case KB_T: // print timer
129 print("timer: "); phex16(timer_count); print("\n");
130 break;
131 case KB_P: // print toggle
132 if (last_print_enable) {
133 print("print disabled.\n");
134 last_print_enable = false;
135 } else {
136 last_print_enable = true;
137 print("print enabled.\n");
138 }
139 break;
140 case KB_S:
141#ifdef HOST_PJRC
142 print("UDCON: "); phex(UDCON); print("\n");
143 print("UDIEN: "); phex(UDIEN); print("\n");
144 print("UDINT: "); phex(UDINT); print("\n");
145 print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
146 print("usb_keyboard_protocol: "); phex(usb_keyboard_protocol); print("\n");
147 print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
148 print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
149#endif
150
151#ifdef HOST_VUSB
152# if USB_COUNT_SOF
153 print("usbSofCount: "); phex(usbSofCount); print("\n");
154# endif
155#endif
156 break;
157#ifdef NKRO_ENABLE
158 case KB_N:
159 // send empty report before change
160 host_clear_keyboard_report();
161 host_send_keyboard_report();
162 keyboard_nkro = !keyboard_nkro;
163 if (keyboard_nkro)
164 print("NKRO: enabled\n");
165 else
166 print("NKRO: disabled\n");
167 break;
168#endif
169#ifdef EXTRAKEY_ENABLE
170 case KB_ESC:
171 host_clear_keyboard_report();
172 host_send_keyboard_report();
173#ifdef HOST_PJRC
174 if (suspend && remote_wakeup) {
175 usb_remote_wakeup();
176 } else {
177 host_system_send(SYSTEM_POWER_DOWN);
178 host_system_send(0);
179 _delay_ms(500);
180 }
181#else
182 host_system_send(SYSTEM_POWER_DOWN);
183 host_system_send(0);
184 _delay_ms(500);
185#endif
186 break;
187#endif
188 case KB_BSPC:
189 matrix_init();
190 print("clear matrix\n");
191 break;
192 case KB_0:
193 switch_layer(0);
194 break;
195 case KB_1:
196 switch_layer(1);
197 break;
198 case KB_2:
199 switch_layer(2);
200 break;
201 case KB_3:
202 switch_layer(3);
203 break;
204 case KB_4:
205 switch_layer(4);
206 break;
207 default:
208 return 0;
209 }
210 return 1;
211}
212
213static void help(void)
214{
215 print("b: jump to bootloader\n");
216 print("d: toggle debug enable\n");
217 print("x: toggle matrix debug\n");
218 print("k: toggle keyboard debug\n");
219 print("m: toggle mouse debug\n");
220 print("p: toggle print enable\n");
221 print("v: print version\n");
222 print("t: print timer count\n");
223 print("s: print status\n");
224#ifdef NKRO_ENABLE
225 print("n: toggle NKRO\n");
226#endif
227 print("Backspace: clear matrix\n");
228 print("ESC: power down/wake up\n");
229 print("0: switch to Layer0 \n");
230 print("1: switch to Layer1 \n");
231 print("2: switch to Layer2 \n");
232 print("3: switch to Layer3 \n");
233 print("4: switch to Layer4 \n");
234}
235
236static void switch_layer(uint8_t layer)
237{
238 print("current_layer: "); phex(current_layer); print("\n");
239 print("default_layer: "); phex(default_layer); print("\n");
240 current_layer = layer;
241 default_layer = layer;
242 print("switch to Layer: "); phex(layer); print("\n");
243}
diff --git a/common/command.h b/common/command.h
new file mode 100644
index 000000000..4888f5ee0
--- /dev/null
+++ b/common/command.h
@@ -0,0 +1,25 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef COMMAND_H
19#define COMMAND
20
21uint8_t command_proc(void);
22/* This allows to extend commands. Return 0 when command is not processed. */
23uint8_t command_extra(void);
24
25#endif
diff --git a/common/controller_teensy.h b/common/controller_teensy.h
new file mode 100644
index 000000000..6c3f47ce4
--- /dev/null
+++ b/common/controller_teensy.h
@@ -0,0 +1,27 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef TEENSY_H
19#define TEENSY_H 1
20
21// for Teensy/Teensy++ 2.0
22#define DEBUG_LED 1
23#define DEBUG_LED_CONFIG (DDRD |= (1<<6))
24#define DEBUG_LED_ON (PORTD |= (1<<6))
25#define DEBUG_LED_OFF (PORTD &= ~(1<<6))
26
27#endif
diff --git a/common/debug.h b/common/debug.h
new file mode 100644
index 000000000..230d3b349
--- /dev/null
+++ b/common/debug.h
@@ -0,0 +1,36 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef DEBUG_H
19#define DEBUG_H 1
20
21#include "print.h"
22
23
24#define debug(s) if(debug_enable) print(s)
25#define debug_hex(c) if(debug_enable) phex(c)
26#define debug_hex16(i) if(debug_enable) phex16(i)
27#define debug_bin(c) if(debug_enable) pbin(c)
28#define debug_bin_reverse(c) if(debug_enable) pbin_reverse(c)
29
30
31bool debug_enable;
32bool debug_matrix;
33bool debug_keyboard;
34bool debug_mouse;
35
36#endif
diff --git a/common/host.c b/common/host.c
new file mode 100644
index 000000000..cc26d55c2
--- /dev/null
+++ b/common/host.c
@@ -0,0 +1,237 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <stdint.h>
19#include <avr/interrupt.h>
20#include "usb_keycodes.h"
21#include "host.h"
22#include "util.h"
23#include "debug.h"
24
25
26#ifdef NKRO_ENABLE
27bool keyboard_nkro = false;
28#endif
29
30static host_driver_t *driver;
31static report_keyboard_t report0;
32static report_keyboard_t report1;
33report_keyboard_t *keyboard_report = &report0;
34report_keyboard_t *keyboard_report_prev = &report1;
35
36
37static inline void add_key_byte(uint8_t code);
38static inline void del_key_byte(uint8_t code);
39static inline void add_key_bit(uint8_t code);
40static inline void del_key_bit(uint8_t code);
41
42
43void host_set_driver(host_driver_t *d)
44{
45 driver = d;
46}
47
48host_driver_t *host_get_driver(void)
49{
50 return driver;
51}
52
53uint8_t host_keyboard_leds(void)
54{
55 if (!driver) return 0;
56 return (*driver->keyboard_leds)();
57}
58
59/* keyboard report operations */
60void host_add_key(uint8_t key)
61{
62#ifdef NKRO_ENABLE
63 if (keyboard_nkro) {
64 add_key_bit(key);
65 return;
66 }
67#endif
68 add_key_byte(key);
69}
70
71void host_del_key(uint8_t key)
72{
73#ifdef NKRO_ENABLE
74 if (keyboard_nkro) {
75 del_key_bit(key);
76 return;
77 }
78#endif
79 del_key_byte(key);
80}
81
82void host_add_mod_bit(uint8_t mod)
83{
84 keyboard_report->mods |= mod;
85}
86
87void host_del_mod_bit(uint8_t mod)
88{
89 keyboard_report->mods &= ~mod;
90}
91
92void host_set_mods(uint8_t mods)
93{
94 keyboard_report->mods = mods;
95}
96
97void host_add_code(uint8_t code)
98{
99 if (IS_MOD(code)) {
100 host_add_mod_bit(MOD_BIT(code));
101 } else {
102 host_add_key(code);
103 }
104}
105
106void host_del_code(uint8_t code)
107{
108 if (IS_MOD(code)) {
109 host_del_mod_bit(MOD_BIT(code));
110 } else {
111 host_del_key(code);
112 }
113}
114
115void host_swap_keyboard_report(void)
116{
117 uint8_t sreg = SREG;
118 cli();
119 report_keyboard_t *tmp = keyboard_report_prev;
120 keyboard_report_prev = keyboard_report;
121 keyboard_report = tmp;
122 SREG = sreg;
123}
124
125void host_clear_keyboard_report(void)
126{
127 keyboard_report->mods = 0;
128 for (int8_t i = 0; i < REPORT_KEYS; i++) {
129 keyboard_report->keys[i] = 0;
130 }
131}
132
133uint8_t host_has_anykey(void)
134{
135 uint8_t cnt = 0;
136 for (int i = 0; i < REPORT_KEYS; i++) {
137 if (keyboard_report->keys[i])
138 cnt++;
139 }
140 return cnt;
141}
142
143uint8_t host_get_first_key(void)
144{
145#ifdef NKRO_ENABLE
146 if (keyboard_nkro) {
147 uint8_t i = 0;
148 for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
149 ;
150 return i<<3 | biton(keyboard_report->keys[i]);
151 }
152#endif
153 return keyboard_report->keys[0];
154}
155
156
157void host_send_keyboard_report(void)
158{
159 if (!driver) return;
160 (*driver->send_keyboard)(keyboard_report);
161}
162
163void host_mouse_send(report_mouse_t *report)
164{
165 if (!driver) return;
166 (*driver->send_mouse)(report);
167}
168
169void host_system_send(uint16_t data)
170{
171 if (!driver) return;
172 (*driver->send_system)(data);
173}
174
175void host_consumer_send(uint16_t data)
176{
177 // TODO: this is needed?
178 static uint16_t last_data = 0;
179 if (data == last_data) return;
180 last_data = data;
181
182 if (!driver) return;
183 (*driver->send_consumer)(data);
184}
185
186
187static inline void add_key_byte(uint8_t code)
188{
189 // TODO: fix ugly code
190 int8_t i = 0;
191 int8_t empty = -1;
192 for (; i < REPORT_KEYS; i++) {
193 if (keyboard_report_prev->keys[i] == code) {
194 keyboard_report->keys[i] = code;
195 break;
196 }
197 if (empty == -1 &&
198 keyboard_report_prev->keys[i] == 0 &&
199 keyboard_report->keys[i] == 0) {
200 empty = i;
201 }
202 }
203 if (i == REPORT_KEYS) {
204 if (empty != -1) {
205 keyboard_report->keys[empty] = code;
206 }
207 }
208}
209
210static inline void del_key_byte(uint8_t code)
211{
212 int i = 0;
213 for (; i < REPORT_KEYS; i++) {
214 if (keyboard_report->keys[i] == code) {
215 keyboard_report->keys[i] = 0;
216 break;
217 }
218 }
219}
220
221static inline void add_key_bit(uint8_t code)
222{
223 if ((code>>3) < REPORT_KEYS) {
224 keyboard_report->keys[code>>3] |= 1<<(code&7);
225 } else {
226 debug("add_key_bit: can't add: "); phex(code); debug("\n");
227 }
228}
229
230static inline void del_key_bit(uint8_t code)
231{
232 if ((code>>3) < REPORT_KEYS) {
233 keyboard_report->keys[code>>3] &= ~(1<<(code&7));
234 } else {
235 debug("del_key_bit: can't del: "); phex(code); debug("\n");
236 }
237}
diff --git a/common/host.h b/common/host.h
new file mode 100644
index 000000000..11b9aacd7
--- /dev/null
+++ b/common/host.h
@@ -0,0 +1,57 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef HOST_H
19#define HOST_H
20
21#include <stdint.h>
22#include "report.h"
23#include "host_driver.h"
24
25
26#ifdef NKRO_ENABLE
27extern bool keyboard_nkro;
28#endif
29
30extern report_keyboard_t *keyboard_report;
31extern report_keyboard_t *keyboard_report_prev;
32
33
34void host_set_driver(host_driver_t *driver);
35host_driver_t *host_get_driver(void);
36uint8_t host_keyboard_leds(void);
37
38/* keyboard report operations */
39void host_add_key(uint8_t key);
40void host_del_key(uint8_t key);
41void host_add_mod_bit(uint8_t mod);
42void host_del_mod_bit(uint8_t mod);
43void host_set_mods(uint8_t mods);
44void host_add_code(uint8_t code);
45void host_del_code(uint8_t code);
46void host_swap_keyboard_report(void);
47void host_clear_keyboard_report(void);
48uint8_t host_has_anykey(void);
49uint8_t host_get_first_key(void);
50
51
52void host_send_keyboard_report(void);
53void host_mouse_send(report_mouse_t *report);
54void host_system_send(uint16_t data);
55void host_consumer_send(uint16_t data);
56
57#endif
diff --git a/common/host_driver.h b/common/host_driver.h
new file mode 100644
index 000000000..edb9e5dd9
--- /dev/null
+++ b/common/host_driver.h
@@ -0,0 +1,33 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef HOST_DRIVER_H
19#define HOST_DRIVER_H
20
21#include <stdint.h>
22#include "report.h"
23
24
25typedef struct {
26 uint8_t (*keyboard_leds)(void);
27 void (*send_keyboard)(report_keyboard_t *);
28 void (*send_mouse)(report_mouse_t *);
29 void (*send_system)(uint16_t);
30 void (*send_consumer)(uint16_t);
31} host_driver_t;
32
33#endif
diff --git a/common/keyboard.c b/common/keyboard.c
new file mode 100644
index 000000000..5c2643c95
--- /dev/null
+++ b/common/keyboard.c
@@ -0,0 +1,203 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#include "keyboard.h"
18#include "host.h"
19#include "layer.h"
20#include "matrix.h"
21#include "led.h"
22#include "usb_keycodes.h"
23#include "timer.h"
24#include "print.h"
25#include "debug.h"
26#include "command.h"
27#ifdef MOUSEKEY_ENABLE
28#include "mousekey.h"
29#endif
30#ifdef EXTRAKEY_ENABLE
31#include <util/delay.h>
32#endif
33
34
35static uint8_t last_leds = 0;
36
37
38void keyboard_init(void)
39{
40 timer_init();
41 matrix_init();
42#ifdef PS2_MOUSE_ENABLE
43 ps2_mouse_init();
44#endif
45}
46
47void keyboard_proc(void)
48{
49 uint8_t fn_bits = 0;
50#ifdef EXTRAKEY_ENABLE
51 uint16_t consumer_code = 0;
52#endif
53
54 matrix_scan();
55
56 if (matrix_is_modified()) {
57 if (debug_matrix) matrix_print();
58#ifdef DEBUG_LED
59 // LED flash for debug
60 DEBUG_LED_CONFIG;
61 DEBUG_LED_ON;
62#endif
63 }
64
65 if (matrix_has_ghost()) {
66 // should send error?
67 debug("matrix has ghost!!\n");
68 return;
69 }
70
71 host_swap_keyboard_report();
72 host_clear_keyboard_report();
73 for (int row = 0; row < matrix_rows(); row++) {
74 for (int col = 0; col < matrix_cols(); col++) {
75 if (!matrix_is_on(row, col)) continue;
76
77 uint8_t code = layer_get_keycode(row, col);
78 if (code == KB_NO) {
79 // do nothing
80 } else if (IS_MOD(code)) {
81 host_add_mod_bit(MOD_BIT(code));
82 } else if (IS_FN(code)) {
83 fn_bits |= FN_BIT(code);
84 }
85// TODO: use table or something
86#ifdef EXTRAKEY_ENABLE
87 // System Control
88 else if (code == KB_SYSTEM_POWER) {
89#ifdef HOST_PJRC
90 if (suspend && remote_wakeup) {
91 usb_remote_wakeup();
92 } else {
93 host_system_send(SYSTEM_POWER_DOWN);
94 }
95#else
96 host_system_send(SYSTEM_POWER_DOWN);
97#endif
98 host_system_send(0);
99 _delay_ms(500);
100 } else if (code == KB_SYSTEM_SLEEP) {
101 host_system_send(SYSTEM_SLEEP);
102 host_system_send(0);
103 _delay_ms(500);
104 } else if (code == KB_SYSTEM_WAKE) {
105 host_system_send(SYSTEM_WAKE_UP);
106 host_system_send(0);
107 _delay_ms(500);
108 }
109 // Consumer Page
110 else if (code == KB_AUDIO_MUTE) {
111 consumer_code = AUDIO_MUTE;
112 } else if (code == KB_AUDIO_VOL_UP) {
113 consumer_code = AUDIO_VOL_UP;
114 } else if (code == KB_AUDIO_VOL_DOWN) {
115 consumer_code = AUDIO_VOL_DOWN;
116 }
117 else if (code == KB_MEDIA_NEXT_TRACK) {
118 consumer_code = TRANSPORT_NEXT_TRACK;
119 } else if (code == KB_MEDIA_PREV_TRACK) {
120 consumer_code = TRANSPORT_PREV_TRACK;
121 } else if (code == KB_MEDIA_STOP) {
122 consumer_code = TRANSPORT_STOP;
123 } else if (code == KB_MEDIA_PLAY_PAUSE) {
124 consumer_code = TRANSPORT_PLAY_PAUSE;
125 } else if (code == KB_MEDIA_SELECT) {
126 consumer_code = AL_CC_CONFIG;
127 }
128 else if (code == KB_MAIL) {
129 consumer_code = AL_EMAIL;
130 } else if (code == KB_CALCULATOR) {
131 consumer_code = AL_CALCULATOR;
132 } else if (code == KB_MY_COMPUTER) {
133 consumer_code = AL_LOCAL_BROWSER;
134 }
135 else if (code == KB_WWW_SEARCH) {
136 consumer_code = AC_SEARCH;
137 } else if (code == KB_WWW_HOME) {
138 consumer_code = AC_HOME;
139 } else if (code == KB_WWW_BACK) {
140 consumer_code = AC_BACK;
141 } else if (code == KB_WWW_FORWARD) {
142 consumer_code = AC_FORWARD;
143 } else if (code == KB_WWW_STOP) {
144 consumer_code = AC_STOP;
145 } else if (code == KB_WWW_REFRESH) {
146 consumer_code = AC_REFRESH;
147 } else if (code == KB_WWW_FAVORITES) {
148 consumer_code = AC_BOOKMARKS;
149 }
150#endif
151 else if (IS_KEY(code)) {
152 host_add_key(code);
153 }
154#ifdef MOUSEKEY_ENABLE
155 else if (IS_MOUSEKEY(code)) {
156 mousekey_decode(code);
157 }
158#endif
159 else {
160 debug("ignore keycode: "); debug_hex(code); debug("\n");
161 }
162 }
163 }
164
165 layer_switching(fn_bits);
166
167 if (command_proc()) {
168 return;
169 }
170
171 // TODO: should send only when changed from last report
172 if (matrix_is_modified()) {
173 host_send_keyboard_report();
174#ifdef EXTRAKEY_ENABLE
175 host_consumer_send(consumer_code);
176#endif
177#ifdef DEBUG_LED
178 // LED flash for debug
179 DEBUG_LED_CONFIG;
180 DEBUG_LED_OFF;
181#endif
182 }
183
184#ifdef MOUSEKEY_ENABLE
185 mousekey_send();
186#endif
187
188#ifdef PS2_MOUSE_ENABLE
189 // TODO: should comform new API
190 if (ps2_mouse_read() == 0)
191 ps2_mouse_usb_send();
192#endif
193
194 if (last_leds != host_keyboard_leds()) {
195 keyboard_set_leds(host_keyboard_leds());
196 last_leds = host_keyboard_leds();
197 }
198}
199
200void keyboard_set_leds(uint8_t leds)
201{
202 led_set(leds);
203}
diff --git a/common/keyboard.h b/common/keyboard.h
new file mode 100644
index 000000000..988dac36e
--- /dev/null
+++ b/common/keyboard.h
@@ -0,0 +1,28 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef KEYBOARD_H
19#define KEYBOARD_H
20
21#include <stdint.h>
22
23
24void keyboard_init(void);
25void keyboard_proc(void);
26void keyboard_set_leds(uint8_t leds);
27
28#endif
diff --git a/common/keymap.h b/common/keymap.h
new file mode 100644
index 000000000..7dfd6c2a1
--- /dev/null
+++ b/common/keymap.h
@@ -0,0 +1,34 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef KEYMAP_H
19#define KEYMAP_H
20
21#include <stdint.h>
22#include <stdbool.h>
23
24
25/* keycode in specific layer */
26uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col);
27
28/* layer to move during press Fn key */
29uint8_t keymap_fn_layer(uint8_t fn_bits);
30
31/* keycode to send when release Fn key without using */
32uint8_t keymap_fn_keycode(uint8_t fn_bits);
33
34#endif
diff --git a/common/layer.c b/common/layer.c
new file mode 100644
index 000000000..0854eede0
--- /dev/null
+++ b/common/layer.c
@@ -0,0 +1,207 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "keymap.h"
19#include "host.h"
20#include "debug.h"
21#include "timer.h"
22#include "usb_keycodes.h"
23#include "layer.h"
24
25
26/*
27 * Parameters:
28 * SWITCH_DELAY |=======|
29 * SEND_FN_TERM |================|
30 *
31 * Fn key processing cases:
32 * 1. release Fn after SEND_FN_TERM.
33 * Layer sw ___________|~~~~~~~~~~~|___
34 * Fn press ___|~~~~~~~~~~~~~~~~~~~|___
35 * Fn send ___________________________
36 *
37 * 2. release Fn during SEND_FN_TERM.(not layer used)
38 * Layer sw ___________|~~~~~~|________
39 * Fn press ___|~~~~~~~~~~~~~~|________
40 * Fn key send __________________|~|______
41 * other key press ___________________________
42 * other key send ___________________________
43 *
44 * 3. release Fn during SEND_FN_TERM.(layer used)
45 * Layer sw ___________|~~~~~~|________
46 * Fn press ___|~~~~~~~~~~~~~~|________
47 * Fn key send ___________________________
48 * Fn send ___________________________
49 * other key press _____________|~~|__________
50 * other key send _____________|~~|__________
51 *
52 * 4. press other key during SWITCH_DELAY.
53 * Layer sw ___________________________
54 * Fn key press ___|~~~~~~~~~|_____________
55 * Fn key send ______|~~~~~~|_____________
56 * other key press ______|~~~|________________
57 * other key send _______|~~|________________
58 *
59 * 5. press Fn while press other key.
60 * Layer sw ___________________________
61 * Fn key press ___|~~~~~~~~~|_____________
62 * Fn key send ___|~~~~~~~~~|_____________
63 * other key press ~~~~~~~|___________________
64 * other key send ~~~~~~~|___________________
65 *
66 * 6. press Fn twice quickly and keep holding down.(repeat)
67 * Layer sw ___________________________
68 * Fn key press ___|~|____|~~~~~~~~~~~~~~~~
69 * Fn key send _____|~|__|~~~~~~~~~~~~~~~~
70 */
71
72// LAYER_SWITCH_DELAY: prevent from moving to new layer
73#ifndef LAYER_SWITCH_DELAY
74# define LAYER_SWITCH_DELAY 150
75#endif
76
77// LAYER_SEND_FN_TERM: send keycode if release key in this term
78#ifndef LAYER_SEND_FN_TERM
79# define LAYER_SEND_FN_TERM 500
80#endif
81
82
83uint8_t default_layer = 0;
84uint8_t current_layer = 0;
85
86static bool layer_used = false;
87static uint8_t new_layer(uint8_t fn_bits);
88
89
90uint8_t layer_get_keycode(uint8_t row, uint8_t col)
91{
92 uint8_t code = keymap_get_keycode(current_layer, row, col);
93 // normal key or mouse key
94 if ((IS_KEY(code) || IS_MOUSEKEY(code))) {
95 layer_used = true;
96 }
97 return code;
98}
99
100// bit substract b from a
101#define BIT_SUBST(a, b) (a&(a^b))
102void layer_switching(uint8_t fn_bits)
103{
104 // layer switching
105 static uint8_t last_fn = 0;
106 static uint8_t last_mods = 0;
107 static uint16_t last_timer = 0;
108 static uint8_t sent_fn = 0;
109
110 if (fn_bits == last_fn) { // Fn state is not changed
111 if (fn_bits == 0) {
112 // do nothing
113 } else {
114 if (!keymap_fn_keycode(BIT_SUBST(fn_bits, sent_fn)) ||
115 timer_elapsed(last_timer) > LAYER_SWITCH_DELAY) {
116 uint8_t _layer_to_switch = new_layer(BIT_SUBST(fn_bits, sent_fn));
117 if (current_layer != _layer_to_switch) { // not switch layer yet
118 debug("Fn case: 1,2,3(LAYER_SWITCH_DELAY passed)\n");
119 debug("Switch Layer: "); debug_hex(current_layer);
120 current_layer = _layer_to_switch;
121 layer_used = false;
122 debug(" -> "); debug_hex(current_layer); debug("\n");
123 }
124 } else {
125 if (host_has_anykey()) { // other keys is pressed
126 uint8_t _fn_to_send = BIT_SUBST(fn_bits, sent_fn);
127 if (_fn_to_send) {
128 debug("Fn case: 4(press other key during SWITCH_DELAY.)\n");
129 // send only Fn key first
130 uint8_t tmp_mods = keyboard_report->mods;
131 host_add_code(keymap_fn_keycode(_fn_to_send));
132 host_set_mods(last_mods);
133 host_send_keyboard_report();
134 host_set_mods(tmp_mods);
135 host_del_code(keymap_fn_keycode(_fn_to_send));
136 sent_fn |= _fn_to_send;
137 }
138 }
139 }
140 // add Fn keys to send
141 //host_add_code(keymap_fn_keycode(fn_bits&sent_fn)); // TODO: do all Fn keys
142 }
143 } else { // Fn state is changed(edge)
144 uint8_t fn_changed = 0;
145
146 debug("fn_bits: "); debug_bin(fn_bits); debug("\n");
147 debug("sent_fn: "); debug_bin(sent_fn); debug("\n");
148 debug("last_fn: "); debug_bin(last_fn); debug("\n");
149 debug("last_mods: "); debug_hex(last_mods); debug("\n");
150 debug("last_timer: "); debug_hex16(last_timer); debug("\n");
151 debug("timer_count: "); debug_hex16(timer_count); debug("\n");
152
153 // pressed Fn
154 if ((fn_changed = BIT_SUBST(fn_bits, last_fn))) {
155 debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
156 if (host_has_anykey()) {
157 debug("Fn case: 5(pressed Fn with other key)\n");
158 sent_fn |= fn_changed;
159 } else if (fn_changed & sent_fn) { // pressed same Fn in a row
160 if (timer_elapsed(last_timer) > LAYER_SEND_FN_TERM) {
161 debug("Fn case: 6(not repeat)\n");
162 // time passed: not repeate
163 sent_fn &= ~fn_changed;
164 } else {
165 debug("Fn case: 6(repeat)\n");
166 }
167 }
168 }
169 // released Fn
170 if ((fn_changed = BIT_SUBST(last_fn, fn_bits))) {
171 debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
172 if (timer_elapsed(last_timer) < LAYER_SEND_FN_TERM) {
173 if (!layer_used && BIT_SUBST(fn_changed, sent_fn)) {
174 debug("Fn case: 2(send Fn one shot: released Fn during LAYER_SEND_FN_TERM)\n");
175 // send only Fn key first
176 uint8_t tmp_mods = keyboard_report->mods;
177 host_add_code(keymap_fn_keycode(fn_changed));
178 host_set_mods(last_mods);
179 host_send_keyboard_report();
180 host_set_mods(tmp_mods);
181 host_del_code(keymap_fn_keycode(fn_changed));
182 sent_fn |= fn_changed;
183 }
184 }
185 debug("Switch Layer(released Fn): "); debug_hex(current_layer);
186 current_layer = new_layer(BIT_SUBST(fn_bits, sent_fn));
187 debug(" -> "); debug_hex(current_layer); debug("\n");
188 }
189
190 layer_used = false;
191 last_fn = fn_bits;
192 last_mods = keyboard_report->mods;
193 last_timer = timer_read();
194 }
195 // send Fn keys
196 for (uint8_t i = 0; i < 8; i++) {
197 if ((sent_fn & fn_bits) & (1<<i)) {
198 host_add_code(keymap_fn_keycode(1<<i));
199 }
200 }
201}
202
203inline
204static uint8_t new_layer(uint8_t fn_bits)
205{
206 return (fn_bits ? keymap_fn_layer(fn_bits) : default_layer);
207}
diff --git a/common/layer.h b/common/layer.h
new file mode 100644
index 000000000..d9e8cebb8
--- /dev/null
+++ b/common/layer.h
@@ -0,0 +1,32 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef LAYER_H
19#define LAYER_H 1
20
21#include <stdint.h>
22
23extern uint8_t default_layer;
24extern uint8_t current_layer;
25
26/* return keycode for switch */
27uint8_t layer_get_keycode(uint8_t row, uint8_t col);
28
29/* process layer switching */
30void layer_switching(uint8_t fn_bits);
31
32#endif
diff --git a/common/led.h b/common/led.h
new file mode 100644
index 000000000..402a247b9
--- /dev/null
+++ b/common/led.h
@@ -0,0 +1,33 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef LED_H
19#define LED_H
20#include "stdint.h"
21
22
23/* keyboard LEDs */
24#define USB_LED_NUM_LOCK 0
25#define USB_LED_CAPS_LOCK 1
26#define USB_LED_SCROLL_LOCK 2
27#define USB_LED_COMPOSE 3
28#define USB_LED_KANA 4
29
30
31void led_set(uint8_t usb_led);
32
33#endif
diff --git a/common/matrix.h b/common/matrix.h
new file mode 100644
index 000000000..c4b2cab51
--- /dev/null
+++ b/common/matrix.h
@@ -0,0 +1,49 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef MATRIX_H
19#define MATRIX_H
20
21#include <stdbool.h>
22
23/* number of matrix rows */
24uint8_t matrix_rows(void);
25/* number of matrix columns */
26uint8_t matrix_cols(void);
27/* intialize matrix for scaning. should be called once. */
28void matrix_init(void);
29/* scan all key states on matrix */
30uint8_t matrix_scan(void);
31/* whether modified from previous scan. used after matrix_scan. */
32bool matrix_is_modified(void);
33/* whether ghosting occur on matrix. */
34bool matrix_has_ghost(void);
35/* whether a swtich is on */
36bool matrix_is_on(uint8_t row, uint8_t col);
37/* matrix state on row */
38#if (MATRIX_COLS <= 8)
39uint8_t matrix_get_row(uint8_t row);
40#else
41uint16_t matrix_get_row(uint8_t row);
42#endif
43/* count keys pressed */
44uint8_t matrix_key_count(void);
45/* print matrix for debug */
46void matrix_print(void);
47
48
49#endif
diff --git a/common/mousekey.c b/common/mousekey.c
new file mode 100755
index 000000000..76bd0fd36
--- /dev/null
+++ b/common/mousekey.c
@@ -0,0 +1,132 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <stdint.h>
19#include <util/delay.h>
20#include "usb_keycodes.h"
21#include "host.h"
22#include "timer.h"
23#include "print.h"
24#include "debug.h"
25#include "mousekey.h"
26
27
28static report_mouse_t report;
29static report_mouse_t report_prev;
30
31static uint8_t mousekey_repeat = 0;
32
33static void mousekey_debug(void);
34
35
36/*
37 * TODO: fix acceleration algorithm
38 * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
39 */
40#ifndef MOUSEKEY_DELAY_TIME
41# define MOUSEKEY_DELAY_TIME 255
42#endif
43
44// acceleration parameters
45uint8_t mousekey_move_unit = 2;
46uint8_t mousekey_resolution = 5;
47
48
49static inline uint8_t move_unit(void)
50{
51 uint16_t unit = 5 + mousekey_repeat*2;
52 return (unit > 63 ? 63 : unit);
53}
54
55void mousekey_decode(uint8_t code)
56{
57 if (code == KB_MS_UP) report.y = -move_unit();
58 else if (code == KB_MS_DOWN) report.y = move_unit();
59 else if (code == KB_MS_LEFT) report.x = -move_unit();
60 else if (code == KB_MS_RIGHT) report.x = move_unit();
61 else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1;
62 else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2;
63 else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3;
64 else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4;
65 else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5;
66 else if (code == KB_MS_WH_UP) report.v += move_unit()/4;
67 else if (code == KB_MS_WH_DOWN) report.v -= move_unit()/4;
68 else if (code == KB_MS_WH_LEFT) report.h -= move_unit()/4;
69 else if (code == KB_MS_WH_RIGHT)report.h += move_unit()/4;
70}
71
72bool mousekey_changed(void)
73{
74 return (report.buttons != report_prev.buttons ||
75 report.x || report.y || report.v || report.h);
76}
77
78void mousekey_send(void)
79{
80 static uint16_t last_timer = 0;
81
82 if (!mousekey_changed()) {
83 mousekey_repeat = 0;
84 mousekey_clear_report();
85 return;
86 }
87
88 // send immediately when buttun state is changed
89 if (report.buttons == report_prev.buttons) {
90 if (timer_elapsed(last_timer) < 100) {
91 mousekey_clear_report();
92 return;
93 }
94 }
95
96 if (mousekey_repeat != 0xFF) {
97 mousekey_repeat++;
98 }
99
100 if (report.x && report.y) {
101 report.x *= 0.7;
102 report.y *= 0.7;
103 }
104
105 mousekey_debug();
106 host_mouse_send(&report);
107 report_prev = report;
108 last_timer = timer_read();
109 mousekey_clear_report();
110}
111
112void mousekey_clear_report(void)
113{
114 report.buttons = 0;
115 report.x = 0;
116 report.y = 0;
117 report.v = 0;
118 report.h = 0;
119}
120
121static void mousekey_debug(void)
122{
123 if (!debug_mouse) return;
124 print("mousekey[btn|x y v h]: ");
125 phex(report.buttons); print("|");
126 phex(report.x); print(" ");
127 phex(report.y); print(" ");
128 phex(report.v); print(" ");
129 phex(report.h);
130 phex(mousekey_repeat);
131 print("\n");
132}
diff --git a/common/mousekey.h b/common/mousekey.h
new file mode 100644
index 000000000..c2c24e9fa
--- /dev/null
+++ b/common/mousekey.h
@@ -0,0 +1,29 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef MOUSEKEY_H
19#define MOUSEKEY_H
20
21#include <stdbool.h>
22#include "host.h"
23
24void mousekey_decode(uint8_t code);
25bool mousekey_changed(void);
26void mousekey_send(void);
27void mousekey_clear_report(void);
28
29#endif
diff --git a/common/print.c b/common/print.c
new file mode 100644
index 000000000..558181ea7
--- /dev/null
+++ b/common/print.c
@@ -0,0 +1,93 @@
1/* Very basic print functions, intended to be used with usb_debug_only.c
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2008 PJRC.COM, LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#include <avr/io.h>
25#include <avr/pgmspace.h>
26#include "print.h"
27#include "sendchar.h"
28
29
30bool print_enable = false;
31
32void print_S(const char *s)
33{
34 if (!print_enable) return;
35 char c;
36
37 while (1) {
38 c = *s++;
39 if (!c) break;
40 if (c == '\n') sendchar('\r');
41 sendchar(c);
42 }
43}
44
45void print_P(const char *s)
46{
47 if (!print_enable) return;
48 char c;
49
50 while (1) {
51 c = pgm_read_byte(s++);
52 if (!c) break;
53 if (c == '\n') sendchar('\r');
54 sendchar(c);
55 }
56}
57
58void phex1(unsigned char c)
59{
60 if (!print_enable) return;
61 sendchar(c + ((c < 10) ? '0' : 'A' - 10));
62}
63
64void phex(unsigned char c)
65{
66 if (!print_enable) return;
67 phex1(c >> 4);
68 phex1(c & 15);
69}
70
71void phex16(unsigned int i)
72{
73 if (!print_enable) return;
74 phex(i >> 8);
75 phex(i);
76}
77
78
79void pbin(unsigned char c)
80{
81 if (!print_enable) return;
82 for (int i = 7; i >= 0; i--) {
83 sendchar((c & (1<<i)) ? '1' : '0');
84 }
85}
86
87void pbin_reverse(unsigned char c)
88{
89 if (!print_enable) return;
90 for (int i = 0; i < 8; i++) {
91 sendchar((c & (1<<i)) ? '1' : '0');
92 }
93}
diff --git a/common/print.h b/common/print.h
new file mode 100644
index 000000000..686fa89ac
--- /dev/null
+++ b/common/print.h
@@ -0,0 +1,45 @@
1/* Very basic print functions, intended to be used with usb_debug_only.c
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2008 PJRC.COM, LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef PRINT_H__
25#define PRINT_H__ 1
26
27#include <stdint.h>
28#include <stdbool.h>
29#include <avr/pgmspace.h>
30
31
32extern bool print_enable;
33
34// this macro allows you to write print("some text") and
35// the string is automatically placed into flash memory :)
36#define print(s) print_P(PSTR(s))
37
38void print_S(const char *s);
39void print_P(const char *s);
40void phex(unsigned char c);
41void phex16(unsigned int i);
42void pbin(unsigned char c);
43void pbin_reverse(unsigned char c);
44
45#endif
diff --git a/common/report.h b/common/report.h
new file mode 100644
index 000000000..b85b86c5f
--- /dev/null
+++ b/common/report.h
@@ -0,0 +1,96 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef REPORT_H
19#define REPORT_H
20
21#include <stdint.h>
22
23
24/* report id */
25#define REPORT_ID_MOUSE 1
26#define REPORT_ID_SYSTEM 2
27#define REPORT_ID_CONSUMER 3
28
29/* mouse buttons */
30#define MOUSE_BTN1 (1<<0)
31#define MOUSE_BTN2 (1<<1)
32#define MOUSE_BTN3 (1<<2)
33#define MOUSE_BTN4 (1<<3)
34#define MOUSE_BTN5 (1<<4)
35
36// Consumer Page(0x0C)
37// following are supported by Windows: http://msdn.microsoft.com/en-us/windows/hardware/gg463372.aspx
38#define AUDIO_MUTE 0x00E2
39#define AUDIO_VOL_UP 0x00E9
40#define AUDIO_VOL_DOWN 0x00EA
41#define TRANSPORT_NEXT_TRACK 0x00B5
42#define TRANSPORT_PREV_TRACK 0x00B6
43#define TRANSPORT_STOP 0x00B7
44#define TRANSPORT_PLAY_PAUSE 0x00CD
45#define AL_CC_CONFIG 0x0183
46#define AL_EMAIL 0x018A
47#define AL_CALCULATOR 0x0192
48#define AL_LOCAL_BROWSER 0x0194
49#define AC_SEARCH 0x0221
50#define AC_HOME 0x0223
51#define AC_BACK 0x0224
52#define AC_FORWARD 0x0225
53#define AC_STOP 0x0226
54#define AC_REFRESH 0x0227
55#define AC_BOOKMARKS 0x022A
56// supplement for Bluegiga iWRAP HID(not supported by Windows?)
57#define AL_LOCK 0x019E
58#define TRANSPORT_RECORD 0x00B2
59#define TRANSPORT_REWIND 0x00B4
60#define TRANSPORT_EJECT 0x00B8
61#define AC_MINIMIZE 0x0206
62
63// Generic Desktop Page(0x01)
64#define SYSTEM_POWER_DOWN 0x0081
65#define SYSTEM_SLEEP 0x0082
66#define SYSTEM_WAKE_UP 0x0083
67
68
69// key report size(NKRO or boot mode)
70#if defined(HOST_PJRC)
71# include "usb.h"
72# if defined(KBD2_REPORT_KEYS) && KBD2_REPORT_KEYS > KBD_REPORT_KEYS
73# define REPORT_KEYS KBD2_REPORT_KEYS
74# else
75# define REPORT_KEYS KBD_REPORT_KEYS
76# endif
77#else
78# define REPORT_KEYS 6
79#endif
80
81typedef struct {
82 uint8_t mods;
83 uint8_t rserved;
84 uint8_t keys[REPORT_KEYS];
85} report_keyboard_t;
86
87typedef struct {
88 uint8_t report_id;
89 uint8_t buttons;
90 int8_t x;
91 int8_t y;
92 int8_t v;
93 int8_t h;
94} report_mouse_t;
95
96#endif
diff --git a/common/sendchar.h b/common/sendchar.h
new file mode 100644
index 000000000..7c81303c7
--- /dev/null
+++ b/common/sendchar.h
@@ -0,0 +1,27 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef SENDCHAR_H
19#define SENDCHAR_H
20
21#include <stdint.h>
22
23
24/* transmit a character. return 0 on success, -1 on error. */
25int8_t sendchar(uint8_t c);
26
27#endif
diff --git a/common/sendchar_null.c b/common/sendchar_null.c
new file mode 100644
index 000000000..293330622
--- /dev/null
+++ b/common/sendchar_null.c
@@ -0,0 +1,23 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#include "sendchar.h"
18
19
20int8_t sendchar(uint8_t c)
21{
22 return 0;
23}
diff --git a/common/sendchar_uart.c b/common/sendchar_uart.c
new file mode 100644
index 000000000..0241859eb
--- /dev/null
+++ b/common/sendchar_uart.c
@@ -0,0 +1,25 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#include "uart.h"
18#include "sendchar.h"
19
20
21int8_t sendchar(uint8_t c)
22{
23 uart_putchar(c);
24 return 0;
25}
diff --git a/common/timer.c b/common/timer.c
new file mode 100644
index 000000000..48a38c9b6
--- /dev/null
+++ b/common/timer.c
@@ -0,0 +1,89 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <avr/io.h>
19#include <avr/interrupt.h>
20#include <stdint.h>
21#include "timer.h"
22
23
24// counter resolution 1ms
25volatile uint16_t timer_count = 0;
26
27void timer_init(void)
28{
29 // Timer0 CTC mode
30 TCCR0A = 0x02;
31
32#if TIMER_PRESCALER == 1
33 TCCR0B = 0x01;
34#elif TIMER_PRESCALER == 8
35 TCCR0B = 0x02;
36#elif TIMER_PRESCALER == 64
37 TCCR0B = 0x03;
38#elif TIMER_PRESCALER == 256
39 TCCR0B = 0x04;
40#elif TIMER_PRESCALER == 1024
41 TCCR0B = 0x05;
42#else
43# error "Timer prescaler value is NOT vaild."
44#endif
45
46 OCR0A = TIMER_RAW_TOP;
47 TIMSK0 = (1<<OCIE0A);
48}
49
50inline
51void timer_clear(void)
52{
53 uint8_t sreg = SREG;
54 cli();
55 timer_count = 0;
56 SREG = sreg;
57}
58
59inline
60uint16_t timer_read(void)
61{
62 uint16_t t;
63
64 uint8_t sreg = SREG;
65 cli();
66 t = timer_count;
67 SREG = sreg;
68
69 return t;
70}
71
72inline
73uint16_t timer_elapsed(uint16_t last)
74{
75 uint16_t t;
76
77 uint8_t sreg = SREG;
78 cli();
79 t = timer_count;
80 SREG = sreg;
81
82 return TIMER_DIFF_MS(t, last);
83}
84
85// excecuted once per 1ms.(excess for just timer count?)
86ISR(TIMER0_COMPA_vect)
87{
88 timer_count++;
89}
diff --git a/common/timer.h b/common/timer.h
new file mode 100644
index 000000000..f9e8181e6
--- /dev/null
+++ b/common/timer.h
@@ -0,0 +1,53 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef TIMER_H
19#define TIMER_H 1
20
21#include <stdint.h>
22
23#ifndef TIMER_PRESCALER
24# if F_CPU > 16000000
25# define TIMER_PRESCALER 256
26# elif F_CPU >= 4000000
27# define TIMER_PRESCALER 64
28# else
29# define TIMER_PRESCALER 8
30# endif
31#endif
32#define TIMER_RAW_FREQ (F_CPU/TIMER_PRESCALER)
33#define TIMER_RAW TCNT0
34#define TIMER_RAW_TOP (TIMER_RAW_FREQ/1000)
35
36#if (TIMER_RAW_TOP > 255)
37# error "Timer0 can't count 1ms at this clock freq. Use larger prescaler."
38#endif
39
40#define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a))
41#define TIMER_DIFF_RAW(a, b) TIMER_DIFF(a, b, UINT8_MAX)
42#define TIMER_DIFF_MS(a, b) TIMER_DIFF(a, b, UINT16_MAX)
43
44
45extern volatile uint16_t timer_count;
46
47
48void timer_init(void);
49void timer_clear(void);
50uint16_t timer_read(void);
51uint16_t timer_elapsed(uint16_t last);
52
53#endif
diff --git a/common/uart.c b/common/uart.c
new file mode 100644
index 000000000..c17649b08
--- /dev/null
+++ b/common/uart.c
@@ -0,0 +1,129 @@
1// TODO: Teensy support(ATMega32u4/AT90USB128)
2// Fixed for Arduino Duemilanove ATmega168p by Jun Wako
3/* UART Example for Teensy USB Development Board
4 * http://www.pjrc.com/teensy/
5 * Copyright (c) 2009 PJRC.COM, LLC
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26// Version 1.0: Initial Release
27// Version 1.1: Add support for Teensy 2.0, minor optimizations
28
29
30#include <avr/io.h>
31#include <avr/interrupt.h>
32
33#include "uart.h"
34
35// These buffers may be any size from 2 to 256 bytes.
36#define RX_BUFFER_SIZE 64
37#define TX_BUFFER_SIZE 40
38
39static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
40static volatile uint8_t tx_buffer_head;
41static volatile uint8_t tx_buffer_tail;
42static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
43static volatile uint8_t rx_buffer_head;
44static volatile uint8_t rx_buffer_tail;
45
46// Initialize the UART
47void uart_init(uint32_t baud)
48{
49 cli();
50 UBRR0 = (F_CPU / 4 / baud - 1) / 2;
51 UCSR0A = (1<<U2X0);
52 UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
53 UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
54 tx_buffer_head = tx_buffer_tail = 0;
55 rx_buffer_head = rx_buffer_tail = 0;
56 sei();
57}
58
59// Transmit a byte
60void uart_putchar(uint8_t c)
61{
62 uint8_t i;
63
64 i = tx_buffer_head + 1;
65 if (i >= TX_BUFFER_SIZE) i = 0;
66 while (tx_buffer_tail == i) ; // wait until space in buffer
67 //cli();
68 tx_buffer[i] = c;
69 tx_buffer_head = i;
70 UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (1<<UDRIE0);
71 //sei();
72}
73
74// Receive a byte
75uint8_t uart_getchar(void)
76{
77 uint8_t c, i;
78
79 while (rx_buffer_head == rx_buffer_tail) ; // wait for character
80 i = rx_buffer_tail + 1;
81 if (i >= RX_BUFFER_SIZE) i = 0;
82 c = rx_buffer[i];
83 rx_buffer_tail = i;
84 return c;
85}
86
87// Return the number of bytes waiting in the receive buffer.
88// Call this before uart_getchar() to check if it will need
89// to wait for a byte to arrive.
90uint8_t uart_available(void)
91{
92 uint8_t head, tail;
93
94 head = rx_buffer_head;
95 tail = rx_buffer_tail;
96 if (head >= tail) return head - tail;
97 return RX_BUFFER_SIZE + head - tail;
98}
99
100// Transmit Interrupt
101ISR(USART_UDRE_vect)
102{
103 uint8_t i;
104
105 if (tx_buffer_head == tx_buffer_tail) {
106 // buffer is empty, disable transmit interrupt
107 UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
108 } else {
109 i = tx_buffer_tail + 1;
110 if (i >= TX_BUFFER_SIZE) i = 0;
111 UDR0 = tx_buffer[i];
112 tx_buffer_tail = i;
113 }
114}
115
116// Receive Interrupt
117ISR(USART_RX_vect)
118{
119 uint8_t c, i;
120
121 c = UDR0;
122 i = rx_buffer_head + 1;
123 if (i >= RX_BUFFER_SIZE) i = 0;
124 if (i != rx_buffer_tail) {
125 rx_buffer[i] = c;
126 rx_buffer_head = i;
127 }
128}
129
diff --git a/common/uart.h b/common/uart.h
new file mode 100644
index 000000000..41136a396
--- /dev/null
+++ b/common/uart.h
@@ -0,0 +1,11 @@
1#ifndef _uart_included_h_
2#define _uart_included_h_
3
4#include <stdint.h>
5
6void uart_init(uint32_t baud);
7void uart_putchar(uint8_t c);
8uint8_t uart_getchar(void);
9uint8_t uart_available(void);
10
11#endif
diff --git a/common/usb_keycodes.h b/common/usb_keycodes.h
new file mode 100644
index 000000000..9b6cce153
--- /dev/null
+++ b/common/usb_keycodes.h
@@ -0,0 +1,423 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18/*
19 * Key codes: HID Keyboard/Keypad Page(0x07)
20 * http://www.usb.org/developers/devclass_docs/Hut1_12.pdf
21 */
22#ifndef USB_KEYCODES_H
23#define USB_KEYCODES_H
24
25
26#define IS_ERROR(code) (KB_ROLL_OVER <= (code) && (code) <= KB_UNDEFINED)
27#define IS_KEY(code) (KB_A <= (code) && (code) <= KB_EXSEL)
28#define IS_MOD(code) (KB_LCTRL <= (code) && (code) <= KB_RGUI)
29#define IS_FN(code) (KB_FN0 <= (code) && (code) <= KB_FN7)
30#define IS_MOUSEKEY(code) (KB_MS_UP <= (code) && (code) <= KB_MS_WH_RIGHT)
31#define IS_MOUSEKEY_MOVE(code) (KB_MS_UP <= (code) && (code) <= KB_MS_RIGHT)
32#define IS_MOUSEKEY_BUTTON(code) (KB_MS_BTN1 <= (code) && (code) <= KB_MS_BTN5)
33#define IS_MOUSEKEY_WHEEL(code) (KB_MS_WH_UP <= (code) && (code) <= KB_MS_WH_RIGHT)
34
35#define MOD_BIT(code) (1<<((code) & 0x07))
36#define FN_BIT(code) (1<<((code) - KB_FN0))
37
38
39/* Short names */
40#define KB_LCTL KB_LCTRL
41#define KB_RCTL KB_RCTRL
42#define KB_LSFT KB_LSHIFT
43#define KB_RSFT KB_RSHIFT
44#define KB_ESC KB_ESCAPE
45#define KB_BSPC KB_BSPACE
46#define KB_ENT KB_ENTER
47#define KB_DEL KB_DELETE
48#define KB_INS KB_INSERT
49#define KB_CAPS KB_CAPSLOCK
50#define KB_RGHT KB_RIGHT
51#define KB_PGDN KB_PGDOWN
52#define KB_PSCR KB_PSCREEN
53#define KB_SLCK KB_SCKLOCK
54#define KB_PAUS KB_PAUSE
55#define KB_BRK KB_PAUSE
56#define KB_NLCK KB_NUMLOCK
57#define KB_SPC KB_SPACE
58#define KB_MINS KB_MINUS
59#define KB_EQL KB_EQUAL
60#define KB_GRV KB_GRAVE
61#define KB_RBRC KB_RBRACKET
62#define KB_LBRC KB_LBRACKET
63#define KB_COMM KB_COMMA
64#define KB_BSLS KB_BSLASH
65#define KB_SLSH KB_SLASH
66#define KB_SCLN KB_SCOLON
67#define KB_QUOT KB_QUOTE
68#define KB_APP KB_APPLICATION
69#define KB_NUHS KB_NONUS_HASH
70#define KB_NUBS KB_NONUS_BSLASH
71#define KB_ERAS KB_ALT_ERASE,
72#define KB_CLR KB_CLEAR
73/* for Japanese */
74#define KB_ZKHK KB_GRAVE
75#define KB_RO KB_INT1
76#define KB_KANA KB_INT2
77#define KB_JYEN KB_INT3
78#define KB_HENK KB_INT4
79#define KB_MHEN KB_INT5
80/* Keypad */
81#define KB_P1 KB_KP_1
82#define KB_P2 KB_KP_2
83#define KB_P3 KB_KP_3
84#define KB_P4 KB_KP_4
85#define KB_P5 KB_KP_5
86#define KB_P6 KB_KP_6
87#define KB_P7 KB_KP_7
88#define KB_P8 KB_KP_8
89#define KB_P9 KB_KP_9
90#define KB_P0 KB_KP_0
91#define KB_PDOT KB_KP_DOT
92#define KB_PCMM KB_KP_COMMA
93#define KB_PSLS KB_KP_SLASH
94#define KB_PAST KB_KP_ASTERISK
95#define KB_PMNS KB_KP_MINUS
96#define KB_PPLS KB_KP_PLUS
97#define KB_PEQL KB_KP_EQUAL
98#define KB_PENT KB_KP_ENTER
99/* Mousekey */
100#define KB_MS_U KB_MS_UP
101#define KB_MS_D KB_MS_DOWN
102#define KB_MS_L KB_MS_LEFT
103#define KB_MS_R KB_MS_RIGHT
104#define KB_BTN1 KB_MS_BTN1
105#define KB_BTN2 KB_MS_BTN2
106#define KB_BTN3 KB_MS_BTN3
107#define KB_BTN4 KB_MS_BTN4
108#define KB_BTN5 KB_MS_BTN5
109#define KB_WH_U KB_MS_WH_UP
110#define KB_WH_D KB_MS_WH_DOWN
111#define KB_WH_L KB_MS_WH_LEFT
112#define KB_WH_R KB_MS_WH_RIGHT
113/* Sytem Control & Consumer usage */
114#define KB_PWR KB_SYSTEM_POWER
115#define KB_SLEP KB_SYSTEM_SLEEP
116#define KB_WAKE KB_SYSTEM_WAKE
117#define KB_MUTE KB_AUDIO_MUTE
118#define KB_VOLU KB_AUDIO_VOL_UP
119#define KB_VOLD KB_AUDIO_VOL_DOWN
120#define KB_MNXT KB_MEDIA_NEXT_TRACK
121#define KB_MPRV KB_MEDIA_PREV_TRACK
122#define KB_MSTP KB_MEDIA_STOP
123#define KB_MPLY KB_MEDIA_PLAY_PAUSE
124#define KB_MSEL KB_MEDIA_SELECT
125#define KB_MAIL KB_MAIL
126#define KB_CALC KB_CALCULATOR
127#define KB_MYCM KB_MY_COMPUTER
128#define KB_WSCH KB_WWW_SEARCH
129#define KB_WHOM KB_WWW_HOME
130#define KB_WBAK KB_WWW_BACK
131#define KB_WFWD KB_WWW_FORWARD
132#define KB_WSTP KB_WWW_STOP
133#define KB_WREF KB_WWW_REFRESH
134#define KB_WFAV KB_WWW_FAVORITES
135
136
137/* Special keycode */
138enum special_keycodes {
139 /* System Control */
140 KB_SYSTEM_POWER = 0xB0,
141 KB_SYSTEM_SLEEP,
142 KB_SYSTEM_WAKE,
143
144 /* Consumer Page */
145 KB_AUDIO_MUTE,
146 KB_AUDIO_VOL_UP,
147 KB_AUDIO_VOL_DOWN,
148 KB_MEDIA_NEXT_TRACK,
149 KB_MEDIA_PREV_TRACK,
150 KB_MEDIA_STOP,
151 KB_MEDIA_PLAY_PAUSE,
152 KB_MEDIA_SELECT,
153 KB_MAIL,
154 KB_CALCULATOR,
155 KB_MY_COMPUTER,
156 KB_WWW_SEARCH,
157 KB_WWW_HOME,
158 KB_WWW_BACK, /* 0xC0 */
159 KB_WWW_FORWARD,
160 KB_WWW_STOP,
161 KB_WWW_REFRESH,
162 KB_WWW_FAVORITES,
163
164 /* reserve 0xE0-E7 for Modifiers */
165
166 /* Layer Switching */
167 KB_FN0 = 0xE8,
168 KB_FN1,
169 KB_FN2,
170 KB_FN3,
171 KB_FN4,
172 KB_FN5,
173 KB_FN6,
174 KB_FN7,
175
176 /* Mousekey */
177 KB_MS_UP = 0xF0,
178 KB_MS_DOWN,
179 KB_MS_LEFT,
180 KB_MS_RIGHT,
181 KB_MS_BTN1,
182 KB_MS_BTN2,
183 KB_MS_BTN3,
184 KB_MS_BTN4,
185 KB_MS_BTN5,
186 /* Mousekey wheel */
187 KB_MS_WH_UP,
188 KB_MS_WH_DOWN,
189 KB_MS_WH_LEFT,
190 KB_MS_WH_RIGHT,
191};
192
193enum keycodes {
194 KB_NO = 0,
195 KB_ROLL_OVER,
196 KB_POST_FAIL,
197 KB_UNDEFINED,
198 KB_A,
199 KB_B,
200 KB_C,
201 KB_D,
202 KB_E,
203 KB_F,
204 KB_G,
205 KB_H,
206 KB_I,
207 KB_J,
208 KB_K,
209 KB_L,
210 KB_M, /* 0x10 */
211 KB_N,
212 KB_O,
213 KB_P,
214 KB_Q,
215 KB_R,
216 KB_S,
217 KB_T,
218 KB_U,
219 KB_V,
220 KB_W,
221 KB_X,
222 KB_Y,
223 KB_Z,
224 KB_1,
225 KB_2,
226 KB_3, /* 0x20 */
227 KB_4,
228 KB_5,
229 KB_6,
230 KB_7,
231 KB_8,
232 KB_9,
233 KB_0,
234 KB_ENTER,
235 KB_ESCAPE,
236 KB_BSPACE,
237 KB_TAB,
238 KB_SPACE,
239 KB_MINUS,
240 KB_EQUAL,
241 KB_LBRACKET,
242 KB_RBRACKET, /* 0x30 */
243 KB_BSLASH, /* \ (and |) */
244 KB_NONUS_HASH, /* Non-US # and ~ */
245 KB_SCOLON, /* ; (and :) */
246 KB_QUOTE, /* ' and " */
247 KB_GRAVE, /* Grave accent and tilde */
248 KB_COMMA, /* , and < */
249 KB_DOT, /* . and > */
250 KB_SLASH, /* / and ? */
251 KB_CAPSLOCK,
252 KB_F1,
253 KB_F2,
254 KB_F3,
255 KB_F4,
256 KB_F5,
257 KB_F6,
258 KB_F7, /* 0x40 */
259 KB_F8,
260 KB_F9,
261 KB_F10,
262 KB_F11,
263 KB_F12,
264 KB_PSCREEN,
265 KB_SCKLOCK,
266 KB_PAUSE,
267 KB_INSERT,
268 KB_HOME,
269 KB_PGUP,
270 KB_DELETE,
271 KB_END,
272 KB_PGDOWN,
273 KB_RIGHT,
274 KB_LEFT, /* 0x50 */
275 KB_DOWN,
276 KB_UP,
277 KB_NUMLOCK,
278 KB_KP_SLASH,
279 KB_KP_ASTERISK,
280 KB_KP_MINUS,
281 KB_KP_PLUS,
282 KB_KP_ENTER,
283 KB_KP_1,
284 KB_KP_2,
285 KB_KP_3,
286 KB_KP_4,
287 KB_KP_5,
288 KB_KP_6,
289 KB_KP_7,
290 KB_KP_8, /* 0x60 */
291 KB_KP_9,
292 KB_KP_0,
293 KB_KP_DOT,
294 KB_NONUS_BSLASH, /* Non-US \ and | */
295 KB_APPLICATION,
296 KB_POWER,
297 KB_KP_EQUAL,
298 KB_F13,
299 KB_F14,
300 KB_F15,
301 KB_F16,
302 KB_F17,
303 KB_F18,
304 KB_F19,
305 KB_F20,
306 KB_F21, /* 0x70 */
307 KB_F22,
308 KB_F23,
309 KB_F24,
310 KB_EXECUTE,
311 KB_HELP,
312 KB_MENU,
313 KB_SELECT,
314 KB_STOP,
315 KB_AGAIN,
316 KB_UNDO,
317 KB_CUT,
318 KB_COPY,
319 KB_PASTE,
320 KB_FIND,
321 KB__MUTE,
322 KB__VOLUP, /* 0x80 */
323 KB__VOLDOWN,
324 KB_LOCKING_CAPS, /* locking Caps Lock */
325 KB_LOCKING_NUM, /* locking Num Lock */
326 KB_LOCKING_SCROLL, /* locking Scroll Lock */
327 KB_KP_COMMA,
328 KB_KP_EQUAL_AS400, /* equal sign on AS/400 */
329 KB_INT1,
330 KB_INT2,
331 KB_INT3,
332 KB_INT4,
333 KB_INT5,
334 KB_INT6,
335 KB_INT7,
336 KB_INT8,
337 KB_INT9,
338 KB_LANG1, /* 0x90 */
339 KB_LANG2,
340 KB_LANG3,
341 KB_LANG4,
342 KB_LANG5,
343 KB_LANG6,
344 KB_LANG7,
345 KB_LANG8,
346 KB_LANG9,
347 KB_ALT_ERASE,
348 KB_SYSREQ,
349 KB_CANCEL,
350 KB_CLEAR,
351 KB_PRIOR,
352 KB_RETURN,
353 KB_SEPARATOR,
354 KB_OUT, /* 0xA0 */
355 KB_OPER,
356 KB_CLEAR_AGAIN,
357 KB_CRSEL,
358 KB_EXSEL,
359
360 /* NOTE: 0xB0-DF are used as special_keycodes */
361#if 0
362 KB_KP_00 = 0xB0,
363 KB_KP_000,
364 KB_THOUSANDS_SEPARATOR,
365 KB_DECIMAL_SEPARATOR,
366 KB_CURRENCY_UNIT,
367 KB_CURRENCY_SUB_UNIT,
368 KB_KP_LPAREN,
369 KB_KP_RPAREN,
370 KB_KP_LCBRACKET, /* { */
371 KB_KP_RCBRACKET, /* } */
372 KB_KP_TAB,
373 KB_KP_BSPACE,
374 KB_KP_A,
375 KB_KP_B,
376 KB_KP_C,
377 KB_KP_D,
378 KB_KP_E, /* 0xC0 */
379 KB_KP_F,
380 KB_KP_XOR,
381 KB_KP_HAT,
382 KB_KP_PERC,
383 KB_KP_LT,
384 KB_KP_GT,
385 KB_KP_AND,
386 KB_KP_LAZYAND,
387 KB_KP_OR,
388 KB_KP_LAZYOR,
389 KB_KP_COLON,
390 KB_KP_HASH,
391 KB_KP_SPACE,
392 KB_KP_ATMARK,
393 KB_KP_EXCLAMATION,
394 KB_KP_MEM_STORE, /* 0xD0 */
395 KB_KP_MEM_RECALL,
396 KB_KP_MEM_CLEAR,
397 KB_KP_MEM_ADD,
398 KB_KP_MEM_SUB,
399 KB_KP_MEM_MUL,
400 KB_KP_MEM_DIV,
401 KB_KP_PLUS_MINUS,
402 KB_KP_CLEAR,
403 KB_KP_CLEAR_ENTRY,
404 KB_KP_BINARY,
405 KB_KP_OCTAL,
406 KB_KP_DECIMAL,
407 KB_KP_HEXADECIMAL,
408#endif
409
410 /* Modifiers */
411 KB_LCTRL = 0xE0,
412 KB_LSHIFT,
413 KB_LALT,
414 KB_LGUI,
415 KB_RCTRL,
416 KB_RSHIFT,
417 KB_RALT,
418 KB_RGUI,
419
420 /* NOTE: 0xE8-FF are used as special_keycodes */
421};
422
423#endif /* USB_KEYCODES_H */
diff --git a/common/util.c b/common/util.c
new file mode 100644
index 000000000..36afdd447
--- /dev/null
+++ b/common/util.c
@@ -0,0 +1,37 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "util.h"
19
20// bit population
21int bitpop(uint8_t bits)
22{
23 int c;
24 for (c = 0; bits; c++)
25 bits &= bits -1;
26 return c;
27}
28
29// most significant on-bit
30int biton(uint8_t bits)
31{
32 int n = 0;
33 if (bits >> 4) { bits >>= 4; n += 4;}
34 if (bits >> 2) { bits >>= 2; n += 2;}
35 if (bits >> 1) { bits >>= 1; n += 1;}
36 return n;
37}
diff --git a/common/util.h b/common/util.h
new file mode 100644
index 000000000..66bccbfa5
--- /dev/null
+++ b/common/util.h
@@ -0,0 +1,34 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef UTIL_H
19#define UTIL_H 1
20
21#include <stdint.h>
22
23// convert to L string
24#define LSTR(s) XLSTR(s)
25#define XLSTR(s) L ## #s
26// convert to string
27#define STR(s) XSTR(s)
28#define XSTR(s) #s
29
30
31int bitpop(uint8_t bits);
32int biton(uint8_t bits);
33
34#endif