]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/hhkb_rn42/rn42.c
89ecb199cab9eb852d70cdf47b0f0e50331d6373
[tmk_firmware.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 /*
74     PORTD |= (1<<5);    // high
75     wait_ms(5);
76     PORTD &= ~(1<<5);   // low
77 */
78
79     serial_send(0xFD);  // Raw report mode
80     serial_send(9);     // length
81     serial_send(1);     // descriptor type
82     serial_send(report->mods);
83     serial_send(0x00);
84     serial_send(report->keys[0]);
85     serial_send(report->keys[1]);
86     serial_send(report->keys[2]);
87     serial_send(report->keys[3]);
88     serial_send(report->keys[4]);
89     serial_send(report->keys[5]);
90 }
91
92 static void send_mouse(report_mouse_t *report)
93 {
94     // wake from deep sleep
95 /*
96     PORTD |= (1<<5);    // high
97     wait_ms(5);
98     PORTD &= ~(1<<5);   // low
99 */
100
101     serial_send(0xFD);  // Raw report mode
102     serial_send(5);     // length
103     serial_send(2);     // descriptor type
104     serial_send(report->buttons);
105     serial_send(report->x);
106     serial_send(report->y);
107     serial_send(report->v);
108 }
109
110 static void send_system(uint16_t data)
111 {
112     // Table 5-6 of RN-BT-DATA-UB
113     // 81,82,83 scan codes can be used?
114 }
115
116
117 static uint16_t usage2bits(uint16_t usage)
118 {
119     switch (usage) {
120         case AC_HOME:               return 0x01;
121         case AL_EMAIL:              return 0x02;
122         case AC_SEARCH:             return 0x04;
123         //case AL_KBD_LAYOUT:         return 0x08;  // Apple virtual keybaord toggle
124         case AUDIO_VOL_UP:          return 0x10;
125         case AUDIO_VOL_DOWN:        return 0x20;
126         case AUDIO_MUTE:            return 0x40;
127         case TRANSPORT_PLAY_PAUSE:  return 0x80;
128         case TRANSPORT_NEXT_TRACK:  return 0x100;
129         case TRANSPORT_PREV_TRACK:  return 0x200;
130         case TRANSPORT_STOP:        return 0x400;
131         case TRANSPORT_STOP_EJECT:  return 0x800;
132         //case return 0x1000;   // Fast forward
133         //case return 0x2000;   // Rewind
134         //case return 0x4000;   // Stop/eject
135         //case return 0x8000;   // Internet browser
136     };
137     return 0;
138 }
139
140 static void send_consumer(uint16_t data)
141 {
142     uint16_t bits = usage2bits(data);
143     serial_send(0xFD);  // Raw report mode
144     serial_send(3);     // length
145     serial_send(3);     // descriptor type
146     serial_send(bits&0xFF);
147     serial_send((bits>>8)&0xFF);
148 }
149
150
151 /* Null driver for config_mode */
152 static uint8_t config_keyboard_leds(void);
153 static void config_send_keyboard(report_keyboard_t *report);
154 static void config_send_mouse(report_mouse_t *report);
155 static void config_send_system(uint16_t data);
156 static void config_send_consumer(uint16_t data);
157
158 host_driver_t rn42_config_driver = {
159     config_keyboard_leds,
160     config_send_keyboard,
161     config_send_mouse,
162     config_send_system,
163     config_send_consumer
164 };
165
166 static uint8_t config_keyboard_leds(void) { return 0; }
167 static void config_send_keyboard(report_keyboard_t *report) {}
168 static void config_send_mouse(report_mouse_t *report) {}
169 static void config_send_system(uint16_t data) {}
170 static void config_send_consumer(uint16_t data) {}