Mario Bambagini / ssWi

Dependents:   rover_car rover_pc supervisor watering_unit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers xbee.cpp Source File

xbee.cpp

Go to the documentation of this file.
00001 /** \file xbee.cpp
00002  *
00003  *  \brief implementation of the ssWi channel using the XBee module
00004  *
00005  */
00006 
00007 #include "xbee.hpp"
00008 
00009 #include <sstream>
00010 
00011 void XBeeModule::readResponse (char* msg)
00012 {
00013     char c = 0;
00014     int i = 0;
00015 
00016     // read until it sends \r
00017     while (c != '\r') {
00018         if (serial_xbee.readable()) {
00019             c = serial_xbee.getc();
00020             msg[i++] = c;
00021         }
00022     }
00023 }
00024 
00025 bool XBeeModule::executeWithOk (const char* cmd)
00026 {
00027     char msg[5];
00028 
00029     // send command
00030     serial_xbee.printf("%s", cmd);
00031     // read response
00032     readResponse(msg);
00033     // check response
00034     if (strncmp(msg, "OK\r", 3) != 0)
00035         return false;
00036     
00037     return true;
00038 }
00039 
00040 void XBeeModule::executeWithRes (const char* cmd, char* res)
00041 {
00042     // send command
00043     serial_xbee.printf("%s", cmd);
00044     // read response
00045     readResponse(res);
00046 }
00047 
00048 bool XBeeModule::initSequence()
00049 {
00050     // wait 1 second before starting
00051     ThisThread::sleep_for(1000);
00052     // send start command
00053     if (!executeWithOk("+++"))
00054         return false;
00055     // wait 1 more second after start command
00056     ThisThread::sleep_for(1000);
00057     
00058     return true;
00059 }
00060 
00061 XBeeModule::XBeeModule (PinName tx, PinName rx, int panID, int channel)
00062                                                    : serial_xbee(tx, rx, 9600) {
00063     status = _getLocalAddr() && _setChannel(channel) && _setPanID(panID);
00064 }
00065 
00066 bool XBeeModule::getDstAddress (XBeeAddress &addr)
00067 {
00068     char tmp[10];
00069     std::stringstream s1, s2;
00070     uint32_t high, low;
00071     
00072     // start communication with transceiver
00073     if (!initSequence())
00074         return false;
00075     // read high address
00076     executeWithRes("ATDH \r", tmp);
00077     s1<<std::hex<<tmp;
00078     s1>>high;
00079     // read low address
00080     executeWithRes("ATDL \r", tmp);
00081     s2<<std::hex<<tmp;
00082     s2>>low;
00083     // terminate
00084     if (!executeWithOk("ATCN \r"))
00085         return false;
00086     addr.setAddress(low, high);
00087     
00088     return true;
00089 }
00090 
00091 
00092 bool XBeeModule::setDstAddress (XBeeAddress addr)
00093 {
00094     char s[10];
00095     string low, high;
00096     
00097     // start communication with transceiver
00098     if (!initSequence())
00099         return false;
00100     // send high address
00101     sprintf(s, "ATDH%X \r", addr.getHighAddr());
00102     if (!executeWithOk(s))
00103         return false;
00104     // send low address
00105     sprintf(s, "ATDL%X \r", addr.getLowAddr());
00106     if (!executeWithOk(s))
00107         return false;
00108     // terminate
00109     if (!executeWithOk("ATCN \r"))
00110         return false;
00111     ThisThread::sleep_for(1000);
00112 
00113     return true;
00114 }
00115 
00116 bool XBeeModule::_getLocalAddr ()
00117 {
00118     char tmp[10];
00119     uint32_t high, low;
00120     std::stringstream s1, s2;
00121 
00122     // start communication with transceiver
00123     if (!initSequence())
00124         return false;
00125     // read high address
00126     executeWithRes("ATSH \r", tmp);
00127     s1<<std::hex<<tmp;
00128     s1>>high;
00129     // read low address
00130     executeWithRes("ATSL \r", tmp);
00131     s2<<std::hex<<tmp;
00132     s2>>low;
00133     // terminate
00134     if (!executeWithOk("ATCN \r"))
00135         return false;
00136     // save address
00137     local = XBeeAddress(low, high);
00138     
00139     return true;
00140 }
00141 
00142 
00143 bool XBeeModule::_setChannel (int channel)
00144 {
00145     char s[10];
00146     
00147     // start communication with transceiver
00148     if (!initSequence())
00149         return false;
00150     sprintf(s, "ATCH%d \r", channel);
00151     if (!executeWithOk(s))
00152         return false;
00153     if (!executeWithOk("ATCN \r"))
00154         return false;
00155 
00156     return true;
00157 }
00158 
00159 int XBeeModule::getChannel ()
00160 {
00161     int channel;
00162     char s[10];
00163     
00164     // start communication with transceiver
00165     if (!initSequence())
00166         return false;
00167     executeWithRes("ATCH \r", s);
00168     channel = atoi(s);
00169     if (!executeWithOk("ATCN \r"))
00170         return -1;
00171     ThisThread::sleep_for(1000);
00172 
00173     return channel;
00174 }
00175 
00176 
00177 bool XBeeModule::_setPanID (int panID)
00178 {
00179     char s[10];
00180     
00181     // start communication with transceiver
00182     if (!initSequence())
00183         return false;
00184     sprintf(s, "ATID%d \r", panID);
00185     if (!executeWithOk(s))
00186         return false;
00187     if (!executeWithOk("ATCN \r"))
00188         return false;
00189         
00190     return true;
00191 }
00192 
00193 int XBeeModule::getPanID ()
00194 {
00195     int id;
00196     char s[10];
00197     
00198     // start communication with transceiver
00199     if (!initSequence())
00200         return false;
00201     executeWithRes("ATID \r", s);
00202     id = atoi(s);
00203     if (!executeWithOk("ATCN \r"))
00204         return -1;
00205     
00206     return id;
00207 }
00208 
00209 
00210 int XBeeModule::read (char* msg)
00211 {
00212     int i = 0;
00213     
00214     while (serial_xbee.readable())
00215         msg[i++] = serial_xbee.getc();
00216 
00217     return i;
00218 }
00219 
00220 void XBeeModule::write (const char* msg, int n)
00221 {
00222     for (int i=0; i<n; i++) {
00223         while(!serial_xbee.writeable());
00224         serial_xbee.putc(msg[i]);
00225     }
00226 }