Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Radio.cpp

Committer:
elmbed
Date:
2016-01-08
Revision:
38:76e49d045a3b
Parent:
33:0fa936c5a098
Child:
44:4ad6133987ed

File content as of revision 38:76e49d045a3b:

#include <RFM69.h>
#include <SPI.h>
#include "types.h"
#include "TA.h"

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

#define FREQUENCY     RF69_915MHZ

//#define IS_RFM69HW   //NOTE: uncomment this ONLY for RFM69HW or RFM69HCW
#define ENCRYPT_KEY    "EncryptKey123456"  // use same 16byte encryption key for all devices on net
#define ACK_TIME       50                  // max msec for ACK wait
#define LED            9                   // Anardino miniWireless has LEDs on D9
#define SERIAL_BAUD    115200
#define VERSION  "1.0"

#define MSGBUFSIZE 64   // message buffersize, but for this demo we only use: 
                        // 1-byte NODEID + 4-bytes for time + 1-byte for temp in C + 2-bytes for vcc(mV)

//RFM69::RFM69(PinName  PinName mosi, PinName miso, PinName sclk,slaveSelectPin, PinName int)
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
static bool requestACK = false;

static char phone_buffer[50] = {0};
static char radio_buffer[50] = {0};

extern "C" void writeToPhone(char *format, ...);


void radio_init()
{
  radio.initialize(FREQUENCY, NODE_ID, NETWORKID);
  radio.encrypt(0);
  radio.promiscuous(promiscuousMode);
}



void radio_send(Message *m)
{
    static byte payload [6] = {0};
 
    if (m == NULL)
    {
        return;    
    }
    
    //writeToPhone("SM: %c to: %d frm: %d (%d)\r\n", m->command, m->cone, NODE_ID, m->value);  /// DEBUG-only message
    
    payload[0] = (byte)m->command;
    payload[4] = (byte)m->value & 255;
    payload[3] = (byte)(m->value >> 8);
    payload[2] = (byte)(m->value >> 16);
    payload[1] = (byte)(m->value >> 24);
    payload[5] = (byte)'%';
 
    radio.send(m->cone, payload, sizeof(payload));     
}

bool radio_receive_complete()
{
    return radio.receiveDone();    
}

bool radio_receive(Message *m)
{
    if (m == NULL)
    {
        return false;    
    }
    
    if (radio.receiveDone())
    {
        //writeToPhone("Received: %d bytes", radio.DATALEN);  /// DEBUG-only message
        if (radio.DATALEN < 6)
        {
            return false;    
        }
        
        //writeToPhone("GM: %d\r\n", radio.SENDERID);  /// DEBUG-only message
        if (radio.TARGETID == NODE_ID)
        {
            m->cone     = radio.SENDERID;
            m->command  = radio.DATA[0];
            m->value    = (radio.DATA[1] >> 24);
            m->value   |= (radio.DATA[2] >> 16);
            m->value   |= (radio.DATA[3] >> 8);
            m->value   |= (radio.DATA[4]);
            
            writeToPhone("GM: %d %c %d\r\n", radio.SENDERID, m->command, m->value);  /// DEBUG-only message

            return true;
        }
    }
    
    return false;
}

bool radio_ack_received(int cone)
{
    return radio.ACKReceived(cone);
}

void radio_loop()
{
  static int counter = 0;
  
  if (radio.receiveDone()) 
  {
     snprintf(phone_buffer, sizeof(phone_buffer), "%d.\r\n[%s]\r\n", radio.SENDERID, radio.DATA);
     writeToPhone(phone_buffer);

     if (radio.ACKRequested())
     {
        radio.sendACK();
     } 
  }    
}