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 ModeDemo.cpp Source File

ModeDemo.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 
00019 #include "ModeDemo.h"
00020 #include "MTSLog.h"
00021 
00022 // 10 s, 30 s, 1 min, 5 min, 10 min, 15 min, 30 min 1 hour
00023 const uint32_t ModeDemo::_intervals[] = { 10, 30, 60, 5 * 60, 10 * 60, 15 * 60, 30 * 60 };
00024 
00025 ModeDemo::ModeDemo(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, GPSPARSER* gps, SensorHandler* sensors)
00026   : Mode(lcd, buttons, dot, lora, gps, sensors),
00027     _help(lcd),
00028     _sam(lcd),
00029     _interval(0)
00030 {}
00031 
00032 ModeDemo::~ModeDemo() {}
00033 
00034 bool ModeDemo::start() {
00035     bool send = false;
00036     bool no_channel = false;
00037 
00038     // clear any stale signals
00039     osSignalClear(_main_id, buttonSignal | loraSignal);
00040 
00041     _initial_data_rate = _dot->getTxDataRate();
00042 
00043     // use configured data rate and power if possible
00044     if ((_band == mDot::FB_US915 || _band == mDot::FB_AU915) && (_initial_data_rate == mDot::DR0 || _initial_data_rate ==  mDot::SF_10)) {
00045         if(_initial_data_rate == mDot::DR0){
00046             logInfo("using DR1 instead of DR0 - DR0 max packet size is too small for data");
00047             _dot->setTxDataRate(mDot::DR1);
00048         }else{
00049             logInfo("using SF_9 instead of SF_10 - SF_10 max packet size is too small for data");
00050             _dot->setTxDataRate(mDot::SF_9);
00051         }
00052     }
00053 
00054     _state = show_help;
00055     displayHelp();
00056 
00057     while (true) {
00058         osEvent e = Thread::signal_wait(0, 250);
00059         if (e.status == osEventSignal) {
00060             if (e.value.signals & buttonSignal) {
00061                 _be = _buttons->getButtonEvent();
00062 
00063                 switch (_be) {
00064                     case ButtonHandler::sw1_press:
00065                         switch (_state) {
00066                             case show_help:
00067                                 _state = sampling;
00068                                 _mode = trigger;
00069                                 _sam.display();
00070                                 _sam.updateSw1("    Send");
00071                                 _sam.updateSw2("Back");
00072                                 break;
00073                             case sampling:
00074                                 if (_mode == trigger) {
00075                                     if (_dot->getNextTxMs() > 0)
00076                                         no_channel = true;
00077                                     else
00078                                         send = true;
00079                                 } else {
00080                                     _interval = (_interval + 1) % (sizeof(_intervals) / sizeof(uint32_t));
00081                                     _sam.updateInterval(_intervals[_interval]);
00082                                 }
00083                                 break;
00084                         }
00085                         break;
00086 
00087                     case ButtonHandler::sw2_press:
00088                         switch (_state) {
00089                             case show_help:
00090                                 _state = sampling;
00091                                 _mode = interval;
00092                                 _send_timer.start();
00093                                 _sam.display();
00094                                 _sam.updateSw1("Interval");
00095                                 _sam.updateSw2("Back");
00096                                 _sam.updateInterval(_intervals[_interval]);
00097                                 break;
00098                             case sampling:
00099                                 no_channel = false;
00100                                 send = false;
00101                                 _send_timer.stop();
00102                                 _send_timer.reset();
00103                                 _state = show_help;
00104                                 displayHelp();
00105                                 break;
00106                         }
00107                         break;
00108                     case ButtonHandler::sw1_hold:
00109                         _send_timer.stop();
00110                         _send_timer.reset();
00111                         if (_band == mDot::FB_US915 || _band == mDot::FB_AU915) {
00112                             _dot->setTxDataRate(_initial_data_rate);
00113                         }
00114                         return true;
00115                 }
00116             }
00117             if (e.value.signals & loraSignal) {
00118                 _ls = _lora->getStatus();
00119                 switch (_ls) {
00120                     case LoRaHandler::send_success:
00121                         switch (_state) {
00122                             case sampling:
00123                                 if (_mode == trigger) {
00124                                     _sam.updateSw1("    Send");
00125                                     _sam.updateInfo("                 ");
00126                                 } else {
00127                                     _sam.updateSw1("Interval");
00128                                     _sam.updateInterval(_intervals[_interval]);
00129                                 }
00130                                 _sam.updateSw2("Back");
00131                                 break;
00132                         }
00133                         break;
00134 
00135                     case LoRaHandler::send_failure:
00136                         switch (_state) {
00137                             case sampling:
00138                                 if (_mode == trigger) {
00139                                     _sam.updateSw1("    Send");
00140                                     _sam.updateInfo("                 ");
00141                                 } else {
00142                                     _sam.updateSw1("Interval");
00143                                     _sam.updateInterval(_intervals[_interval]);
00144                                 }
00145                                 _sam.updateSw2("Back");
00146                                 break;
00147                         }
00148                         break;
00149                 }
00150             }
00151         }
00152 
00153         if (_send_timer.read_ms() > _intervals[_interval] * 1000) {
00154             _send_timer.reset();
00155             if (_dot->getNextTxMs() > 0)
00156                 no_channel = true;
00157             else
00158                 send = true;
00159         }
00160         if (no_channel) {
00161             uint32_t t = _dot->getNextTxMs();
00162             if (t > 0) {
00163                 logInfo("next tx %lu ms", t);
00164                 _sam.updateCountdown(t / 1000);
00165             } else {
00166                 no_channel = false;
00167                 send = true;
00168             }
00169         }
00170         if (send) {
00171             std::vector<uint8_t> s_data = formatSensorData(_data);
00172             logInfo("sending data %s %d", _dot->DataRateStr(_dot->getTxDataRate()).c_str(), _dot->getTxPower());
00173             _sam.updateInfo("Sending...");
00174             _sam.updateSw1("        ");
00175             _sam.updateSw2("        ");
00176             send = false;
00177             // we don't care if the server actually gets this packet or not
00178             // we won't retry anyway
00179             _dot->setAck(0);
00180             _dot->setTxWait(false);
00181             _lora->send(s_data);
00182             osDelay(500);
00183         }
00184         if(_state != show_help){
00185             updateSensorData(_data);
00186             _sam.updateAccelerationX(_data.accel_data._x);
00187             _sam.updateAccelerationY(_data.accel_data._y);
00188             _sam.updateAccelerationZ(_data.accel_data._z);
00189             _sam.updatePressure(_data.pressure);
00190             _sam.updateAltitude(_data.altitude);
00191             _sam.updateTemperature(_data.temperature);
00192             _sam.updateLight(_data.light);        
00193         }
00194     }
00195 }
00196 
00197 void ModeDemo::displayHelp() {
00198     _help.display();
00199     _help.updateMode("LoRa Demo");
00200     _help.updateDescription("Select TX Method");
00201     _help.updateSw1(" Trigger");
00202     _help.updateSw2("Interval");
00203 }
00204