]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - ps2_vusb/main.c
PS/2 library receives data partially by interrupt
[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 #define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
32 #define DEBUGP(x) do { PORTC = x; } while (0)
33
34 static uint8_t last_led = 0;
35 int main(void)
36 {
37     DEBUGP_INIT();
38     wdt_enable(WDTO_1S);
39     /* Even if you don't use the watchdog, turn it off here. On newer devices,
40      * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
41      */
42
43     /* RESET status: all port bits are inputs without pull-up.
44      * That's the way we need D+ and D-. Therefore we don't need any
45      * additional hardware initialization.
46      */
47     odDebugInit();
48     usbInit();
49
50     print_enable = true;
51     //debug_enable = true;
52     timer_init();
53     matrix_init();
54
55     /* enforce re-enumeration, do this while interrupts are disabled! */
56     usbDeviceDisconnect();
57     uint8_t i = 0;
58     while(--i){             /* fake USB disconnect for > 250 ms */
59         wdt_reset();
60         _delay_ms(1);
61     }
62     usbDeviceConnect();
63     sei();
64
65     uint8_t fn_bits = 0;
66     while (1) {                /* main event loop */
67         DEBUGP(0x01);
68         wdt_reset();
69         usbPoll();
70         host_vusb_keyboard_send();
71
72         DEBUGP(0x02);
73         matrix_scan();
74         fn_bits = 0;
75         keyboard_swap_report();
76         keyboard_clear_report();
77         mousekey_clear_report();
78         for (int row = 0; row < matrix_rows(); row++) {
79             for (int col = 0; col < matrix_cols(); col++) {
80                 if (!matrix_is_on(row, col)) continue;
81
82                 uint8_t code = layer_get_keycode(row, col);
83                 if (code == KB_NO) {
84                     // do nothing
85                 }
86                 else if (IS_MOD(code)) {
87                     keyboard_add_mod(MOD_BIT(code));
88                 }
89                 else if (IS_KEY(code)) {
90                     keyboard_add_key(code);
91                 }
92                 else if (IS_FN(code)) {
93                     fn_bits |= FN_BIT(code);
94                 }
95                 else if (IS_MOUSEKEY(code)) {
96                     mousekey_decode(code);
97                 }
98                 else {
99                     debug("ignore keycode: "); debug_hex(code); debug("\n");
100                 }
101             }
102         }
103         DEBUGP(0x03);
104         layer_switching(fn_bits);
105         if (matrix_is_modified()) {
106             keyboard_send();
107         }
108         mousekey_send();
109
110         if (last_led != host_keyboard_led) {
111             keyboard_set_led(host_keyboard_led);
112             last_led = host_keyboard_led;
113         }
114     }
115 }