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
; }
137 static void send_keyboard(report_keyboard_t
*report
)
139 // wake from deep sleep
141 PORTD |= (1<<5); // high
143 PORTD &= ~(1<<5); // low
146 serial_send(0xFD); // Raw report mode
147 serial_send(9); // length
148 serial_send(1); // descriptor type
149 serial_send(report
->mods
);
151 serial_send(report
->keys
[0]);
152 serial_send(report
->keys
[1]);
153 serial_send(report
->keys
[2]);
154 serial_send(report
->keys
[3]);
155 serial_send(report
->keys
[4]);
156 serial_send(report
->keys
[5]);
159 static void send_mouse(report_mouse_t
*report
)
161 // wake from deep sleep
163 PORTD |= (1<<5); // high
165 PORTD &= ~(1<<5); // low
168 serial_send(0xFD); // Raw report mode
169 serial_send(5); // length
170 serial_send(2); // descriptor type
171 serial_send(report
->buttons
);
172 serial_send(report
->x
);
173 serial_send(report
->y
);
174 serial_send(report
->v
);
177 static void send_system(uint16_t data
)
179 // Table 5-6 of RN-BT-DATA-UB
180 // 81,82,83 scan codes can be used?
184 static uint16_t usage2bits(uint16_t usage
)
187 case AC_HOME
: return 0x01;
188 case AL_EMAIL
: return 0x02;
189 case AC_SEARCH
: return 0x04;
190 //case AL_KBD_LAYOUT: return 0x08; // Apple virtual keybaord toggle
191 case AUDIO_VOL_UP
: return 0x10;
192 case AUDIO_VOL_DOWN
: return 0x20;
193 case AUDIO_MUTE
: return 0x40;
194 case TRANSPORT_PLAY_PAUSE
: return 0x80;
195 case TRANSPORT_NEXT_TRACK
: return 0x100;
196 case TRANSPORT_PREV_TRACK
: return 0x200;
197 case TRANSPORT_STOP
: return 0x400;
198 case TRANSPORT_STOP_EJECT
: return 0x800;
199 //case return 0x1000; // Fast forward
200 //case return 0x2000; // Rewind
201 //case return 0x4000; // Stop/eject
202 //case return 0x8000; // Internet browser
207 static void send_consumer(uint16_t data
)
209 uint16_t bits
= usage2bits(data
);
210 serial_send(0xFD); // Raw report mode
211 serial_send(3); // length
212 serial_send(3); // descriptor type
213 serial_send(bits
&0xFF);
214 serial_send((bits
>>8)&0xFF);
218 /* Null driver for config_mode */
219 static uint8_t config_keyboard_leds(void);
220 static void config_send_keyboard(report_keyboard_t
*report
);
221 static void config_send_mouse(report_mouse_t
*report
);
222 static void config_send_system(uint16_t data
);
223 static void config_send_consumer(uint16_t data
);
225 host_driver_t rn42_config_driver
= {
226 config_keyboard_leds
,
227 config_send_keyboard
,
233 static uint8_t config_keyboard_leds(void) { return leds
; }
234 static void config_send_keyboard(report_keyboard_t
*report
) {}
235 static void config_send_mouse(report_mouse_t
*report
) {}
236 static void config_send_system(uint16_t data
) {}
237 static void config_send_consumer(uint16_t data
) {}