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.
main.cpp
00001 #include "mbed.h" 00002 #include "nRF24L01P.h" 00003 00004 #define BUFFER_SIZE (sizeof(int)*4 + sizeof(float)*3) 00005 #define BAUDR 115200 00006 00007 #define SAVING_START 'S' 00008 #define SAVING_STOP 'E' 00009 #define TRANSFER_START 'T' 00010 #define CHECK_READY 'R' 00011 #define SEND_ACK 'A' 00012 00013 #define NACK 'N' 00014 #define ACK 'A' 00015 00016 Serial pc(USBTX, USBRX); 00017 nRF24L01P rf(p5, p6, p7, p8, p9); 00018 //nRF24L01P rf(p11, p12, p13, p14, p15); 00019 DigitalOut led(LED1); 00020 00021 00022 void send_ack() 00023 { 00024 char buffer[BUFFER_SIZE]; 00025 buffer[0] = 0x00; 00026 buffer[1] = SEND_ACK; 00027 00028 rf.write(NRF24L01P_PIPE_P0, buffer, BUFFER_SIZE); 00029 } 00030 00031 void check_ack() 00032 { 00033 char buffer[BUFFER_SIZE]; 00034 wait_ms(10); 00035 if (!rf.readable()) { 00036 printf("#%c\n", NACK); 00037 return; 00038 } 00039 00040 rf.read(NRF24L01P_PIPE_P0, buffer, BUFFER_SIZE); 00041 if (buffer[0] != 0x00 || buffer[1] != SEND_ACK) 00042 printf("#%c\n", NACK); 00043 00044 printf("#%c\n", ACK); 00045 } 00046 00047 int send_cmd(void) 00048 { 00049 char buffer[BUFFER_SIZE]; 00050 char c; 00051 static char last = 0; 00052 00053 c = pc.getc(); 00054 if (c != '#' && last != 1) 00055 return -1; 00056 if (c == '#') { 00057 last = 1; 00058 return 0; 00059 } 00060 00061 last = 0; 00062 00063 buffer[0] = 0x00; 00064 buffer[1] = c; 00065 rf.write(NRF24L01P_PIPE_P0, buffer, BUFFER_SIZE); 00066 check_ack(); 00067 return 0; 00068 } 00069 00070 void communication(void) 00071 { 00072 char rxBuf[BUFFER_SIZE+1]; 00073 int bytes; 00074 int counter, acc_x, acc_y, acc_z; 00075 float gyro_x, gyro_y, gyro_z; 00076 00077 printf("\n\nReceiving data\n\r"); 00078 while(1) { 00079 if (pc.readable()) 00080 send_cmd(); 00081 00082 bytes = rf.read(NRF24L01P_PIPE_P0, rxBuf, BUFFER_SIZE); 00083 if (bytes <= 0) 00084 continue; 00085 00086 //send_ack(); 00087 00088 counter = *((int *) rxBuf); 00089 acc_x = *((int *) &rxBuf[sizeof(int)]); 00090 acc_y = *((int *) &rxBuf[sizeof(int)*2]); 00091 acc_z = *((int *) &rxBuf[sizeof(int)*3]); 00092 gyro_x = *((float *) &rxBuf[sizeof(int)*4]); 00093 gyro_y = *((float *) &rxBuf[sizeof(int)*4 + sizeof(float)]); 00094 gyro_z = *((float *) &rxBuf[sizeof(int)*4 + sizeof(float)*2]); 00095 00096 /* transmission start */ 00097 if (counter == -1 && acc_x >= 0) { 00098 printf("Swimmer id: %d\n", acc_x); 00099 continue; 00100 } 00101 if (counter == -1 && acc_x == -1) { 00102 printf("End of DATA\n"); 00103 continue; 00104 } 00105 printf("%d %d %d %d %f %f %f\n", counter, acc_x, acc_y, acc_z, gyro_x, gyro_y, gyro_z); 00106 } 00107 } 00108 00109 int main(void) 00110 { 00111 pc.baud(BAUDR); 00112 00113 rf.setTransferSize(BUFFER_SIZE); 00114 rf.setRfOutputPower(NRF24L01P_TX_PWR_ZERO_DB); 00115 rf.setAirDataRate(NRF24L01P_DATARATE_2_MBPS); 00116 rf.setReceiveMode(); 00117 rf.enable(); 00118 00119 printf("Freq: %d MHz\n\r", rf.getRfFrequency()); 00120 printf("Power: %d dBm\n\r", rf.getRfOutputPower()); 00121 printf("TX Address: %llu\n\r", rf.getTxAddress()); 00122 printf("RX Address: %llu\n\r", rf.getRxAddress()); 00123 00124 communication(); 00125 }
Generated on Wed Jul 20 2022 02:45:25 by
1.7.2