Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: nRF24L01P cantcoap3
radioWrapper.cpp@0:6a6f97ca5572, 2019-01-18 (annotated)
- Committer:
- Ka_myk
- Date:
- Fri Jan 18 14:12:24 2019 +0000
- Revision:
- 0:6a6f97ca5572
- Child:
- 1:1d936c763440
initial version
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 | 0:6a6f97ca5572 | 4 | int RadioWrapper::read(uin8_t* buffer, int len, int timeout) { |
| Ka_myk | 0:6a6f97ca5572 | 5 | Timer t; |
| Ka_myk | 0:6a6f97ca5572 | 6 | t.start() |
| Ka_myk | 0:6a6f97ca5572 | 7 | // check if buffor is large enough to conaint packet. |
| Ka_myk | 0:6a6f97ca5572 | 8 | if (len < TRANSFER_SIZE) { |
| 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 | 0:6a6f97ca5572 | 16 | return ret; |
| Ka_myk | 0:6a6f97ca5572 | 17 | } |
| Ka_myk | 0:6a6f97ca5572 | 18 | |
| Ka_myk | 0:6a6f97ca5572 | 19 | int RadioWrapper::write(uin8_t* buffer, int len) { |
| Ka_myk | 0:6a6f97ca5572 | 20 | // check if buffor is small enough to send in one package |
| Ka_myk | 0:6a6f97ca5572 | 21 | if (len > TRANSFER_SIZE) { |
| Ka_myk | 0:6a6f97ca5572 | 22 | return -1; |
| Ka_myk | 0:6a6f97ca5572 | 23 | } |
| Ka_myk | 0:6a6f97ca5572 | 24 | int ret = radio.write(NRF24L01P_PIPE_P0, (char*) buffer, packetSize()); |
| Ka_myk | 0:6a6f97ca5572 | 25 | return ret; |
| Ka_myk | 0:6a6f97ca5572 | 26 | } |
| Ka_myk | 0:6a6f97ca5572 | 27 | |
| Ka_myk | 0:6a6f97ca5572 | 28 | RadioWrapper::RadioWrapper(int channel, unsigned long long rx_address, unsigned long long tx_address) { |
| Ka_myk | 0:6a6f97ca5572 | 29 | radio.powerDown(); |
| Ka_myk | 0:6a6f97ca5572 | 30 | radio.powerUp(); |
| Ka_myk | 0:6a6f97ca5572 | 31 | |
| Ka_myk | 0:6a6f97ca5572 | 32 | radio.setAirDataRate(DATA_RATE); |
| Ka_myk | 0:6a6f97ca5572 | 33 | radio.setRfOutputPower(POWER); |
| Ka_myk | 0:6a6f97ca5572 | 34 | radio.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY + 4 * channel); |
| Ka_myk | 0:6a6f97ca5572 | 35 | |
| Ka_myk | 0:6a6f97ca5572 | 36 | radio.setCrcWidth(NRF24L01P_CRC_8_BIT); |
| Ka_myk | 0:6a6f97ca5572 | 37 | |
| Ka_myk | 0:6a6f97ca5572 | 38 | radio.setTxAddress(tx_address, 4); |
| Ka_myk | 0:6a6f97ca5572 | 39 | radio.setRxAddress(rx_address, 4, NRF24L01P_PIPE_P0); |
| Ka_myk | 0:6a6f97ca5572 | 40 | radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P0); |
| Ka_myk | 0:6a6f97ca5572 | 41 | } |