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_cmd.cpp Source File

IM920_cmd.cpp

00001 #include "IM920.h"
00002 
00003 void IM920::clearFlags () {
00004     _state.ok = false;
00005     _state.failure = false;
00006     _state.res = RES_NULL;
00007     _state.n = 0;
00008 }
00009 
00010 int IM920::sendCommand (const char * cmd, Response res, int timeout) {
00011     int i;
00012     Timer t;
00013 
00014     if (lockUart(timeout)) return -1;
00015 
00016     clearFlags();
00017     _state.res = res;
00018     for (i = 0; i < strlen(cmd); i ++) {
00019         putUart(cmd[i]);
00020     }
00021     putUart('\r');
00022     putUart('\n');
00023     unlockUart();
00024     INFO("command: '%s'\r\n", cmd);
00025 
00026     if (timeout) {
00027         t.start();
00028         for (;;) {
00029             if (_state.ok && _state.res == RES_NULL) break;
00030             if (_state.failure || t.read_ms() > timeout) {
00031                 WARN("failure or timeout\r\n");
00032                 _state.res = RES_NULL;
00033                 return -1;
00034             }
00035         }
00036         t.stop();
00037     }
00038     INFO("ok\r\n");
00039     _state.res = RES_NULL;
00040 
00041     return 0;
00042 }
00043 
00044 int IM920::sendData(const char * data, int len, int timeout) {
00045     int i;
00046     Timer t;
00047 
00048     if (lockUart(timeout)) return -1;
00049 
00050     if (len > 64) len = 64;
00051     clearFlags();
00052     putUart('T');
00053     putUart('X');
00054     putUart('D');
00055     putUart('A');
00056     putUart(' ');
00057     for (i = 0; i < len; i ++) {
00058         putUart(i2x((data[i]>>4) & 0x0f));
00059         putUart(i2x(data[i] & 0x0f));
00060     }
00061     putUart('\r');
00062     putUart('\n');
00063     unlockUart();
00064     INFO("data: TXDA %d\r\n", len);
00065 
00066     if (timeout) {
00067         t.start();
00068         for (;;) {
00069             if (_state.ok) break;
00070             if (_state.failure || t.read_ms() > timeout) {
00071                 WARN("failure or timeout\r\n");
00072                 return -1;
00073             }
00074         }
00075         t.stop();
00076     }
00077 
00078     return i;
00079 }
00080 
00081 int IM920::cmdENWR () {
00082     return sendCommand("ENWR");
00083 }
00084 
00085 int IM920::cmdDSWR () {
00086     return sendCommand("DSWR");
00087 }
00088 
00089 int IM920::cmdRDID () {
00090     return sendCommand("RDID", RES_RDID);
00091 }
00092 
00093 int IM920::cmdSTNN (int n) {
00094     char cmd[CFG_CMD_SIZE];
00095     sprintf(cmd, "STNN %02X", n);
00096     return sendCommand(cmd);
00097 }
00098 
00099 int IM920::cmdRDNN () {
00100     return sendCommand("RDNN", RES_RDNN);
00101 }
00102 
00103 int IM920::cmdSRID (int n) {
00104     char cmd[CFG_CMD_SIZE];
00105     sprintf(cmd, "SRID %04X", n);
00106     return sendCommand(cmd);
00107 }
00108 
00109 int IM920::cmdERID () {
00110     return sendCommand("ERID");
00111 }
00112 
00113 int IM920::cmdSTCH (int n) {
00114     char cmd[CFG_CMD_SIZE];
00115     sprintf(cmd, "STCH %02d", n);
00116     return sendCommand(cmd);
00117 }
00118 
00119 int IM920::cmdRDRS () {
00120     return sendCommand("RDRS", RES_RDRS);
00121 }
00122 
00123 int IM920::cmdSTPO (int n) {
00124     char cmd[CFG_CMD_SIZE];
00125     sprintf(cmd, "STPO %d", n);
00126     return sendCommand(cmd);
00127 }
00128 
00129 int IM920::cmdSTRT (int n) {
00130     char cmd[CFG_CMD_SIZE];
00131     sprintf(cmd, "STRT %d", n);
00132     return sendCommand(cmd);
00133 }
00134 
00135 int IM920::cmdSBRT (int n) {
00136     char cmd[CFG_CMD_SIZE];
00137     sprintf(cmd, "SBRT %d", n);
00138     return sendCommand(cmd);
00139 }
00140 
00141 int IM920::cmdDSRX () {
00142     return sendCommand("DSRX");
00143 }
00144 
00145 int IM920::cmdENRX () {
00146     return sendCommand("ENRX");
00147 }
00148 
00149 int IM920::cmdEGRX () {
00150     return sendCommand("EGRX");
00151 }