base station for dump truck

Dependencies:   mbed nRF24L01P

Fork of nRF24L01P_Hello_World by Owen Edwards

Files at this revision

API Documentation at this revision

Comitter:
simplyellow
Date:
Tue Apr 25 05:25:18 2017 +0000
Parent:
2:d77298c11128
Child:
4:3c37c857665c
Commit message:
final base station implementation

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Apr 13 17:31:57 2017 +0000
+++ b/main.cpp	Tue Apr 25 05:25:18 2017 +0000
@@ -1,5 +1,7 @@
 #include "mbed.h"
 #include "nRF24L01P.h"
+#include <iostream>
+#include <string>
 
 #define TRANSFER_SIZE   8
 
@@ -13,7 +15,45 @@
 bool ack = false;
 int txDataCnt = 0;
 int rxDataCnt = 0;
-int matched = 0;
+
+void waitForAck() {
+    //RECEIVE ACK
+    if (my_nrf24l01p.readable()) {
+        // ...read the data into the receive buffer
+        rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof(rxData));
+        // match with ack array
+        //for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
+//            if(rxData[i] == acked[i]) {
+//                matched++;
+//            }
+//        }
+        //printf("%d \r\n", strcmp(rxData, acked));
+        if(strcmp(rxData, acked)==-1) {
+            pc.printf("ACK\n\r");
+            ack = true;
+        } else {
+            pc.printf("NACK\n\r");
+            ack = false;
+        }
+        rxDataCnt = 0;
+        // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
+        myled2 = !myled2;
+    }
+}
+
+void checkValid() {
+    waitForAck();   //other code has SWITCH statement
+    if(ack) {//if valid, wait til next ack
+        pc.printf("valid, please wait to send again\n\r");
+        ack = false;
+        while(!ack) {
+            waitForAck();
+        }
+        pc.printf("ready to send again\r\n");
+    } else {//if invalid
+        pc.printf("invalid command, send another\n\r");
+    }   
+}
 
 void send() {
     //SEND
@@ -36,44 +76,6 @@
     }
 }
 
-void checkValid() {
-    waitForAck();   //other code has SWITCH statement
-    if(ack) {//if valid, wait til next ack
-        pc.printf("valid, please wait to send again\n\r");
-        ack = false;
-        while(!ack) {
-            waitForAck();
-        }
-        pc.printf("ready to send again\r\n");
-        send();
-    } else {//if invalid
-        pc.printf("invalid, send another\n\r");
-        send();
-    }   
-}
-
-void waitForAck() {
-    //RECEIVE ACK
-    if (my_nrf24l01p.readable()) {
-        // ...read the data into the receive buffer
-        rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof(rxData ));
-        // match with ack array
-        for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
-            if(rxData[i] == acked[i]) {
-                matched++;
-            }
-        }
-        if(matched == TRANSFER_SIZE) {
-            pc.printf("ACK\n\r");
-            ack = true;
-        } else {
-            pc.printf("NACK\n\r");
-        }
-        // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
-        myled2 = !myled2;
-    }
-}
-
 int main() {
     //initialize the arrays recognized as ACK and NACK
     for(int i = 0; i < TRANSFER_SIZE; i++) {
@@ -82,11 +84,10 @@
     }
     my_nrf24l01p.powerUp();
     // Display the (default) setup of the nRF24L01+ chip
-    pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
-    pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
-    pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
-    pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
-    pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
+    printf("\n\r--------\r\n");
+    printf("BASE STATION\r\n");
+    printf("Begin communications...\r\n");
+    printf("--------\r\n");
     my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
     my_nrf24l01p.setReceiveMode();
     my_nrf24l01p.enable();