Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of nRF24L01P_Hello_World by
main.cpp
00001 #include "mbed.h" 00002 #include "nRF24L01P.h" 00003 #include <iostream> 00004 #include <string> 00005 00006 #define TRANSFER_SIZE 8 00007 00008 Serial pc(USBTX, USBRX); // tx, rx 00009 nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq 00010 DigitalOut myled1(LED1); 00011 DigitalOut myled2(LED2); 00012 00013 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE], acked[TRANSFER_SIZE], 00014 nacked[TRANSFER_SIZE]; 00015 bool ack = false; 00016 int txDataCnt = 0; 00017 int rxDataCnt = 0; 00018 00019 void waitForAck() { 00020 //RECEIVE ACK 00021 if (my_nrf24l01p.readable()) { 00022 // ...read the data into the receive buffer 00023 rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof(rxData)); 00024 // match with ack array 00025 if(strcmp(rxData, acked)==-1) { 00026 pc.printf("ACK\n\r"); 00027 ack = true; 00028 } else { 00029 pc.printf("NACK\n\r"); 00030 ack = false; 00031 } 00032 rxDataCnt = 0; 00033 // Toggle LED2 (to help debug nRF24L01+ -> Host communication) 00034 myled2 = !myled2; 00035 } 00036 } 00037 00038 void checkValid() { 00039 waitForAck(); //other code has SWITCH statement 00040 if(ack) {//if valid, wait til next ack 00041 pc.printf("valid, please wait to send again\n\r"); 00042 ack = false; 00043 while(!ack) { 00044 waitForAck(); 00045 } 00046 pc.printf("ready to send again\r\n"); 00047 } else {//if invalid 00048 pc.printf("invalid command, send another\n\r"); 00049 } 00050 } 00051 00052 void send() { 00053 //SEND 00054 // If we've received anything over the host serial link... 00055 if (pc.readable()) { 00056 // ...add it to the transmit buffer 00057 txData[txDataCnt++] = pc.getc(); 00058 // If the transmit buffer is full 00059 if (txDataCnt >= sizeof(txData)) { 00060 // Send the transmitbuffer via the nRF24L01+ 00061 my_nrf24l01p.write(NRF24L01P_PIPE_P0, txData, txDataCnt); 00062 txDataCnt = 0; 00063 pc.printf("Sent command\n\r"); 00064 00065 //wait for valid confirmation 00066 checkValid(); 00067 } 00068 // Toggle LED1 (to help debug Host -> nRF24L01+ communication) 00069 myled1 = !myled1; 00070 } 00071 } 00072 00073 int main() { 00074 //initialize the arrays recognized as ACK and NACK 00075 for(int i = 0; i < TRANSFER_SIZE; i++) { 00076 acked[i] = '0'; 00077 nacked[i] = '1'; 00078 } 00079 my_nrf24l01p.powerUp(); 00080 // Display the (default) setup of the nRF24L01+ chip 00081 printf("\n\r--------\r\n"); 00082 printf("BASE STATION\r\n"); 00083 printf("Begin communications...\r\n"); 00084 printf("--------\r\n"); 00085 my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); 00086 my_nrf24l01p.setReceiveMode(); 00087 my_nrf24l01p.enable(); 00088 00089 while (1) { 00090 if(!ack) { 00091 waitForAck(); 00092 } else { 00093 send(); 00094 } 00095 } 00096 }
Generated on Thu Jul 14 2022 01:36:11 by
1.7.2
