RobOmega - PSL RoboCup / Mbed 2 deprecated nRF24L01P_L475VG_CarteBleu

Dependencies:   mbed nRF24L01P

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "main.h"
00002 #include "mbed.h"
00003 #include "nRF24L01P.h"
00004 #include "protocol.h"
00005 
00006 
00007 Serial pc(USBTX, USBRX); // tx, rx
00008 
00009 nRF24L01P my_nrf24l01p(SPI2_MOSI, SPI2_MISO, SPI2_SCLK, CSN, CE, IRQ);    // mosi, miso, sck, csn, ce, irq
00010 
00011 DigitalOut myled1(LED1);
00012 DigitalOut myled2(LED2);
00013 
00014 int main() 
00015 {
00016 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
00017 //  "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
00018 //  only handles 4 byte transfers in the ATMega code.
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     pc.printf("Default setup : \r\n");
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     pc.printf("\r\n\r\n");
00034 
00035     my_nrf24l01p.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY);
00036     my_nrf24l01p.setAirDataRate(NRF24L01P_DATARATE_2_MBPS);
00037     my_nrf24l01p.setTxAddress(0xFFA);
00038     my_nrf24l01p.setRxAddress(0xFFE);
00039     
00040     pc.printf("Custom setup : \r\n");
00041     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
00042     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
00043     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00044     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
00045     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
00046     pc.printf("\r\n\r\n");
00047 
00048     my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
00049 
00050     my_nrf24l01p.setTransmitMode();
00051     my_nrf24l01p.enable();
00052     
00053     //Test Protocol + envoie Trame
00054     char message[] = "Bonjour";
00055     int taille = 8;
00056     char trame[taille + 6];
00057     encodeMessage(COMMAND_ID_TEXT, taille, message, trame);
00058     
00059     
00060     //Infinite Loop
00061     while (1) 
00062     {
00063         // If we've received anything over the host serial link...
00064         if ( pc.readable() ) 
00065         {
00066             // ...add it to the transmit buffer
00067             txData[txDataCnt++] = pc.getc();
00068 
00069             // If the transmit buffer is full
00070             if ( txDataCnt >= sizeof( txData ) ) 
00071             {
00072                 // Send the transmitbuffer via the nRF24L01+
00073                 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
00074 
00075                 txDataCnt = 0;
00076             }
00077 
00078             // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
00079             myled1 = !myled1;
00080         }
00081 
00082         // If we've received anything in the nRF24L01+...
00083         if ( my_nrf24l01p.readable() ) 
00084         {
00085             // ...read the data into the receive buffer
00086             rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
00087 
00088             // Display the receive buffer contents via the host serial link
00089             for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) 
00090                 pc.putc( rxData[i] );
00091 
00092             // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
00093             myled2 = !myled2;
00094         }
00095     }
00096 }