client version of coap

Dependencies:   nRF24L01P cantcoap3

Dependents:   client3

radioWrapper.cpp

Committer:
Ka_myk
Date:
2019-01-20
Revision:
1:1d936c763440
Parent:
0:6a6f97ca5572
Child:
2:e8823d9fa162

File content as of revision 1:1d936c763440:

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

int RadioWrapper::read(uint8_t* buffer, int len, int timeout) {
    Timer t;
    t.start();
    // check if buffor is large enough to conaint packet.
    if (len < packetSize()) {
        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());
    }
    t.stop();
    return ret;
}

int RadioWrapper::write(uint8_t* buffer, int len) {
    // check if buffor is small enough to send in one package
    if (len > packetSize()) {
        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(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2) {
    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);
}