Library for handling subset of coap functionality by radio transmitter.
Dependencies: nRF24L01P cantcoap
radioWrapper.cpp@1:1d936c763440, 2019-01-20 (annotated)
- Committer:
- Ka_myk
- Date:
- Sun Jan 20 13:48:02 2019 +0000
- Revision:
- 1:1d936c763440
- Parent:
- 0:6a6f97ca5572
- Child:
- 2:c3ca8b8526e0
try to make it work;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ka_myk | 0:6a6f97ca5572 | 1 | #include "radioWrapper.h" |
Ka_myk | 0:6a6f97ca5572 | 2 | #include "mbed.h" |
Ka_myk | 0:6a6f97ca5572 | 3 | |
Ka_myk | 1:1d936c763440 | 4 | int RadioWrapper::read(uint8_t* buffer, int len, int timeout) { |
Ka_myk | 0:6a6f97ca5572 | 5 | Timer t; |
Ka_myk | 1:1d936c763440 | 6 | t.start(); |
Ka_myk | 0:6a6f97ca5572 | 7 | // check if buffor is large enough to conaint packet. |
Ka_myk | 1:1d936c763440 | 8 | if (len < packetSize()) { |
Ka_myk | 0:6a6f97ca5572 | 9 | return -1; |
Ka_myk | 0:6a6f97ca5572 | 10 | } |
Ka_myk | 0:6a6f97ca5572 | 11 | int ret = radio.read(NRF24L01P_PIPE_P0, (char*) buffer, packetSize()); |
Ka_myk | 0:6a6f97ca5572 | 12 | while(ret == 0 && t.read_ms() < timeout) { |
Ka_myk | 0:6a6f97ca5572 | 13 | wait_ms(10); |
Ka_myk | 0:6a6f97ca5572 | 14 | ret = radio.read(NRF24L01P_PIPE_P0, (char*) buffer, packetSize()); |
Ka_myk | 0:6a6f97ca5572 | 15 | } |
Ka_myk | 1:1d936c763440 | 16 | t.stop(); |
Ka_myk | 0:6a6f97ca5572 | 17 | return ret; |
Ka_myk | 0:6a6f97ca5572 | 18 | } |
Ka_myk | 0:6a6f97ca5572 | 19 | |
Ka_myk | 1:1d936c763440 | 20 | int RadioWrapper::write(uint8_t* buffer, int len) { |
Ka_myk | 0:6a6f97ca5572 | 21 | // check if buffor is small enough to send in one package |
Ka_myk | 1:1d936c763440 | 22 | if (len > packetSize()) { |
Ka_myk | 0:6a6f97ca5572 | 23 | return -1; |
Ka_myk | 0:6a6f97ca5572 | 24 | } |
Ka_myk | 0:6a6f97ca5572 | 25 | int ret = radio.write(NRF24L01P_PIPE_P0, (char*) buffer, packetSize()); |
Ka_myk | 0:6a6f97ca5572 | 26 | return ret; |
Ka_myk | 0:6a6f97ca5572 | 27 | } |
Ka_myk | 0:6a6f97ca5572 | 28 | |
Ka_myk | 1:1d936c763440 | 29 | RadioWrapper::RadioWrapper(int channel, unsigned long long rx_address, unsigned long long tx_address) : |
Ka_myk | 1:1d936c763440 | 30 | radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2) { |
Ka_myk | 0:6a6f97ca5572 | 31 | radio.powerDown(); |
Ka_myk | 0:6a6f97ca5572 | 32 | radio.powerUp(); |
Ka_myk | 0:6a6f97ca5572 | 33 | |
Ka_myk | 0:6a6f97ca5572 | 34 | radio.setAirDataRate(DATA_RATE); |
Ka_myk | 0:6a6f97ca5572 | 35 | radio.setRfOutputPower(POWER); |
Ka_myk | 0:6a6f97ca5572 | 36 | radio.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY + 4 * channel); |
Ka_myk | 0:6a6f97ca5572 | 37 | |
Ka_myk | 0:6a6f97ca5572 | 38 | radio.setCrcWidth(NRF24L01P_CRC_8_BIT); |
Ka_myk | 0:6a6f97ca5572 | 39 | |
Ka_myk | 0:6a6f97ca5572 | 40 | radio.setTxAddress(tx_address, 4); |
Ka_myk | 0:6a6f97ca5572 | 41 | radio.setRxAddress(rx_address, 4, NRF24L01P_PIPE_P0); |
Ka_myk | 0:6a6f97ca5572 | 42 | radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P0); |
Ka_myk | 0:6a6f97ca5572 | 43 | } |