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

Fork of xbee_lib by Tristan Hughes

Committer:
gert_lauritsen
Date:
Thu Oct 17 14:39:52 2013 +0000
Revision:
13:1b557befdeff
Parent:
12:debf76f0c0bf
Child:
16:65c47cf83467
add eksemple

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gert_lauritsen 11:18ff088287ea 1 #ifndef telegesis_h
gert_lauritsen 11:18ff088287ea 2 #define telegesis_h
gert_lauritsen 11:18ff088287ea 3
tristanjph 0:2656fb225c5d 4 #include "mbed.h"
tristanjph 0:2656fb225c5d 5
gert_lauritsen 13:1b557befdeff 6 /**Eksemple
gert_lauritsen 13:1b557befdeff 7 #include "telegesis.h"
gert_lauritsen 13:1b557befdeff 8 zigbee ZB(p9,p10);
gert_lauritsen 13:1b557befdeff 9
gert_lauritsen 13:1b557befdeff 10 int main() {
gert_lauritsen 13:1b557befdeff 11 ZB.Reset();
gert_lauritsen 13:1b557befdeff 12 wait(1);
gert_lauritsen 13:1b557befdeff 13 ZB.GetSerial();
gert_lauritsen 13:1b557befdeff 14 while(1) {
gert_lauritsen 13:1b557befdeff 15 ZB.GetSerial();
gert_lauritsen 13:1b557befdeff 16 ZB.RecieveData(read_data,0);
gert_lauritsen 13:1b557befdeff 17 ZB.UniCast("0000","tester forbindelse");
gert_lauritsen 13:1b557befdeff 18 if (ZB.Zdat) {
gert_lauritsen 13:1b557befdeff 19 ZB.Zdat=0;
gert_lauritsen 13:1b557befdeff 20 printf(ZB.Zdata);
gert_lauritsen 13:1b557befdeff 21 }
gert_lauritsen 13:1b557befdeff 22 }
gert_lauritsen 13:1b557befdeff 23 }
gert_lauritsen 13:1b557befdeff 24
gert_lauritsen 13:1b557befdeff 25 */
gert_lauritsen 7:45511c3d2950 26 /** Zigbee interface class for configuring, sending and recieving data using an telegesis zigbee */
gert_lauritsen 11:18ff088287ea 27 #define START_BYTE 0x7e
gert_lauritsen 11:18ff088287ea 28 #define ESCAPE 0x7d
gert_lauritsen 11:18ff088287ea 29 #define CR 0x0D
gert_lauritsen 11:18ff088287ea 30 #define LF 0x0A
gert_lauritsen 11:18ff088287ea 31 #define MAX_FRAME_DATA_SIZE 110
gert_lauritsen 11:18ff088287ea 32
gert_lauritsen 7:45511c3d2950 33 class zigbee
tristanjph 2:cb627ea9b817 34 {
tristanjph 0:2656fb225c5d 35 private:
tristanjph 0:2656fb225c5d 36 PinName _tx;
tristanjph 0:2656fb225c5d 37 PinName _rx;
tristanjph 3:682615a0717e 38 PinName _reset;
tristanjph 0:2656fb225c5d 39 public:
gert_lauritsen 7:45511c3d2950 40
gert_lauritsen 11:18ff088287ea 41 zigbee(PinName tx, PinName rx);
gert_lauritsen 7:45511c3d2950 42 ~zigbee();
gert_lauritsen 7:45511c3d2950 43
gert_lauritsen 7:45511c3d2950 44
gert_lauritsen 7:45511c3d2950 45 /** Gets the serial number/mac address of the zigbee and places it into serial_no.
gert_lauritsen 7:45511c3d2950 46 * @param serial_no array to store the serial of zigbee (must be 8 long).
tristanjph 2:cb627ea9b817 47 * @return Returns 1 on success.
tristanjph 2:cb627ea9b817 48 */
gert_lauritsen 11:18ff088287ea 49 int GetSerial();
tristanjph 6:6455a079bdb3 50 /** Sets the encryption key. This should be a 128-bit key.
tristanjph 2:cb627ea9b817 51 * @param key Pointer to the network key to set.
tristanjph 2:cb627ea9b817 52 * @return Returns 1 on success.
tristanjph 1:c3d9bdcb0b03 53 */
tristanjph 5:714651141a83 54 int SetKey(char*);
gert_lauritsen 7:45511c3d2950 55
tristanjph 2:cb627ea9b817 56 /** Recieves data sent to the xbee.
tristanjph 2:cb627ea9b817 57 * @param data_buf Pointer to the buffer to put recieved data into.
tristanjph 2:cb627ea9b817 58 * @param numchar Number of characters to read. If 0, will use the size of data_buf.
tristanjph 2:cb627ea9b817 59 */
tristanjph 2:cb627ea9b817 60 void RecieveData(char*, int);
gert_lauritsen 10:263f7251c111 61 /** Get hw version and local ID (64bit uniq number)
gert_lauritsen 10:263f7251c111 62 *
gert_lauritsen 10:263f7251c111 63 */
gert_lauritsen 7:45511c3d2950 64 int ATI();
gert_lauritsen 10:263f7251c111 65 /** Sends a ping to Coo or any other adresse
gert_lauritsen 8:4682155753ec 66 *
gert_lauritsen 7:45511c3d2950 67 */
gert_lauritsen 7:45511c3d2950 68 int PingOut();
gert_lauritsen 10:263f7251c111 69 /** Scan the Pan for nodes
gert_lauritsen 7:45511c3d2950 70 */
gert_lauritsen 7:45511c3d2950 71 int PanScan();
gert_lauritsen 10:263f7251c111 72 /** Make a new network. It then gets the role as coordinator
gert_lauritsen 7:45511c3d2950 73 */
gert_lauritsen 7:45511c3d2950 74 int Establish_Network();
gert_lauritsen 10:263f7251c111 75 /** Join a pan
gert_lauritsen 7:45511c3d2950 76 */
gert_lauritsen 7:45511c3d2950 77 int JoinNetwork();
gert_lauritsen 10:263f7251c111 78 /**
gert_lauritsen 10:263f7251c111 79 * Scan network
gert_lauritsen 7:45511c3d2950 80 */
gert_lauritsen 7:45511c3d2950 81 int ScanNetwork();
gert_lauritsen 10:263f7251c111 82 /**
gert_lauritsen 10:263f7251c111 83 *Give info on what role the modem has in the network
gert_lauritsen 7:45511c3d2950 84 */
gert_lauritsen 10:263f7251c111 85
gert_lauritsen 7:45511c3d2950 86 int NetworkInfo();
gert_lauritsen 10:263f7251c111 87 /**
gert_lauritsen 7:45511c3d2950 88 */
gert_lauritsen 12:debf76f0c0bf 89 int Reset();
gert_lauritsen 8:4682155753ec 90 /** Sends data using the ascii mode
gert_lauritsen 10:263f7251c111 91 */
gert_lauritsen 9:c8e4339ccc29 92 int UniCast(char *adr,char *payload);
gert_lauritsen 8:4682155753ec 93 /** Sends data using the binary mode
gert_lauritsen 8:4682155753ec 94 */
gert_lauritsen 9:c8e4339ccc29 95 int UniCastb(char *adr,char *payload, char payloadSize);
gert_lauritsen 11:18ff088287ea 96
gert_lauritsen 10:263f7251c111 97
gert_lauritsen 9:c8e4339ccc29 98 /** converts a string to a long
gert_lauritsen 9:c8e4339ccc29 99 */
gert_lauritsen 11:18ff088287ea 100 unsigned long hextolong(const char *hex);
gert_lauritsen 8:4682155753ec 101 /** convertes a string to a int
gert_lauritsen 8:4682155753ec 102 */
gert_lauritsen 11:18ff088287ea 103 unsigned int hextoint(const char *hex);
gert_lauritsen 9:c8e4339ccc29 104 /**
gert_lauritsen 9:c8e4339ccc29 105 */
gert_lauritsen 7:45511c3d2950 106 int EUI64;
gert_lauritsen 9:c8e4339ccc29 107 /** COO or FBB
gert_lauritsen 9:c8e4339ccc29 108 */
gert_lauritsen 7:45511c3d2950 109 char Devicetype[3];
gert_lauritsen 9:c8e4339ccc29 110 /** Gives type of HW
gert_lauritsen 9:c8e4339ccc29 111 */
gert_lauritsen 9:c8e4339ccc29 112 char HWType[15];
gert_lauritsen 9:c8e4339ccc29 113 /** incomming data
gert_lauritsen 9:c8e4339ccc29 114 */
gert_lauritsen 9:c8e4339ccc29 115 char Zdata[100];
gert_lauritsen 11:18ff088287ea 116 /**
gert_lauritsen 11:18ff088287ea 117
gert_lauritsen 11:18ff088287ea 118 */
gert_lauritsen 12:debf76f0c0bf 119 uint64_t LocalID;
gert_lauritsen 11:18ff088287ea 120 /**
gert_lauritsen 11:18ff088287ea 121 * indication of incoming data
gert_lauritsen 11:18ff088287ea 122 */
gert_lauritsen 11:18ff088287ea 123 uint8_t Zdat;
gert_lauritsen 11:18ff088287ea 124 /**
gert_lauritsen 11:18ff088287ea 125 * Indicator on that we have got a ack on a packet
gert_lauritsen 11:18ff088287ea 126 */
gert_lauritsen 11:18ff088287ea 127 uint8_t PacketAck;
gert_lauritsen 11:18ff088287ea 128
gert_lauritsen 11:18ff088287ea 129 int channel, NodeID, EPID,framesize, PanOnline;
gert_lauritsen 11:18ff088287ea 130 /**
gert_lauritsen 11:18ff088287ea 131 * Reads all available serial bytes until a packet is parsed, an error occurs, or the buffer is empty.
gert_lauritsen 11:18ff088287ea 132 * You may call <i>xbee</i>.getResponse().isAvailable() after calling this method to determine if
gert_lauritsen 11:18ff088287ea 133 * a packet is ready, or <i>xbee</i>.getResponse().isError() to determine if
gert_lauritsen 11:18ff088287ea 134 * a error occurred.
gert_lauritsen 11:18ff088287ea 135 * <p/>
gert_lauritsen 11:18ff088287ea 136 * This method should always return quickly since it does not wait for serial data to arrive.
gert_lauritsen 11:18ff088287ea 137 * You will want to use this method if you are doing other timely stuff in your loop, where
gert_lauritsen 11:18ff088287ea 138 * a delay would cause problems.
gert_lauritsen 11:18ff088287ea 139 * NOTE: calling this method resets the current response, so make sure you first consume the
gert_lauritsen 11:18ff088287ea 140 * current response
gert_lauritsen 11:18ff088287ea 141 */
gert_lauritsen 11:18ff088287ea 142 void readPacket();
gert_lauritsen 11:18ff088287ea 143 /**Looks for a packet but dont wait
gert_lauritsen 11:18ff088287ea 144 */
gert_lauritsen 12:debf76f0c0bf 145 void SeePacket();
gert_lauritsen 11:18ff088287ea 146
gert_lauritsen 11:18ff088287ea 147
gert_lauritsen 11:18ff088287ea 148 private:
gert_lauritsen 11:18ff088287ea 149 uint8_t _pos;
gert_lauritsen 11:18ff088287ea 150 // last byte read
gert_lauritsen 11:18ff088287ea 151 uint8_t b;
gert_lauritsen 11:18ff088287ea 152 uint8_t _checksumTotal;
gert_lauritsen 11:18ff088287ea 153 uint8_t _nextFrameId;
gert_lauritsen 11:18ff088287ea 154 int SeqNumber;
gert_lauritsen 12:debf76f0c0bf 155 uint8_t GotFrame; //indicate that a frame has been recieved
gert_lauritsen 11:18ff088287ea 156 // buffer for incoming RX packets. holds only the api specific frame data, starting after the api id byte and prior to checksum
gert_lauritsen 11:18ff088287ea 157 uint8_t _responseFrameData[MAX_FRAME_DATA_SIZE];
gert_lauritsen 11:18ff088287ea 158 char _responseFrameString[MAX_FRAME_DATA_SIZE];
gert_lauritsen 11:18ff088287ea 159
gert_lauritsen 11:18ff088287ea 160 Serial _zbee;
gert_lauritsen 12:debf76f0c0bf 161 bool wait4OK();
gert_lauritsen 11:18ff088287ea 162 };
gert_lauritsen 11:18ff088287ea 163
gert_lauritsen 11:18ff088287ea 164 #endif