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: TextLCD nRF24L01P mbed
Diff: main.cpp
- Revision:
- 8:0de75e0480aa
- Parent:
- 6:19012a85ce35
- Child:
- 9:53da36425601
diff -r 077e6f35d4d1 -r 0de75e0480aa main.cpp
--- a/main.cpp Tue Dec 20 10:49:37 2016 +0000
+++ b/main.cpp Thu Jan 12 16:43:57 2017 +0000
@@ -2,7 +2,8 @@
#include "rtos.h"
#include "TextLCD.h"
#include "custom-chars.h"
-
+#include "nRF24L01P.h"
+
// Host PC Communication channels
Serial pc(USBTX, USBRX); // tx, rx
@@ -12,6 +13,48 @@
//Light sensor
AnalogIn light_sensor(A1);
+//2.4 GHz radio
+#define RX_ADDRESS ((unsigned long long) 0xABCDEF00)
+#define TX_ADDRESS ((unsigned long long) 0xABCDEF01)
+#define TRANSFER_SIZE 32
+
+nRF24L01P radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2);
+
+void initRadio(int channel, int power, int datarate){
+ radio.powerDown();
+ radio.powerUp();
+
+ radio.setAirDataRate(datarate);
+ radio.setRfOutputPower(power);
+ radio.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY + 4 * channel);
+
+ radio.setCrcWidth(NRF24L01P_CRC_8_BIT);
+ radio.enableAutoAcknowledge(NRF24L01P_PIPE_P0);
+ radio.enableAutoAcknowledge(NRF24L01P_PIPE_P1);
+ radio.enableAutoRetransmit(0x0F, 0x0F);
+
+ radio.setTxAddress(TX_ADDRESS, 4);
+ radio.setRxAddress(TX_ADDRESS, 4, NRF24L01P_PIPE_P0);
+
+ radio.setRxAddress(RX_ADDRESS, 4, NRF24L01P_PIPE_P1);
+
+ // Display the (default) setup of the nRF24L01+ chip
+ pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", radio.getRfFrequency() );
+ pc.printf( "nRF24L01+ Output power : %d dBm\r\n", radio.getRfOutputPower() );
+ pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", radio.getAirDataRate() );
+ pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", radio.getTxAddress() );
+ pc.printf( "nRF24L01+ RX0 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P0) );
+ pc.printf( "nRF24L01+ RX1 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P1) );
+
+
+ radio.setTransferSize(TRANSFER_SIZE, NRF24L01P_PIPE_P0);
+ radio.setTransferSize(TRANSFER_SIZE, NRF24L01P_PIPE_P1);
+
+ radio.setReceiveMode();
+ radio.enable();
+};
+
+//Display
void backlightTimeout(void const *arg);
RtosTimer backlightTimer(&backlightTimeout, osTimerPeriodic, (void*)0);
@@ -109,18 +152,38 @@
backlightTimedOut = 1;
}
+//TODO: I don't think it needs to be in a separate thread,
+//we probably can just put it into the main loop.
+void radio_recv(void const* args){
+ char rxData[TRANSFER_SIZE+1];
+ while (1) {
+ int rx_bytes=0;
+
+ if(radio.readable(NRF24L01P_PIPE_P1)){
+ rx_bytes = radio.read(NRF24L01P_PIPE_P1, rxData, TRANSFER_SIZE);
+ rxData[TRANSFER_SIZE] = '\0';
+ pc.printf("Received %d >>%s<<\r\n",rx_bytes, rxData);
+ }
+
+ }
+}
+
Ticker display_update_ticker;
int main() {
set_time(1256729737); //DEBUG: Set RTC time to Wed, 28 Oct 2009 11:35:37
//Initialization
disp.backlightOn();
- //display_update_ticker.attach(&display_update,0.2);
button.rise(&userButtonPress);
button.fall(&userButtonRelease);
+
+ initRadio(6, NRF24L01P_TX_PWR_ZERO_DB, NRF24L01P_DATARATE_250_KBPS);
+ Thread radio_thread(radio_recv);
+
while (1){
pc.printf("ok\r\n");
+ //Handling button presses
if (userButtonPressed) {
userButtonPressed = 0;
userButtonTimer.reset();
@@ -140,12 +203,15 @@
}
}
+ //Handling backlight
if (backlightTimedOut){
backlightTimedOut = 0;
disp.backlightOff();
}
+ //Updating display
disp.update();
+
Thread::wait(100);
};
}

