Scott Hoppe / Mbed OS 4_Ecolab_RSSI_Checker

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

Fork of MTDOT-BOX-EVB-Factory-Firmware by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ModeSingle.cpp Source File

ModeSingle.cpp

00001 /* Copyright (c) <2016> <MultiTech Systems>, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 //THIS CUSTOM INSTALLER ASSISTANT, WORKS WITH mbed-os 5.1.2 and mdot lib 5.1.5
00019 #include "ModeSingle.h"
00020 #include "MTSLog.h"
00021 
00022 #include "font_6x8.h"
00023 #include <string>
00024 #include <sstream>
00025 ModeSingle::ModeSingle(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, GPSPARSER* gps, SensorHandler* sensors)
00026   : Mode(lcd, buttons, dot, lora, gps, sensors),
00027     _help(lcd),
00028     _file(lcd),
00029     _confirm(lcd),
00030     _progress(lcd),
00031     _success(lcd),
00032     _failure(lcd)
00033 {}
00034 ModeSingle::~ModeSingle() {}
00035 
00036 bool ModeSingle::start() {
00037 
00038     
00039     bool send_link_check = false;
00040     bool send_data = false;
00041     bool no_channel_link_check = false;
00042     bool no_channel_data = false;
00043 
00044     // clear any stale signals
00045     osSignalClear(_main_id, buttonSignal | loraSignal);
00046     
00047     _initial_data_rate = _dot->getTxDataRate();
00048     _initial_power = _dot->getTxPower();
00049 
00050     // see if we're supposed to send the data packet after success
00051     // that item is stored in the mDot::StartUpMode config field
00052    // _send_data = _dot->getStartUpMode();
00053 
00054 
00055 //           Stating Start up Values for the Power, DataRate and State
00056 //________________________________________________________________________________________________________
00057 _power = 20;                //Hard setting the power, user dose not need to change it so we state it here
00058 _data_rate = mDot::DR0;     //setting the data rate, going to be increasead by 1 increments
00059 _state = 4;                 //setting the state so it goes right into success behavior off the start
00060 
00061 
00062 //Initalizing Variables for converting the RSSI int value into a string
00063 //_____________________________________________________________________
00064  string rssiStringHolder;
00065  int rssiIntHolder;
00066  ostringstream rssiConverter;
00067  Label rssiLabel(0,3,"RSSI");
00068  
00069  string dataRateStringHolder;
00070  int dataRateIntHolder;
00071  ostringstream dataRateConverter;
00072  Label dataRateLabel(0,6,"Data");
00073  
00074  string marginStringHolder;
00075  int marginIntHolder;
00076  ostringstream marginConverter;
00077  Label marginLabel(0,2,"Margin1");
00078 
00079 //          Initial Settings for the LCD Display
00080 //________________________________________________________________________________________________________
00081 _lcd->startUpdate();    
00082 _lcd->clearBuffer();        //Clears the LCD
00083 _lcd->writeText(0*6, 0, font_6x8, "    Installer    ", 17);
00084 _lcd->writeText(0*6, 1, font_6x8, "    Assistant    ", 17);
00085 _lcd->writeText(0*6, 5, font_6x8, "Power=20         ", 17);
00086 
00087 dataRateIntHolder = _data_rate;
00088 dataRateConverter.str("");
00089 dataRateConverter.clear();
00090 dataRateConverter << dataRateIntHolder;
00091 dataRateStringHolder = dataRateConverter.str();
00092 dataRateLabel._value = "DataRate=" + dataRateStringHolder;
00093 _success.writeLabel(dataRateLabel);
00094 //_success.updateInfo(formatRatePower()); //This Displays the updated DataRate value, it changes so we cant just display the number itsself
00095 
00096 _lcd->endUpdate();
00097 _success.updateSw1("DataRate");
00098 _success.updateSw2("Survey");
00099 
00100     while (true) {
00101         osEvent e = Thread::signal_wait(0, 250);
00102         if (e.status == osEventSignal) {
00103             if (e.value.signals & buttonSignal) {
00104                 _be = _buttons->getButtonEvent();
00105 
00106                 switch (_be) {
00107                     case ButtonHandler::sw1_press:
00108                         switch (_state) {
00109                             case success:
00110                                 incrementRatePower();
00111                                 
00112                                 logInfo("\n\r\n\r I am within the DataRate button press! \n\r");
00113                                 
00114                                 dataRateIntHolder = _data_rate;
00115                                 dataRateConverter.str("");
00116                                 dataRateConverter.clear();
00117                                 dataRateConverter << dataRateIntHolder;
00118                                 dataRateStringHolder = dataRateConverter.str();
00119                                 
00120                                 dataRateLabel._value = "DataRate=" + dataRateStringHolder;
00121                                 
00122                                 
00123                                 _lcd->startUpdate();  
00124                                 _success.writeLabel(dataRateLabel);
00125                                 _lcd->endUpdate();
00126 
00127                 
00128                                 break;
00129                             case failure:
00130                                 incrementRatePower();
00131                                 
00132                                 dataRateIntHolder = _data_rate;
00133                                 dataRateConverter.str("");
00134                                 dataRateConverter.clear();
00135                                 dataRateConverter << dataRateIntHolder;
00136                                 dataRateStringHolder = dataRateConverter.str();
00137                                 
00138                                 dataRateLabel._value = "DataRate=" + dataRateStringHolder;
00139                                 
00140                                 
00141                                 _lcd->startUpdate();  
00142                                 _success.writeLabel(dataRateLabel);
00143                                 _lcd->endUpdate();
00144                                 break;
00145                         }
00146                         break;
00147 
00148                     case ButtonHandler::sw2_press:
00149                         switch (_state) {
00150                             case success:
00151                                 _state = in_progress;
00152                                 _progress.display(); //this is where the survey in progress is located
00153                                 if (_dot->getNextTxMs() > 0)
00154                                     no_channel_link_check = true;
00155                                 else 
00156                                     send_link_check = true;
00157                                 break;
00158                             case failure:
00159                                 _state = in_progress;
00160                                 _progress.display();
00161                                 if (_dot->getNextTxMs() > 0)
00162                                     no_channel_link_check = true;
00163                                 else 
00164                                     send_link_check = true;
00165                                 break;
00166                         }
00167                         break;
00168                 }
00169             }
00170             if (e.value.signals & loraSignal) {
00171                 _ls = _lora->getStatus();
00172                 switch (_ls) {
00173                     case LoRaHandler::link_check_success:
00174                     
00175                         switch (_state) {
00176                             case in_progress:
00177                                 _link_check_result = _lora->getLinkCheckResults();//Where the RSSI value lies
00178                                 //converting the RSSI int value to a string and then creating a new label that can be written to the LCD
00179                                 //______________________________________________________________________________
00180                                 rssiIntHolder = _link_check_result.down.rssi;
00181                                 rssiConverter.str("");
00182                                 rssiConverter.clear();
00183                                 rssiConverter << rssiIntHolder;
00184                                 rssiStringHolder = rssiConverter.str();
00185                                 rssiStringHolder = "Rssi= " + rssiStringHolder + "dBm";
00186         
00187                                 rssiLabel._value = rssiStringHolder;
00188                                 
00189                                 
00190                                 marginIntHolder = _link_check_result.up.dBm;
00191                                 marginConverter.str("");
00192                                 marginConverter.clear();
00193                                 marginConverter << marginIntHolder;
00194                                 marginStringHolder = marginConverter.str();
00195                                 marginStringHolder = "Margin= " + marginStringHolder + "dBm";
00196         
00197                                 marginLabel._value = marginStringHolder;
00198                                 
00199                                 
00200                                 dataRateIntHolder = _data_rate;
00201                                 dataRateConverter.str("");
00202                                 dataRateConverter.clear();
00203                                 dataRateConverter << dataRateIntHolder;
00204                                 dataRateStringHolder = dataRateConverter.str();
00205                                 
00206                                 dataRateLabel._value = "DataRate=" + dataRateStringHolder;
00207                                
00208                                 //writing to the LCD the RSSI value as well as the other information
00209                                 //_________________________________________________________________
00210                                 _lcd->startUpdate();
00211                                 _lcd->clearBuffer();
00212                                 _lcd->writeText(0*6, 0, font_6x8, "    Installer    ", 17);
00213                                 _lcd->writeText(0*6, 1, font_6x8, "    Assistant    ", 17);
00214                                 _lcd->writeText(0*6, 5, font_6x8, "Power=20         ", 17);
00215                                 
00216                                // _success.updateInfo(formatRatePower());
00217                                 _success.writeLabel(rssiLabel);
00218                                 _success.writeLabel(marginLabel);
00219                                 _success.writeLabel(dataRateLabel);
00220                                 _lcd->endUpdate();
00221                                 
00222                                 logInfo("link check successful\tMargin %ld\tRSSI %d dBm\tSNR %2.3f", _link_check_result.up.dBm, _link_check_result.down.rssi, (float)_link_check_result.down.snr / 10.0);
00223 
00224                                 _state = success;
00225                                 _success.updateSw1("DataRate");
00226                                 _success.updateSw2("Survey");
00227                         }
00228                         break;
00229 
00230                     case LoRaHandler::link_check_failure:
00231                         switch (_state) {
00232                             case in_progress:
00233                                 _state = failure;
00234                                 
00235                                 dataRateIntHolder = _data_rate;
00236                                 dataRateConverter.str("");
00237                                 dataRateConverter.clear();
00238                                 dataRateConverter << dataRateIntHolder;
00239                                 dataRateStringHolder = dataRateConverter.str();
00240                                 
00241                                 dataRateLabel._value = "DataRate=" + dataRateStringHolder;
00242                                 
00243                                 //writing to the LCD the RSSI value as well as the other information
00244                                 //_________________________________________________________________
00245                                 _lcd->startUpdate();                                
00246                                 _lcd->clearBuffer();
00247                                 _lcd->writeText(0*6, 0, font_6x8, "    Installer    ", 17);
00248                                 _lcd->writeText(0*6, 1, font_6x8, "    Assistant    ", 17);
00249                                 _lcd->writeText(0*6, 5, font_6x8, "Power=20         ", 17);
00250                                 _lcd->writeText(0*6, 3, font_6x8, "Unable to Connect", 17);
00251                                 _lcd->endUpdate();
00252                                // _success.updateInfo(formatRatePower());
00253                                
00254                                 _success.writeLabel(dataRateLabel);
00255                                 _failure.updateSw1("DataRate");
00256                                 _failure.updateSw2("Survey");              
00257                                 logInfo("link check failed");
00258                                 break;
00259                         }
00260                         break;
00261                 }
00262             }
00263         }
00264 
00265         if (send_link_check) {
00266             logInfo("sending link check %s %d", _dot->DataRateStr(_data_rate).c_str(), _power);
00267             send_link_check = false;
00268             _dot->setTxDataRate(_data_rate);
00269             _dot->setTxPower(_power);
00270             _lora->linkCheck();
00271             _index++;
00272         }
00273     }
00274 
00275 }
00276 
00277 
00278 
00279 //Cycles the datarates through from DR0-DR4
00280 void ModeSingle::incrementRatePower() {
00281     if(_data_rate == mDot::DR4){
00282         _data_rate = mDot::DR0;
00283         }
00284     else{
00285         _data_rate++;
00286         }
00287     logInfo("new data rate %s, power %lu", mDot::DataRateStr(_data_rate).c_str(), _power);
00288 }
00289