Wireless / Mbed 2 deprecated spi_buttons_leds

Dependencies:   mbed nRF24L01P

Revision:
5:7891519b8809
Parent:
4:ae1520241a90
Child:
6:c3d9f6a28df6
diff -r ae1520241a90 -r 7891519b8809 main.cpp
--- a/main.cpp	Tue Dec 04 13:36:13 2018 +0000
+++ b/main.cpp	Mon Dec 10 13:26:08 2018 +0000
@@ -7,10 +7,12 @@
 nRF24L01P my_nrf24l01p(D11, D12, D13, D8, D9, D7);    // mosi, miso, sck, csn, ce, irq
 
     
-#define TRANSFER_SIZE   4
+#define TRANSFER_SIZE   2
 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
 int txDataCnt = 0;
 int rxDataCnt = 0;
+
+int led = 0;
     
 void init() {
     cs = 0;
@@ -51,48 +53,57 @@
     cs = 1;
 }
 
-int readButton() {
-    cs = 0;
-    spi.write(0x47);
-    spi.write(0x09);
-    int misoOutput = spi.write(0x00);
-    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);
     
-    misoOutput = misoOutput & 192;
-    printf("output miso: %d \n\r", misoOutput);
-    if (misoOutput == 128) { // S1 pressed
-        return 1;    
-    } else if (misoOutput == 64) { // S2 pressed
-        return 5;
-    }
-    return 0;
-}
-
-int sendNumber(int number) {
-    // ...add it to the transmit buffer
-    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;
     }
-    
-    // If we've received anything in the nRF24L01+...
+}
+
+void readNRF() {
+// If we've received anything in the nRF24L01+...
     if ( my_nrf24l01p.readable() ) {
 
         // ...read the data into the receive buffer
         rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
 
         // Display the receive buffer contents via the host serial link
-        for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
-            printf(rxData);
-//            pc.putc( rxData[i] );
-        }
+        printf("Received %d for: %d\n\r", rxData[0], rxData[1]);
+    }    
+}
+
+void readButton() {
+    cs = 0;
+    spi.write(0x47);
+    spi.write(0x09);
+    int misoOutput = spi.write(0x00);
+    cs = 1;
+    misoOutput = misoOutput & 192;
+        
+    //printf("output miso: %d \n\r", misoOutput);
+    if (misoOutput == 128) { // S1 pressed
+        led = (led + 1) % 6;
+        sendNumber(led);
+    } else if (misoOutput == 64) { // S2 pressed
+        led = (led + 5) % 6;
+        sendNumber(led);
     }
+    
+    setLed(led);
 }
 
 int main() {    
@@ -102,12 +113,10 @@
     printf("Setting Led. \n\r");
     
     // Select the device by seting chip select low
-    int led = 0;
     printf("Starting \n\r");
     while (true) {
-        led = (led + readButton()) % 6;
-        setLed(led);
-        sendNumber(led);
+        readButton();
+        readNRF();
         wait(0.1);
     }
 }
\ No newline at end of file