Wireless / Mbed 2 deprecated spi_buttons_leds

Dependencies:   mbed nRF24L01P

Revision:
6:c3d9f6a28df6
Parent:
5:7891519b8809
Child:
7:43bb8be87709
diff -r 7891519b8809 -r c3d9f6a28df6 main.cpp
--- a/main.cpp	Mon Dec 10 13:26:08 2018 +0000
+++ b/main.cpp	Mon Dec 10 13:51:23 2018 +0000
@@ -13,6 +13,8 @@
 int rxDataCnt = 0;
 
 int led = 0;
+bool ackknowledged = true;
+
     
 void init() {
     cs = 0;
@@ -53,27 +55,6 @@
     cs = 1;
 }
 
-void sendNumber(int number) {
-    printf("Sending led: %d\n\r", number);
-    // ...add it to the transmit buffer
-    txData[txDataCnt++] = 0;
-    txData[txDataCnt++] = (number);
-    
-    bool ackReceived = false;
-
-    // If the transmit buffer is full
-    if ( txDataCnt >= sizeof( txData ) ) {
-        for(int i = 0; i <= txDataCnt; i++){
-            printf("%c\n\r", txData[i]);
-        }
-
-        // Send the transmitbuffer via the nRF24L01+
-        my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
-
-        txDataCnt = 0;
-    }
-}
-
 void readNRF() {
 // If we've received anything in the nRF24L01+...
     if ( my_nrf24l01p.readable() ) {
@@ -83,9 +64,36 @@
 
         // Display the receive buffer contents via the host serial link
         printf("Received %d for: %d\n\r", rxData[0], rxData[1]);
+        
+        if (rxData[0] == 0) {
+            txData[0] = 1;
+            txData[1] = rxData[1];
+            my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE );
+        }
+        if (rxData[0] == 1) {
+            ackknowledged = true;
+            setLed(rxData[1]);    
+        }
     }    
 }
 
+void sendNumber(int number) {
+    
+    // ...add it to the transmit buffer
+    txData[0] = 0;
+    txData[1] = number;
+    
+    ackknowledged = false;
+    
+    while (!ackknowledged) {
+        printf("Sending led: %d\n\r", number);
+        // Send the transmitbuffer via the nRF24L01+
+        my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE );
+        readNRF();
+        wait(0.5);
+    }
+}
+
 void readButton() {
     cs = 0;
     spi.write(0x47);
@@ -102,8 +110,6 @@
         led = (led + 5) % 6;
         sendNumber(led);
     }
-    
-    setLed(led);
 }
 
 int main() {