]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - ps2_vusb/main.c
added initial support of mousekeys to ps2_vusb
[max/tmk_keyboard.git] / ps2_vusb / main.c
1 /* Name: main.c
2  * Project: hid-mouse, a very simple HID example
3  * Author: Christian Starkjohann
4  * Creation Date: 2008-04-07
5  * Tabsize: 4
6  * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
7  * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
8  * This Revision: $Id: main.c 790 2010-05-30 21:00:26Z cs $
9  */
10 #include <stdint.h>
11 #include <avr/io.h>
12 #include <avr/wdt.h>
13 #include <avr/interrupt.h>  /* for sei() */
14 #include <avr/pgmspace.h>   /* required by usbdrv.h */
15 #include <util/delay.h>     /* for _delay_ms() */
16 #include "usbdrv.h"
17 #include "usart_print.h"        /* This is also an example for using debug macros */
18 #include "usb_keycodes.h"
19 #include "matrix_skel.h"
20 #include "keymap_skel.h"
21 #include "mousekey.h"
22 #include "keyboard.h"
23 #include "layer.h"
24 #include "print.h"
25 #include "debug.h"
26 #include "sendchar.h"
27 #include "host.h"
28 #include "host_vusb.h"
29 #include "timer.h"
30
31
32 int main(void)
33 {
34     wdt_enable(WDTO_1S);
35     /* Even if you don't use the watchdog, turn it off here. On newer devices,
36      * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
37      */
38
39     /* RESET status: all port bits are inputs without pull-up.
40      * That's the way we need D+ and D-. Therefore we don't need any
41      * additional hardware initialization.
42      */
43     odDebugInit();
44     usbInit();
45
46     print_enable = true;
47     //debug_enable = true;
48     timer_init();
49     matrix_init();
50
51     /* enforce re-enumeration, do this while interrupts are disabled! */
52     usbDeviceDisconnect();
53     uint8_t i = 0;
54     while(--i){             /* fake USB disconnect for > 250 ms */
55         wdt_reset();
56         _delay_ms(1);
57     }
58     usbDeviceConnect();
59     sei();
60
61     uint8_t fn_bits = 0;
62     while (1) {                /* main event loop */
63         wdt_reset();
64         usbPoll();
65         host_vusb_keyboard_send();
66
67         matrix_scan();
68         fn_bits = 0;
69         keyboard_swap_report();
70         keyboard_clear_report();
71         mousekey_clear_report();
72         for (int row = 0; row < matrix_rows(); row++) {
73             for (int col = 0; col < matrix_cols(); col++) {
74                 if (!matrix_is_on(row, col)) continue;
75
76                 uint8_t code = layer_get_keycode(row, col);
77                 if (code == KB_NO) {
78                     // do nothing
79                 }
80                 else if (IS_MOD(code)) {
81                     keyboard_add_mod(MOD_BIT(code));
82                 }
83                 else if (IS_KEY(code)) {
84                     keyboard_add_key(code);
85                 }
86                 else if (IS_FN(code)) {
87                     fn_bits |= FN_BIT(code);
88                 }
89                 else if (IS_MOUSEKEY(code)) {
90                     mousekey_decode(code);
91                 }
92                 else {
93                     debug("ignore keycode: "); debug_hex(code); debug("\n");
94                 }
95             }
96         }
97         layer_switching(fn_bits);
98         if (matrix_is_modified()) {
99             keyboard_send();
100         }
101         mousekey_send();
102     }
103 }