]> git.friedersdorff.com Git - max/tmk_keyboard.git/blobdiff - keyboard/hhkb_rn42/rn42/rn42.c
rn42: Add commands for RN-42
[max/tmk_keyboard.git] / keyboard / hhkb_rn42 / rn42 / rn42.c
index 3fcd64ad6a5276b8775d35d1f9b9b106ef6cfb04..aa6015cbf23f3880c0190fe5f9553d3c8ebbefe0 100644 (file)
@@ -4,6 +4,7 @@
 #include "serial.h"
 #include "rn42.h"
 #include "print.h"
+#include "timer.h"
 #include "wait.h"
 
 
@@ -25,12 +26,17 @@ host_driver_t rn42_driver = {
 
 void rn42_init(void)
 {
-    // PF7: BT connection control(HiZ: connect, low: disconnect)
     // JTAG disable for PORT F. write JTD bit twice within four cycles.
     MCUCR |= (1<<JTD);
     MCUCR |= (1<<JTD);
+
+    // 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);
@@ -42,11 +48,39 @@ void rn42_init(void)
     serial_init();
 }
 
+int16_t rn42_getc(void)
+{
+    return serial_recv2();
+}
+
+char *rn42_gets(uint16_t timeout)
+{
+    static char s[16];
+    uint16_t t = timer_read();
+    uint8_t i = 0;
+    int16_t c;
+    while (i < 15 && 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)
@@ -86,8 +120,19 @@ void rn42_cts_lo(void)
     PORTD &= ~(1<<5);
 }
 
+bool rn42_linked(void)
+{
+    // RN-42 GPIO2
+    //   Hi-Z:  Not powered
+    //   High:  Linked
+    //   Low:   Connecting
+    return !rn42_rts() && PINF&(1<<6);
+}
+
 
-static uint8_t keyboard_leds(void) { return 0; }
+static uint8_t leds = 0;
+static uint8_t keyboard_leds(void) { return leds; }
+void rn42_set_leds(uint8_t l) { leds = l; }
 
 static void send_keyboard(report_keyboard_t *report)
 {
@@ -185,7 +230,7 @@ host_driver_t rn42_config_driver = {
     config_send_consumer
 };
 
-static uint8_t config_keyboard_leds(void) { return 0; }
+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_system(uint16_t data) {}