client version of coap

Dependencies:   nRF24L01P cantcoap3

Dependents:   client3

radioWrapper.cpp

Committer:
Ka_myk
Date:
2019-01-18
Revision:
0:6a6f97ca5572
Child:
1:1d936c763440

File content as of revision 0:6a6f97ca5572:

#include "radioWrapper.h"
#include "mbed.h"

int RadioWrapper::read(uin8_t* buffer, int len, int timeout) {
    Timer t;
    t.start()
    // check if buffor is large enough to conaint packet.
    if (len < TRANSFER_SIZE) {
        return -1;
    }
    int ret = radio.read(NRF24L01P_PIPE_P0, (char*) buffer, packetSize());
    while(ret == 0 && t.read_ms() < timeout) {    
        wait_ms(10);
        ret = radio.read(NRF24L01P_PIPE_P0, (char*) buffer, packetSize());
    }
    return ret;
}

int RadioWrapper::write(uin8_t* buffer, int len) {
    // check if buffor is small enough to send in one package
    if (len > TRANSFER_SIZE) {
        return -1;
    }
    int ret = radio.write(NRF24L01P_PIPE_P0, (char*) buffer, packetSize());
    return ret;
}

RadioWrapper::RadioWrapper(int channel, unsigned long long rx_address, unsigned long long tx_address) {
    radio.powerDown();
    radio.powerUp();

    radio.setAirDataRate(DATA_RATE);
    radio.setRfOutputPower(POWER);
    radio.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY + 4 * channel);

    radio.setCrcWidth(NRF24L01P_CRC_8_BIT);

    radio.setTxAddress(tx_address, 4);
    radio.setRxAddress(rx_address, 4, NRF24L01P_PIPE_P0);
    radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P0);
}