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

Fork of xbee_lib by Tristan Hughes

telegesis.cpp

Committer:
gert_lauritsen
Date:
2013-10-13
Revision:
9:c8e4339ccc29
Parent:
8:4682155753ec
Child:
11:18ff088287ea

File content as of revision 9:c8e4339ccc29:

#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)
{ 
/** comes with something like this
  Telegesis ETRX357-LRS
  R305C
  000D6F0000D5F06A
  OK
*/   
    Serial DATA(_tx,_rx);
    wait_ms(50);
    DATA.printf("ATI \r");
    DATA.scanf("Telegesis ",HWType);
    DATA.scanf ("%*s");
    DATA.scanf ("%X",serial_no);
    return 1;
}

int zigbee::SetKey(char* key)
{
    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;
}

int 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;
}

int 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;
}
//---------------------------------------------------------------