현성 김 / Mbed 2 deprecated 181202_Castone_design_master

Dependencies:   mbed nRF24L01P

Branch:
RF24_library_test_tx
Revision:
8:78294b936c70
Parent:
7:faf9014d2512
Child:
9:d85804e8d3b3
--- a/main.cpp	Wed Nov 21 12:08:43 2018 +0000
+++ b/main.cpp	Sun Nov 25 10:34:05 2018 +0000
@@ -1,21 +1,27 @@
 #include "mbed.h"
-#include <RF24Network.h>
-#include <RF24.h>
+#include "nRF24L01P.h"
 #include "beep.h"
 
-/* Set */
-#define NODE_ADDR 00
+#define PING 1
+#define PONG 2
 
 #define nrf_CE      D2
 #define nrf_CSN     A3
 #define spi_SCK     D13
 #define spi_MOSI    D11
 #define spi_MISO    D12
-RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN );
-RF24Network network(radio);
+#define spi_IRQ     D4
+
+#define TRANSFER_SIZE 14
+
+nRF24L01P nrf(spi_MOSI, spi_MISO, spi_SCK, nrf_CSN, nrf_CE, spi_IRQ);    // mosi, miso, sck, csn, ce, irq
+
+volatile int role = PING;
+//volatile int role = PONG;
 
 Serial pc(USBTX, USBRX);
 Serial lidar(D1, D0);
+
 PwmOut motor_RA(D9);
 PwmOut motor_RB(D10);
 PwmOut motor_LA(D3);
@@ -26,118 +32,77 @@
 PwmOut buzzer(D5);
 AnalogIn batteryCheck(A0);
 
-const uint16_t addr = NODE_ADDR;
-struct payload_t 
+void beepStart();
+void endBeep();
+
+void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData)
 {
-    unsigned long ms;
-    unsigned long counter;
-};
-
-void goForward();
-void initLED();
-
+    *(txData+0) = id/10+'0';
+    *(txData+1) = id%10+'0';
+    *(txData+2) = count/10000+'0';
+    *(txData+3) = count/1000%10+'0';
+    *(txData+4) = count/100%10+'0';
+    *(txData+5) = count/10%10+'0';
+    *(txData+6) = count%10+'0';
+    *(txData+7) = lspeed/100+'0';
+    *(txData+8) = lspeed/10%10+'0';
+    *(txData+9) = lspeed%10+'0';
+    *(txData+10) = rspeed/100+'0';
+    *(txData+11) = rspeed/10%10+'0';
+    *(txData+12) = rspeed%10+'0';
+    *(txData+13) = '\0';
+}
 
 int main() {
+
+    char txData[TRANSFER_SIZE];
+    int txDataCnt = 0;
+    char rxData[TRANSFER_SIZE];
+    int rxDataCnt = 0;
     
-    initLED();
-    initBuzzer();
+    beepStart();
     pc.baud(115200);
-    radio.begin();
-    network.begin(/*channel*/ 90, /*node address*/ addr);
-    wait(2.0f);
+    // Print setting of radio module
     
-    // Display the (default) setup this node
-    pc.printf("My ADDR : %d\r\n", NODE_ADDR);
-    pc.printf("Complete Setting\r\n");
-    
-    while(1) {
-        // Network should be updated regularly to keep layer going
-        network.update();
-        
-        while (network.available()) {
-            RF24NetworkHeader header_rx;
-            payload_t payload_rx;
-            network.read(header_rx,&payload_rx,sizeof(payload_rx));
-            pc.printf("Received packet # %d at %d ms\n",payload_rx.counter,payload_rx.ms);
-        }
+    if(role == PING) {
+        nrf.setTxAddress(0xDEADBEEF0F);
+        nrf.powerUp();
+        nrf.setTransferSize( TRANSFER_SIZE );
+        nrf.setTransmitMode();
+        nrf.enable();
+    } else {
+        nrf.setRxAddress(0xDEADBEEF0F);
+        nrf.powerUp();
+        nrf.setTransferSize( TRANSFER_SIZE );
+        nrf.setReceiveMode();
+        nrf.enable();
     }
-}
-
-void initLED() {    
-    led_R=1;
-    led_G=1;
-    led_B=1;
-}
-
-void goForward()
-{
-    motor_RA = 1;
-    motor_RB = 0;
-    motor_LA = 0;
-    motor_LB = 1;
-}
-
-/*
-
-// The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
-//  "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
-//  only handles 4 byte transfers in the ATMega code.
-#define TRANSFER_SIZE   4
-
-    wait(2.0);
- 
-    char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
-    int txDataCnt = 0;
-    int rxDataCnt = 0;
- 
-    nrf.powerUp();
-    nrf.setTxAddress(0xE7E7E7EF, 4);
- 
-    // Display the (default) setup of the nRF24L01+ chip
+    
     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  nrf.getRfFrequency() );
     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  nrf.getRfOutputPower() );
     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", nrf.getAirDataRate() );
     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", nrf.getTxAddress() );
     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", nrf.getRxAddress() );
-    pc.printf( "nRF24L01+ CRCWidth   : %d \r\n", nrf.getCrcWidth() );
- 
-    pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
- 
-    nrf.setTransferSize( TRANSFER_SIZE );
- 
-    nrf.setReceiveMode();
-    nrf.enable();
- 
-    while (1) {
- 
-        // If we've received anything over the host serial link...
-        if ( pc.readable() ) {
- 
-            // ...add it to the transmit buffer
-            txData[txDataCnt++] = pc.getc();
- 
-            // If the transmit buffer is full
-            if ( txDataCnt >= sizeof( txData ) ) {
- 
-                // Send the transmitbuffer via the nRF24L01+
-                nrf.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
- 
-                txDataCnt = 0;
+    
+    while(1) {
+        if(role == PING) {
+            int id = 1;
+            int lspeed=100;
+            int rspeed=123;
+            //printf("transmitting\r\n");
+            txDataCnt++;
+            getPayload(id, txDataCnt, lspeed, rspeed, txData);
+            pc.printf("%s\r\n", txData);
+            char txData_[] = "123456789ABCD";
+            nrf.write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE);
+            wait(0.2);
+        }
+        else if(role == PONG) {
+            if ( nrf.readable() ) {
+                rxDataCnt = nrf.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE);
+                printf("%s\r\n", rxData);
             }
-            endBeep();
-        }
- 
-        // If we've received anything in the nRF24L01+...
-        if ( nrf.readable() ) {
- 
-            // ...read the data into the receive buffer
-            rxDataCnt = nrf.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++ ) {
-                pc.putc( rxData[i] );
-            }
-            beepStart();
         }
     }
-    */
\ No newline at end of file
+}
+