First Commit IR remote class This class constructer needs (PwmOutPin,DigitalInPin,data array pointer,data array length,Serial instance pointer)

Files at this revision

API Documentation at this revision

Comitter:
lelect
Date:
Sun Apr 06 09:12:22 2014 +0000
Commit message:
IRremote class

Changed in this revision

IRremote.cpp Show annotated file Show diff for this revision Revisions of this file
IRremote.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IRremote.cpp	Sun Apr 06 09:12:22 2014 +0000
@@ -0,0 +1,124 @@
+#include "mbed.h"
+#include "IRremote.h"
+#include "debug.h"
+IRremote::IRremote(PinName trans,PinName receive,int* point,int length,Serial* device)
+    :_trans(trans)
+    ,_receive(receive)
+{
+    _device=device;
+    _point=point;
+    _length=length;
+    _trans.period_us(26);
+    _trans.pulsewidth_us(13);
+}
+void IRremote::setArray(int* point,int length)
+{
+    _point=point;
+    _length=length;
+}
+void IRremote::showArray()
+{
+    for(int i=0; i<_length; i++) {
+        if(_point[i]==0)break;
+        log_info("%03d:%d\r\n",i,_point[i]);
+    }
+}
+int IRremote::sum(void)
+{
+    if(_length!=0) {
+        if(_sum==0) {
+            for(int i=0; i<_length; i++) {
+                _sum += _point[i];
+            }
+        }
+        return _sum;
+    } else {
+        return -1;
+    }
+}
+float IRremote::average(void)
+{
+    if(_length!=0) {
+        if(_sum==0) {
+            for(int i=0; i<_length; i++) {
+                _sum += _point[i];
+            }
+        }
+        if(_ave==0.0) {
+            _ave=(float)_sum/(float)_length;
+        }
+        return _ave;
+    } else {
+        return -1.0;
+    }
+}
+void IRremote::trans(void (*afunc)(void),void (*bfunc)(void))
+{
+    afunc();
+    _timer.start();
+    _trans = 0;
+    if (_point[0] == 0) {
+        _timer.stop();
+        _trans=0;
+        return;
+    }
+    for (int i = 0; i < _length; i++) {
+        if (_point[i] == 0) {
+            _timer.stop();
+            _trans=0.0f;
+            bfunc();
+            break;
+        }
+        if (i&1) { //odd
+            _timer.reset();
+            while (_timer.read_us() <= _point[i]) {
+                _trans=0.0f;
+            }
+        } else { //even
+            _timer.reset();
+            while (_timer.read_us() <= _point[i]) {
+                _trans=0.5f;
+            }
+        }
+    }
+    _timer.stop();
+    _trans=0;
+}
+void IRremote::receive(void (*afunc)(void),void (*bfunc)(void))
+{
+    afunc();
+    for (int i = 0; i < _length; i++) {
+        _point[i] = 0;
+    }
+    int lastStatus=0;
+    log_info("Common!\r\n");
+    _timer.start();
+    _timer.reset();
+    while (_receive) {
+        if (_timer.read_us()>=5000000) {
+            _timer.stop();
+            log_info("TimeOut..\r\n");
+            return;
+        }
+    }
+    _timer.reset();
+    for (int i =0; i < _length; i++) {
+        while (lastStatus == _receive) {
+            if (_timer.read_us()>=1000000) {
+                log_info("less than %d..Done!\r\n",_length);
+                _timer.stop();
+                bfunc();
+                return;
+            }
+        }
+        _point[i] = _timer.read_us();
+        lastStatus = !lastStatus;
+        _timer.reset();
+    }
+    log_info("over %d...\r\n",_length);
+}
+
+void IRremote::init(void)
+{
+    _length=0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IRremote.h	Sun Apr 06 09:12:22 2014 +0000
@@ -0,0 +1,32 @@
+#ifndef MBED_IRREMOTE_H
+#define MBED_IRREMOTE_H
+#include "mbed.h"
+#include "debug.h"
+
+class IRremote
+{
+public:
+    IRremote(PinName trans,PinName receive,int* point,int length,Serial* device);
+    void setArray(int* point,int length);
+    void showArray();
+    int sum();
+    float average();
+    int datalength();
+    void trans(void (*afunc)(void),void (*bfunc)(void));
+    void receive(void (*afunc)(void),void (*bfunc)(void));
+protected:
+    void init();
+    int* _point;
+    int _length;
+    int _sum;
+    float _ave;
+    PwmOut _trans;
+    DigitalIn _receive;
+    Timer _timer;
+    Serial* _device;
+    struct {
+        unsigned lastStatus : 1;
+    } flag;
+};
+
+#endif
\ No newline at end of file