Suga koubou / IM920

Dependents:   IM920_sample IM920_SDlog IM920_sample IM920_sample3 ... more

Files at this revision

API Documentation at this revision

Comitter:
idealtechlab
Date:
Mon Jan 05 03:47:08 2015 +0000
Parent:
1:81b2fd407327
Child:
3:db269462ad1c
Commit message:
support attach

Changed in this revision

IM920.cpp Show annotated file Show diff for this revision Revisions of this file
IM920.h Show annotated file Show diff for this revision Revisions of this file
IM920_msg.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/IM920.cpp	Fri Dec 26 15:51:37 2014 +0000
+++ b/IM920.cpp	Mon Jan 05 03:47:08 2015 +0000
@@ -28,10 +28,9 @@
     setReset(false);
 }
 
-int IM920::init (int node, void(*func)()) {
+int IM920::init (int node) {
 
     _state.node = node;
-    _state.func = func;
 
     cmdRDID();
     cmdSTNN(_state.node);
@@ -43,8 +42,8 @@
 void IM920::poll () {
 
     if (_state.received && _state.buf != NULL)
-      if (_state.func != NULL && !_state.data->isEmpty()) {
-        _state.func();
+      if (!_state.data->isEmpty()) {
+        _func.call();
         if (_state.data->isEmpty()) {
             _state.received = false;
         }
--- a/IM920.h	Fri Dec 26 15:51:37 2014 +0000
+++ b/IM920.h	Mon Jan 05 03:47:08 2015 +0000
@@ -21,6 +21,7 @@
 #include "IM920_conf.h"
 
 #include "mbed.h"
+#include "FunctionPointer.h"
 #include "CBuffer.h"
 #include <ctype.h>
 #include <stdlib.h>
@@ -60,7 +61,17 @@
 
     IM920 (PinName tx, PinName rx, PinName busy = NC, PinName reset = NC, int baud = IM920_BAUD);
 
-    int init (int node, void(*func)() = NULL);
+    int init (int node);
+
+    void attach (void(*fptr)() = NULL) {
+        _func.attach(fptr);
+    }
+    template<typename T>
+    void attach (T* tptr, void (T::*mptr)()) {
+        if ((mptr != NULL) && (tptr != NULL)) {
+            _func.attach(tptr, mptr);
+        }
+    }
 
     void poll ();
 
@@ -81,6 +92,7 @@
     DigitalIn *_busy;
     DigitalOut *_reset;
     int _baud;
+    FunctionPointer _func;
 
     struct STATE {
         int id, node, rssi;
@@ -96,7 +108,6 @@
 
         CircBuffer<char> *data;
         volatile bool received;
-        void(*func)();
     } _state;
 
     // ----- IM920_util.cpp -----
--- a/IM920_msg.cpp	Fri Dec 26 15:51:37 2014 +0000
+++ b/IM920_msg.cpp	Mon Jan 05 03:47:08 2015 +0000
@@ -55,7 +55,7 @@
             sub ++;
             if (_state.data!= NULL) {
                 _state.data->queue(chr);
-                if (_state.func != NULL && _state.data->available() >= CFG_DATA_SIZE) {
+                if (_state.data->available() >= CFG_DATA_SIZE) {
                     _state.received = true;
                     WARN("buf full");
                 }