mbed library to connect to rfduino

Dependents:   RFDuino_example

RFDuino.cpp

Committer:
dbarbi1
Date:
2014-01-06
Revision:
0:af5f495861b2
Child:
2:effa15a46f51

File content as of revision 0:af5f495861b2:

#include "RFDuino.h"

//Commands
#define HANDSHAKE  0x11
#define CONNECTED  0x22 
#define TRANSMIT   0x33
#define RECEIVE    0x44



RFDuino::RFDuino(PinName tx, PinName rx): rfd(tx,rx)    {
    //init
        dataFlag=false;
        //attach Serial isr
        rfd.attach(this, &RFDuino::receive_isr);
}



//rfduino seems to take a few seconds to be ready
//for serial comm
bool RFDuino::handshake()   {
    unsigned char temp = 0;
    __disable_irq();

    rfd.putc(HANDSHAKE);
    wait(0.1);
    if(rfd.readable())  {
            temp = rfd.getc();
    }
    __enable_irq();
    if(temp) { return 1;} 
            else { return 0;};


}

bool RFDuino::dataReady()   {
    return dataFlag;
}

bool RFDuino::isConnected() {
    unsigned char temp;
    __disable_irq();
    
    rfd.putc(CONNECTED);
    temp = rfd.getc();
    

  __enable_irq();
    return temp;
}




//needs to be less than 255 bytes
void RFDuino::transmit(unsigned char* buff, int len)    {
    int i;
    __disable_irq();
    
    //send command
    rfd.putc(TRANSMIT);
    rfd.putc((unsigned char)len);
    for(i=0;i<len;i++)  {
        rfd.putc(buff[i]);
    }
    
    __enable_irq();
}


int RFDuino::copyData(unsigned char* buff, int size)    {
    
    memcpy(buff, data.buff, size/*data.len*/);
    dataFlag = false;
    
    return data.len;
}

void RFDuino::receive_isr() {
    
    if(rfd.getc() == RECEIVE)   {
        data.len = (int)rfd.getc();
        for(int i=0;i<data.len;i++) {
            data.buff[i] = rfd.getc();
        }
        //handshake
        //rfd.putc(HANDSHAKE);
        
        dataFlag=true;
    } else  {
    //we dont know this command, read and disregard
        while(rfd.readable())   {
            rfd.getc();
        }
    }
    
}