Custom Channel Plan version of MTDOT Box firmware

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

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