Library for handling subset of coap functionality by radio transmitter.

Dependencies:   nRF24L01P cantcoap

Dependents:   server2

Committer:
thewiztory
Date:
Fri Jan 25 02:10:16 2019 +0000
Revision:
5:b8d21be6b36c
Parent:
4:9f635ab44d8e
-

Who changed what in which revision?

UserRevisionLine numberNew 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 4:9f635ab44d8e 5 int RadioWrapper::read(uint8_t* buffer, int len, int timeout, int *pipe) {
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 4:9f635ab44d8e 12 int ret = -1;
Ka_myk 2:c3ca8b8526e0 13 while(t.read_ms() < timeout) {
thewiztory 5:b8d21be6b36c 14 for(int i = 1; i <= 3; ++i) {
Ka_myk 4:9f635ab44d8e 15 ret = readFromPipe(buffer, len, i);
Ka_myk 4:9f635ab44d8e 16 if(ret >0) {
Ka_myk 4:9f635ab44d8e 17 *pipe = i;
Ka_myk 4:9f635ab44d8e 18 return ret;
Ka_myk 4:9f635ab44d8e 19 }
Ka_myk 2:c3ca8b8526e0 20 }
Ka_myk 0:6a6f97ca5572 21 }
Ka_myk 1:1d936c763440 22 t.stop();
Ka_myk 2:c3ca8b8526e0 23 return 0;
Ka_myk 0:6a6f97ca5572 24 }
Ka_myk 0:6a6f97ca5572 25
Ka_myk 4:9f635ab44d8e 26 int RadioWrapper::readFromPipe(uint8_t* buffer, int len, int pipe) {
Ka_myk 4:9f635ab44d8e 27 if(radio.readable(pipe)) {
Ka_myk 4:9f635ab44d8e 28 uint8_t message[packetSize()];
Ka_myk 4:9f635ab44d8e 29 int ret = radio.read(pipe, (char*) message, packetSize());
Ka_myk 4:9f635ab44d8e 30 DBG("RECIEVED %d, %s \r\n", ret, buffer);
Ka_myk 4:9f635ab44d8e 31 if(ret>0) {
Ka_myk 4:9f635ab44d8e 32 uint8_t encodedLen = message[0];
Ka_myk 4:9f635ab44d8e 33 std::memcpy(buffer, message+1, encodedLen);
Ka_myk 4:9f635ab44d8e 34 return encodedLen;
Ka_myk 4:9f635ab44d8e 35 } else {
Ka_myk 4:9f635ab44d8e 36 return ret;
Ka_myk 4:9f635ab44d8e 37 }
Ka_myk 4:9f635ab44d8e 38 }
Ka_myk 4:9f635ab44d8e 39 return 0;
Ka_myk 4:9f635ab44d8e 40 }
Ka_myk 4:9f635ab44d8e 41
Ka_myk 4:9f635ab44d8e 42
Ka_myk 4:9f635ab44d8e 43 int RadioWrapper::write(uint8_t* buffer, uint8_t len, int pipe) {
Ka_myk 4:9f635ab44d8e 44 // check if buffor is small enough to send in one package + len
Ka_myk 1:1d936c763440 45 if (len > packetSize()) {
Ka_myk 0:6a6f97ca5572 46 return -1;
Ka_myk 0:6a6f97ca5572 47 }
Ka_myk 4:9f635ab44d8e 48
Ka_myk 4:9f635ab44d8e 49 uint8_t message[packetSize()];
Ka_myk 4:9f635ab44d8e 50 message[0] = len;
Ka_myk 4:9f635ab44d8e 51 std::memcpy(message+1, buffer, len);
Ka_myk 4:9f635ab44d8e 52 int ret = radio.write(pipe, (char*) message, packetSize());
Ka_myk 0:6a6f97ca5572 53 return ret;
Ka_myk 0:6a6f97ca5572 54 }
Ka_myk 0:6a6f97ca5572 55
Ka_myk 4:9f635ab44d8e 56 RadioWrapper::RadioWrapper(int channel, unsigned long long tx_address, unsigned long long rx_addresses[]) :
Ka_myk 4:9f635ab44d8e 57 radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2) {
Ka_myk 0:6a6f97ca5572 58 radio.powerDown();
Ka_myk 0:6a6f97ca5572 59 radio.powerUp();
Ka_myk 0:6a6f97ca5572 60
Ka_myk 0:6a6f97ca5572 61 radio.setAirDataRate(DATA_RATE);
Ka_myk 0:6a6f97ca5572 62 radio.setRfOutputPower(POWER);
Ka_myk 0:6a6f97ca5572 63 radio.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY + 4 * channel);
Ka_myk 0:6a6f97ca5572 64
Ka_myk 0:6a6f97ca5572 65 radio.setCrcWidth(NRF24L01P_CRC_8_BIT);
Ka_myk 0:6a6f97ca5572 66
Ka_myk 0:6a6f97ca5572 67 radio.setTxAddress(tx_address, 4);
Ka_myk 2:c3ca8b8526e0 68 radio.setRxAddress(tx_address, 4, NRF24L01P_PIPE_P0);
Ka_myk 4:9f635ab44d8e 69 radio.setRxAddress(rx_addresses[0], 4, NRF24L01P_PIPE_P1);
Ka_myk 4:9f635ab44d8e 70 radio.setRxAddress(rx_addresses[1], 4, NRF24L01P_PIPE_P2);
Ka_myk 4:9f635ab44d8e 71 radio.setRxAddress(rx_addresses[2], 4, NRF24L01P_PIPE_P3);
Ka_myk 4:9f635ab44d8e 72
Ka_myk 0:6a6f97ca5572 73 radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P0);
Ka_myk 2:c3ca8b8526e0 74 radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P1);
Ka_myk 4:9f635ab44d8e 75 radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P2);
Ka_myk 4:9f635ab44d8e 76 radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P3);
Ka_myk 4:9f635ab44d8e 77
Ka_myk 4:9f635ab44d8e 78
Ka_myk 2:c3ca8b8526e0 79 radio.setReceiveMode();
Ka_myk 2:c3ca8b8526e0 80 radio.enable();
Ka_myk 2:c3ca8b8526e0 81
Ka_myk 2:c3ca8b8526e0 82 // Display the (default) setup of the nRF24L01+ chip
Ka_myk 2:c3ca8b8526e0 83 DBG( "nRF24L01+ Frequency : %d MHz\r\n", radio.getRfFrequency() );
Ka_myk 2:c3ca8b8526e0 84 DBG( "nRF24L01+ Output power : %d dBm\r\n", radio.getRfOutputPower() );
Ka_myk 2:c3ca8b8526e0 85 DBG( "nRF24L01+ Data Rate : %d kbps\r\n", radio.getAirDataRate() );
Ka_myk 2:c3ca8b8526e0 86 DBG( "nRF24L01+ TX Address : 0x%010llX\r\n", radio.getTxAddress() );
Ka_myk 2:c3ca8b8526e0 87 DBG( "nRF24L01+ RX0 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P0) );
Ka_myk 2:c3ca8b8526e0 88 DBG( "nRF24L01+ RX1 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P1) );
Ka_myk 0:6a6f97ca5572 89 }