master to control sensor node

Dependencies:   mbed

gateway.cpp

Committer:
mfrede
Date:
2015-11-17
Revision:
6:9daa56b8a027
Parent:
5:85fea4ceaa1f

File content as of revision 6:9daa56b8a027:

#include "mbed.h"
#include "MRF24J40.h"
#include <string>
#include "packet.h"

#define NODE 1

//unsigned short TYPE = 4;

// RF tranceiver to link with handheld.
MRF24J40 mrf(p11, p12, p13, p14, p21);

DigitalOut myled(LED1);

// Serial port for showing RX data.
Serial pc(USBTX, USBRX);

// Used for sending and receiving
char txBuffer[128];
char rxBuffer[128];
int rxLen;

packet* sxBuffer;
packet* gxBuffer;

//***************** Do not change these methods (please) *****************//

/**
* Receive data from the MRF24J40.
*
* @param data A pointer to a char array to hold the data
* @param maxLength The max amount of data to read.
*/
int rf_receive(char *data, uint8_t maxLength, uint8_t *rssi)
{
    uint8_t len = mrf.Receive((uint8_t *)data, maxLength,rssi);
    uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};

    if(len > 10) {
        //Remove the header and footer of the message
        for(uint8_t i = 0; i < len-2; i++) {
            if(i<8) {
                //Make sure our header is valid first
                if(data[i] != header[i])
                    return 0;
            } else {
                data[i-8] = data[i];
            }
        }

        //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10);
    }
    return ((int)len)-10;
}

/**
* Send data to another MRF24J40.
*
* @param data The string to send
* @param maxLength The length of the data to send.
*                  If you are sending a null-terminated string you can pass strlen(data)+1
*/
void rf_send(packet *data, uint8_t len)
{
    
    uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
    uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) );

    for(uint8_t i = 0; i < len+8; i++) {
        //prepend the 8-byte header
        send_buf[i] = (i<8) ? header[i] : ((uint8_t *) data)[i-8];
    }
      
    mrf.Send((uint8_t *) data, (len*4)+12);
    //free(send_buf);
}

int main() {
    mrf.SetChannel(15);
    int nodes = 6;
  //  char buffer[128];
    
    
        packet buffer(NODE,2,HELLO_TYPE,4.0,5.0);
        rf_send(&buffer, 2);
        pc.printf("sent\r\n");
    /* while(1) {
       pc.printf("Select operation: \r\n");
       pc.printf("1) get node list\r\n");
       pc.printf("2) Update data rates\r\n");
       pc.gets(buffer,1);
       switch (buffer[0]) {
           case '1':
           
           for(int i=0;i<nodes;i++)
           {
               pc.printf("Node %d Neighbors:\r\n");
               for (int j=0;j<neighbors[i][0];j++)
               {
                   pc.printf("Node %d: %d cycles\r\n",neighbors[i][j],cycles[i][j]);
               }
            }
            break;
            case '2':
            pc.printf("How many seconds between updates? ");
            pc.gets(buffer,2);
            int secs = atoi(buffer);
            //build data packet
            
            //broadcast type
            
            //send data packet
            
            break;
            default:
            pc.printf("unknown command\r\n");
            break;
        }
   */
       //}
}