Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mykey.c 4.3KB

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