3 #include "host_driver.h"
12 static uint8_t keyboard_leds(void);
13 static void send_keyboard(report_keyboard_t *report);
14 static void send_mouse(report_mouse_t *report);
15 static void send_system(uint16_t data);
16 static void send_consumer(uint16_t data);
18 host_driver_t rn42_driver = {
29 // JTAG disable for PORT F. write JTD bit twice within four cycles.
33 // PF7: BT connection control(high: connect, low: disconnect)
36 // PF6: linked(input without pull-up)
40 // PF1: RTS(low: allowed to send, high: not allowed)
44 // PD5: CTS(low: allow to send, high:not allow)
51 int16_t rn42_getc(void)
53 return serial_recv2();
56 const char *rn42_gets(uint16_t timeout)
59 uint16_t t = timer_read();
62 while (i < 23 && timer_elapsed(t) < timeout) {
63 if ((c = rn42_getc()) != -1) {
64 if ((char)c == '\r') continue;
65 if ((char)c == '\n') break;
73 void rn42_putc(uint8_t c)
78 void rn42_puts(char *s)
84 bool rn42_autoconnecting(void)
86 // GPIO6 for control connection(high: auto connect, low: disconnect)
87 // Note that this needs config: SM,4(Auto-Connect DTR Mode)
88 return (PORTF & (1<<7) ? true : false);
91 void rn42_autoconnect(void)
98 void rn42_disconnect(void)
107 // low when RN-42 is powered and ready to receive
111 void rn42_cts_hi(void)
117 void rn42_cts_lo(void)
123 bool rn42_linked(void)
133 static uint8_t leds = 0;
134 static uint8_t keyboard_leds(void) { return leds; }
135 void rn42_set_leds(uint8_t l) { leds = l; }
138 void rn42_send_str(const char *str)
141 while ((c = pgm_read_byte(str++)))
145 const char *rn42_send_command(const char *cmd)
147 static const char *s;
151 xprintf("%s\r\n", s);
152 rn42_print_response();
156 void rn42_print_response(void)
159 while ((c = rn42_getc()) != -1) {
165 static void send_keyboard(report_keyboard_t *report)
167 // wake from deep sleep
169 PORTD |= (1<<5); // high
171 PORTD &= ~(1<<5); // low
174 serial_send(0xFD); // Raw report mode
175 serial_send(9); // length
176 serial_send(1); // descriptor type
177 serial_send(report->mods);
179 serial_send(report->keys[0]);
180 serial_send(report->keys[1]);
181 serial_send(report->keys[2]);
182 serial_send(report->keys[3]);
183 serial_send(report->keys[4]);
184 serial_send(report->keys[5]);
187 static void send_mouse(report_mouse_t *report)
189 // wake from deep sleep
191 PORTD |= (1<<5); // high
193 PORTD &= ~(1<<5); // low
196 serial_send(0xFD); // Raw report mode
197 serial_send(5); // length
198 serial_send(2); // descriptor type
199 serial_send(report->buttons);
200 serial_send(report->x);
201 serial_send(report->y);
202 serial_send(report->v);
205 static void send_system(uint16_t data)
207 // Table 5-6 of RN-BT-DATA-UB
208 // 81,82,83 scan codes can be used?
212 static uint16_t usage2bits(uint16_t usage)
215 case APPCONTROL_HOME: return 0x01;
216 case APPLAUNCH_EMAIL: return 0x02;
217 case APPCONTROL_SEARCH: return 0x04;
218 //case AL_KBD_LAYOUT: return 0x08; // Apple virtual keybaord toggle
219 case AUDIO_VOL_UP: return 0x10;
220 case AUDIO_VOL_DOWN: return 0x20;
221 case AUDIO_MUTE: return 0x40;
222 case TRANSPORT_PLAY_PAUSE: return 0x80;
223 case TRANSPORT_NEXT_TRACK: return 0x100;
224 case TRANSPORT_PREV_TRACK: return 0x200;
225 case TRANSPORT_STOP: return 0x400;
226 case TRANSPORT_STOP_EJECT: return 0x800;
227 case TRANSPORT_FAST_FORWARD: return 0x1000;
228 case TRANSPORT_REWIND: return 0x2000;
229 //case return 0x4000; // Stop/eject
230 //case return 0x8000; // Internet browser
235 static void send_consumer(uint16_t data)
237 uint16_t bits = usage2bits(data);
238 serial_send(0xFD); // Raw report mode
239 serial_send(3); // length
240 serial_send(3); // descriptor type
241 serial_send(bits&0xFF);
242 serial_send((bits>>8)&0xFF);
246 /* Null driver for config_mode */
247 static uint8_t config_keyboard_leds(void);
248 static void config_send_keyboard(report_keyboard_t *report);
249 static void config_send_mouse(report_mouse_t *report);
250 static void config_send_system(uint16_t data);
251 static void config_send_consumer(uint16_t data);
253 host_driver_t rn42_config_driver = {
254 config_keyboard_leds,
255 config_send_keyboard,
261 static uint8_t config_keyboard_leds(void) { return leds; }
262 static void config_send_keyboard(report_keyboard_t *report) {}
263 static void config_send_mouse(report_mouse_t *report) {}
264 static void config_send_system(uint16_t data) {}
265 static void config_send_consumer(uint16_t data) {}