XBEE code to reduce program size
Dependencies: mbed
Diff: XBEE/XBEE.cpp
- Revision:
- 0:7c9e8aaa85d4
- Child:
- 1:b0c665b697c7
diff -r 000000000000 -r 7c9e8aaa85d4 XBEE/XBEE.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/XBEE/XBEE.cpp Sun Oct 14 05:47:42 2018 +0000 @@ -0,0 +1,197 @@ +#include "XBEE.h" + +/* Class constructor */ +XBEE::XBEE(Serial* serial, int baud):xbee(serial) +{ + xbee->baud(baud); + /* Enable the reception of bytes on the serial interface by providing a cb */ + xbee->attach(callback(this, &XBEE::recvAttach), Serial::RxIrq); + init(); +} + +/* Initialize the Device XBEE */ +void XBEE::init(void) +{ + count_tx = 0; + count_rx = 0; + memset(&buf_tx,0,sizeof(buf_tx)); + memset(&buf_rx,0,sizeof(buf_rx)); +} + +/* Send message to the destination */ +//int XBEE::send_msg(char * str, int dest) +int XBEE::send_msg(char * str) +{ + int n = strlen(str); + if(n > 92) { + return -1; + } + //create_TX(str,dest); + create_TX(str); + sendBuffer(); + wait(0.2); + return 0; +} + +/* Receives a message, writes in str */ +int XBEE::recv_msg(char * buf, int tam) +{ + pkt_tx *p; + int i; + int ptam = 0; + int j = 0; + + memset(buf,0,tam); + + for(i = 0; i < count_rx; i++) { + if(buf_rx[i] == 0x7E) { + + p = (pkt_tx *) &buf_rx[i]; + + if(p->frame_type == 0x90) { + ptam = p->length - 0x0C; + if(tam < ptam + j) { + return -1; + } + // posicao aonde comeca a msg em um pacote de Receive + memcpy(&buf[j], ((char *)(&p->broadcast_radius)), ptam); + j += ptam; + } + } + } + return j; +} + +/* Chamada por Interrupcao, RX */ +void XBEE::recvAttach (void) +{ + //if (serial_RX_ctrl == 1) + while(xbee->readable()) { + buf_rx[count_rx%sizeof(buf_rx)] = xbee->getc(); + count_rx++; + } +} + +/* Envia pacote do buffer_tx */ +void XBEE::sendBuffer(void) +{ + if(xbee->writeable()) { + for(int i = 0; i < count_tx; i++) { + xbee->putc(buf_tx[i]); + } + //pode zerar pacote aqui + } +} + +/* Cria pacote de mensagem */ +//int XBEE::create_TX(char * str, int dest) +int XBEE::create_TX(char * str) +{ + int i = 0; + int n = strlen(str); + uint8_t count = 0; + + pkt_tx *p = (pkt_tx *) buf_tx; // ponteiro para buffer global + + + if(n > 92) { + // Error: data nao pode exceder 92 bytes + return -1; + } + + count_tx = 0; + memset(&buf_tx,0,sizeof(buf_tx)); + + p->start_delimiter = 0x7E; + p->length = 0x0E + n; // pacote vazio + dados + p->frame_type = 0x10; // transmit request + p->frame_id = 1; + + //if(dest == XBEE_COORD) { + p->dest_addr64[6] = 0x00; //SL_H + p->dest_addr64[7] = 0x00; //SL_L + /*} else if(dest == XBEE_BROAD) { + p->dest_addr64[6] = 0xFF; //SL_H + p->dest_addr64[7] = 0xFF; //SL_L + } else { + p->dest_addr64[0] = 0x00; + p->dest_addr64[1] = 0x13; + p->dest_addr64[2] = 0xA2; + p->dest_addr64[3] = 0x00; + + p->dest_addr64[4] = (dest >> 24); + p->dest_addr64[5] = (dest & 0x00FF0000) >> 16; + p->dest_addr64[6] = (dest & 0x0000FF00) >> 8; + p->dest_addr64[7] = (dest & 0x000000FF); + } */ + p->dest_addr16[0] = 0xFF; //MY_H + p->dest_addr16[1] = 0xFF; //MY_L + + memcpy(&p->rf_data, str, n); + + // Calcula Checksum + + // comeca 3 pra frente, tenq passar 3 adiante no final + // para ler o mesmo tamanho de pacote + for(i = 3; i < p->length + 3; i++) { + count += buf_tx[i]; + //printf("\n%d\n", count); + } + count = 0xFF - count; + + p->rf_data[n] = count; + + //p->length tem o tamanho soh do frame, + //pacote total eh o starter + length alto + length baixo + checksum + count_tx = p->length + 4; + return count_tx; +} + +/* Active or Desactive Serial RX */ +/* +void XBEE::read_active(uint8_t val){ + serial_RX_ctrl = val; +}*/ + +/* Printa todo o buffer de Receive, e limpa */ +/* +void XBEE::printBuffer(void) +{ + char flag_msg = 0; + int size = sizeof(buf_rx); + if(count_rx < size) { + size = count_rx; + } + for(int i = 0; i < size; i++) { + printf("%c", buf_rx[i]); + flag_msg++; + } + if (flag_msg) { + printf("\n"); + } + + //Limpa buffer + count_rx = 0; + memset(&buf_rx,0,sizeof(buf_rx)); +} +*/ + +// Verify Packet Checksum return 0 if msg OK, just work with Transmit Package +/* +char XBEE::validPackage(uint8_t * buf, int tam) +{ + int i = 0; + uint8_t count = 0; + + if(buf[0] != 0x7E) return 1; + if(buf[1] != 0) return 2; + if(buf[2] != tam-4) return 3; + + for(i = 3; i < tam; i++) { + count += buf[i]; + } + if(count != 0xFF) return 4; + + return 0; // OK +} +*/