]> git.gir.st - tmk_keyboard.git/blob - mykey.c
change file name.
[tmk_keyboard.git] / mykey.c
1 /* Keyboard example with debug channel, for Teensy USB Development Board
2 * http://www.pjrc.com/teensy/usb_keyboard.html
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 <avr/interrupt.h>
27 #include <util/delay.h>
28 #include "usb_keyboard_debug.h"
29 #include "print.h"
30
31 #define LED_CONFIG (DDRD |= (1<<6))
32 #define LED_ON (PORTD &= ~(1<<6))
33 #define LED_OFF (PORTD |= (1<<6))
34 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
35
36 uint8_t number_keys[10]=
37 {KEY_0,KEY_1,KEY_2,KEY_3,KEY_4,KEY_5,KEY_6,KEY_7,KEY_8,KEY_9};
38
39 uint16_t idle_count=0;
40
41 int main(void)
42 {
43 uint8_t b, d, mask, i, reset_idle;
44 uint8_t b_prev=0xFF, d_prev=0xFF;
45
46 // set for 16 MHz clock
47 CPU_PRESCALE(0);
48
49 // Configure all port B and port D pins as inputs with pullup resistors.
50 // See the "Using I/O Pins" page for details.
51 // http://www.pjrc.com/teensy/pins.html
52 DDRD = 0x00;
53 DDRB = 0x00;
54 PORTB = 0xFF;
55 PORTD = 0xFF;
56
57 // Initialize the USB, and then wait for the host to set configuration.
58 // If the Teensy is powered without a PC connected to the USB port,
59 // this will wait forever.
60 usb_init();
61 while (!usb_configured()) /* wait */ ;
62
63 // Wait an extra second for the PC's operating system to load drivers
64 // and do whatever it does to actually be ready for input
65 _delay_ms(1000);
66
67 // Configure timer 0 to generate a timer overflow interrupt every
68 // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
69 // This demonstrates how to use interrupts to implement a simple
70 // inactivity timeout.
71 TCCR0A = 0x00;
72 TCCR0B = 0x05;
73 TIMSK0 = (1<<TOIE0);
74
75 print("Begin keyboard example program\n");
76 print("All Port B or Port D pins are inputs with pullup resistors.\n");
77 print("Any connection to ground on Port B or D pins will result in\n");
78 print("keystrokes sent to the PC (and debug messages here).\n");
79 while (1) {
80 // read all port B and port D pins
81 b = PINB;
82 d = PIND;
83 // check if any pins are low, but were high previously
84 mask = 1;
85 reset_idle = 0;
86 for (i=0; i<8; i++) {
87 if (((b & mask) == 0) && (b_prev & mask) != 0) {
88 //usb_keyboard_press(KEY_B, KEY_SHIFT);
89 //usb_keyboard_press(number_keys[i], 0);
90 print("Port B, bit ");
91 phex(i);
92 print("\n");
93 reset_idle = 1;
94 }
95 if (((d & mask) == 0) && (d_prev & mask) != 0) {
96 //usb_keyboard_press(KEY_D, KEY_SHIFT);
97 //usb_keyboard_press(number_keys[i], 0);
98 print("Port D, bit ");
99 phex(i);
100 print("\n");
101 reset_idle = 1;
102 }
103 mask = mask << 1;
104 }
105 // if any keypresses were detected, reset the idle counter
106 if (reset_idle) {
107 // variables shared with interrupt routines must be
108 // accessed carefully so the interrupt routine doesn't
109 // try to use the variable in the middle of our access
110 cli();
111 idle_count = 0;
112 sei();
113 }
114 // now the current pins will be the previous, and
115 // wait a short delay so we're not highly sensitive
116 // to mechanical "bounce".
117 b_prev = b;
118 d_prev = d;
119 _delay_ms(2);
120 }
121 }
122
123 // This interrupt routine is run approx 61 times per second.
124 // A very simple inactivity timeout is implemented, where we
125 // will send a space character and print a message to the
126 // hid_listen debug message window.
127 ISR(TIMER0_OVF_vect)
128 {
129 idle_count++;
130 if (idle_count > 61 * 8) {
131 idle_count = 0;
132 print("Timer Event :)\n");
133 //usb_keyboard_press(KEY_SPACE, 0);
134 }
135 }
136
137
Imprint / Impressum