Merge remote-tracking branch 'upstream/dev' into feat/kiss-agc-fem-control
This commit is contained in:
commit
c0ccb9c7ff
123 files changed
+2095
-396
No files matched your search
+6
-6
@@ -87,13 +87,14 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) {
|
||||
}
|
||||
|
||||
if (self_id.isHashMatch(pkt->path, pkt->getPathHashSize()) && allowPacketForward(pkt)) {
|
||||
uint32_t d = getDirectRetransmitDelay(pkt);
|
||||
if (pkt->getPayloadType() == PAYLOAD_TYPE_MULTIPART) {
|
||||
return forwardMultipartDirect(pkt);
|
||||
return forwardMultipartDirect(pkt, d);
|
||||
} else if (pkt->getPayloadType() == PAYLOAD_TYPE_ACK) {
|
||||
if (!_tables->wasSeen(pkt)) { // don't retransmit!
|
||||
_tables->markSeen(pkt);
|
||||
removeSelfFromPath(pkt);
|
||||
routeDirectRecvAcks(pkt, 0);
|
||||
routeDirectRecvAcks(pkt, d);
|
||||
}
|
||||
return ACTION_RELEASE;
|
||||
}
|
||||
@@ -102,7 +103,6 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) {
|
||||
_tables->markSeen(pkt);
|
||||
removeSelfFromPath(pkt);
|
||||
|
||||
uint32_t d = getDirectRetransmitDelay(pkt);
|
||||
return ACTION_RETRANSMIT_DELAYED(0, d); // Routed traffic is HIGHEST priority
|
||||
}
|
||||
}
|
||||
@@ -356,7 +356,7 @@ DispatcherAction Mesh::routeRecvPacket(Packet* packet) {
|
||||
return ACTION_RELEASE;
|
||||
}
|
||||
|
||||
DispatcherAction Mesh::forwardMultipartDirect(Packet* pkt) {
|
||||
DispatcherAction Mesh::forwardMultipartDirect(Packet* pkt, uint32_t delay_millis) {
|
||||
uint8_t remaining = pkt->payload[0] >> 4; // num of packets in this multipart sequence still to be sent
|
||||
uint8_t type = pkt->payload[0] & 0x0F;
|
||||
|
||||
@@ -370,7 +370,7 @@ DispatcherAction Mesh::forwardMultipartDirect(Packet* pkt) {
|
||||
if (!_tables->wasSeen(&tmp)) { // don't retransmit!
|
||||
_tables->markSeen(&tmp);
|
||||
removeSelfFromPath(&tmp);
|
||||
routeDirectRecvAcks(&tmp, ((uint32_t)remaining + 1) * 300); // expect multipart ACKs 300ms apart (x2)
|
||||
routeDirectRecvAcks(&tmp, delay_millis + ((uint32_t)remaining + 1) * 300); // expect multipart ACKs 300ms apart (x2)
|
||||
}
|
||||
}
|
||||
return ACTION_RELEASE;
|
||||
@@ -380,13 +380,13 @@ void Mesh::routeDirectRecvAcks(Packet* packet, uint32_t delay_millis) {
|
||||
if (!packet->isMarkedDoNotRetransmit()) {
|
||||
uint8_t extra = getExtraAckTransmitCount();
|
||||
while (extra > 0) {
|
||||
delay_millis += getDirectRetransmitDelay(packet) + 300;
|
||||
auto a1 = createMultiAck(packet->payload, packet->payload_len, extra);
|
||||
if (a1) {
|
||||
a1->path_len = Packet::copyPath(a1->path, packet->path, packet->path_len);
|
||||
a1->header &= ~PH_ROUTE_MASK;
|
||||
a1->header |= ROUTE_TYPE_DIRECT;
|
||||
sendPacket(a1, 0, delay_millis);
|
||||
delay_millis += 300;
|
||||
}
|
||||
extra--;
|
||||
}
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ class Mesh : public Dispatcher {
|
||||
void removeSelfFromPath(Packet* packet);
|
||||
void routeDirectRecvAcks(Packet* packet, uint32_t delay_millis);
|
||||
//void routeRecvAcks(Packet* packet, uint32_t delay_millis);
|
||||
DispatcherAction forwardMultipartDirect(Packet* pkt);
|
||||
DispatcherAction forwardMultipartDirect(Packet* pkt, uint32_t delay_millis);
|
||||
|
||||
protected:
|
||||
DispatcherAction onRecvPacket(Packet* pkt) override;
|
||||
|
||||
@@ -68,6 +68,16 @@ public:
|
||||
// Power management interface (boards with power management override these)
|
||||
virtual bool isPwrMgtInitialised() const { return false; }
|
||||
virtual bool isExternalPowered() { return false; }
|
||||
// True while a board can identify an active battery-charging source.
|
||||
virtual bool isChargerActive() { return false; }
|
||||
|
||||
// Optional, source-specific power detection. Boards that can distinguish a USB
|
||||
// supply from a solar charger override these; defaults keep every other board
|
||||
// unaffected (the generic CLI prints "n/a" when a capability is absent).
|
||||
virtual bool hasUsbPowerDetect() const { return false; }
|
||||
virtual bool isUsbPowered() { return false; }
|
||||
virtual bool hasSolarChargerDetect() const { return false; }
|
||||
virtual bool isSolarChargerActive() { return false; }
|
||||
virtual uint16_t getBootVoltage() { return 0; }
|
||||
virtual bool getWakeLpcompSupported() const { return false; }
|
||||
virtual uint32_t getResetReason() const { return 0; }
|
||||
|
||||
+5
-2
@@ -147,6 +147,9 @@ int Utils::encryptThenMAC(const uint8_t* shared_secret, uint8_t* dest, const uin
|
||||
int Utils::MACThenDecrypt(const uint8_t* shared_secret, uint8_t* dest, const uint8_t* src, int src_len) {
|
||||
if (src_len <= CIPHER_MAC_SIZE) return 0; // invalid src bytes
|
||||
|
||||
int enc_len = src_len - CIPHER_MAC_SIZE;
|
||||
if (enc_len % CIPHER_BLOCK_SIZE != 0) return 0; // reject non-block-aligned ciphertext
|
||||
|
||||
uint8_t hmac[CIPHER_MAC_SIZE];
|
||||
#ifdef USE_CC310_HW_CRYPTO
|
||||
{
|
||||
@@ -161,12 +164,12 @@ int Utils::MACThenDecrypt(const uint8_t* shared_secret, uint8_t* dest, const uin
|
||||
{
|
||||
SHA256 sha;
|
||||
sha.resetHMAC(shared_secret, PUB_KEY_SIZE);
|
||||
sha.update(src + CIPHER_MAC_SIZE, src_len - CIPHER_MAC_SIZE);
|
||||
sha.update(src + CIPHER_MAC_SIZE, enc_len);
|
||||
sha.finalizeHMAC(shared_secret, PUB_KEY_SIZE, hmac, CIPHER_MAC_SIZE);
|
||||
}
|
||||
#endif
|
||||
if (memcmp(hmac, src, CIPHER_MAC_SIZE) == 0) {
|
||||
return decrypt(shared_secret, dest, src + CIPHER_MAC_SIZE, src_len - CIPHER_MAC_SIZE);
|
||||
return decrypt(shared_secret, dest, src + CIPHER_MAC_SIZE, enc_len);
|
||||
}
|
||||
return 0; // invalid HMAC
|
||||
}
|
||||
|
||||
+1
-1
@@ -50,7 +50,7 @@ public:
|
||||
|
||||
/**
|
||||
* \brief checks the MAC (in leading bytes of 'src'), then if valid, decrypts remaining bytes in src.
|
||||
* \returns zero if MAC is invalid, otherwise the length of decrypted bytes in 'dest'
|
||||
* \returns zero if MAC is invalid or ciphertext is not block-aligned, otherwise the length of decrypted bytes in 'dest'
|
||||
*/
|
||||
static int MACThenDecrypt(const uint8_t* shared_secret, uint8_t* dest, const uint8_t* src, int src_len);
|
||||
|
||||
|
||||
@@ -128,6 +128,7 @@ void BaseChatMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id,
|
||||
|
||||
ContactInfo* from = NULL;
|
||||
for (int i = 0; i < num_contacts; i++) {
|
||||
if (contacts[i].isEmptySlot()) continue;
|
||||
if (id.matches(contacts[i].id)) { // is from one of our contacts
|
||||
from = &contacts[i];
|
||||
if (timestamp <= from->last_advert_timestamp) { // check for replay attacks!!
|
||||
@@ -203,6 +204,11 @@ void BaseChatMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id,
|
||||
int BaseChatMesh::searchPeersByHash(const uint8_t* hash) {
|
||||
int n = 0;
|
||||
for (int i = 0; i < num_contacts && n < MAX_SEARCH_RESULTS; i++) {
|
||||
// Skip empty/unconfigured anon slots. An empty slot has an all-zero pub_key
|
||||
// (an order-4 Edwards25519 point whose clamped X25519 key exchange yields an
|
||||
// all-zero shared secret for any private key) and would otherwise match
|
||||
// src_hash == 0x00 as a null-key peer sink while consuming search slots.
|
||||
if (contacts[i].isEmptySlot()) continue;
|
||||
if (contacts[i].id.isHashMatch(hash)) {
|
||||
matching_peer_indexes[n++] = i; // store the INDEXES of matching contacts (for subsequent 'peer' methods)
|
||||
}
|
||||
@@ -516,6 +522,11 @@ int BaseChatMesh::sendCommandData(const ContactInfo& recipient, uint32_t timest
|
||||
}
|
||||
|
||||
bool BaseChatMesh::sendGroupMessage(uint32_t timestamp, mesh::GroupChannel& channel, const char* sender_name, const char* text, int text_len) {
|
||||
if (text_len < 0) { // memcpy() below would read this as a size_t, so clamping cannot catch it
|
||||
MESH_DEBUG_PRINTLN("sendGroupMessage: invalid negative text_len=%d", text_len);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t temp[5+MAX_TEXT_LEN+32];
|
||||
memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique
|
||||
temp[4] = 0; // TXT_TYPE_PLAIN
|
||||
@@ -866,6 +877,7 @@ ContactInfo* BaseChatMesh::searchContactsByPrefix(const char* name_prefix) {
|
||||
int len = strlen(name_prefix);
|
||||
for (int i = 0; i < num_contacts; i++) {
|
||||
auto c = &contacts[i];
|
||||
if (c->isEmptySlot() || c->name[0] == 0) continue;
|
||||
if (memcmp(c->name, name_prefix, len) == 0) return c;
|
||||
}
|
||||
return NULL; // not found
|
||||
@@ -874,6 +886,7 @@ ContactInfo* BaseChatMesh::searchContactsByPrefix(const char* name_prefix) {
|
||||
ContactInfo* BaseChatMesh::lookupContactByPubKey(const uint8_t* pub_key, int prefix_len) {
|
||||
for (int i = 0; i < num_contacts; i++) {
|
||||
auto c = &contacts[i];
|
||||
if (c->isEmptySlot()) continue;
|
||||
if (memcmp(c->id.pub_key, pub_key, prefix_len) == 0) return c;
|
||||
}
|
||||
return NULL; // not found
|
||||
@@ -891,7 +904,7 @@ bool BaseChatMesh::addContact(const ContactInfo& contact) {
|
||||
|
||||
bool BaseChatMesh::removeContact(ContactInfo& contact) {
|
||||
int idx = 0;
|
||||
while (idx < num_contacts && !contacts[idx].id.matches(contact.id)) {
|
||||
while (idx < num_contacts && (contacts[idx].isEmptySlot() || !contacts[idx].id.matches(contact.id))) {
|
||||
idx++;
|
||||
}
|
||||
if (idx >= num_contacts) return false; // not found
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#include "ContactInfo.h"
|
||||
|
||||
#define MAX_SEARCH_RESULTS 8
|
||||
#define MAX_SEARCH_RESULTS 24
|
||||
|
||||
#define MSG_SEND_FAILED 0
|
||||
#define MSG_SEND_SENT_FLOOD 1
|
||||
|
||||
@@ -284,6 +284,19 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
sprintf(reply, "%s (Build: %s)", _callbacks->getFirmwareVer(), _callbacks->getBuildDate());
|
||||
} else if (memcmp(command, "board", 5) == 0) {
|
||||
sprintf(reply, "%s", _board->getManufacturerName());
|
||||
} else if (strcmp(command, "power") == 0) {
|
||||
const char* usb_state = _board->hasUsbPowerDetect()
|
||||
? (_board->isUsbPowered() ? "yes" : "no")
|
||||
: "n/a";
|
||||
const char* solar_state = _board->hasSolarChargerDetect()
|
||||
? (_board->isSolarChargerActive() ? "yes" : "no")
|
||||
: "n/a";
|
||||
snprintf(reply, 160, "batt:%u mV usb:%s solar_chg:%s ext:%s charger:%s",
|
||||
(unsigned)_board->getBattMilliVolts(),
|
||||
usb_state,
|
||||
solar_state,
|
||||
_board->isExternalPowered() ? "yes" : "no",
|
||||
_board->isChargerActive() ? "yes" : "no");
|
||||
} else if (memcmp(command, "sensor get ", 11) == 0) {
|
||||
const char* key = command + 11;
|
||||
const char* val = _sensors->getSettingByKey(key);
|
||||
@@ -313,18 +326,26 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
if (start >= end) {
|
||||
strcpy(reply, "no custom var");
|
||||
} else {
|
||||
const int reply_max = 160; // callers pass a 160-byte reply buffer
|
||||
const int next_reserve = 16; // room for "... next:NNN"
|
||||
sprintf(dp, "%d vars\n", end);
|
||||
dp = strchr(dp, 0);
|
||||
int i;
|
||||
for (i = start; i < end && (dp-reply < 134); i++) {
|
||||
sprintf(dp, "%s=%s\n",
|
||||
_sensors->getSettingName(i),
|
||||
_sensors->getSettingValue(i));
|
||||
for (i = start; i < end; i++) {
|
||||
const char* name = _sensors->getSettingName(i);
|
||||
const char* value = _sensors->getSettingValue(i);
|
||||
if (name == NULL) name = "?";
|
||||
if (value == NULL) value = "";
|
||||
int room = reply_max - next_reserve - (int)(dp - reply);
|
||||
int need = snprintf(NULL, 0, "%s=%s\n", name, value);
|
||||
if (need >= room && i > start) break; // continue on the next page
|
||||
snprintf(dp, room, "%s=%s\n", name, value); // a single oversized entry is truncated
|
||||
dp = strchr(dp, 0);
|
||||
if (need >= room) { i++; break; }
|
||||
}
|
||||
if (i < end) {
|
||||
sprintf(dp, "... next:%d", i);
|
||||
} else {
|
||||
snprintf(dp, reply_max - (int)(dp - reply), "... next:%d", i);
|
||||
} else if (dp > reply && *(dp-1) == '\n') {
|
||||
*(dp-1) = 0; // remove last CR
|
||||
}
|
||||
}
|
||||
@@ -355,6 +376,20 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
} else {
|
||||
strcpy(reply, "gps provider not found");
|
||||
}
|
||||
} else if (strcmp(command, "gps diag") == 0) {
|
||||
LocationProvider * l = _sensors->getLocationProvider();
|
||||
if (l != NULL) {
|
||||
const char* setting = _sensors->getSettingByKey("gps");
|
||||
bool requested = setting != NULL && strcmp(setting, "1") == 0;
|
||||
int prefix_len = snprintf(reply, 160, "req:%u ", requested ? 1U : 0U);
|
||||
if (prefix_len < 0 || prefix_len >= 160) {
|
||||
reply[159] = 0;
|
||||
} else {
|
||||
l->formatDiagnostics(reply + prefix_len, 160 - prefix_len);
|
||||
}
|
||||
} else {
|
||||
strcpy(reply, "gps provider not found");
|
||||
}
|
||||
} else if (memcmp(command, "gps setloc", 10) == 0) {
|
||||
_prefs->node_lat = _sensors->node_lat;
|
||||
_prefs->node_lon = _sensors->node_lon;
|
||||
|
||||
@@ -115,8 +115,8 @@ private:
|
||||
void setIntThresh(uint8_t t) override { _parent->interference_threshold = t; markDirty(); }
|
||||
uint8_t getRxGain() const override { return _parent->rx_boosted_gain; }
|
||||
void setRxGain(uint8_t g) override { _parent->rx_boosted_gain = g; markDirty(); }
|
||||
uint8_t getTxPower() const override { return _parent->tx_power_dbm; }
|
||||
void setTxPower(uint8_t dbm) override { _parent->tx_power_dbm = dbm; markDirty(); }
|
||||
int8_t getTxPower() const override { return _parent->tx_power_dbm; }
|
||||
void setTxPower(int8_t dbm) override { _parent->tx_power_dbm = dbm; markDirty(); }
|
||||
float getRxDelay() const override { return _parent->rx_delay_base; }
|
||||
void setRxDelay(float d) override { _parent->rx_delay_base = d; markDirty(); }
|
||||
uint16_t getAgcResetInt() const override { return _parent->agc_reset_interval * 4; }
|
||||
|
||||
@@ -2,6 +2,22 @@
|
||||
#include "TxtDataHelpers.h"
|
||||
#include "Utils.h"
|
||||
#include "target.h"
|
||||
#include <ctype.h>
|
||||
|
||||
namespace {
|
||||
|
||||
// strtof() and atof() also read "nan" and "inf". A NaN cannot be refused by a
|
||||
// range check: no ordered comparison is true for it, and under -ffast-math,
|
||||
// which nRF52 builds use, the compiler may assume it never occurs and fold any
|
||||
// test on the parsed value away. So the text is checked before it is parsed:
|
||||
// after optional white space and sign, a number starts with a digit or a '.'.
|
||||
bool looksLikeANumber(const char* s) {
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
if (*s == '+' || *s == '-') s++;
|
||||
return isdigit((unsigned char)*s) || *s == '.';
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool CommonRadioPrefs::getByKey(const char* key, char* value, size_t max_len) {
|
||||
if (strcmp(key, "fem_rxgain") == 0) {
|
||||
@@ -69,7 +85,7 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
|
||||
if (memcmp(command, "set af ", 7) == 0) {
|
||||
char* end;
|
||||
float af = strtof(&command[7], &end);
|
||||
if (end == &command[7] || af < 0 || af > 9) {
|
||||
if (end == &command[7] || !looksLikeANumber(&command[7]) || af < 0 || af > 9) {
|
||||
strcpy(reply, "ERROR: af must be 0-9");
|
||||
} else {
|
||||
setAirtimeFactor(af);
|
||||
@@ -87,7 +103,7 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
|
||||
}
|
||||
if (memcmp(command, "set dutycycle ", 14) == 0) {
|
||||
float dc = atof(&command[14]);
|
||||
if (dc < 1 || dc > 100) {
|
||||
if (!looksLikeANumber(&command[14]) || dc < 1 || dc > 100) {
|
||||
strcpy(reply, "ERROR: dutycycle must be 1-100");
|
||||
} else {
|
||||
setAirtimeFactor((100.0f / dc) - 1.0f);
|
||||
|
||||
@@ -35,8 +35,8 @@ public:
|
||||
virtual uint8_t getRxGain() const = 0;
|
||||
virtual void setRxGain(uint8_t g) = 0;
|
||||
|
||||
virtual uint8_t getTxPower() const = 0;
|
||||
virtual void setTxPower(uint8_t dbm) = 0;
|
||||
virtual int8_t getTxPower() const = 0;
|
||||
virtual void setTxPower(int8_t dbm) = 0;
|
||||
|
||||
virtual float getRxDelay() const = 0;
|
||||
virtual void setRxDelay(float d) = 0;
|
||||
|
||||
@@ -31,6 +31,12 @@ struct ContactInfo {
|
||||
bool isTelemLocAllowed() const { return flags & 0x04; }
|
||||
bool isTelemEnvAllowed() const { return flags & 0x08; }
|
||||
bool isRemoteCLIAllowed() const { return flags & 0x10; }
|
||||
bool isEmptySlot() const {
|
||||
for (int i = 0; i < PUB_KEY_SIZE; i++) {
|
||||
if (id.pub_key[i] != 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable uint8_t shared_secret[PUB_KEY_SIZE];
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
#include "ESP32Board.h"
|
||||
#include <target.h>
|
||||
|
||||
// The RTC backup of ESP32RTCClock, declared in ESP32Board.h.
|
||||
RTC_NOINIT_ATTR uint32_t _rtc_backup_time;
|
||||
RTC_NOINIT_ATTR uint32_t _rtc_backup_magic;
|
||||
|
||||
#if defined(ADMIN_PASSWORD) && !defined(DISABLE_WIFI_OTA) // Repeater or Room Server only
|
||||
#include <WiFi.h>
|
||||
#include <AsyncTCP.h>
|
||||
|
||||
@@ -200,19 +200,33 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
// The time ESP32RTCClock restores after a reset, in RTC memory that a reset does
|
||||
// not clear. Defined once, in ESP32Board.cpp: the inline members below run in
|
||||
// every translation unit that uses them and must all see the same backup.
|
||||
extern uint32_t _rtc_backup_time;
|
||||
extern uint32_t _rtc_backup_magic;
|
||||
#define RTC_BACKUP_MAGIC 0xAA55CC33
|
||||
#define RTC_TIME_MIN 1772323200 // 1 Mar 2026
|
||||
|
||||
class ESP32RTCClock : public mesh::RTCClock {
|
||||
public:
|
||||
ESP32RTCClock() { }
|
||||
void begin() {
|
||||
esp_reset_reason_t reason = esp_reset_reason();
|
||||
if (reason == ESP_RST_POWERON) {
|
||||
// start with some date/time in the recent past
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 1715770351; // 15 May 2024, 8:50pm
|
||||
if (reason == ESP_RST_DEEPSLEEP) {
|
||||
return; // ESP-IDF preserves system time across deep sleep
|
||||
}
|
||||
// All other resets (power-on, crash, WDT, brownout) lose system time.
|
||||
// Restore from RTC backup if valid, otherwise use hardcoded seed.
|
||||
struct timeval tv;
|
||||
if (_rtc_backup_magic == RTC_BACKUP_MAGIC && _rtc_backup_time > RTC_TIME_MIN) {
|
||||
tv.tv_sec = _rtc_backup_time;
|
||||
} else {
|
||||
tv.tv_sec = RTC_TIME_MIN;
|
||||
}
|
||||
tv.tv_usec = 0;
|
||||
settimeofday(&tv, NULL);
|
||||
}
|
||||
}
|
||||
uint32_t getCurrentTime() override {
|
||||
time_t _now;
|
||||
time(&_now);
|
||||
@@ -223,6 +237,16 @@ public:
|
||||
tv.tv_sec = time;
|
||||
tv.tv_usec = 0;
|
||||
settimeofday(&tv, NULL);
|
||||
_rtc_backup_time = time;
|
||||
_rtc_backup_magic = RTC_BACKUP_MAGIC;
|
||||
}
|
||||
void tick() override {
|
||||
time_t now;
|
||||
time(&now);
|
||||
if (now > RTC_TIME_MIN && (uint32_t)now != _rtc_backup_time) {
|
||||
_rtc_backup_time = (uint32_t)now;
|
||||
_rtc_backup_magic = RTC_BACKUP_MAGIC;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -188,6 +188,7 @@ int RegionMap::getTransportKeysFor(const RegionEntry& src, TransportKey dest[],
|
||||
}
|
||||
|
||||
RegionEntry* RegionMap::findMatch(mesh::Packet* packet, uint8_t mask) {
|
||||
if (packet->transport_codes[0] == 0) return (wildcard.flags & mask) == 0 ? &wildcard : NULL;
|
||||
for (int i = 0; i < num_regions; i++) {
|
||||
auto region = ®ions[i];
|
||||
if ((region->flags & mask) == 0) { // does region allow this? (per 'mask' param)
|
||||
|
||||
@@ -17,6 +17,7 @@ inline bool isFloodHopLimitExceeded(const Packet* packet, uint8_t flood_max,
|
||||
uint8_t hops = packet->getPathHashCount();
|
||||
if (hops >= flood_max) return true;
|
||||
if (packet->getRouteType() == ROUTE_TYPE_FLOOD && hops >= flood_max_unscoped) return true;
|
||||
if (packet->getRouteType() == ROUTE_TYPE_TRANSPORT_FLOOD && packet->transport_codes[0] == 0 && hops >= flood_max_unscoped) return true; // scope 0, treat as unscoped
|
||||
if (packet->getPayloadType() == PAYLOAD_TYPE_ADVERT && hops >= flood_max_advert) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -129,8 +129,12 @@ void SerialBLEInterface::begin(const char* prefix, char* name, uint32_t pin_code
|
||||
char charpin[20];
|
||||
snprintf(charpin, sizeof(charpin), "%lu", (unsigned long)pin_code);
|
||||
|
||||
// If we want to control BLE LED ourselves, uncomment this:
|
||||
// Bluefruit.autoConnLed(false);
|
||||
#ifdef DISABLE_BLE_LED // use variant platformio.ini build_flags macro: -D DISABLE_BLE_LED
|
||||
Bluefruit.autoConnLed(false); // false removes the automatic control of the LED by the Bluefruit library
|
||||
#else
|
||||
Bluefruit.autoConnLed(true); // true leaves the default automatic control of the LED by the Bluefruit library
|
||||
#endif
|
||||
|
||||
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
|
||||
Bluefruit.begin();
|
||||
|
||||
|
||||
@@ -20,6 +20,11 @@ public:
|
||||
|
||||
}
|
||||
|
||||
bool lastRxHadCRC() override {
|
||||
bool crc = true;
|
||||
((CustomLLCC68 *)_radio)->getLoRaRxHeaderInfo(NULL, &crc);
|
||||
return crc;
|
||||
}
|
||||
bool isReceivingPacket() override {
|
||||
return ((CustomLLCC68 *)_radio)->isReceiving();
|
||||
}
|
||||
|
||||
@@ -19,6 +19,11 @@ public:
|
||||
((CustomLR1110 *)_radio)->setMaxPayloadMillis(pm.payloadMillis);
|
||||
}
|
||||
|
||||
bool lastRxHadCRC() override {
|
||||
bool crc = true;
|
||||
((CustomLR1110 *)_radio)->getLoRaRxHeaderInfo(NULL, &crc);
|
||||
return crc;
|
||||
}
|
||||
bool isReceivingPacket() override {
|
||||
return ((CustomLR1110 *)_radio)->isReceiving();
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ class CustomLR1121 : public LR1121 {
|
||||
uint32_t _activityAt = 0;
|
||||
bool _headerSeen = false;
|
||||
bool _rx_boosted = false;
|
||||
int8_t _txPower = 0;
|
||||
bool _txPowerSet = false;
|
||||
|
||||
public:
|
||||
CustomLR1121(Module *mod) : LR1121(mod) { }
|
||||
@@ -36,6 +38,33 @@ class CustomLR1121 : public LR1121 {
|
||||
return len;
|
||||
}
|
||||
|
||||
using LR1121::setOutputPower;
|
||||
using LR1121::setFrequency;
|
||||
|
||||
int16_t setOutputPower(int8_t power) override {
|
||||
#ifdef LR1121_FORCE_HP_PA
|
||||
const bool force_hp = true; // boards with only RFO_HP_LF wired: never select the LP PA
|
||||
#else
|
||||
const bool force_hp = false;
|
||||
#endif
|
||||
// clip to the active PA's range (HF PA tops out at 13 dBm) rather than fail and keep the other band's PA
|
||||
int8_t clipped = power;
|
||||
checkOutputPower(power, &clipped, force_hp);
|
||||
int16_t state = LR1121::setOutputPower(clipped, force_hp);
|
||||
if (state == RADIOLIB_ERR_NONE) {
|
||||
_txPower = power;
|
||||
_txPowerSet = true;
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
int16_t setFrequency(float freq) override {
|
||||
int16_t state = LR1121::setFrequency(freq);
|
||||
// PA selection depends on the band, and set radio / tempradio don't call setTxPower
|
||||
if (state == RADIOLIB_ERR_NONE && _txPowerSet) state = setOutputPower(_txPower);
|
||||
return state;
|
||||
}
|
||||
|
||||
float getFreqMHz() const { return freqMHz; }
|
||||
|
||||
int16_t setRxBoostedGainMode(bool en) {
|
||||
|
||||
@@ -20,6 +20,11 @@ public:
|
||||
}
|
||||
|
||||
void doResetAGC() override { lr11x0ResetAGC((LR11x0 *)_radio, ((CustomLR1121 *)_radio)->getFreqMHz(), getRxBoostedGainMode()); }
|
||||
bool lastRxHadCRC() override {
|
||||
bool crc = true;
|
||||
((CustomLR1121 *)_radio)->getLoRaRxHeaderInfo(NULL, &crc);
|
||||
return crc;
|
||||
}
|
||||
bool isReceivingPacket() override {
|
||||
return ((CustomLR1121 *)_radio)->isReceiving();
|
||||
}
|
||||
|
||||
@@ -16,15 +16,23 @@ public:
|
||||
CustomLR2021Wrapper(CustomLR2021& radio, mesh::MainBoard& board) : RadioLibWrapper(radio, board) { }
|
||||
|
||||
void setParams(float freq, float bw, uint8_t sf, uint8_t cr) override {
|
||||
((CustomLR2021 *)_radio)->setFrequency(freq);
|
||||
((CustomLR2021 *)_radio)->setSpreadingFactor(sf);
|
||||
((CustomLR2021 *)_radio)->setBandwidth(bw);
|
||||
((CustomLR2021 *)_radio)->setCodingRate(cr);
|
||||
auto r = (CustomLR2021 *)_radio;
|
||||
// LR2021 only accepts RF/modulation config commands while in STANDBY mode.
|
||||
// Re-configuring while in continuous RX gets silently rejected (-706),
|
||||
// which broke hot frequency changes over BLE.
|
||||
int16_t status = r->standby();
|
||||
if (status != RADIOLIB_ERR_NONE) { MESH_DEBUG_PRINTLN("CustomLR2021Wrapper: standby() -> %d", status); }
|
||||
if ((status = r->setFrequency(freq)) != RADIOLIB_ERR_NONE) { MESH_DEBUG_PRINTLN("CustomLR2021Wrapper: setFrequency() -> %d", status); }
|
||||
if ((status = r->setSpreadingFactor(sf)) != RADIOLIB_ERR_NONE) { MESH_DEBUG_PRINTLN("CustomLR2021Wrapper: setSpreadingFactor() -> %d", status); }
|
||||
if ((status = r->setBandwidth(bw)) != RADIOLIB_ERR_NONE) { MESH_DEBUG_PRINTLN("CustomLR2021Wrapper: setBandwidth() -> %d", status); }
|
||||
if ((status = r->setCodingRate(cr)) != RADIOLIB_ERR_NONE) { MESH_DEBUG_PRINTLN("CustomLR2021Wrapper: setCodingRate() -> %d", status); }
|
||||
updatePreamble(sf);
|
||||
applySideDetectorConfig();
|
||||
// internal idle() sets STATE_IDLE, which triggers a startReceive() on the next loop
|
||||
status = applySideDetectorConfig();
|
||||
if (status != RADIOLIB_ERR_NONE) { MESH_DEBUG_PRINTLN("CustomLR2021Wrapper: applySideDetectorConfig() -> %d", status); }
|
||||
PacketMillis pm = calcMaxPacketMillis(sf, bw, cr, preambleLengthForSF(sf));
|
||||
((CustomLR2021 *)_radio)->setPreambleMillis(pm.preambleMillis);
|
||||
((CustomLR2021 *)_radio)->setMaxPayloadMillis(pm.payloadMillis);
|
||||
r->setPreambleMillis(pm.preambleMillis); // pure in-memory setters, no standby needed
|
||||
r->setMaxPayloadMillis(pm.payloadMillis);
|
||||
|
||||
}
|
||||
|
||||
@@ -73,6 +81,11 @@ public:
|
||||
return tSym;
|
||||
}
|
||||
|
||||
bool lastRxHadCRC() override {
|
||||
bool crc = true;
|
||||
((CustomLR2021 *)_radio)->getLoRaRxHeaderInfo(NULL, &crc);
|
||||
return crc;
|
||||
}
|
||||
bool isReceivingPacket() override {
|
||||
return ((CustomLR2021 *)_radio)->isReceiving();
|
||||
}
|
||||
|
||||
@@ -20,6 +20,11 @@ public:
|
||||
((CustomSTM32WLx *)_radio)->setMaxPayloadMillis(pm.payloadMillis);
|
||||
}
|
||||
|
||||
bool lastRxHadCRC() override {
|
||||
bool crc = true;
|
||||
((CustomSTM32WLx *)_radio)->getLoRaRxHeaderInfo(NULL, &crc);
|
||||
return crc;
|
||||
}
|
||||
bool isReceivingPacket() override {
|
||||
return ((CustomSTM32WLx *)_radio)->isReceiving();
|
||||
}
|
||||
|
||||
@@ -23,6 +23,11 @@ public:
|
||||
((CustomSX1262 *)_radio)->setMaxPayloadMillis(pm.payloadMillis);
|
||||
}
|
||||
|
||||
bool lastRxHadCRC() override {
|
||||
bool crc = true;
|
||||
((CustomSX1262 *)_radio)->getLoRaRxHeaderInfo(NULL, &crc);
|
||||
return crc;
|
||||
}
|
||||
bool isReceivingPacket() override {
|
||||
return ((CustomSX1262 *)_radio)->isReceiving();
|
||||
}
|
||||
|
||||
@@ -23,6 +23,11 @@ public:
|
||||
((CustomSX1268 *)_radio)->setMaxPayloadMillis(pm.payloadMillis);
|
||||
}
|
||||
|
||||
bool lastRxHadCRC() override {
|
||||
bool crc = true;
|
||||
((CustomSX1268 *)_radio)->getLoRaRxHeaderInfo(NULL, &crc);
|
||||
return crc;
|
||||
}
|
||||
bool isReceivingPacket() override {
|
||||
return ((CustomSX1268 *)_radio)->isReceiving();
|
||||
}
|
||||
|
||||
@@ -132,6 +132,9 @@ int RadioLibWrapper::recvRaw(uint8_t* bytes, int sz) {
|
||||
if (len > 0) {
|
||||
if (len > sz) { len = sz; }
|
||||
int err = _radio->readData(bytes, len);
|
||||
if (err == RADIOLIB_ERR_NONE && !lastRxHadCRC()) {
|
||||
err = RADIOLIB_ERR_LORA_HEADER_DAMAGED; // header says no CRC, so the payload was never checked
|
||||
}
|
||||
if (err != RADIOLIB_ERR_NONE) {
|
||||
MESH_DEBUG_PRINTLN("RadioLibWrapper: error: readData(%d)", err);
|
||||
len = 0;
|
||||
|
||||
@@ -26,10 +26,14 @@ protected:
|
||||
void startRecv();
|
||||
float packetScoreInt(float snr, int sf, int packet_len);
|
||||
virtual bool isReceivingPacket() =0;
|
||||
virtual bool lastRxHadCRC() { return true; }
|
||||
virtual void doResetAGC();
|
||||
|
||||
public:
|
||||
RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board) : _radio(&radio), _board(&board), _preamble_sf(0) { n_recv = n_sent = 0; }
|
||||
RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board)
|
||||
: _radio(&radio), _board(&board), _preamble_sf(0) {
|
||||
n_recv = n_sent = n_recv_errors = 0;
|
||||
}
|
||||
|
||||
void begin() override;
|
||||
virtual void powerOff() { _radio->sleep(); }
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include <helpers/sensors/LocationProvider.h>
|
||||
|
||||
// Board must keep the GPS RTC backup pin powered and can cut GPS power once we return.
|
||||
// Returns true if the command was acked within 3 attempts, otherwise returns false.
|
||||
static inline bool airohaEnterSleep(LocationProvider* nmea) {
|
||||
for (uint8_t attempt = 0; attempt < 3; attempt++) {
|
||||
nmea->drain();
|
||||
nmea->sendSentence("$PAIR650,0");
|
||||
if (nmea->waitFor("$PAIR001,650,0", 50)) { // wait for the command received signal
|
||||
#ifdef GPS_NMEA_DEBUG
|
||||
Serial.printf("Airoha RTC Backup sleep command accepted by GPS after %u attempts\r\n", attempt + 1);
|
||||
#endif
|
||||
nmea->waitFor("$PAIR650,0", 50); // give the GPS 50ms grace to signal it is ready for sleep
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -765,14 +765,18 @@ void EnvironmentSensorManager::initBasicGPS() {
|
||||
MESH_DEBUG_PRINTLN("No GPS wake/reset pin found for this board. Continuing on...");
|
||||
#endif
|
||||
|
||||
// Give GPS a moment to power up and send data
|
||||
delay(1000);
|
||||
|
||||
// We'll consider GPS detected if we see any data on Serial1
|
||||
// Give the GPS time to power up and emit its first NMEA sentence. Cold-start
|
||||
// modules can take several seconds, so poll for up to ~5s instead of checking
|
||||
// once after a fixed 1s delay -- returning as soon as any data appears, so
|
||||
// fast modules add no boot delay (issue #2258).
|
||||
#ifdef ENV_SKIP_GPS_DETECT
|
||||
gps_detected = true;
|
||||
#else
|
||||
gps_detected = (Serial1.available() > 0);
|
||||
gps_detected = false;
|
||||
for (int i = 0; i < 20 && !gps_detected; i++) {
|
||||
delay(250);
|
||||
gps_detected = (Serial1.available() > 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (gps_detected) {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "Mesh.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
class LocationProvider {
|
||||
@@ -17,9 +19,21 @@ public:
|
||||
virtual bool isValid() = 0;
|
||||
virtual long getTimestamp() = 0;
|
||||
virtual void sendSentence(const char * sentence);
|
||||
virtual bool waitFor(const char* prefix, uint32_t timeout_ms) { return false; }
|
||||
virtual void drain() { }
|
||||
virtual void reset() = 0;
|
||||
virtual void begin() = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual void loop() = 0;
|
||||
virtual bool isEnabled() = 0;
|
||||
|
||||
// Format compact diagnostics in the caller-provided buffer. Providers that
|
||||
// have more information (for example UART counters) can override this.
|
||||
virtual void formatDiagnostics(char* out, size_t out_size) {
|
||||
if (out_size == 0) return;
|
||||
snprintf(out, out_size, "en:%u sat:%ld fix:%u",
|
||||
isEnabled() ? 1U : 0U,
|
||||
satellitesCount(),
|
||||
isValid() ? 1U : 0U);
|
||||
}
|
||||
};
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <MicroNMEA.h>
|
||||
#include <RTClib.h>
|
||||
#include <helpers/RefCountedDigitalPin.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef GPS_EN
|
||||
#ifdef PIN_GPS_EN
|
||||
@@ -51,6 +52,42 @@ class MicroNMEALocationProvider : public LocationProvider {
|
||||
unsigned long _last_time_sync = 0;
|
||||
static const unsigned long TIME_SYNC_INTERVAL = 1800000; // Re-sync every 30 minutes
|
||||
|
||||
#ifdef GPS_L76K
|
||||
void configureL76K() {
|
||||
// Reapply the constellation mode at each provider start instead of
|
||||
// assuming it survives a receiver power cycle.
|
||||
// Mode 7 enables GPS + BeiDou + GLONASS.
|
||||
delay(250);
|
||||
MicroNMEA::sendSentence(*_gps_serial, "$PCAS04,7");
|
||||
delay(250);
|
||||
}
|
||||
#endif
|
||||
// Observation-only diagnostics. These fields do not change GPS power,
|
||||
// timing, reset behaviour, or parser input.
|
||||
uint32_t _uart_bytes = 0;
|
||||
uint32_t _nmea_checksum_ok = 0;
|
||||
uint32_t _nmea_checksum_bad = 0;
|
||||
uint32_t _begin_calls = 0;
|
||||
uint32_t _stop_calls = 0;
|
||||
unsigned long _last_uart_ms = 0;
|
||||
unsigned long _last_nmea_ms = 0;
|
||||
unsigned long _last_fix_ms = 0;
|
||||
bool _uart_seen = false;
|
||||
bool _nmea_seen = false;
|
||||
bool _fix_seen = false;
|
||||
|
||||
static uint32_t ageMs(unsigned long timestamp) {
|
||||
return (uint32_t)(millis() - timestamp);
|
||||
}
|
||||
|
||||
static void formatAge(char* out, size_t out_size, bool seen, unsigned long timestamp) {
|
||||
if (!seen) {
|
||||
snprintf(out, out_size, "never");
|
||||
} else {
|
||||
snprintf(out, out_size, "%lu", (unsigned long)ageMs(timestamp));
|
||||
}
|
||||
}
|
||||
|
||||
public :
|
||||
MicroNMEALocationProvider(Stream& ser, mesh::RTCClock* clock = NULL, int pin_reset = GPS_RESET, int pin_en = GPS_EN,RefCountedDigitalPin* peripher_power=NULL) :
|
||||
nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _clock(clock), _gps_serial(&ser), _peripher_power(peripher_power), _pin_reset(pin_reset), _pin_en(pin_en) {
|
||||
@@ -76,6 +113,7 @@ public :
|
||||
}
|
||||
|
||||
void begin() override {
|
||||
_begin_calls++;
|
||||
claim();
|
||||
if (_pin_en != -1) {
|
||||
digitalWrite(_pin_en, GPS_EN_ACTIVE);
|
||||
@@ -91,9 +129,15 @@ public :
|
||||
delay(10);
|
||||
digitalWrite(_pin_reset, !GPS_RESET_ACTIVE);
|
||||
}
|
||||
#ifdef GPS_L76K
|
||||
// EnvironmentSensorManager calls reset() immediately after begin().
|
||||
// Configure only after that reset so the setting is not discarded.
|
||||
configureL76K();
|
||||
#endif
|
||||
}
|
||||
|
||||
void stop() override {
|
||||
_stop_calls++;
|
||||
if (_pin_en != -1) {
|
||||
digitalWrite(_pin_en, !GPS_EN_ACTIVE);
|
||||
}
|
||||
@@ -129,18 +173,65 @@ public :
|
||||
return dt.unixtime();
|
||||
}
|
||||
|
||||
void drain() { while (_gps_serial->available()) _gps_serial->read(); }
|
||||
|
||||
const char* getSentence() const { return nmea.getSentence(); }
|
||||
|
||||
void sendSentence(const char *sentence) override {
|
||||
nmea.sendSentence(*_gps_serial, sentence);
|
||||
}
|
||||
|
||||
bool waitFor(const char* prefix, uint32_t timeout_ms) {
|
||||
size_t plen = strlen(prefix);
|
||||
uint32_t timeout = millis() + timeout_ms;
|
||||
while ((int32_t)(millis() - timeout) < 0) {
|
||||
if (_gps_serial->available()) {
|
||||
char c = _gps_serial->read();
|
||||
#ifdef GPS_NMEA_DEBUG
|
||||
Serial.print(c);
|
||||
#endif
|
||||
if (nmea.process(c) && strncmp(nmea.getSentence(), prefix, plen) == 0) return true;
|
||||
} else {
|
||||
yield();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void loop() override {
|
||||
|
||||
while (_gps_serial->available()) {
|
||||
char c = _gps_serial->read();
|
||||
_uart_bytes++;
|
||||
_last_uart_ms = millis();
|
||||
_uart_seen = true;
|
||||
#ifdef GPS_NMEA_DEBUG
|
||||
Serial.print(c);
|
||||
#endif
|
||||
nmea.process(c);
|
||||
bool parsed = nmea.process(c);
|
||||
|
||||
// MicroNMEA leaves the completed sentence in its buffer after the
|
||||
// first line terminator. CRLF therefore counts once: CR observes the
|
||||
// sentence, then LF clears the now-empty buffer.
|
||||
if ((c == '\0' || c == '\r' || c == '\n') && nmea.getSentence()[0] != '\0') {
|
||||
if (MicroNMEA::testChecksum(nmea.getSentence())) {
|
||||
_nmea_checksum_ok++;
|
||||
_last_nmea_ms = millis();
|
||||
_nmea_seen = true;
|
||||
|
||||
// Refresh fix age only when a newly parsed GGA/RMC sentence
|
||||
// carries a valid fix. Other valid NMEA sentences must not
|
||||
// make an old position look fresh.
|
||||
const char* message_id = nmea.getMessageID();
|
||||
if (parsed && isValid() &&
|
||||
(strcmp(message_id, "GGA") == 0 || strcmp(message_id, "RMC") == 0)) {
|
||||
_last_fix_ms = millis();
|
||||
_fix_seen = true;
|
||||
}
|
||||
} else {
|
||||
_nmea_checksum_bad++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isValid()) time_valid = 0;
|
||||
@@ -163,4 +254,31 @@ public :
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void formatDiagnostics(char* out, size_t out_size) override {
|
||||
if (out_size == 0) return;
|
||||
|
||||
char uart_age[11];
|
||||
char nmea_age[11];
|
||||
char fix_age[11];
|
||||
formatAge(uart_age, sizeof(uart_age), _uart_seen, _last_uart_ms);
|
||||
formatAge(nmea_age, sizeof(nmea_age), _nmea_seen, _last_nmea_ms);
|
||||
formatAge(fix_age, sizeof(fix_age), _fix_seen, _last_fix_ms);
|
||||
|
||||
// Compact enough for CommonCLI's 160-byte reply buffer, including the
|
||||
// "req:" prefix added by CommonCLI.
|
||||
snprintf(out, out_size,
|
||||
"en:%u ub:%lu ua:%s ok:%lu bad:%lu na:%s sat:%ld fix:%u fa:%s bc:%lu sc:%lu",
|
||||
isEnabled() ? 1U : 0U,
|
||||
(unsigned long)_uart_bytes,
|
||||
uart_age,
|
||||
(unsigned long)_nmea_checksum_ok,
|
||||
(unsigned long)_nmea_checksum_bad,
|
||||
nmea_age,
|
||||
satellitesCount(),
|
||||
isValid() ? 1U : 0U,
|
||||
fix_age,
|
||||
(unsigned long)_begin_calls,
|
||||
(unsigned long)_stop_calls);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "AutoDetectDisplay.h"
|
||||
#include <MeshCore.h>
|
||||
|
||||
int AutoDetectDisplay::getDisplayStatus(TwoWire& wire, uint8_t addr) {
|
||||
int status = -1;
|
||||
for(int i = 0; i < 4; i++){
|
||||
|
||||
// request display status
|
||||
wire.beginTransmission(addr);
|
||||
wire.write((uint8_t)0x00);
|
||||
|
||||
// ensure response received
|
||||
if (wire.endTransmission() != 0) return -1; // expect success
|
||||
if (wire.requestFrom((int)addr, 1) == 0) return -1; // expect at least 1 byte
|
||||
|
||||
// ensure two consecutive reads are the same
|
||||
int previous = status;
|
||||
status = wire.read() & 0x0F;
|
||||
if(status == previous){
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
bool AutoDetectDisplay::begin() {
|
||||
|
||||
// request status from display address
|
||||
int status = getDisplayStatus(Wire, DISPLAY_ADDRESS);
|
||||
MESH_DEBUG_PRINTLN("AutoDetectDisplay: status=0x%02X", status);
|
||||
|
||||
// detect SH1106
|
||||
if(status == 0x00 || status == 0x08){
|
||||
MESH_DEBUG_PRINTLN("AutoDetectDisplay: SH1106 detected");
|
||||
_impl = &_sh1106;
|
||||
return _sh1106.begin();
|
||||
}
|
||||
|
||||
// fallback to SSD1306
|
||||
MESH_DEBUG_PRINTLN("AutoDetectDisplay: falling back to SSD1306");
|
||||
_impl = &_ssd1306;
|
||||
return _ssd1306.begin();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "DisplayDriver.h"
|
||||
#include "SSD1306Display.h"
|
||||
#include "SH1106Display.h"
|
||||
|
||||
// Auto detects SH1106 display at runtime, otherwise falls back to using SSD1306
|
||||
class AutoDetectDisplay : public DisplayDriver {
|
||||
DisplayDriver* _impl;
|
||||
SSD1306Display _ssd1306;
|
||||
SH1106Display _sh1106;
|
||||
|
||||
int getDisplayStatus(TwoWire& wire, uint8_t addr);
|
||||
public:
|
||||
AutoDetectDisplay() : DisplayDriver(128, 64), _impl(&_ssd1306) { }
|
||||
bool begin();
|
||||
|
||||
bool isOn() override { return _impl->isOn(); }
|
||||
void turnOn() override { _impl->turnOn(); }
|
||||
void turnOff() override { _impl->turnOff(); }
|
||||
void clear() override { _impl->clear(); }
|
||||
void startFrame(ColorVal bkg = UIColor::window_bkg) override { _impl->startFrame(bkg); }
|
||||
void setTextSize(int sz) override { _impl->setTextSize(sz); }
|
||||
void setColor(ColorVal c) override { _impl->setColor(c); }
|
||||
void setCursor(int x, int y) override { _impl->setCursor(x, y); }
|
||||
void print(const char* str) override { _impl->print(str); }
|
||||
void fillRect(int x, int y, int w, int h) override { _impl->fillRect(x, y, w, h); }
|
||||
void drawRect(int x, int y, int w, int h) override { _impl->drawRect(x, y, w, h); }
|
||||
void drawXbm(int x, int y, const uint8_t* bits, int w, int h) override { _impl->drawXbm(x, y, bits, w, h); }
|
||||
uint16_t getTextWidth(const char* str) override { return _impl->getTextWidth(str); }
|
||||
void endFrame() override { _impl->endFrame(); }
|
||||
};
|
||||
@@ -25,7 +25,9 @@ public:
|
||||
virtual bool isEink() { return false; } // default to non-eink, override in eink drivers
|
||||
virtual void forceFullRefresh() {} // next refresh will be full for eink
|
||||
virtual void turnOn() = 0;
|
||||
virtual void turnOn(int max_partial) { turnOn(); }
|
||||
virtual void turnOff() = 0;
|
||||
virtual void turnOff(bool blank) { turnOff(); }
|
||||
virtual void clear() = 0;
|
||||
virtual void startFrame(ColorVal bkg = UIColor::window_bkg) = 0;
|
||||
virtual void setTextSize(int sz) = 0;
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
|
||||
#include "GxEPDDisplay.h"
|
||||
|
||||
#ifdef EXP_PIN_BACKLIGHT
|
||||
#include <PCA9557.h>
|
||||
extern PCA9557 expander;
|
||||
#endif
|
||||
|
||||
#ifndef DISPLAY_ROTATION
|
||||
#define DISPLAY_ROTATION 3
|
||||
#endif
|
||||
@@ -52,7 +47,7 @@ bool GxEPDDisplay::begin() {
|
||||
}
|
||||
|
||||
display.setPartialWindow(0, 0, display.width(), display.height());
|
||||
resetPartialRefreshCounter();
|
||||
_partial_refresh_count = 0;
|
||||
|
||||
#if DISP_BACKLIGHT
|
||||
digitalWrite(DISP_BACKLIGHT, LOW);
|
||||
@@ -65,29 +60,55 @@ bool GxEPDDisplay::begin() {
|
||||
|
||||
void GxEPDDisplay::turnOn() {
|
||||
if (!_init) begin();
|
||||
#if defined(DISP_BACKLIGHT) && !defined(BACKLIGHT_BTN)
|
||||
digitalWrite(DISP_BACKLIGHT, HIGH);
|
||||
#elif defined(EXP_PIN_BACKLIGHT) && !defined(BACKLIGHT_BTN)
|
||||
expander.digitalWrite(EXP_PIN_BACKLIGHT, HIGH);
|
||||
#endif
|
||||
if (!_isOn) {
|
||||
_isOn = true;
|
||||
}
|
||||
}
|
||||
|
||||
void GxEPDDisplay::turnOff() {
|
||||
#if defined(DISP_BACKLIGHT) && !defined(BACKLIGHT_BTN)
|
||||
digitalWrite(DISP_BACKLIGHT, LOW);
|
||||
#elif defined(EXP_PIN_BACKLIGHT) && !defined(BACKLIGHT_BTN)
|
||||
expander.digitalWrite(EXP_PIN_BACKLIGHT, LOW);
|
||||
#endif
|
||||
void GxEPDDisplay::turnOn(int max_partial) {
|
||||
setMaxPartialRefresh(max_partial);
|
||||
turnOn();
|
||||
}
|
||||
|
||||
// 24x24 sleep icon, MSB-first (matches GxEPDDisplay::drawXbm)
|
||||
static const uint8_t sleep_icon_24x24[] = {
|
||||
0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xe0, 0x0e, 0x03, 0xc0, 0x18,
|
||||
0x0f, 0x80, 0x70, 0x1f, 0x80, 0xff, 0x3f, 0x80, 0xff, 0x3f, 0x00, 0x00,
|
||||
0x7f, 0x3f, 0x00, 0x7f, 0x3f, 0x00, 0xff, 0x0e, 0x00, 0xff, 0x1c, 0x7c,
|
||||
0xff, 0xbf, 0x7c, 0xff, 0xbf, 0x38, 0xff, 0xc0, 0x7c, 0xff, 0xe0, 0x7c,
|
||||
0xff, 0xf0, 0x00, 0x7f, 0xfc, 0x00, 0x7f, 0xff, 0xf8, 0x3f, 0xff, 0xf0,
|
||||
0x1f, 0xff, 0xf0, 0x0f, 0xff, 0xe0, 0x07, 0xff, 0x80, 0x01, 0xff, 0x00
|
||||
};
|
||||
|
||||
void GxEPDDisplay::drawSleepIcon() {
|
||||
const int W = 24, H = 24;
|
||||
int x = (int)(display.width() / scale_x) - W; // top-right, logical coordinates
|
||||
int y = 0;
|
||||
|
||||
display.fillRect((x - 5) * scale_x - 1, y * scale_y, (W+5) * scale_x + 2, (H-2) * scale_y + 1, GxEPD_WHITE);
|
||||
_curr_color = GxEPD_BLACK;
|
||||
drawXbm(x, y, sleep_icon_24x24, W, H);
|
||||
display.display(true);
|
||||
}
|
||||
|
||||
void GxEPDDisplay::turnOff(bool blank) {
|
||||
if (blank) {
|
||||
blankScreen();
|
||||
} else {
|
||||
drawSleepIcon();
|
||||
}
|
||||
_isOn = false;
|
||||
last_display_crc_value=0;
|
||||
}
|
||||
|
||||
void GxEPDDisplay::blankScreen() {
|
||||
// do full refresh before powering off to clear screen
|
||||
// no full refresh needed at wakeup
|
||||
display.clearScreen(0xFF); // Clears microcontroller side RAM
|
||||
display.writeScreenBuffer(0xFF); // Forces 0xFF (White) into the display controller's history registers
|
||||
resetPartialRefreshCounter();
|
||||
last_display_crc_value=0;
|
||||
_partial_refresh_count = 0;
|
||||
last_display_crc_value = 0;
|
||||
// we can hibernate as screen has been cleaned
|
||||
display.hibernate();
|
||||
}
|
||||
|
||||
@@ -101,15 +122,18 @@ void GxEPDDisplay::startFrame(ColorVal bkg) {
|
||||
display.fillScreen(bkg);
|
||||
display.setTextColor(_curr_color = UIColor::primary_txt);
|
||||
display_crc.reset();
|
||||
if (_cycles_before_full_refresh != 0) {
|
||||
display.setPartialWindow(0, 0, display.width(), display.height());
|
||||
} else {
|
||||
if (_force_full_refresh
|
||||
|| (_max_partial_refresh > 0
|
||||
&& _partial_refresh_count >= _max_partial_refresh)) {
|
||||
// forces a full wipe of the screen ...
|
||||
display.clearScreen(0xFF); // Clears microcontroller side RAM
|
||||
display.writeScreenBuffer(0xFF); // Forces 0xFF (White) into the display controller's history registers
|
||||
// we'll need a partial refresh after that (whatever crc value is)
|
||||
last_display_crc_value = 0;
|
||||
resetPartialRefreshCounter();
|
||||
_partial_refresh_count = 0;
|
||||
_force_full_refresh = false;
|
||||
} else {
|
||||
display.setPartialWindow(0, 0, display.width(), display.height());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,9 +240,7 @@ void GxEPDDisplay::endFrame() {
|
||||
uint32_t crc = display_crc.finalize();
|
||||
if (crc != last_display_crc_value) {
|
||||
display.display(true);
|
||||
if (_cycles_before_full_refresh > 0) {
|
||||
_cycles_before_full_refresh--;
|
||||
}
|
||||
_partial_refresh_count++;
|
||||
}
|
||||
last_display_crc_value = crc;
|
||||
}
|
||||
@@ -16,12 +16,6 @@
|
||||
|
||||
#include "DisplayDriver.h"
|
||||
|
||||
#ifndef EINK_MAX_PARTIAL_REFRESH
|
||||
// 60 to prevent ghosting when refreshing every minute ...
|
||||
// set to -1 to disable the counter
|
||||
#define EINK_MAX_PARTIAL_REFRESH -1
|
||||
#endif
|
||||
|
||||
class GxEPDDisplay : public DisplayDriver {
|
||||
|
||||
#if defined(EINK_DISPLAY_MODEL)
|
||||
@@ -42,8 +36,9 @@ class GxEPDDisplay : public DisplayDriver {
|
||||
uint16_t _curr_color;
|
||||
CRC32 display_crc;
|
||||
int last_display_crc_value = 0;
|
||||
int _cycles_before_full_refresh;
|
||||
int max_partial_refresh = EINK_MAX_PARTIAL_REFRESH; // so this can be changed by an option after
|
||||
bool _force_full_refresh = false;
|
||||
int _partial_refresh_count = 0;
|
||||
int _max_partial_refresh = 0;
|
||||
|
||||
public:
|
||||
#if defined(EINK_DISPLAY_MODEL)
|
||||
@@ -56,10 +51,13 @@ public:
|
||||
|
||||
bool isOn() override { return _isOn; }
|
||||
bool isEink() override { return true; }
|
||||
void forceFullRefresh() override { _cycles_before_full_refresh = 0; };
|
||||
void resetPartialRefreshCounter() { _cycles_before_full_refresh = max_partial_refresh; };
|
||||
void forceFullRefresh() override { _force_full_refresh = true; }
|
||||
void turnOn(int max_partial) override;
|
||||
void turnOn() override;
|
||||
void turnOff() override;
|
||||
void turnOff(bool blank) override;
|
||||
void turnOff() override { turnOff(false); }
|
||||
virtual void blankScreen();
|
||||
virtual void setMaxPartialRefresh(int count) {_max_partial_refresh = count; }
|
||||
void clear() override;
|
||||
void startFrame(ColorVal bkg = UIColor::window_bkg) override;
|
||||
void setTextSize(int sz) override;
|
||||
@@ -69,6 +67,7 @@ public:
|
||||
void fillRect(int x, int y, int w, int h) override;
|
||||
void drawRect(int x, int y, int w, int h) override;
|
||||
void drawXbm(int x, int y, const uint8_t* bits, int w, int h) override;
|
||||
void drawSleepIcon();
|
||||
uint16_t getTextWidth(const char* str) override;
|
||||
void endFrame() override;
|
||||
};
|
||||
@@ -10,15 +10,15 @@ bool SH1106Display::i2c_probe(TwoWire &wire, uint8_t addr)
|
||||
}
|
||||
|
||||
// Color scheme
|
||||
ColorVal UIColor::window_bkg = SH110X_BLACK;
|
||||
ColorVal UIColor::title_bkg = SH110X_BLACK;
|
||||
ColorVal UIColor::title_txt = SH110X_WHITE;
|
||||
ColorVal UIColor::primary_txt = SH110X_WHITE;
|
||||
ColorVal UIColor::secondary_txt = SH110X_WHITE;
|
||||
ColorVal UIColor::warning_txt = SH110X_WHITE;
|
||||
ColorVal UIColor::popup_bkg = SH110X_BLACK;
|
||||
ColorVal UIColor::popup_txt = SH110X_WHITE;
|
||||
ColorVal UIColor::corp_blue = SH110X_WHITE;
|
||||
__attribute__((weak)) ColorVal UIColor::window_bkg = SH110X_BLACK;
|
||||
__attribute__((weak)) ColorVal UIColor::title_bkg = SH110X_BLACK;
|
||||
__attribute__((weak)) ColorVal UIColor::title_txt = SH110X_WHITE;
|
||||
__attribute__((weak)) ColorVal UIColor::primary_txt = SH110X_WHITE;
|
||||
__attribute__((weak)) ColorVal UIColor::secondary_txt = SH110X_WHITE;
|
||||
__attribute__((weak)) ColorVal UIColor::warning_txt = SH110X_WHITE;
|
||||
__attribute__((weak)) ColorVal UIColor::popup_bkg = SH110X_BLACK;
|
||||
__attribute__((weak)) ColorVal UIColor::popup_txt = SH110X_WHITE;
|
||||
__attribute__((weak)) ColorVal UIColor::corp_blue = SH110X_WHITE;
|
||||
|
||||
bool SH1106Display::begin()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user