Daisen Remote library

DAISEN リモコン用ライブラリ

Sample

リモコンの受信機(受信モジュール)とはシリアル(UART)で接続します。

#include "mbed.h"
#include "daisenRemote.h"

Serial pc(USBTX, USBRX);
DigitalOut myled(LED1);

daisenRemote remote(p10, daisenRemote::RF); // rx, type

void isrRemote () {
    pc.printf("Recv: %d\r\n", remote.read());
}

int main() {
    pc.baud(115200);
    pc.printf("*** REMOTE\r\n");
    remote.attach(isrRemote);
    for (;;) {
        led1 = ! led1;
        wait(0.2);
    }
}
Revision:
0:9edcf415fe92
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/daisenRemote.cpp	Tue Apr 21 02:07:53 2015 +0000
@@ -0,0 +1,82 @@
+/*
+ * Daisen Remote library
+ * Copyright (c) 2015 Hiroshi Suga
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ *   2.4GHz: http://www.daisendenshi.com/category_list.php?master_categories_id=102&categories_id=141&categories_name=2.4GHz%91%D1%96%B3%90%FC%83%8A%83%82%83R%83%93%91%97%90M%8A%ED
+ *   Infrared: http://www.daisendenshi.com/category_list.php?master_categories_id=102&categories_id=106&categories_name=%90%D4%8AO%83%8A%83%82%83R%83%93%91%97%90M%8A%ED
+ */
+
+#include "daisenRemote.h"
+
+daisenRemote::daisenRemote (PinName rx, enum remoteType type) : _ir(NC, rx) {
+    _recv = 0;
+    _count = 0;
+    _type = type;
+    if (_type == RF) {
+        _ir.baud(38400);
+    } else
+    if (_type == IR) {
+        _ir.baud(19200);
+    }
+    _ir.attach(this, &daisenRemote::isrRemote, Serial::RxIrq);
+}
+
+int daisenRemote::read () {
+    int r = _recv;
+    _recv = 0;
+    return r;
+}
+
+void daisenRemote::isrRemote () {
+    char c = _ir.getc();
+
+    if (_type == RF) {
+        // 2.4GHz Radio
+        if (_count == 0 && c == 0x0f) {
+            _buf[_count] = c;
+            _count ++;
+        } else
+        if (_count == 1) {
+            if (c == 0x5a) {
+                _buf[_count] = c;
+                _count ++;
+            } else {
+                _count = 0;
+            }
+        } else
+        if (_count >= 2 && _count < 19) {
+            _buf[_count] = c;
+            _count ++;
+            if (_count == 19) {
+                if (_buf[13] == 0x83 && _buf[14] == 0x41 &&
+                  _buf[17] != 0xff && _buf[17] == (~_buf[18] & 0xff)) {
+                    _recv = _buf[17];
+                    _func.call();
+                }
+                _count = 0;
+            }
+        } else {
+            _count = 0;
+        }
+    } else
+    if (_type == IR) {
+        // Infrared
+        if (c == 0x0a || c == 0x0d) {
+            if (_count == 2) {
+                _buf[_count] = 0;
+                _recv = atoi(_buf);
+                _func.call();
+            }
+            _count = 0;
+        } else
+        if (_count < 2) {
+            if (c >= '0' && c <= '9') {
+                _buf[_count] = c;
+                _count ++;
+            } else {
+                _count = 0;
+            }
+        }
+    }
+}