Shailesh k s / nRF24L01P_Hello_World

Dependencies:   mbed nRF24L01P

Fork of nRF24L01P_Hello_World by Owen Edwards

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "nRF24L01P.h"
00003 
00004 Serial pc(USBTX, USBRX); // tx, rx
00005 
00006 nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10);    // mosi, miso, sck, csn, ce, irq
00007 
00008 DigitalOut myled1(LED1);
00009 DigitalOut myled2(LED2);
00010 
00011 Timer t;
00012 
00013 int main() {
00014 
00015 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
00016 //  "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
00017 //  only handles 4 byte transfers in the ATMega code.
00018 #define TRANSFER_SIZE   4
00019 
00020     char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
00021     int txDataCnt = 0;
00022     int rxDataCnt = 0;
00023     int tmpDataCnt = 0;
00024     char tmpData[TRANSFER_SIZE+1];
00025 
00026     my_nrf24l01p.powerUp();
00027 
00028     // Display the (default) setup of the nRF24L01+ chip
00029     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
00030     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
00031     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00032     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
00033     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
00034 
00035     pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
00036 
00037     my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
00038 
00039     my_nrf24l01p.setReceiveMode();
00040     my_nrf24l01p.enable();
00041 
00042     while (1) {
00043 
00044         // If we've received anything over the host serial link...
00045         if ( pc.readable() ) {
00046 
00047             // ...add it to the transmit buffer
00048             txData[txDataCnt++] = pc.getc();
00049 
00050             // If the transmit buffer is full
00051             if ( txDataCnt >= sizeof( txData ) ) {
00052 
00053                 t.start();
00054                 
00055                 // Send the transmitbuffer via the nRF24L01+
00056                 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
00057                 
00058                 txDataCnt = 0;
00059             }
00060 
00061             // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
00062             myled1 = !myled1;
00063         }
00064 
00065         // If we've received anything in the nRF24L01+...
00066         if ( my_nrf24l01p.readable() ) {
00067                 
00068             // ...read the data into the receive buffer
00069             rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
00070 
00071             // Display the receive buffer contents via the host serial link
00072             for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
00073             
00074                 pc.putc( rxData[i] );
00075                 tmpData[tmpDataCnt++] = rxData[i]; 
00076             }
00077             
00078             if ( tmpDataCnt >= TRANSFER_SIZE ) {
00079                 tmpData[TRANSFER_SIZE] = '\0';
00080                 //pc.printf( "Received data : %s \r\n", tmpData);
00081                 if (strcmp(tmpData, "ackn"))
00082                 {
00083                     tmpData[0] = 'a';
00084                     tmpData[1] = 'c';
00085                     tmpData[2] = 'k';
00086                     tmpData[3] = 'n';
00087                     //pc.printf( "Sending data : %s \r\n", "ackn");
00088                     my_nrf24l01p.write( NRF24L01P_PIPE_P0, tmpData, tmpDataCnt );  
00089                 }
00090                 else
00091                 {
00092                     t.stop();
00093                     pc.printf("\r\nThe time taken was %f seconds \r\n", t.read());
00094                     t.reset();
00095                 }
00096                 
00097                  
00098                 tmpDataCnt = 0;   
00099             }
00100 
00101             // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
00102             myled2 = !myled2;
00103         }
00104     }
00105 }