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