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.
Dependencies: mbed 4DGL-uLCD-SE mbed-rtos nRF24L01P
Diff: main.cpp
- Revision:
- 40:4f7d95c68d29
- Parent:
- 33:5d86c111d9bc
- Child:
- 41:9ed924a1f2e0
--- a/main.cpp Wed Apr 25 15:39:03 2018 +0000 +++ b/main.cpp Fri Apr 27 22:03:51 2018 +0000 @@ -15,7 +15,7 @@ // How maybe bytes to send over RF #define DATA_PACKET_SIZE 32 // How quickly to sample the mic / play the speaker -#define SAMPLE_PERIOD 1/16000.0 +#define SAMPLE_PERIOD 1/8000.0 Serial pc(USBTX, USBRX); @@ -40,6 +40,7 @@ int rfFreq; int dataRate; unsigned long long rxAddr, txAddr; +int pipe = 0; enum operatingMode { RECEIVE = 0, @@ -53,15 +54,55 @@ return a * (int)(a <= b) + b * (int)(b < a); } -// Sets the frequency of the RF device based on which swtiches are flipped +// Sets the channel of the RF device based on which swtiches are flipped +// by changing RX/TX pipes // TODO: Make sure we don't have to restart the device or anything to change this -void setRFFrequency() { - int channelNum = 0;//channel.read(); +void setChannel() { + int width; + switch (channel) { + case 0: // Channel 0 + rxAddr = txAddr = 0xE7D3F03577; + pipe = NRF24L01P_PIPE_P0; + width = 5; + break; + case 1: // Channel 1 + rxAddr = txAddr = 0xC2C2C2C2C1; + pipe = NRF24L01P_PIPE_P1; + width = 5; + break; + case 2: // Channel 2 + rxAddr = txAddr = 0xC2C2C2C2C2; + pipe = NRF24L01P_PIPE_P2; + width = 5; + break; + case 4: // Channel 3 + rxAddr = txAddr = 0xC2C2C2C2C3; + pipe = NRF24L01P_PIPE_P3; + width = 5; + break; + case 8: // Channel 4 + rxAddr = txAddr = 0xC2C2C2C2C4; + pipe = NRF24L01P_PIPE_P4; + width = 5; + break; + case 16: // Channel 5 + rxAddr = txAddr = 0xC2C2C2C2C5; + pipe = NRF24L01P_PIPE_P5; + width = 5; + break; + default: + break; + } + my_nrf24l01p.setRxAddress(rxAddr, width, pipe); + my_nrf24l01p.setTxAddress(txAddr, width); + + //pc.printf("Pipe = %d\r\n", pipe); + // TODO: Don't force it to the default RF frequency - channelNum = 2; + //channelNum = 2; - my_nrf24l01p.setRfFrequency(channelNum + NRF24L01P_MIN_RF_FREQUENCY); + //my_nrf24l01p.setRfFrequency(channelNum + NRF24L01P_MIN_RF_FREQUENCY); } // Callback interrupt from the button to shift into transmit mode @@ -123,7 +164,7 @@ // Only place into the buffer the number of bytes received rxbuff.push(spkrPacket, min(DATA_PACKET_SIZE, numReceived)); - pc.printf("Receiviing....\n\r"); + //pc.printf("Receiviing....\n\r"); myled2 = !myled2; } } else { // mode == TRANSMIT @@ -203,6 +244,12 @@ my_nrf24l01p.setReceiveMode(); my_nrf24l01p.enable(); + pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", rfFreq ); + pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); + pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); + pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", txAddr ); + pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", rxAddr ); + pc.printf("Finished starting up....\n\r"); mode = RECEIVE; @@ -228,6 +275,7 @@ // Heartbeat thread while (1) { myled4 = !myled4; + setChannel(); // Poll the DIP switch and change channels accordingly Thread::wait(100); } } \ No newline at end of file