]> git.gir.st - tmk_keyboard.git/blob - keyboard/hhkb_rn42/rn42.c
Add RN-42 Bluetooth module support
[tmk_keyboard.git] / keyboard / hhkb_rn42 / rn42.c
1 #include "host.h"
2 #include "host_driver.h"
3 #include "serial.h"
4 #include "rn42.h"
5 #include "print.h"
6 #include "wait.h"
7
8
9 /* Host driver */
10 static uint8_t keyboard_leds(void);
11 static void send_keyboard(report_keyboard_t *report);
12 static void send_mouse(report_mouse_t *report);
13 static void send_system(uint16_t data);
14 static void send_consumer(uint16_t data);
15
16 host_driver_t rn42_driver = {
17 keyboard_leds,
18 send_keyboard,
19 send_mouse,
20 send_system,
21 send_consumer
22 };
23
24
25 void rn42_init(void)
26 {
27 // PF1: check RTS(active low)
28 DDRF &= ~(1<<1);
29 PORTF &= ~(1<<1);
30
31 // PF7: BT connection control(HiZ: connect, low: disconnect)
32 // JTAG disable for PORT F. write JTD bit twice within four cycles.
33 MCUCR |= (1<<JTD);
34 MCUCR |= (1<<JTD);
35 rn42_autoconnect();
36
37 // PD5: CTS (low: allow to send, high:not allowed)
38 DDRD |= (1<<5);
39 PORTD &= ~(1<<5);
40
41 serial_init();
42 }
43
44 void rn42_putc(uint8_t c)
45 {
46 serial_send(c);
47 }
48
49 void rn42_autoconnect(void)
50 {
51 DDRF &= ~(1<<7);
52 PORTF &= ~(1<<7);
53 }
54
55 void rn42_disconnect(void)
56 {
57 DDRF |= (1<<7);
58 PORTF &= ~(1<<7);
59 }
60
61 bool rn42_ready(void)
62 {
63 // RTS low
64 return PINF&(1<<1) ? false : true;
65 }
66
67
68 static uint8_t keyboard_leds(void) { return 0; }
69
70 static void send_keyboard(report_keyboard_t *report)
71 {
72 // wake from deep sleep
73 PORTD |= (1<<5); // high
74 wait_ms(5);
75 PORTD &= ~(1<<5); // low
76
77 serial_send(0xFD); // Raw report mode
78 serial_send(9); // length
79 serial_send(1); // descriptor type
80 serial_send(report->mods);
81 serial_send(0x00);
82 serial_send(report->keys[0]);
83 serial_send(report->keys[1]);
84 serial_send(report->keys[2]);
85 serial_send(report->keys[3]);
86 serial_send(report->keys[4]);
87 serial_send(report->keys[5]);
88 }
89
90 static void send_mouse(report_mouse_t *report)
91 {
92 // wake from deep sleep
93 PORTD |= (1<<5); // high
94 wait_ms(5);
95 PORTD &= ~(1<<5); // low
96
97 serial_send(0xFD); // Raw report mode
98 serial_send(5); // length
99 serial_send(2); // descriptor type
100 serial_send(report->buttons);
101 serial_send(report->x);
102 serial_send(report->y);
103 serial_send(report->v);
104 }
105
106 static void send_system(uint16_t data)
107 {
108 // Table 5-6 of RN-BT-DATA-UB
109 // 81,82,83 scan codes can be used?
110 }
111
112
113 static uint16_t usage2bits(uint16_t usage)
114 {
115 switch (usage) {
116 case AC_HOME: return 0x01;
117 case AL_EMAIL: return 0x02;
118 case AC_SEARCH: return 0x04;
119 //case AL_KBD_LAYOUT: return 0x08; // Apple virtual keybaord toggle
120 case AUDIO_VOL_UP: return 0x10;
121 case AUDIO_VOL_DOWN: return 0x20;
122 case AUDIO_MUTE: return 0x40;
123 case TRANSPORT_PLAY_PAUSE: return 0x80;
124 case TRANSPORT_NEXT_TRACK: return 0x100;
125 case TRANSPORT_PREV_TRACK: return 0x200;
126 case TRANSPORT_STOP: return 0x400;
127 case TRANSPORT_STOP_EJECT: return 0x800;
128 //case return 0x1000; // Fast forward
129 //case return 0x2000; // Rewind
130 //case return 0x4000; // Stop/eject
131 //case return 0x8000; // Internet browser
132 };
133 return 0;
134 }
135
136 static void send_consumer(uint16_t data)
137 {
138 uint16_t bits = usage2bits(data);
139 serial_send(0xFD); // Raw report mode
140 serial_send(3); // length
141 serial_send(3); // descriptor type
142 serial_send((bits>>8)&0xFF);
143 serial_send(bits&0xFF);
144 }
145
146
147 /* Null driver for config_mode */
148 static uint8_t config_keyboard_leds(void);
149 static void config_send_keyboard(report_keyboard_t *report);
150 static void config_send_mouse(report_mouse_t *report);
151 static void config_send_system(uint16_t data);
152 static void config_send_consumer(uint16_t data);
153
154 host_driver_t rn42_config_driver = {
155 config_keyboard_leds,
156 config_send_keyboard,
157 config_send_mouse,
158 config_send_system,
159 config_send_consumer
160 };
161
162 static uint8_t config_keyboard_leds(void) { return 0; }
163 static void config_send_keyboard(report_keyboard_t *report) {}
164 static void config_send_mouse(report_mouse_t *report) {}
165 static void config_send_system(uint16_t data) {}
166 static void config_send_consumer(uint16_t data) {}
Imprint / Impressum