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.
Diff: sensor_board.cpp
- Revision:
- 29:c2838405fa5c
- Parent:
- 28:80bc5ebf3ae0
- Child:
- 37:1251e35fe43e
--- a/sensor_board.cpp Tue Jan 10 09:23:14 2017 +0000
+++ b/sensor_board.cpp Tue Jan 10 16:40:04 2017 +0000
@@ -1,5 +1,7 @@
#include "sensor_board.h"
+#include <ctime>
+
bool send_from_digital_sensor = false;
bool send_from_analog_sensor = false;
@@ -11,11 +13,15 @@
send_from_analog_sensor = true;
}
+const int Board::MAX_RETRY = 3;
+
Board::Board(unsigned long long rx_address, DigitalSensor* digital_sensor,
AnalogSensor* analog_sensor, unsigned long long tx_address) :
pc_(USBTX, USBRX), radio_(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2),
analog_sensor_(analog_sensor), digital_sensor_(digital_sensor),
analog_ticker_(Ticker()), digital_ticker_(Ticker()) {
+
+ std::srand(std::time(0));
pc_.baud(115200);
radio_init(&radio_, rx_address, tx_address);
displayRadioSetup();
@@ -53,6 +59,24 @@
pc_.printf( "nRF24L01+ RX1 Address : 0x%010llX\r\n", radio_.getRxAddress(NRF24L01P_PIPE_P1) );
}
+int Board::getRandomWaitMs() {
+ return std::rand() % 500;
+}
+
+bool Board::sendByRadio(char message[TRANSFER_SIZE]) {
+ int retry_counter = 0;
+ while (retry_counter < Board::MAX_RETRY) {
+ wait_ms(getRandomWaitMs());
+ int tx_bytes = radio_.write(NRF24L01P_PIPE_P0, message, TRANSFER_SIZE);
+ if (tx_bytes >= 0) {
+ pc_.printf("Board retry %d, radio retry %d\r\n", retry_counter, radio_.getRetrCount());
+ return true;
+ }
+ retry_counter++;
+ }
+ return false;
+}
+
bool Board::send(Data data) {
std::string serialized_data = data.serialize();
pc_.printf("string data '%s', len %d\r\n", serialized_data.c_str(), serialized_data.size());
@@ -60,13 +84,5 @@
char message[TRANSFER_SIZE];
memset(message, 0, sizeof(message));
memcpy(message, serialized_data.c_str(), serialized_data.length());
- int tx_bytes = radio_.write(NRF24L01P_PIPE_P0, message, TRANSFER_SIZE);
-
- if (tx_bytes >= 0) {
- pc_.printf("RETR: %d\r\n", radio_.getRetrCount());
- }
-
- if(tx_bytes < 0)
- pc_.printf("TX ERROR\r\n");
- return tx_bytes;
+ return sendByRadio(message);
}
\ No newline at end of file