summaryrefslogtreecommitdiff
path: root/keyboards/hhkb/rn42/rn42.c
blob: b35b3193520017e70fc45d712affdc65b3aaa551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <avr/io.h>
#include "host.h"
#include "host_driver.h"
#include "../serial.h"
#include "rn42.h"
#include "print.h"
#include "timer.h"
#include "wait.h"


/* Host driver */
static uint8_t keyboard_leds(void);
static void send_keyboard(report_keyboard_t *report);
static void send_mouse(report_mouse_t *report);
static void send_extra(uint8_t report_id, uint16_t data);

host_driver_t rn42_driver = {
    keyboard_leds,
    send_keyboard,
    send_mouse,
    send_extra
};


void rn42_init(void)
{
    // PF7: BT connection control(high: connect, low: disconnect)
    rn42_autoconnect();

    // PF6: linked(input without pull-up)
    DDRF  &= ~(1<<6);
    PORTF |=  (1<<6);

    // PF1: RTS(low: allowed to send, high: not allowed)
    DDRF &= ~(1<<1);
    PORTF &= ~(1<<1);

    // PD5: CTS(low: allow to send, high:not allow)
    DDRD |= (1<<5);
    PORTD &= ~(1<<5);

    serial_init();
}

int16_t rn42_getc(void)
{
    return serial_recv2();
}

const char *rn42_gets(uint16_t timeout)
{
    static char s[24];
    uint16_t t = timer_read();
    uint8_t i = 0;
    int16_t c;
    while (i < 23 && timer_elapsed(t) < timeout) {
        if ((c = rn42_getc()) != -1) {
            if ((char)c == '\r') continue;
            if ((char)c == '\n') break;
            s[i++] = c;
        }
    }
    s[i] = '\0';
    return s;
}

void rn42_putc(uint8_t c)
{
    serial_send(c);
}

void rn42_puts(char *s)
{
    while (*s)
	serial_send(*s++);
}

bool rn42_autoconnecting(void)
{
    // GPIO6 for control connection(high: auto connect, low: disconnect)
    // Note that this needs config: SM,4(Auto-Connect DTR Mode)
    return (PORTF & (1<<7) ? true : false);
}

void rn42_autoconnect(void)
{
    // hi to auto connect
    DDRF |= (1<<7);
    PORTF |= (1<<7);
}

void rn42_disconnect(void)
{
    // low to disconnect
    DDRF |= (1<<7);
    PORTF &= ~(1<<7);
}

bool rn42_rts(void)
{
    // low when RN-42 is powered and ready to receive
    return PINF&(1<<1);
}

void rn42_cts_hi(void)
{
    // not allow to send
    PORTD |= (1<<5);
}

void rn42_cts_lo(void)
{
    // allow to send
    PORTD &= ~(1<<5);
}

bool rn42_linked(void)
{
    // RN-42 GPIO2
    //   Hi-Z:  Not powered
    //   High:  Linked
    //   Low:   Connecting
    return PINF&(1<<6);
}


static uint8_t leds = 0;
static uint8_t keyboard_leds(void) { return leds; }
void rn42_set_leds(uint8_t l) { leds = l; }


void rn42_send_str(const char *str)
{
    uint8_t c;
    while ((c = pgm_read_byte(str++)))
        rn42_putc(c);
}

const char *rn42_send_command(const char *cmd)
{
    static const char *s;
    rn42_send_str(cmd);
    wait_ms(500);
    s = rn42_gets(100);
    xprintf("%s\r\n", s);
    rn42_print_response();
    return s;
}

void rn42_print_response(void)
{
    int16_t c;
    while ((c = rn42_getc()) != -1) {
        xprintf("%c", c);
    }
}


static void send_keyboard(report_keyboard_t *report)
{
    // wake from deep sleep
/*
    PORTD |= (1<<5);    // high
    wait_ms(5);
    PORTD &= ~(1<<5);   // low
*/

    serial_send(0xFD);  // Raw report mode
    serial_send(9);     // length
    serial_send(1);     // descriptor type
    serial_send(report->mods);
    serial_send(0x00);
    serial_send(report->keys[0]);
    serial_send(report->keys[1]);
    serial_send(report->keys[2]);
    serial_send(report->keys[3]);
    serial_send(report->keys[4]);
    serial_send(report->keys[5]);
}

static void send_mouse(report_mouse_t *report)
{
    // wake from deep sleep
/*
    PORTD |= (1<<5);    // high
    wait_ms(5);
    PORTD &= ~(1<<5);   // low
*/

    serial_send(0xFD);  // Raw report mode
    serial_send(5);     // length
    serial_send(2);     // descriptor type
    serial_send(report->buttons);
    serial_send(report->x);
    serial_send(report->y);
    serial_send(report->v);
}


static uint16_t usage2bits(uint16_t usage)
{
    switch (usage) {
        case AC_HOME:                 return 0x01;
        case AL_EMAIL:                return 0x02;
        case AC_SEARCH:               return 0x04;
        //case AL_KBD_LAYOUT:         return 0x08;  // Apple virtual keybaord toggle
        case AUDIO_VOL_UP:            return 0x10;
        case AUDIO_VOL_DOWN:          return 0x20;
        case AUDIO_MUTE:              return 0x40;
        case TRANSPORT_PLAY_PAUSE:    return 0x80;
        case TRANSPORT_NEXT_TRACK:    return 0x100;
        case TRANSPORT_PREV_TRACK:    return 0x200;
        case TRANSPORT_STOP:          return 0x400;
        case TRANSPORT_STOP_EJECT:    return 0x800;
        case TRANSPORT_FAST_FORWARD:  return 0x1000;
        case TRANSPORT_REWIND:        return 0x2000;
        //case return 0x4000;   // Stop/eject
        //case return 0x8000;   // Internet browser
    };
    return 0;
}


static void send_extra(uint8_t report_id, uint16_t data)
{
    if (report_id == REPORT_ID_CONSUMER) {
        uint16_t bits = usage2bits(data);
        serial_send(0xFD);  // Raw report mode
        serial_send(3);     // length
        serial_send(3);     // descriptor type
        serial_send(bits&0xFF);
        serial_send((bits>>8)&0xFF);
    }
}


/* Null driver for config_mode */
static uint8_t config_keyboard_leds(void);
static void config_send_keyboard(report_keyboard_t *report);
static void config_send_mouse(report_mouse_t *report);
static void config_send_extra(uint8_t report_id, uint16_t data);

host_driver_t rn42_config_driver = {
    config_keyboard_leds,
    config_send_keyboard,
    config_send_mouse,
    config_send_extra
};

static uint8_t config_keyboard_leds(void) { return leds; }
static void config_send_keyboard(report_keyboard_t *report) {}
static void config_send_mouse(report_mouse_t *report) {}
static void config_send_extra(uint8_t report_id, uint16_t data) {}