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

Fork of xbee_lib by Tristan Hughes

Revision:
7:45511c3d2950
Parent:
6:6455a079bdb3
Child:
8:4682155753ec
diff -r 6455a079bdb3 -r 45511c3d2950 telegesis.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/telegesis.cpp	Sun Oct 13 09:46:49 2013 +0000
@@ -0,0 +1,212 @@
+#include "telegesis.h"
+
+
+zigbee::zigbee(PinName tx, PinName rx, PinName reset)
+{
+    _tx = tx;
+    _rx = rx;
+    _reset = reset;
+}
+
+zigbee::~zigbee()
+{
+}
+
+
+int zigbee::GetSerial(int *serial_no)
+{
+    int sh1,sh2,sh3,sl1,sl2,sl3,sl4;
+    Serial DATA(_tx,_rx);
+    wait_ms(50);
+    DATA.printf("ATSL \r");
+    DATA.scanf ("%2x%2x%2x%2x",&sl1,&sl2,&sl3,&sl4);
+    wait_ms(500);
+    DATA.printf("ATSH \r");
+    DATA.scanf ("%2x%2x%2x",&sh1,&sh2,&sh3);
+
+    serial_no[0] = sh1;
+    serial_no[1] = sh2;
+    serial_no[2] = sh3;
+    serial_no[3] = sl1;
+    serial_no[4] = sl2;
+    serial_no[5] = sl3;
+    serial_no[6] = sl4;
+
+    return 1;
+}
+
+int zigbee::SetKey(char* key)
+{
+    Serial DATA(_tx,_rx);
+    DATA.printf("ATEE 1 \r");
+
+    DATA.scanf ("%*s");
+    wait_ms(1);
+    DATA.printf("ATKY %s \r",key);
+    DATA.scanf ("%*s");
+    return 1;
+}
+
+int zigbee::WriteSettings()
+{
+    Serial DATA(_tx,_rx);
+    wait_ms(5);
+    DATA.printf("ATWR \r");
+    DATA.scanf ("%*s");
+    return 1;
+}
+
+
+void zigbee::RecieveData(char *data_buf, int numchar)
+{
+    int count=0;
+    if(numchar == 0) {
+        numchar = sizeof(data_buf);
+    }
+    Serial DATA(_tx,_rx);
+    while(numchar!=count) {
+        if(DATA.readable()) {
+            *data_buf = DATA.getc();
+            data_buf+=1;
+            count++;
+        }
+
+    }
+}
+
+int zigbee::ATI()
+{
+    Serial DATA(_tx,_rx);
+    wait_ms(5);
+    DATA.printf("ATI\r");
+    DATA.scanf ("%*s");
+    return 1;
+}
+
+
+int zigbee::PingOut()
+{
+    Serial DATA(_tx,_rx);
+    wait_ms(5);
+    DATA.printf("AT+ANNCE\r");
+    DATA.scanf ("%*s");
+    return 1;
+}
+
+
+int zigbee::PanScan()
+{
+    Serial DATA(_tx,_rx);
+    wait_ms(5);
+    DATA.printf("AT+PANSCAN\r");
+    DATA.scanf ("%*s");
+    return 1;
+}
+
+
+int zigbee::Establish_Network()
+{
+    Serial DATA(_tx,_rx);
+    wait_ms(5);
+    DATA.printf("AT+EN\r");
+    DATA.scanf ("%*s");
+    return 1;
+}
+
+
+int zigbee::JoinNetwork()
+{
+    Serial DATA(_tx,_rx);
+    wait_ms(5);
+    DATA.printf("AT+JN\r");
+    DATA.scanf ("%*s");
+    return 1;
+}
+
+int zigbee::ScanNetwork()
+{
+    Serial DATA(_tx,_rx);
+    wait_ms(5);
+    DATA.printf("AT+SN\r");
+    DATA.scanf ("%*s");
+    return 1;
+}
+
+int zigbee::NetworkInfo()
+{
+//Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522"
+    Serial DATA(_tx,_rx);
+    wait_ms(5);
+    DATA.printf("AT+N\r");
+    DATA.scanf ("+N=%s,%d,%d,%4X,%X",Devicetype,&channel,&NodeID,&EPID);
+    return 1;
+}
+
+void zigbee::UniCast(char *adr,char *payload)  //Ascii mode with null terminated string
+{
+    Serial DATA(_tx,_rx);
+    wait_ms(5);
+    DATA.printf("AT+UCAST:%s=%s\r",adr,payload);
+
+    DATA.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata);
+    return 1;
+}
+
+void zigbee::UniCastb(char *adr,char *payload, char payloadSize) //sends data in binary format
+{
+    Serial DATA(_tx,_rx);
+    wait_ms(5);
+    DATA.printf("AT+UCASTB:%X,%s\r",adr,payloadSize);
+    DATA.scanf ("%*s");
+    return 1;
+}
+
+void zigbee::Reset()
+{
+    DigitalOut rst(_reset);
+    rst = 0;
+    wait_ms(10);
+    rst = 1;
+    wait_ms(1);
+}
+
+//-----------------------Håndtere indkommende data------------------------------
+
+unsigned long zigbee::hexToLong(const char *hex)
+{
+    //return 32 bit
+    unsigned long result = 0;
+    while (*hex) { //så længe det ikke er null
+        if (*hex >= '0' && *hex <= '9')
+            result += (*hex - '0');
+        else if (*hex >= 'A' && *hex <= 'F')
+            result += (*hex - 'A' +10);
+        else if (*hex >= 'a' && *hex <= 'f')
+            result += (*hex - 'a'+ 10);
+
+        if (*++hex) //hvis den næstee ikke er null
+            result <<= 4;
+    }
+    return result;
+}
+
+
+unsigned int zigbee::hexToInt(const char *hex)
+{
+    //return 16 bit
+    unsigned int result = 0;
+    while (*hex) { //så længe det ikke er null
+        if (*hex >= '0' && *hex <= '9')
+            result += (*hex - '0');
+        else if (*hex >= 'A' && *hex <= 'F')
+            result += (*hex - 'A' +10);
+        else if (*hex >= 'a' && *hex <= 'f')
+            result += (*hex - 'a'+ 10);
+
+        if (*++hex) //hvis den næstee ikke er null
+            result <<= 4;
+    }
+    return result;
+}
+//---------------------------------------------------------------
+