A USB foot pedal for turning pages of PDF piano sheet music
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.

example.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Keyboard example 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.h"
  28. #define LED_CONFIG (DDRD |= (1<<6))
  29. #define LED_ON (PORTD &= ~(1<<6))
  30. #define LED_OFF (PORTD |= (1<<6))
  31. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  32. #undef STR_MANUFACTURER
  33. #undef STR_PRODUCT
  34. #define STR_MANUFACTURER L"Clayton G. Hobbs"
  35. #define STR_PRODUCT L"Music Score Foot Pedal"
  36. uint16_t idle_count = 0;
  37. uint8_t pressed = 0;
  38. uint8_t last = 0;
  39. int main(void)
  40. {
  41. uint8_t d, mask;
  42. uint8_t d_prev=0xFF;
  43. // set for 16 MHz clock
  44. CPU_PRESCALE(0);
  45. // Configure all port B pins as inputs with pullup resistors.
  46. // See the "Using I/O Pins" page for details.
  47. // http://www.pjrc.com/teensy/pins.html
  48. DDRD = 0x00;
  49. PORTD = 0xFF;
  50. // Initialize the USB, and then wait for the host to set configuration.
  51. // If the Teensy is powered without a PC connected to the USB port,
  52. // this will wait forever.
  53. usb_init();
  54. while (!usb_configured())
  55. ;
  56. // Wait an extra second for the PC's operating system to load drivers
  57. // and do whatever it does to actually be ready for input
  58. _delay_ms(1000);
  59. // Configure timer 0 to generate a timer overflow interrupt every
  60. // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
  61. // This demonstrates how to use interrupts to implement a simple
  62. // inactivity timeout.
  63. TCCR0A = 0x00;
  64. TCCR0B = 0x05;
  65. TIMSK0 = (1<<TOIE0);
  66. while (1) {
  67. // read all port B and port D pins
  68. d = PIND;
  69. // check if any pins are low, but were high previously
  70. mask = 1 << 5;
  71. if (((d & mask) == 0) && (d_prev & mask) != 0) {
  72. cli();
  73. pressed += 1;
  74. idle_count = 0;
  75. sei();
  76. }
  77. // now the current pins will be the previous, and
  78. // wait a short delay so we're not highly sensitive
  79. // to mechanical "bounce".
  80. d_prev = d;
  81. _delay_ms(2);
  82. }
  83. }
  84. // This interrupt routine is run approx 61 times per second.
  85. // All keypresses are sent here. When a single press occurred with nothing
  86. // else for a whole second, send a page down. Further presses less than a
  87. // second apart following that timeout send more page downs. If after a period
  88. // of inactivity, two presses occur within one second, send a page up. Further
  89. // presses less than a second apart send more page ups.
  90. ISR(TIMER0_OVF_vect)
  91. {
  92. idle_count++;
  93. if (idle_count > 30 && pressed > 0 && last != KEY_PAGE_UP) {
  94. usb_keyboard_press(KEY_PAGE_DOWN, 0);
  95. pressed -= 1;
  96. idle_count = 0;
  97. last = KEY_PAGE_DOWN;
  98. } else if (idle_count <= 30 && pressed == 2 && last != KEY_PAGE_DOWN) {
  99. usb_keyboard_press(KEY_PAGE_UP, 0);
  100. pressed -= 1;
  101. idle_count = 0;
  102. last = KEY_PAGE_UP;
  103. } else if (idle_count <= 30 && pressed > 0 && last == KEY_PAGE_DOWN) {
  104. usb_keyboard_press(KEY_PAGE_DOWN, 0);
  105. pressed -= 1;
  106. idle_count = 0;
  107. last = KEY_PAGE_DOWN;
  108. } else if (idle_count > 30 && pressed == 0) {
  109. last = 0;
  110. pressed = 0;
  111. idle_count = 0;
  112. } else if (idle_count > 30 && pressed > 0 && last == KEY_PAGE_UP) {
  113. last = 0;
  114. pressed = 0;
  115. idle_count = 0;
  116. }
  117. }