|
1 | 1 | #include "MyMesh.h" |
2 | 2 | #include <algorithm> |
| 3 | +#include <climits> |
3 | 4 |
|
4 | 5 | /* ------------------------------ Config -------------------------------- */ |
5 | 6 |
|
@@ -87,6 +88,42 @@ void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float sn |
87 | 88 | #endif |
88 | 89 | } |
89 | 90 |
|
| 91 | +int8_t MyMesh::findNeighbourSNR(const uint8_t* hash, uint8_t hash_size) { |
| 92 | +#if MAX_NEIGHBOURS |
| 93 | + for (int i = 0; i < MAX_NEIGHBOURS; i++) { |
| 94 | + if (neighbours[i].heard_timestamp != 0 && neighbours[i].id.isHashMatch(hash, hash_size)) { |
| 95 | + return neighbours[i].snr; |
| 96 | + } |
| 97 | + } |
| 98 | +#endif |
| 99 | + return INT8_MAX; |
| 100 | +} |
| 101 | + |
| 102 | +// Approximate SNR demod floor per SF (same as RadioLibWrappers.cpp) |
| 103 | +static float cr_snr_thresholds[] = { |
| 104 | + -7.5f, // SF7 |
| 105 | + -10.0f, // SF8 |
| 106 | + -12.5f, // SF9 |
| 107 | + -15.0f, // SF10 |
| 108 | + -17.5f, // SF11 |
| 109 | + -20.0f // SF12 |
| 110 | +}; |
| 111 | + |
| 112 | +uint8_t MyMesh::selectCodingRateForPeer(const uint8_t* hash, uint8_t hash_size) { |
| 113 | + int8_t snr4 = findNeighbourSNR(hash, hash_size); |
| 114 | + if (snr4 == INT8_MAX) return 0; // unknown neighbor, use default |
| 115 | + |
| 116 | + float snr = snr4 / 4.0f; |
| 117 | + float threshold = (_prefs.sf >= 7 && _prefs.sf <= 12) |
| 118 | + ? cr_snr_thresholds[_prefs.sf - 7] : -15.0f; |
| 119 | + float margin = snr - threshold; |
| 120 | + |
| 121 | + if (margin < 3.0f) return 8; |
| 122 | + if (margin < 6.0f) return 7; |
| 123 | + if (margin < 10.0f) return 6; |
| 124 | + return 5; // good margin, use lightest CR |
| 125 | +} |
| 126 | + |
90 | 127 | uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood) { |
91 | 128 | ClientInfo* client = NULL; |
92 | 129 | if (data[0] == 0) { // blank password, just check if sender is in ACL |
@@ -961,6 +998,7 @@ void MyMesh::begin(FILESYSTEM *fs) { |
961 | 998 |
|
962 | 999 | radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); |
963 | 1000 | radio_driver.setTxPower(_prefs.tx_power_dbm); |
| 1001 | + setDefaultCR(_prefs.cr); |
964 | 1002 |
|
965 | 1003 | radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain); |
966 | 1004 | MESH_DEBUG_PRINTLN("RX Boosted Gain Mode: %s", |
|
0 commit comments