Library for handling subset of coap functionality by radio transmitter.

Dependencies:   nRF24L01P cantcoap

Dependents:   server2

Revision:
2:c3ca8b8526e0
Parent:
1:1d936c763440
Child:
4:9f635ab44d8e
--- a/radioWrapper.cpp	Sun Jan 20 13:48:02 2019 +0000
+++ b/radioWrapper.cpp	Sun Jan 20 23:27:10 2019 +0000
@@ -1,5 +1,6 @@
 #include "radioWrapper.h"
 #include "mbed.h"
+#include "dbg.h"
 
 int RadioWrapper::read(uint8_t* buffer, int len, int timeout) {
     Timer t;
@@ -8,13 +9,17 @@
     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());
+    while(t.read_ms() < timeout) {
+        if(radio.readable(NRF24L01P_PIPE_P1)) {
+            DBG("%d", t.read_ms());
+            int ret = radio.read(NRF24L01P_PIPE_P1, (char*) buffer, packetSize());
+            DBG("RECIEVED %d, %s \r\n", ret, buffer);
+            t.stop();
+            return ret;
+        }
     }
     t.stop();
-    return ret;
+    return 0;
 }
 
 int RadioWrapper::write(uint8_t* buffer, int len) {
@@ -27,7 +32,7 @@
 }
 
 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(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2) {
     radio.powerDown();
     radio.powerUp();
 
@@ -38,6 +43,19 @@
     radio.setCrcWidth(NRF24L01P_CRC_8_BIT);
 
     radio.setTxAddress(tx_address, 4);
-    radio.setRxAddress(rx_address, 4, NRF24L01P_PIPE_P0);
+    radio.setRxAddress(tx_address, 4, NRF24L01P_PIPE_P0);
+    radio.setRxAddress(rx_address, 4, NRF24L01P_PIPE_P1);
     radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P0);
+    radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P1);
+    radio.enableDynamicPayload(NRF24L01P_PIPE_P1);
+    radio.setReceiveMode();
+    radio.enable();
+
+    // Display the (default) setup of the nRF24L01+ chip
+    DBG( "nRF24L01+ Frequency    : %d MHz\r\n",  radio.getRfFrequency() );
+    DBG( "nRF24L01+ Output power : %d dBm\r\n",  radio.getRfOutputPower() );
+    DBG( "nRF24L01+ Data Rate    : %d kbps\r\n", radio.getAirDataRate() );
+    DBG( "nRF24L01+ TX Address   : 0x%010llX\r\n", radio.getTxAddress() );
+    DBG( "nRF24L01+ RX0 Address   : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P0) );
+    DBG( "nRF24L01+ RX1 Address   : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P1) );
 }