scooter / Mbed 2 deprecated 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(D4, D5, D3, D10, D8, D9);    // mosi, miso, sck, csn, ce, irq
00007 
00008 
00009 int main() {
00010 
00011 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
00012 //  "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
00013 //  only handles 4 byte transfers in the ATMega code.
00014 #define TRANSFER_SIZE   1
00015 
00016     char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
00017     int txDataCnt = 0;
00018     int rxDataCnt = 0;
00019 
00020     my_nrf24l01p.powerUp();
00021     my_nrf24l01p.setAirDataRate(250);
00022     my_nrf24l01p.setRxAddress(0xE7E7E7E7E1, DEFAULT_NRF24L01P_ADDRESS_WIDTH, NRF24L01P_PIPE_P0);
00023     my_nrf24l01p.setTxAddress(0xE7E7E7E7E6, DEFAULT_NRF24L01P_ADDRESS_WIDTH);
00024     
00025     
00026     // Display the (default) setup of the nRF24L01+ chip
00027     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
00028     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
00029     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00030     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
00031     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
00032 
00033     pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
00034 
00035     my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
00036 
00037     my_nrf24l01p.setReceiveMode();
00038     my_nrf24l01p.enable();
00039     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00040     while (1) {
00041 
00042         // If we've received anything over the host serial link...
00043         if ( pc.readable() ) {
00044 
00045             // ...add it to the transmit buffer
00046             txData[txDataCnt++] = pc.getc();
00047 
00048             // If the transmit buffer is full
00049             if ( txDataCnt >= sizeof( txData ) ) {
00050 
00051                 // Send the transmitbuffer via the nRF24L01+
00052                 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
00053 
00054                 txDataCnt = 0;
00055             }
00056 
00057         }
00058 
00059         // If we've received anything in the nRF24L01+...
00060         if ( my_nrf24l01p.readable() ) {
00061 
00062             // ...read the data into the receive buffer
00063             rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
00064 
00065             // Display the receive buffer contents via the host serial link
00066             for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
00067 
00068                 pc.putc( rxData[i] );
00069             }
00070 
00071         }
00072     }
00073 }