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@15:217b3e5a2275, 2018-12-01 (annotated)
- Committer:
- hyunsungkim
- Date:
- Sat Dec 01 15:37:05 2018 +0000
- Branch:
- RF24_library_test_tx
- Revision:
- 15:217b3e5a2275
- Parent:
- 14:a4402559cd6e
- Child:
- 16:7a78838b3b8a
Master ??? ???, ??? ??
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| hyunsungkim | 0:2dade6db4d3a | 1 | #include "mbed.h" |
| hyunsungkim | 8:78294b936c70 | 2 | #include "nRF24L01P.h" |
| hyunsungkim | 3:690740ab3394 | 3 | #include "beep.h" |
| hyunsungkim | 3:690740ab3394 | 4 | |
| hyunsungkim | 3:690740ab3394 | 5 | #define nrf_CE D2 |
| hyunsungkim | 3:690740ab3394 | 6 | #define nrf_CSN A3 |
| hyunsungkim | 3:690740ab3394 | 7 | #define spi_SCK D13 |
| hyunsungkim | 3:690740ab3394 | 8 | #define spi_MOSI D11 |
| hyunsungkim | 3:690740ab3394 | 9 | #define spi_MISO D12 |
| hyunsungkim | 8:78294b936c70 | 10 | #define spi_IRQ D4 |
| hyunsungkim | 8:78294b936c70 | 11 | |
| hyunsungkim | 14:a4402559cd6e | 12 | #define TRANSFER_SIZE 15 |
| hyunsungkim | 8:78294b936c70 | 13 | |
| hyunsungkim | 8:78294b936c70 | 14 | nRF24L01P nrf(spi_MOSI, spi_MISO, spi_SCK, nrf_CSN, nrf_CE, spi_IRQ); // mosi, miso, sck, csn, ce, irq |
| hyunsungkim | 8:78294b936c70 | 15 | |
| hyunsungkim | 0:2dade6db4d3a | 16 | Serial pc(USBTX, USBRX); |
| hyunsungkim | 0:2dade6db4d3a | 17 | PwmOut buzzer(D5); |
| hyunsungkim | 0:2dade6db4d3a | 18 | |
| hyunsungkim | 8:78294b936c70 | 19 | void beepStart(); |
| hyunsungkim | 8:78294b936c70 | 20 | void endBeep(); |
| hyunsungkim | 15:217b3e5a2275 | 21 | void initNRF(); |
| hyunsungkim | 11:1b56bb8ccd04 | 22 | void dumpRFInfo(); |
| hyunsungkim | 10:02d3ca034103 | 23 | void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData); |
| hyunsungkim | 8:78294b936c70 | 24 | |
| hyunsungkim | 6:42ecdff5956a | 25 | |
| hyunsungkim | 0:2dade6db4d3a | 26 | int main() { |
| hyunsungkim | 12:cf6cbf1d1ebf | 27 | int inByteCnt = 0; |
| hyunsungkim | 12:cf6cbf1d1ebf | 28 | char buf[TRANSFER_SIZE+3]; |
| hyunsungkim | 12:cf6cbf1d1ebf | 29 | |
| hyunsungkim | 8:78294b936c70 | 30 | beepStart(); |
| hyunsungkim | 0:2dade6db4d3a | 31 | pc.baud(115200); |
| hyunsungkim | 15:217b3e5a2275 | 32 | printf("I'm Master\r\n"); |
| hyunsungkim | 15:217b3e5a2275 | 33 | initNRF(); |
| hyunsungkim | 11:1b56bb8ccd04 | 34 | dumpRFInfo(); |
| hyunsungkim | 0:2dade6db4d3a | 35 | |
| hyunsungkim | 10:02d3ca034103 | 36 | while(1) { |
| hyunsungkim | 15:217b3e5a2275 | 37 | while(pc.readable()) { |
| hyunsungkim | 15:217b3e5a2275 | 38 | char inByte = pc.getc(); |
| hyunsungkim | 15:217b3e5a2275 | 39 | if(inByteCnt > TRANSFER_SIZE+2) { |
| hyunsungkim | 15:217b3e5a2275 | 40 | printf("Data size overflow!\r\n"); |
| hyunsungkim | 15:217b3e5a2275 | 41 | inByteCnt = 0; |
| hyunsungkim | 15:217b3e5a2275 | 42 | break; |
| hyunsungkim | 15:217b3e5a2275 | 43 | } else if(inByte == '\0') { |
| hyunsungkim | 15:217b3e5a2275 | 44 | buf[inByteCnt] = '\0'; |
| hyunsungkim | 15:217b3e5a2275 | 45 | printf("Command: %s\r\n", buf); |
| hyunsungkim | 15:217b3e5a2275 | 46 | nrf.write(NRF24L01P_PIPE_P0, buf, TRANSFER_SIZE); |
| hyunsungkim | 15:217b3e5a2275 | 47 | inByteCnt=0; |
| hyunsungkim | 15:217b3e5a2275 | 48 | } else { |
| hyunsungkim | 15:217b3e5a2275 | 49 | buf[inByteCnt++] = inByte; |
| hyunsungkim | 10:02d3ca034103 | 50 | } |
| hyunsungkim | 10:02d3ca034103 | 51 | } |
| hyunsungkim | 10:02d3ca034103 | 52 | } |
| hyunsungkim | 10:02d3ca034103 | 53 | } |
| hyunsungkim | 10:02d3ca034103 | 54 | |
| hyunsungkim | 10:02d3ca034103 | 55 | void dumpRFInfo() |
| hyunsungkim | 10:02d3ca034103 | 56 | { |
| hyunsungkim | 10:02d3ca034103 | 57 | printf( "nRF24L01+ Frequency : %d MHz\r\n", nrf.getRfFrequency() ); |
| hyunsungkim | 10:02d3ca034103 | 58 | printf( "nRF24L01+ Output power : %d dBm\r\n", nrf.getRfOutputPower() ); |
| hyunsungkim | 10:02d3ca034103 | 59 | printf( "nRF24L01+ Data Rate : %d kbps\r\n", nrf.getAirDataRate() ); |
| hyunsungkim | 10:02d3ca034103 | 60 | printf( "nRF24L01+ TX Address : 0x%010llX\r\n", nrf.getTxAddress() ); |
| hyunsungkim | 10:02d3ca034103 | 61 | printf( "nRF24L01+ RX Address : 0x%010llX\r\n", nrf.getRxAddress() ); |
| hyunsungkim | 10:02d3ca034103 | 62 | } |
| hyunsungkim | 10:02d3ca034103 | 63 | |
| hyunsungkim | 15:217b3e5a2275 | 64 | void initNRF() |
| hyunsungkim | 10:02d3ca034103 | 65 | { |
| hyunsungkim | 15:217b3e5a2275 | 66 | nrf.setTxAddress(0xDEADBEEF0F); |
| hyunsungkim | 15:217b3e5a2275 | 67 | nrf.powerUp(); |
| hyunsungkim | 15:217b3e5a2275 | 68 | nrf.setTransferSize( TRANSFER_SIZE ); |
| hyunsungkim | 15:217b3e5a2275 | 69 | nrf.setTransmitMode(); |
| hyunsungkim | 15:217b3e5a2275 | 70 | nrf.enable(); |
| hyunsungkim | 15:217b3e5a2275 | 71 | |
| hyunsungkim | 8:78294b936c70 | 72 | } |
| hyunsungkim | 8:78294b936c70 | 73 | |
| hyunsungkim | 10:02d3ca034103 | 74 | void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData) |
| hyunsungkim | 10:02d3ca034103 | 75 | { |
| hyunsungkim | 14:a4402559cd6e | 76 | *(txData+0) = '$'; |
| hyunsungkim | 14:a4402559cd6e | 77 | *(txData+1) = id/10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 78 | *(txData+2) = id%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 79 | *(txData+3) = count/10000+'0'; |
| hyunsungkim | 14:a4402559cd6e | 80 | *(txData+4) = count/1000%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 81 | *(txData+5) = count/100%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 82 | *(txData+6) = count/10%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 83 | *(txData+7) = count%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 84 | *(txData+8) = lspeed>0?'+':'-'; |
| hyunsungkim | 14:a4402559cd6e | 85 | *(txData+9) = abs(lspeed)/10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 86 | *(txData+10) = abs(lspeed)%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 87 | *(txData+11) = rspeed>0?'+':'-'; |
| hyunsungkim | 14:a4402559cd6e | 88 | *(txData+12) = abs(rspeed)/10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 89 | *(txData+13) = abs(rspeed)%10+'0'; |
| hyunsungkim | 14:a4402559cd6e | 90 | *(txData+14) = '\0'; |
| hyunsungkim | 13:f0f9a5586e45 | 91 | } |
| hyunsungkim | 13:f0f9a5586e45 | 92 | |
| hyunsungkim | 13:f0f9a5586e45 | 93 | /* |
| hyunsungkim | 13:f0f9a5586e45 | 94 | xxyyyyyabbcddn |
| hyunsungkim | 13:f0f9a5586e45 | 95 | |
| hyunsungkim | 14:a4402559cd6e | 96 | 14 chars |
| hyunsungkim | 14:a4402559cd6e | 97 | |
| hyunsungkim | 13:f0f9a5586e45 | 98 | xx: robot_id |
| hyunsungkim | 13:f0f9a5586e45 | 99 | yyyyy: packet_id |
| hyunsungkim | 13:f0f9a5586e45 | 100 | a: sign of lspeed |
| hyunsungkim | 13:f0f9a5586e45 | 101 | bb: lspeed |
| hyunsungkim | 13:f0f9a5586e45 | 102 | c: sign of rspeed |
| hyunsungkim | 13:f0f9a5586e45 | 103 | dd: rspeed |
| hyunsungkim | 14:a4402559cd6e | 104 | n: NULL 0 |
| hyunsungkim | 13:f0f9a5586e45 | 105 | |
| hyunsungkim | 14:a4402559cd6e | 106 | 0100001+30+30 |
| hyunsungkim | 13:f0f9a5586e45 | 107 | */ |