A libery to connect to telegesis zigbee module. Bassed on implemtation of XBEE

Fork of xbee_lib by Tristan Hughes

Revision:
12:debf76f0c0bf
Parent:
11:18ff088287ea
Child:
14:dcf2390f89e2
--- a/telegesis.cpp	Tue Oct 15 09:55:41 2013 +0000
+++ b/telegesis.cpp	Tue Oct 15 14:10:08 2013 +0000
@@ -4,21 +4,27 @@
 zigbee::zigbee(PinName tx, PinName rx): _zbee(tx, rx) {
     _tx = tx;
     _rx = rx;
-     
+    _pos=0; 
       _zbee.baud(19200);
+      _zbee.attach(this, &zigbee::SeePacket, Serial::RxIrq);
 }
 
 zigbee::~zigbee()
 {
 }
 
-int zigbee::wait4OK() {
+bool zigbee::wait4OK() {
+    Timer t;
+    t.reset();
+  t.start();
     readPacket();
-    if (strstr(_responseFrameString,"OK"))  return 1; else return 0;
+    while ((strstr(_responseFrameString,"OK")==0) & (t.read_ms() < 500))    readPacket();
+    return (strstr(_responseFrameString,"OK")>0);
 }
 
 int zigbee::GetSerial()
 { 
+        
     /** comes with something like this
   Telegesis ETRX357-LRS
   R305C
@@ -30,7 +36,8 @@
       readPacket();
     sscanf(_responseFrameString,"Telegesis %s",HWType);
       readPacket(); readPacket();
-    sscanf (_responseFrameString,"%s",LocalID);
+      LocalID=hextoint(_responseFrameString);
+      wait_ms(5);
     return 1;
 }
 
@@ -58,36 +65,38 @@
     }
 }
 
+int zigbee::Reset()
+{
+    _zbee.printf("ATZ\r");
+    wait4OK();
+    return 1;
+}
+
 int zigbee::ATI()
 {
-    _zbee.printf("ATI\r");
-    _zbee.scanf ("%*s");
+    GetSerial();
     return 1;
 }
 
 
 int zigbee::PingOut()
-{
+{ //just return a OK (sends it ID out out the pan)
     _zbee.printf("AT+ANNCE\r");
-    _zbee.scanf ("%*s");
+    wait4OK();
     return 1;
 }
 
 
 int zigbee::PanScan()
-{
-    
-    
+{//
     _zbee.printf("AT+PANSCAN\r");
-    _zbee.scanf ("%*s");
     return 1;
 }
 
 
 int zigbee::Establish_Network()
-{
+{//
     _zbee.printf("AT+EN\r");
-    _zbee.scanf ("%*s");
     return 1;
 }
 
@@ -95,14 +104,12 @@
 int zigbee::JoinNetwork()
 {
     _zbee.printf("AT+JN\r");
-    _zbee.scanf ("%*s");
     return 1;
 }
 
 int zigbee::ScanNetwork()
 {
     _zbee.printf("AT+SN\r");
-    _zbee.scanf ("%*s");
     return 1;
 }
 
@@ -110,25 +117,24 @@
 {
 //Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522"
     _zbee.printf("AT+N\r");
-    _zbee.scanf ("+N=%s,%d,%d,%4X,%X",Devicetype,&channel,&NodeID,&EPID);
     return 1;
 }
 
 int zigbee::UniCast(char *adr,char *payload)  //Ascii mode with null terminated string
 {
     _zbee.printf("AT+UCAST:%s=%s\r",adr,payload);
-    _zbee.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata);
+//    _zbee.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata);
     return 1;
 }
 
 int zigbee::UniCastb(char *adr,char *payload, char payloadSize) //sends data in binary format
 {
     _zbee.printf("AT+UCASTB:%X,%s\r",adr,payloadSize);
-    _zbee.scanf ("%*s");
+    //_zbee.scanf ("%*s");
     return 1;
 }
 
-//-----------------------Håndtere indkommende data------------------------------
+//-----------------------Handle incoming data------------------------------
 
 unsigned long zigbee::hextolong(const char *hex)
 {
@@ -142,7 +148,7 @@
         else if (*hex >= 'a' && *hex <= 'f')
             result += (*hex - 'a'+ 10);
 
-        if (*++hex) //hvis den næstee ikke er null
+        if (*++hex) //if the neext isn't a null
             result <<= 4;
     }
     return result;
@@ -161,7 +167,7 @@
         else if (*hex >= 'a' && *hex <= 'f')
             result += (*hex - 'a'+ 10);
 
-        if (*++hex) //hvis den næstee ikke er null
+        if (*++hex) //if the neext isn't a null
             result <<= 4;
     }
     return result;
@@ -169,54 +175,64 @@
 //---------------------------------------------------------------
 void zigbee::readPacket() {
     //read and waits for the packet
-    while (SeePacket()!='\n') {
-    }
+    Timer t;
+    t.reset();
+  t.start();
+    while ((GotFrame==0) & (t.read_ms() < 500)){}
+    t.stop();   
+    GotFrame=0; 
 }   
 
 
-uint8_t zigbee::SeePacket() {
+void zigbee::SeePacket() {
  char *p;   
-      _pos=0; b=0;
-    if (_zbee.readable()) {
-        b = _zbee.getc();
-              if ((b!=CR) & (b!=LF)) _responseFrameString[_pos++]=b;
-            if (b==CR) {
-              _responseFrameString[_pos]=0; //Nul terminate
-                 if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something
-          //Do something 
+    b = _zbee.getc();
+    if ((b!=CR) & (b!=LF)) _responseFrameData[_pos]=b;
+    _pos=(_pos+1) % MAX_FRAME_DATA_SIZE;
+    if (b==CR) {
+         memcpy(&_responseFrameString,&_responseFrameData,_pos);
+         GotFrame=1;
+         _responseFrameString[_pos]=0; //Nul terminate
+         _pos=0;
+         if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something
+            //Do something 
          } else
-                 if (strstr(_responseFrameString,"UCAST:")) {//checke for incoming UCAST data
-           Zdat=1;
-           p=strstr(_responseFrameString,"="); p++;
-           strcpy(Zdata,p);
+         if (strstr(_responseFrameString,"UCAST:")) {//checke for incoming UCAST data
+             //if (sscanf (_responseFrameString,"UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata)>0)
+             p=strstr(_responseFrameString,"="); 
+             if (p) {
+               p++;
+               strcpy(Zdata,p);
+               Zdat=1;
+             }   
          }
-               if (strstr(_responseFrameString,"LeftPAN:")) PanOnline=0;  //Local node has left the Pan
-
-                 if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID>
-                     //new node on the pan
-                 }
+         if (strstr(_responseFrameString,"LeftPAN:")) PanOnline=0;  //Local node has left the Pan
 
-                 if (strstr(_responseFrameString,"OK")) {  
-                        //if (ScriptState) ConnectScript();
-                        //Ok=1; Cmd=0; LineNo=0; 
-                 }
-                 
-                 if (strstr(_responseFrameString,"ACK")) { //
-                    PacketAck=1;
-                    p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
-                    SeqNumber=hextoint(p);
-                 }
-                 if (strstr(_responseFrameString,"NAK")) {
-                    PacketAck=0; 
-                    p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
-                    SeqNumber=hextoint(p);
-                 }
-                 if (strstr(_responseFrameString,"SEQ")) { //
-                    PacketAck=1;
-                    p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
-                    SeqNumber=hextoint(p);
-                 }
-            }   
-    }
-    return b;
+         if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID>
+             //new node on the pan
+         }
+
+         if ((strstr(_responseFrameString,"OK")>0) & (strstr(_responseFrameString,"TOKDUMP")==0) ) {  
+                //if (ScriptState) ConnectScript();
+                //Ok=1; Cmd=0; LineNo=0; 
+         }
+         if (strstr(_responseFrameString,"+N=")) {  
+          sscanf (_responseFrameString,"+N=%s,%d,%d,%4X,",Devicetype,&channel,&NodeID,&EPID);
+         }   
+         if (strstr(_responseFrameString,"ACK")) { //
+            PacketAck=1;
+            p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
+            SeqNumber=hextoint(p);
+         }
+         if (strstr(_responseFrameString,"NAK")) {
+            PacketAck=0; 
+            p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
+            SeqNumber=hextoint(p);
+         }
+         if (strstr(_responseFrameString,"SEQ")) { //
+            PacketAck=1;
+            p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
+            SeqNumber=hextoint(p);
+         }      
+    }   
 }