main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Bluefruit Protocol for TMK firmware
  3. Author: Benjamin Gould, 2013
  4. Based on code Copyright 2011 Jun Wako <wakojun@gmail.com>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <avr/interrupt.h>
  18. #include <avr/wdt.h>
  19. #include <avr/sleep.h>
  20. #include <util/delay.h>
  21. #include "../serial.h"
  22. #include "keyboard.h"
  23. #include "usb.h"
  24. #include "host.h"
  25. #include "timer.h"
  26. #include "print.h"
  27. #include "debug.h"
  28. #include "sendchar.h"
  29. #include "suspend.h"
  30. #include "bluefruit.h"
  31. #include "pjrc.h"
  32. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  33. #define HOST_DRIVER_NOT_SET 0
  34. #define BLUEFRUIT_HOST_DRIVER 1
  35. #define PJRC_HOST_DRIVER 2
  36. int main(void)
  37. {
  38. CPU_PRESCALE(0);
  39. // DDRD = _BV(PD5);
  40. // DDRB = _BV(PB0);
  41. // PORTD = _BV(PD5);
  42. // PORTB = _BV(PB0);
  43. print_set_sendchar(sendchar);
  44. // usb_init();
  45. // _delay_ms(2000);
  46. // while (!usb_configured()) /* wait */
  47. keyboard_setup();
  48. dprintf("Initializing keyboard...\n");
  49. keyboard_init();
  50. // This implementation is pretty simplistic... if the USB connection
  51. // is not configured, choose the Bluefruit, otherwise use USB
  52. // Definitely would prefer to have this driven by an input pin and make
  53. // it switch dynamically - BCG
  54. // if (!usb_configured()) {
  55. // // Send power to Bluefruit... Adafruit says it takes 27 mA, I think
  56. // // the pins should provide 40 mA, but just in case I switch the
  57. // // Bluefruit using a transistor - BCG
  58. // DDRB = _BV(PB6);
  59. // PORTB |= _BV(PB6);
  60. dprintf("Setting host driver to bluefruit...\n");
  61. host_set_driver(bluefruit_driver());
  62. dprintf("Initializing serial...\n");
  63. serial_init();
  64. // char swpa[] = "+++\r\n";
  65. // for (int i = 0; i < 5; i++) {
  66. // serial_send(swpa[i]);
  67. // }
  68. // char ble_enable[] = "AT+BLEKEYBOARDEN=1\r\n";
  69. // for (int i = 0; i < 20; i++) {
  70. // serial_send(ble_enable[i]);
  71. // }
  72. // char reset[] = "ATZ\r\n";
  73. // for (int i = 0; i < 5; i++) {
  74. // serial_send(reset[i]);
  75. // }
  76. // for (int i = 0; i < 5; i++) {
  77. // serial_send(swpa[i]);
  78. // }
  79. // wait an extra second for the PC's operating system
  80. // to load drivers and do whatever it does to actually
  81. // be ready for input
  82. _delay_ms(1000);
  83. // PORTD = ~_BV(PD5);
  84. dprintf("Starting main loop");
  85. while (1) {
  86. keyboard_task();
  87. }
  88. // } else {
  89. // // I'm not smart enough to get this done with LUFA - BCG
  90. // dprintf("Setting host driver to PJRC...\n");
  91. // host_set_driver(pjrc_driver());
  92. // #ifdef SLEEP_LED_ENABLE
  93. // sleep_led_init();
  94. // #endif
  95. // // wait an extra second for the PC's operating system
  96. // // to load drivers and do whatever it does to actually
  97. // // be ready for input
  98. // _delay_ms(1000);
  99. // PORTB = ~_BV(PB0);
  100. // dprintf("Starting main loop");
  101. // while (1) {
  102. // while (suspend) {
  103. // suspend_power_down();
  104. // if (remote_wakeup && suspend_wakeup_condition()) {
  105. // usb_remote_wakeup();
  106. // }
  107. // }
  108. // keyboard_task();
  109. // }
  110. // }
  111. }