Sensor CDT / nRFGateway

Dependencies:   mbed nRF24L01P

Fork of nRF24L01P_Hello_World_Gateway by TJ Projects

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(p11, p12, p13, p14, p15, p10);    // mosi, miso, sck, csn, ce, irq
00007 //nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTE1, PTE0, PTD0);  
00008 nRF24L01P my_nrf24l01p(p5, p6, p7, p9, p10, p8);    // mosi, miso, sck, csn, ce, irq
00009 
00010 DigitalOut myled1(LED1);
00011 DigitalOut myled2(LED2);
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   24
00019 
00020     char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
00021     int txDataCnt = 0;
00022     int rxDataCnt = 0;
00023 
00024     my_nrf24l01p.powerUp();
00025 
00026     // Display the (default) setup of the nRF24L01+ chip
00027    
00028    /* pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
00029     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
00030     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00031     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
00032     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
00033 
00034     pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
00035 */
00036     my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
00037 
00038     my_nrf24l01p.setReceiveMode();
00039     my_nrf24l01p.enable();
00040 
00041     while (1) {
00042 
00043         // If we've received anything over the host serial link...
00044         if ( pc.readable() ) {
00045 
00046             // ...add it to the transmit buffer
00047             txData[txDataCnt++] = pc.getc();
00048 
00049             // If the transmit buffer is full
00050             if ( txDataCnt >= sizeof( txData ) ) {
00051 
00052                 // Send the transmitbuffer via the nRF24L01+
00053                 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
00054 
00055                 txDataCnt = 0;
00056             }
00057 
00058             // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
00059             myled1 = !myled1;
00060         }
00061 
00062         // If we've received anything in the nRF24L01+...
00063         if ( my_nrf24l01p.readable() ) {
00064 
00065             // ...read the data into the receive buffer
00066             rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
00067 
00068             // Display the receive buffer contents via the host serial link
00069             for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
00070 
00071                 pc.putc( rxData[i] );
00072             }
00073 
00074             // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
00075             myled2 = !myled2;
00076         }
00077     }
00078 }