IoT - Kubus / Mbed 2 deprecated Kubus

Dependencies:   mbed nRF24L01P

Revision:
56:065bd3a75d97
Parent:
54:2551a3c781cf
Child:
60:b419ea9472de
--- a/master.cpp	Thu Jan 19 02:42:15 2017 +0100
+++ b/master.cpp	Thu Jan 19 11:26:38 2017 +0100
@@ -4,23 +4,21 @@
 #include "cantcoap.h"
 #include <sstream>
 
-const int boards_count = 3;
-
-
 int master_loop() {
     Serial pc(USBTX, USBRX); // tx, rx
     Serial wifi(PA_9, PA_10);
     DigitalOut frm(PC_8);
     nRF24L01P radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2);    // mosi, miso, sck, csn, ce, irq
 
+    const unsigned long long RX_ADDRESS = MASTER_ADDRESS;
+    const unsigned long long TX_ADDRESS = BOARD1_ADDRESS;
+
     char rxData[TRANSFER_SIZE];
 
     pc.baud(115200);
     wifi.baud(115200);
 
-    vector<unsigned long long> rx_addresses(addresses, addresses + boards_count);
-
-    radio_init_master(&radio, rx_addresses);
+    radio_init(&radio, RX_ADDRESS, TX_ADDRESS);
 
     // Display the (default) setup of the nRF24L01+ chip
     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  radio.getRfFrequency() );
@@ -29,61 +27,55 @@
     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", radio.getTxAddress() );
     pc.printf( "nRF24L01+ RX0 Address   : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P0) );
     pc.printf( "nRF24L01+ RX1 Address   : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P1) );
-    pc.printf( "nRF24L01+ RX2 Address   : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P2) );
-    pc.printf( "nRF24L01+ RX3 Address   : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P3) );
 
     frm = 1;
     while (1) {
-        for (int board = 0; board < boards_count; ++board) {
-            int pipe = pipes[board];
-            //pc.printf("pipe: %x\r\n", pipe);
-            if(radio.readable(pipe)){
-                int rx_bytes = radio.read(pipe, rxData, sizeof(rxData));
-                CoapPDU pdu;
-                pdu.setVersion(1);
-                pdu.setType(CoapPDU::COAP_NON_CONFIRMABLE);
-                pdu.setCode(CoapPDU::COAP_POST);
-                pdu.setToken((uint8_t*)"\3\2\1\1",4);
-                pdu.setMessageID(0x0005);
-                std::string output = str_hex(rxData, rx_bytes);
-                //wifi.printf("received: %s\r\n", output.c_str());
-                pc.printf("received: %s\r\n", output.c_str());
-                Data d;
-                d.deserialize(std::string(rxData, 32));
-                std::stringstream ss;
+        if(radio.readable(NRF24L01P_PIPE_P1)){
+            int rx_bytes = radio.read(NRF24L01P_PIPE_P1, rxData, sizeof(rxData));
+            CoapPDU pdu;
+            pdu.setVersion(1);
+            pdu.setType(CoapPDU::COAP_NON_CONFIRMABLE);
+            pdu.setCode(CoapPDU::COAP_POST);
+            pdu.setToken((uint8_t*)"\3\2\1\1",4);
+            pdu.setMessageID(0x0005);
+            std::string output = str_hex(rxData, rx_bytes);
+            //wifi.printf("received: %s\r\n", output.c_str());
+            pc.printf("received: %s\r\n", output.c_str());
+            Data d;
+            d.deserialize(std::string(rxData, 32));
+            std::stringstream ss;
 
-                for (int i = 0; i < rx_bytes; ++i) {
-                    ss << std::hex << rxData[i] / 16 << rxData[i] % 16; // ugly!
-                }
-                pc.printf("received: %s|id %d \r\n", ss.str().c_str(), d.type);
-                //wifi.printf("received: %s, pkt:", ss.str().c_str());
+            for (int i = 0; i < rx_bytes; ++i) {
+                ss << std::hex << rxData[i] / 16 << rxData[i] % 16; // ugly!
+            }
+            pc.printf("received: %s|id %d \r\n", ss.str().c_str(), d.type);
+            //wifi.printf("received: %s, pkt:", ss.str().c_str());
 
-                char uribuf[64];
-                switch(d.type) {
-                    case PIR1:
-                        strcpy(uribuf, "pir1");
-                        break;
-                    case PIR2:
-                        strcpy(uribuf, "pir2");
-                        break;
-                    case DISTANCE:
-                        strcpy(uribuf, "distance");
-                        break;
-                    case SOUND:
-                        strcpy(uribuf, "sound");
-                        break;
-                }
-                pdu.setURI(uribuf,strlen(uribuf));
+            char uribuf[64];
+            switch(d.type) {
+                case PIR1:
+                    strcpy(uribuf, "pir1");
+                    break;
+                case PIR2:
+                    strcpy(uribuf, "pir2");
+                    break;
+                case DISTANCE:
+                    strcpy(uribuf, "distance");
+                    break;
+                case SOUND:
+                    strcpy(uribuf, "sound");
+                    break;
+            }
+            pdu.setURI(uribuf,strlen(uribuf));
 
-                char pbuf[32];
-                int psize = sprintf(pbuf, "%ld", d.value.i);
-                pdu.setPayload((uint8_t*)pbuf, psize);
-                frm = 0;
-                wifi.write(pdu.getPDUPointer(), pdu.getPDULength(), 0, 0);
-                wait(0.1);
-                frm = 1;
-                //wifi.printf("\n");
-            }
+            char pbuf[32];
+            int psize = sprintf(pbuf, "%ld", d.value.i);
+            pdu.setPayload((uint8_t*)pbuf, psize);
+            frm = 0;
+            wifi.write(pdu.getPDUPointer(), pdu.getPDULength(), 0, 0);
+            wait(0.1);
+            frm = 1;
+            //wifi.printf("\n");
         }
     }
 }