Skip to content

Commit 1a1fe30

Browse files
committed
Improved WiFi logging 2
1 parent 639e996 commit 1a1fe30

5 files changed

Lines changed: 3406 additions & 17 deletions

File tree

lib/LoadComms/LoadComms.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
// Initialization of static members
1515
QueueHandle_t LoadComms::priorityDataQueue = nullptr;
16-
uint_fast32_t LoadComms::txEvents = 0; // DONE: check against last years code
17-
uint_fast32_t LoadComms::bytesSent = 0;
18-
uint_fast32_t LoadComms::bytesNotSent = 0;
19-
uint_fast32_t LoadComms::rxEvents = 0;
20-
uint_fast32_t LoadComms::bytesReceived = 0;
16+
std::atomic<uint_fast32_t> LoadComms::txEvents = 0; // DONE: check against last years code
17+
std::atomic<uint_fast32_t> LoadComms::bytesSent = 0;
18+
std::atomic<uint_fast32_t> LoadComms::bytesNotSent = 0;
19+
std::atomic<uint_fast32_t> LoadComms::rxEvents = 0;
20+
std::atomic<uint_fast32_t> LoadComms::bytesReceived = 0;
2121

2222
/**
2323
* @brief MAC address of the nacelle controller.
@@ -122,19 +122,19 @@ etl::string<LoadComms::LOG_STRING_SIZE> LoadComms::getLogString() const {
122122
* https://stackoverflow.com/questions/12346487/what-do-each-memory-order-mean
123123
* @see https://en.cppreference.com/cpp/atomic/memory_order
124124
*/
125-
etl::to_string(txEvents, logString, decFormatA, true); // 6 chars
125+
etl::to_string(txEvents.load(std::memory_order_relaxed), logString, decFormatA, true); // 6 chars
126126
logString.append(", TxBS: "); // 8 chars
127127

128128
etl::format_spec decFormatB;
129129
decFormatB.width(7).fill('0'); // [7 chars]
130-
etl::to_string(bytesSent, logString, decFormatB, true); // 7 chars
130+
etl::to_string(bytesSent.load(std::memory_order_relaxed), logString, decFormatB, true); // 7 chars
131131
logString.append(", TxBF: "); // 8 chars
132-
etl::to_string(bytesNotSent, logString, decFormatB, true); // 7 chars
132+
etl::to_string(bytesNotSent.load(std::memory_order_relaxed), logString, decFormatB, true); // 7 chars
133133

134134
logString.append(", RxE: "); // 7 chars
135-
etl::to_string(rxEvents, logString, decFormatA, true); // 6 chars
135+
etl::to_string(rxEvents.load(std::memory_order_relaxed), logString, decFormatA, true); // 6 chars
136136
logString.append(", RxBS: "); // 8 chars
137-
etl::to_string(bytesReceived, logString, decFormatB, true); // 7 chars
137+
etl::to_string(bytesReceived.load(std::memory_order_relaxed), logString, decFormatB, true); // 7 chars
138138

139139
return logString;
140140
}
@@ -154,6 +154,9 @@ void LoadComms::onDataSent_(const wifi_tx_info_t *tx_info, esp_now_send_status_t
154154
}
155155

156156
void LoadComms::onDataRecv_(const esp_now_recv_info_t *recv_info, const uint8_t *data, int len) {
157+
rxEvents++;
158+
bytesReceived += len;
159+
157160
if (s_instance == nullptr) {
158161
ESP_LOGE(TAG, "Rx CB: Invalid instance");
159162
return;
@@ -174,8 +177,6 @@ void LoadComms::onDataRecv_(const esp_now_recv_info_t *recv_info, const uint8_t
174177

175178
// Serial.println("Received NacellePacket:");
176179
// printNacellePacket(s_instance->incomingPacket_, Serial);
177-
rxEvents++;
178-
bytesReceived += len;
179180

180181
(void)xQueueOverwrite(priorityDataQueue, data); // Allegedly cannot fail
181182
} else {

lib/LoadComms/LoadComms.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <etl/format_spec.h>
2424
#include <etl/string.h>
2525
#include <etl/to_string.h>
26+
#include <atomic>
27+
#include <cstdint>
2628

2729
/**
2830
* @brief MAC address of the nacelle controller.
@@ -101,11 +103,11 @@ class LoadComms {
101103
LoadboxPacket outgoingPacket_; ///< Outgoing packet to send.
102104
unsigned long lastSendTime_; ///< Timestamp of last transmission.
103105
unsigned long lastRxTime_; ///< Timestamp of last received packet.
104-
static uint_fast32_t txEvents; // DONE: check against last years code
105-
static uint_fast32_t bytesSent;
106-
static uint_fast32_t bytesNotSent;
107-
static uint_fast32_t rxEvents;
108-
static uint_fast32_t bytesReceived;
106+
static std::atomic<uint_fast32_t> txEvents; // DONE: check against last years code
107+
static std::atomic<uint_fast32_t> bytesSent;
108+
static std::atomic<uint_fast32_t> bytesNotSent;
109+
static std::atomic<uint_fast32_t> rxEvents;
110+
static std::atomic<uint_fast32_t> bytesReceived;
109111
bool linkAlive_; ///< Link health status.
110112
// float nacelleRPM_; ///< Cached RPM value.
111113

0 commit comments

Comments
 (0)