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);
    }
}

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Tue Apr 21 02:07:53 2015 +0000
Commit message:
1st build

Changed in this revision

daisenRemote.cpp Show annotated file Show diff for this revision Revisions of this file
daisenRemote.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 9edcf415fe92 daisenRemote.cpp
--- /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;
+            }
+        }
+    }
+}
diff -r 000000000000 -r 9edcf415fe92 daisenRemote.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/daisenRemote.h	Tue Apr 21 02:07:53 2015 +0000
@@ -0,0 +1,42 @@
+/*
+ * 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 "mbed.h"
+#include "FunctionPointer.h"
+
+class daisenRemote {
+public:
+    enum remoteType {
+        RF, // 2.4GHz
+        IR, // Infrared
+    };
+
+    daisenRemote (PinName rx, remoteType type = RF);
+
+    int read ();
+ 
+    void attach(void (*fptr)(void)) { 
+        _func.attach(fptr);
+    }
+    template<typename T>
+    void attach(T *tptr, void (T::*mptr)(void)) { 
+        _func.attach(tptr, mptr); 
+    }
+
+private:
+    RawSerial _ir;
+    remoteType _type;
+    int _recv;
+    int _count;
+    char _buf[20];
+
+    FunctionPointer _func;
+
+    void isrRemote ();
+};