Use early returns

This commit is contained in:
Sybren A. Stüvel 2026-03-14 20:36:14 +01:00
parent d4fbc17d43
commit d697d8a790

View file

@ -30,19 +30,21 @@ class BLELogInterface : public Print, BLEServerCallbacks {
int _line_len; int _line_len;
void flushLine() { void flushLine() {
if (_line_len > 0 && _connected) { if (_line_len == 0 || !_connected) {
// BLE notifications are capped at ATT_MTU-3 bytes; MTU negotiation is not _line_len = 0;
// guaranteed, so always chunk at 20 bytes (the safe minimum) to ensure the return;
// full line is delivered regardless of the negotiated MTU. }
const int CHUNK = 20; // BLE notifications are capped at ATT_MTU-3 bytes; MTU negotiation is not
int offset = 0; // guaranteed, so always chunk at 20 bytes (the safe minimum) to ensure the
while (offset < _line_len) { // full line is delivered regardless of the negotiated MTU.
int n = _line_len - offset; const int CHUNK = 20;
if (n > CHUNK) n = CHUNK; int offset = 0;
_tx_char->setValue((uint8_t*)_line_buf + offset, n); while (offset < _line_len) {
_tx_char->notify(); int n = _line_len - offset;
offset += n; if (n > CHUNK) n = CHUNK;
} _tx_char->setValue((uint8_t*)_line_buf + offset, n);
_tx_char->notify();
offset += n;
} }
_line_len = 0; _line_len = 0;
} }
@ -97,11 +99,10 @@ public:
} }
void loop() { void loop() {
if (_adv_restart_time && millis() >= _adv_restart_time) { if (!_adv_restart_time || millis() < _adv_restart_time) return;
if (_server->getConnectedCount() == 0) { _adv_restart_time = 0;
BLEDevice::startAdvertising(); if (_server->getConnectedCount() == 0) {
} BLEDevice::startAdvertising();
_adv_restart_time = 0;
} }
} }