MTDOT-BOX-EVB-Factory-Firmware

Dependencies:   NCP5623B GpsParser ISL29011 libmDot-mbed5 MTS-Serial MMA845x DOGS102 MPL3115A2

Revision:
12:05435282f899
Parent:
7:a31236c2e75c
--- a/Mode/ModeSweep.cpp	Thu Nov 10 22:10:58 2016 +0000
+++ b/Mode/ModeSweep.cpp	Tue Oct 09 13:49:30 2018 -0500
@@ -261,7 +261,7 @@
         }
 
         // wait 5s in EU mode to compensate for potential "no free channel" situations on server
-        if ((_band != mDot::FB_EU868 && _display_timer.read_ms() > 2000) || _display_timer.read_ms() > 5000) {
+        if ((lora::ChannelPlan::IsPlanDynamic(_band) && _dot->getChannelPlanName() == "EU868" && _display_timer.read_ms() > 2000) || _display_timer.read_ms() > 5000) {
             _display_timer.stop();
             _display_timer.reset();
             if (_survey_current == _survey_total) {
@@ -361,41 +361,55 @@
     uint8_t min_rate;
     uint8_t max_rate;
 
-    max_rate = payloadToRate(_min_payload);
-    min_rate = payloadToRate(_max_payload);
+    max_rate = payloadToRate(_min_payload, false);
+    min_rate = payloadToRate(_max_payload, true);
 
-    for (int rate = min_rate; rate >= max_rate; rate--) {
+    for (int rate = max_rate; rate <= min_rate; rate++) {
         if (_max_power - _min_power < 4) {
-            for (uint32_t power = _min_power; power <= _max_power; power++)
+            for (uint32_t power = _max_power; power >= _min_power; power--)
                 p.push_back(std::make_pair(rate, power));
         } else {
-            p.push_back(std::make_pair(rate, _min_power));
+            p.push_back(std::make_pair(rate, _max_power));
+            p.push_back(std::make_pair(rate, (uint32_t)ceil( (float(_max_power) - float(_min_power)) * 0.66 + float(_min_power))));
             p.push_back(std::make_pair(rate, (uint32_t)ceil( (float(_max_power) - float(_min_power)) * 0.33 + float(_min_power))));
-            p.push_back(std::make_pair(rate, (uint32_t)ceil( (float(_max_power) - float(_min_power)) * 0.66 + float(_min_power))));
-            p.push_back(std::make_pair(rate, _max_power));
+            p.push_back(std::make_pair(rate, _min_power));
         }
     }
 
     return p;
 }
 
-uint8_t ModeSweep::payloadToRate(uint8_t payload) {
-    if (_band == mDot::FB_EU868) {
-        if (payload <= mDot::MaxLengths_868[mDot::DR0])
-            return mDot::DR0;
-        else if (payload <= mDot::MaxLengths_868[mDot::DR3])
-            return mDot::DR3;
-        else
-            return mDot::DR6;
+uint8_t ModeSweep::payloadToRate(uint8_t payload, bool max) {
+    uint8_t ret = 0;
+    uint8_t tmp = 0;
+    if(!max) {
+        for(uint8_t i = _dot->getMinDatarate(); i < _dot->getMaxDatarate(); i++) {
+            _dot->setTxDataRate(i);
+            if(payload <= _dot->getMaxPacketLength()) {
+                ret = i;
+                tmp = _dot->getMaxPacketLength();
+                i = _dot->getMaxDatarate();
+            }
+        }
     } else {
-        if (payload <= mDot::MaxLengths_915[mDot::DR0])
-            return mDot::DR0;
-        else if (payload <= mDot::MaxLengths_915[mDot::DR1])
-            return mDot::DR1;
-        else if (payload <= mDot::MaxLengths_915[mDot::DR2])
-            return mDot::DR2;
-        else
-            return mDot::DR4;
+        //This may or may not work for some channel plans
+        for(uint8_t i = _dot->getMaxDatarate(); i > _dot->getMinDatarate(); i--) {
+            _dot->setTxDataRate(i);
+            if(payload <= _dot->getMaxPacketLength()) {
+                ret = i;
+                tmp = _dot->getMaxPacketLength();
+            }
+        }
+        _dot->setTxDataRate(_dot->getMaxDatarate());
+        if(tmp == _dot->getMaxPacketLength()) {
+            for(uint8_t i = _dot->getMaxDatarate(); i > _dot->getMinDatarate(); i--) {
+                _dot->setTxDataRate(i);
+                if(_dot->getChannelPlan()->GetDatarate(i).SpreadingFactor != lora::SF_FSK) {
+                    return i;
+                }
+            }
+        }
     }
+    return ret;
 }