Interplan IM920 library, 920MHz module

Dependents:   IM920_sample IM920_SDlog IM920_sample IM920_sample3 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IM920.h Source File

IM920.h

00001 /* Copyright (C) 2014 Suga, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 #ifndef _IM920_h_
00019 #define _IM920_h_
00020 
00021 #include "IM920_conf.h"
00022 
00023 #include "mbed.h"
00024 #include "FunctionPointer.h"
00025 #include "CBuffer.h"
00026 #include <ctype.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 
00030 //Debug is disabled by default
00031 #if defined(DEBUG)
00032 #define DBG(x, ...) std::printf("[DBG]" x "\r\n", ##__VA_ARGS__);
00033 #define WARN(x, ...) std::printf("[WARN]" x "\r\n", ##__VA_ARGS__);
00034 #define ERR(x, ...) std::printf("[ERR]" x "\r\n", ##__VA_ARGS__);
00035 #define INFO(x, ...) std::printf("[INFO]" x "\r\n", ##__VA_ARGS__);
00036 #else
00037 #define DBG(x, ...)
00038 #define WARN(x, ...)
00039 #define ERR(x, ...)
00040 #define INFO(x, ...)
00041 #endif
00042 
00043 class IM920 {
00044 public:
00045     enum Response {
00046         RES_NULL,
00047         RES_RDID,
00048         RES_RDNN,
00049         RES_RDRS,
00050     };
00051 
00052     enum Mode {
00053         MODE_COMMAND,
00054         MODE_DATA_RX,
00055     };
00056 
00057     enum Status {
00058         STAT_NONE,
00059         STAT_SLEEP,
00060     };
00061 
00062     IM920 (PinName tx, PinName rx, PinName busy, PinName reset, int baud = IM920_BAUD);
00063 
00064     int init ();
00065     void poll ();
00066     int send (char *buf, int len);
00067     int recv (char *buf, int len);
00068 
00069     void attach (void(*fptr)() = NULL) {
00070         _func.attach(fptr);
00071     }
00072     template<typename T>
00073     void attach (T* tptr, void (T::*mptr)()) {
00074         if ((mptr != NULL) && (tptr != NULL)) {
00075             _func.attach(tptr, mptr);
00076         }
00077     }
00078 
00079     // ----- IM920_util.cpp -----
00080     int setNode (int node);
00081     int getNode ();
00082     int setCh (int ch);
00083     int setPower (int pwr);
00084     int setSpeed (int spd);
00085     int getRssi ();
00086     int sleep ();
00087     int wakeup ();
00088     int test ();
00089 
00090     // ----- IM920_cmd.cpp -----
00091     int sendCommand(const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
00092     int sendData(const char * data, int len, int timeout = CFG_TIMEOUT);
00093 
00094 private:
00095     RawSerial _im;
00096     DigitalIn *_busy;
00097     DigitalInOut *_reset;
00098     int _baud;
00099     FunctionPointer _func;
00100 
00101     struct STATE {
00102         int id, node, rssi;
00103 
00104         time_t time;
00105         bool initialized;
00106         volatile Mode mode;
00107         volatile Status status;
00108         volatile bool ok, failure;
00109         volatile Response res;
00110         int n;
00111         char buf[CFG_BUF_SIZE];
00112 
00113         CircBuffer<char> *data;
00114         volatile bool received;
00115     } _state;
00116 
00117     // ----- IM920_util.cpp -----
00118     int x2i (char c);
00119     char i2x (int i);
00120 
00121     // ----- IM920_msg.cpp -----
00122     void recvData (char c);
00123     int parseMessage ();
00124     void msgOk (const char*);
00125     void msgError (const char*);
00126     void msgConnect (const char*);
00127     void resRDID (const char *buf);
00128     void resRDNN (const char *buf);
00129     void resRDRS (const char *buf);
00130 
00131     // ----- IM920_cmd.cpp -----
00132     void clearFlags ();
00133     int cmdENWR ();
00134     int cmdDSWR ();
00135     int cmdRDID ();
00136     int cmdSTNN (int n);
00137     int cmdRDNN ();
00138     int cmdSRID (int n);
00139     int cmdERID ();
00140     int cmdSTCH (int n);
00141     int cmdRDRS ();
00142     int cmdSTPO (int n);
00143     int cmdSTRT (int n);
00144     int cmdSBRT (int n);
00145     int cmdDSRX ();
00146     int cmdENRX ();
00147     int cmdEGRX ();
00148     int cmdDGRX ();
00149 
00150     // ----- IM920_hal.cpp -----
00151     void setReset (bool flg);
00152     void isrUart ();
00153     int getUart ();
00154     void putUart (char c);
00155     int lockUart (int ms);
00156     void unlockUart ();
00157     void initUart (PinName busy, PinName reset, int baud);
00158  };
00159 
00160 #endif
00161