football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

main.cpp

Committer:
elmbed
Date:
2016-04-27
Revision:
76:6638d83939d5
Parent:
75:1b357bee1839
Child:
78:43a6b54f0372

File content as of revision 76:6638d83939d5:

#include <RFM69.h>
#include <SPI.h>

#define NODE_ID 1     // ID of the current node
#define SEND_TO_ID 2  // ID to send messages to 

#define NETWORKID     101   //the same on all nodes that talk to each other

#define FREQUENCY     RF69_915MHZ

Serial pc(USBTX, USBRX);

static RFM69 radio(P0_24,P0_23,P0_25,P0_28,P0_7);
static bool promiscuousMode = true; // set 'true' to sniff all packets on the same network

int main()
{
    unsigned long last_send = 0L;
    char tx_buff[100] = {0};
    char rx_buff[100] = {0};
    
    pc.baud(9600);
    pc.printf("RFM69 test starting with ID: %d\r\n", NODE_ID);
    
    radio.initialize(FREQUENCY, NODE_ID, NETWORKID);
    radio.encrypt(0);
    radio.promiscuous(promiscuousMode);

    Timer t;
    t.start();

    while (true)
    {
        unsigned long current_time = t.read_ms();
        
        if (current_time - last_send > 2000L)
        {
            // Send message
            snprintf(tx_buff, sizeof(tx_buff), "msg frm: %d at %lu", NODE_ID, current_time);
            radio.send(SEND_TO_ID, tx_buff, strlen(tx_buff));
            last_send = current_time;    
            
            pc.printf("Sent: %s\r\n", tx_buff);
        }
        
        if (radio.receiveDone())
        {
            memset(rx_buff, 0x00, sizeof(rx_buff));
            memcpy(rx_buff, (char*)radio.DATA, radio.DATALEN > sizeof(rx_buff)-1 ? sizeof(rx_buff)-1 : radio.DATALEN);
            
            pc.printf("Got: %s\r\n", rx_buff);
        }
    }
}