Library for handling subset of coap functionality by radio transmitter.
Dependencies: nRF24L01P cantcoap
radioWrapper.cpp@2:c3ca8b8526e0, 2019-01-20 (annotated)
- Committer:
- Ka_myk
- Date:
- Sun Jan 20 23:27:10 2019 +0000
- Revision:
- 2:c3ca8b8526e0
- Parent:
- 1:1d936c763440
- Child:
- 4:9f635ab44d8e
should work somehow;
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 | 2:c3ca8b8526e0 | 3 | #include "dbg.h" |
Ka_myk | 0:6a6f97ca5572 | 4 | |
Ka_myk | 1:1d936c763440 | 5 | int RadioWrapper::read(uint8_t* buffer, int len, int timeout) { |
Ka_myk | 0:6a6f97ca5572 | 6 | Timer t; |
Ka_myk | 1:1d936c763440 | 7 | t.start(); |
Ka_myk | 0:6a6f97ca5572 | 8 | // check if buffor is large enough to conaint packet. |
Ka_myk | 1:1d936c763440 | 9 | if (len < packetSize()) { |
Ka_myk | 0:6a6f97ca5572 | 10 | return -1; |
Ka_myk | 0:6a6f97ca5572 | 11 | } |
Ka_myk | 2:c3ca8b8526e0 | 12 | while(t.read_ms() < timeout) { |
Ka_myk | 2:c3ca8b8526e0 | 13 | if(radio.readable(NRF24L01P_PIPE_P1)) { |
Ka_myk | 2:c3ca8b8526e0 | 14 | DBG("%d", t.read_ms()); |
Ka_myk | 2:c3ca8b8526e0 | 15 | int ret = radio.read(NRF24L01P_PIPE_P1, (char*) buffer, packetSize()); |
Ka_myk | 2:c3ca8b8526e0 | 16 | DBG("RECIEVED %d, %s \r\n", ret, buffer); |
Ka_myk | 2:c3ca8b8526e0 | 17 | t.stop(); |
Ka_myk | 2:c3ca8b8526e0 | 18 | return ret; |
Ka_myk | 2:c3ca8b8526e0 | 19 | } |
Ka_myk | 0:6a6f97ca5572 | 20 | } |
Ka_myk | 1:1d936c763440 | 21 | t.stop(); |
Ka_myk | 2:c3ca8b8526e0 | 22 | return 0; |
Ka_myk | 0:6a6f97ca5572 | 23 | } |
Ka_myk | 0:6a6f97ca5572 | 24 | |
Ka_myk | 1:1d936c763440 | 25 | int RadioWrapper::write(uint8_t* buffer, int len) { |
Ka_myk | 0:6a6f97ca5572 | 26 | // check if buffor is small enough to send in one package |
Ka_myk | 1:1d936c763440 | 27 | if (len > packetSize()) { |
Ka_myk | 0:6a6f97ca5572 | 28 | return -1; |
Ka_myk | 0:6a6f97ca5572 | 29 | } |
Ka_myk | 0:6a6f97ca5572 | 30 | int ret = radio.write(NRF24L01P_PIPE_P0, (char*) buffer, packetSize()); |
Ka_myk | 0:6a6f97ca5572 | 31 | return ret; |
Ka_myk | 0:6a6f97ca5572 | 32 | } |
Ka_myk | 0:6a6f97ca5572 | 33 | |
Ka_myk | 1:1d936c763440 | 34 | RadioWrapper::RadioWrapper(int channel, unsigned long long rx_address, unsigned long long tx_address) : |
Ka_myk | 2:c3ca8b8526e0 | 35 | radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2) { |
Ka_myk | 0:6a6f97ca5572 | 36 | radio.powerDown(); |
Ka_myk | 0:6a6f97ca5572 | 37 | radio.powerUp(); |
Ka_myk | 0:6a6f97ca5572 | 38 | |
Ka_myk | 0:6a6f97ca5572 | 39 | radio.setAirDataRate(DATA_RATE); |
Ka_myk | 0:6a6f97ca5572 | 40 | radio.setRfOutputPower(POWER); |
Ka_myk | 0:6a6f97ca5572 | 41 | radio.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY + 4 * channel); |
Ka_myk | 0:6a6f97ca5572 | 42 | |
Ka_myk | 0:6a6f97ca5572 | 43 | radio.setCrcWidth(NRF24L01P_CRC_8_BIT); |
Ka_myk | 0:6a6f97ca5572 | 44 | |
Ka_myk | 0:6a6f97ca5572 | 45 | radio.setTxAddress(tx_address, 4); |
Ka_myk | 2:c3ca8b8526e0 | 46 | radio.setRxAddress(tx_address, 4, NRF24L01P_PIPE_P0); |
Ka_myk | 2:c3ca8b8526e0 | 47 | radio.setRxAddress(rx_address, 4, NRF24L01P_PIPE_P1); |
Ka_myk | 0:6a6f97ca5572 | 48 | radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P0); |
Ka_myk | 2:c3ca8b8526e0 | 49 | radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P1); |
Ka_myk | 2:c3ca8b8526e0 | 50 | radio.enableDynamicPayload(NRF24L01P_PIPE_P1); |
Ka_myk | 2:c3ca8b8526e0 | 51 | radio.setReceiveMode(); |
Ka_myk | 2:c3ca8b8526e0 | 52 | radio.enable(); |
Ka_myk | 2:c3ca8b8526e0 | 53 | |
Ka_myk | 2:c3ca8b8526e0 | 54 | // Display the (default) setup of the nRF24L01+ chip |
Ka_myk | 2:c3ca8b8526e0 | 55 | DBG( "nRF24L01+ Frequency : %d MHz\r\n", radio.getRfFrequency() ); |
Ka_myk | 2:c3ca8b8526e0 | 56 | DBG( "nRF24L01+ Output power : %d dBm\r\n", radio.getRfOutputPower() ); |
Ka_myk | 2:c3ca8b8526e0 | 57 | DBG( "nRF24L01+ Data Rate : %d kbps\r\n", radio.getAirDataRate() ); |
Ka_myk | 2:c3ca8b8526e0 | 58 | DBG( "nRF24L01+ TX Address : 0x%010llX\r\n", radio.getTxAddress() ); |
Ka_myk | 2:c3ca8b8526e0 | 59 | DBG( "nRF24L01+ RX0 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P0) ); |
Ka_myk | 2:c3ca8b8526e0 | 60 | DBG( "nRF24L01+ RX1 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P1) ); |
Ka_myk | 0:6a6f97ca5572 | 61 | } |