Updated to libmDot 1.0.8-1

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

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

Committer:
jreiss
Date:
Wed Jul 13 19:14:04 2016 +0000
Revision:
6:ab581c4260e7
Parent:
1:71125aa00e33
Use DR enum values instead of SF values

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mike Fiore 1:71125aa00e33 1 /* Copyright (c) <2016> <MultiTech Systems>, MIT License
Mike Fiore 1:71125aa00e33 2 *
Mike Fiore 1:71125aa00e33 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Mike Fiore 1:71125aa00e33 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Mike Fiore 1:71125aa00e33 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Mike Fiore 1:71125aa00e33 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Mike Fiore 1:71125aa00e33 7 * furnished to do so, subject to the following conditions:
Mike Fiore 1:71125aa00e33 8 *
Mike Fiore 1:71125aa00e33 9 * The above copyright notice and this permission notice shall be included in all copies or
Mike Fiore 1:71125aa00e33 10 * substantial portions of the Software.
Mike Fiore 1:71125aa00e33 11 *
Mike Fiore 1:71125aa00e33 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Mike Fiore 1:71125aa00e33 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Mike Fiore 1:71125aa00e33 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Mike Fiore 1:71125aa00e33 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Mike Fiore 1:71125aa00e33 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Mike Fiore 1:71125aa00e33 17 */
Mike Fiore 1:71125aa00e33 18
Mike Fiore 1:71125aa00e33 19 #include "ModeDemo.h"
Mike Fiore 1:71125aa00e33 20 #include "MTSLog.h"
Mike Fiore 1:71125aa00e33 21
Mike Fiore 1:71125aa00e33 22 // 10 s, 30 s, 1 min, 5 min, 10 min, 15 min, 30 min 1 hour
Mike Fiore 1:71125aa00e33 23 const uint32_t ModeDemo::_intervals[] = { 10, 30, 60, 5 * 60, 10 * 60, 15 * 60, 30 * 60 };
Mike Fiore 1:71125aa00e33 24
Mike Fiore 1:71125aa00e33 25 ModeDemo::ModeDemo(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, GPSPARSER* gps, SensorHandler* sensors)
Mike Fiore 1:71125aa00e33 26 : Mode(lcd, buttons, dot, lora, gps, sensors),
Mike Fiore 1:71125aa00e33 27 _help(lcd),
Mike Fiore 1:71125aa00e33 28 _sam(lcd),
Mike Fiore 1:71125aa00e33 29 _interval(0)
Mike Fiore 1:71125aa00e33 30 {}
Mike Fiore 1:71125aa00e33 31
Mike Fiore 1:71125aa00e33 32 ModeDemo::~ModeDemo() {}
Mike Fiore 1:71125aa00e33 33
Mike Fiore 1:71125aa00e33 34 bool ModeDemo::start() {
Mike Fiore 1:71125aa00e33 35 bool send = false;
Mike Fiore 1:71125aa00e33 36 bool no_channel = false;
Mike Fiore 1:71125aa00e33 37
Mike Fiore 1:71125aa00e33 38 // clear any stale signals
Mike Fiore 1:71125aa00e33 39 osSignalClear(_main_id, buttonSignal | loraSignal);
Mike Fiore 1:71125aa00e33 40
Mike Fiore 1:71125aa00e33 41 _initial_data_rate = _dot->getTxDataRate();
Mike Fiore 1:71125aa00e33 42
Mike Fiore 1:71125aa00e33 43 // use configured data rate and power if possible
jreiss 6:ab581c4260e7 44 if (_band == mDot::FB_915 && _initial_data_rate == mDot::DR0) {
jreiss 6:ab581c4260e7 45 logInfo("using DR1 instead of DR0 - DR0 max packet size is too small for data");
jreiss 6:ab581c4260e7 46 _dot->setTxDataRate(mDot::DR1);
Mike Fiore 1:71125aa00e33 47 }
Mike Fiore 1:71125aa00e33 48
Mike Fiore 1:71125aa00e33 49 _state = show_help;
Mike Fiore 1:71125aa00e33 50 displayHelp();
Mike Fiore 1:71125aa00e33 51
Mike Fiore 1:71125aa00e33 52 while (true) {
Mike Fiore 1:71125aa00e33 53 osEvent e = Thread::signal_wait(0, 250);
Mike Fiore 1:71125aa00e33 54 if (e.status == osEventSignal) {
Mike Fiore 1:71125aa00e33 55 if (e.value.signals & buttonSignal) {
Mike Fiore 1:71125aa00e33 56 _be = _buttons->getButtonEvent();
Mike Fiore 1:71125aa00e33 57
Mike Fiore 1:71125aa00e33 58 switch (_be) {
Mike Fiore 1:71125aa00e33 59 case ButtonHandler::sw1_press:
Mike Fiore 1:71125aa00e33 60 switch (_state) {
Mike Fiore 1:71125aa00e33 61 case show_help:
Mike Fiore 1:71125aa00e33 62 _state = sampling;
Mike Fiore 1:71125aa00e33 63 _mode = trigger;
Mike Fiore 1:71125aa00e33 64 _sam.display();
Mike Fiore 1:71125aa00e33 65 _sam.updateSw1(" Send");
Mike Fiore 1:71125aa00e33 66 _sam.updateSw2("Back");
Mike Fiore 1:71125aa00e33 67 break;
Mike Fiore 1:71125aa00e33 68 case sampling:
Mike Fiore 1:71125aa00e33 69 if (_mode == trigger) {
Mike Fiore 1:71125aa00e33 70 if (_dot->getNextTxMs() > 0)
Mike Fiore 1:71125aa00e33 71 no_channel = true;
Mike Fiore 1:71125aa00e33 72 else
Mike Fiore 1:71125aa00e33 73 send = true;
Mike Fiore 1:71125aa00e33 74 } else {
Mike Fiore 1:71125aa00e33 75 _interval = (_interval + 1) % (sizeof(_intervals) / sizeof(uint32_t));
Mike Fiore 1:71125aa00e33 76 _sam.updateInterval(_intervals[_interval]);
Mike Fiore 1:71125aa00e33 77 }
Mike Fiore 1:71125aa00e33 78 break;
Mike Fiore 1:71125aa00e33 79 }
Mike Fiore 1:71125aa00e33 80 break;
Mike Fiore 1:71125aa00e33 81
Mike Fiore 1:71125aa00e33 82 case ButtonHandler::sw2_press:
Mike Fiore 1:71125aa00e33 83 switch (_state) {
Mike Fiore 1:71125aa00e33 84 case show_help:
Mike Fiore 1:71125aa00e33 85 _state = sampling;
Mike Fiore 1:71125aa00e33 86 _mode = interval;
Mike Fiore 1:71125aa00e33 87 _send_timer.start();
Mike Fiore 1:71125aa00e33 88 _sam.display();
Mike Fiore 1:71125aa00e33 89 _sam.updateSw1("Interval");
Mike Fiore 1:71125aa00e33 90 _sam.updateSw2("Back");
Mike Fiore 1:71125aa00e33 91 _sam.updateInterval(_intervals[_interval]);
Mike Fiore 1:71125aa00e33 92 break;
Mike Fiore 1:71125aa00e33 93 case sampling:
Mike Fiore 1:71125aa00e33 94 no_channel = false;
Mike Fiore 1:71125aa00e33 95 send = false;
Mike Fiore 1:71125aa00e33 96 _send_timer.stop();
Mike Fiore 1:71125aa00e33 97 _send_timer.reset();
Mike Fiore 1:71125aa00e33 98 _state = show_help;
Mike Fiore 1:71125aa00e33 99 displayHelp();
Mike Fiore 1:71125aa00e33 100 break;
Mike Fiore 1:71125aa00e33 101 }
Mike Fiore 1:71125aa00e33 102 break;
Mike Fiore 1:71125aa00e33 103 case ButtonHandler::sw1_hold:
Mike Fiore 1:71125aa00e33 104 _send_timer.stop();
Mike Fiore 1:71125aa00e33 105 _send_timer.reset();
Mike Fiore 1:71125aa00e33 106 if (_band == mDot::FB_915)
Mike Fiore 1:71125aa00e33 107 _dot->setTxDataRate(_initial_data_rate);
Mike Fiore 1:71125aa00e33 108 return true;
Mike Fiore 1:71125aa00e33 109 }
Mike Fiore 1:71125aa00e33 110 }
Mike Fiore 1:71125aa00e33 111 if (e.value.signals & loraSignal) {
Mike Fiore 1:71125aa00e33 112 _ls = _lora->getStatus();
Mike Fiore 1:71125aa00e33 113 switch (_ls) {
Mike Fiore 1:71125aa00e33 114 case LoRaHandler::send_success:
Mike Fiore 1:71125aa00e33 115 switch (_state) {
Mike Fiore 1:71125aa00e33 116 case sampling:
Mike Fiore 1:71125aa00e33 117 if (_mode == trigger) {
Mike Fiore 1:71125aa00e33 118 _sam.updateSw1(" Send");
Mike Fiore 1:71125aa00e33 119 _sam.updateInfo(" ");
Mike Fiore 1:71125aa00e33 120 } else {
Mike Fiore 1:71125aa00e33 121 _sam.updateSw1("Interval");
Mike Fiore 1:71125aa00e33 122 _sam.updateInterval(_intervals[_interval]);
Mike Fiore 1:71125aa00e33 123 }
Mike Fiore 1:71125aa00e33 124 _sam.updateSw2("Back");
Mike Fiore 1:71125aa00e33 125 break;
Mike Fiore 1:71125aa00e33 126 }
Mike Fiore 1:71125aa00e33 127 break;
Mike Fiore 1:71125aa00e33 128
Mike Fiore 1:71125aa00e33 129 case LoRaHandler::send_failure:
Mike Fiore 1:71125aa00e33 130 switch (_state) {
Mike Fiore 1:71125aa00e33 131 case sampling:
Mike Fiore 1:71125aa00e33 132 if (_mode == trigger) {
Mike Fiore 1:71125aa00e33 133 _sam.updateSw1(" Send");
Mike Fiore 1:71125aa00e33 134 _sam.updateInfo(" ");
Mike Fiore 1:71125aa00e33 135 } else {
Mike Fiore 1:71125aa00e33 136 _sam.updateSw1("Interval");
Mike Fiore 1:71125aa00e33 137 _sam.updateInterval(_intervals[_interval]);
Mike Fiore 1:71125aa00e33 138 }
Mike Fiore 1:71125aa00e33 139 _sam.updateSw2("Back");
Mike Fiore 1:71125aa00e33 140 break;
Mike Fiore 1:71125aa00e33 141 }
Mike Fiore 1:71125aa00e33 142 break;
Mike Fiore 1:71125aa00e33 143 }
Mike Fiore 1:71125aa00e33 144 }
Mike Fiore 1:71125aa00e33 145 }
Mike Fiore 1:71125aa00e33 146
Mike Fiore 1:71125aa00e33 147 if (_send_timer.read_ms() > _intervals[_interval] * 1000) {
Mike Fiore 1:71125aa00e33 148 _send_timer.reset();
Mike Fiore 1:71125aa00e33 149 if (_dot->getNextTxMs() > 0)
Mike Fiore 1:71125aa00e33 150 no_channel = true;
Mike Fiore 1:71125aa00e33 151 else
Mike Fiore 1:71125aa00e33 152 send = true;
Mike Fiore 1:71125aa00e33 153 }
Mike Fiore 1:71125aa00e33 154 if (no_channel) {
Mike Fiore 1:71125aa00e33 155 uint32_t t = _dot->getNextTxMs();
Mike Fiore 1:71125aa00e33 156 if (t > 0) {
Mike Fiore 1:71125aa00e33 157 logInfo("next tx %lu ms", t);
Mike Fiore 1:71125aa00e33 158 _sam.updateCountdown(t / 1000);
Mike Fiore 1:71125aa00e33 159 } else {
Mike Fiore 1:71125aa00e33 160 no_channel = false;
Mike Fiore 1:71125aa00e33 161 send = true;
Mike Fiore 1:71125aa00e33 162 }
Mike Fiore 1:71125aa00e33 163 }
Mike Fiore 1:71125aa00e33 164 if (send) {
Mike Fiore 1:71125aa00e33 165 std::vector<uint8_t> s_data = formatSensorData(_data);
Mike Fiore 1:71125aa00e33 166 logInfo("sending data %s %d", _dot->DataRateStr(_dot->getTxDataRate()).c_str(), _dot->getTxPower());
Mike Fiore 1:71125aa00e33 167 _sam.updateInfo("Sending...");
Mike Fiore 1:71125aa00e33 168 _sam.updateSw1(" ");
Mike Fiore 1:71125aa00e33 169 _sam.updateSw2(" ");
Mike Fiore 1:71125aa00e33 170 send = false;
Mike Fiore 1:71125aa00e33 171 // we don't care if the server actually gets this packet or not
Mike Fiore 1:71125aa00e33 172 // we won't retry anyway
Mike Fiore 1:71125aa00e33 173 _dot->setAck(0);
Mike Fiore 1:71125aa00e33 174 _dot->setTxWait(false);
Mike Fiore 1:71125aa00e33 175 _lora->send(s_data);
Mike Fiore 1:71125aa00e33 176 osDelay(500);
Mike Fiore 1:71125aa00e33 177 }
Mike Fiore 1:71125aa00e33 178 if(_state != show_help){
Mike Fiore 1:71125aa00e33 179 updateSensorData(_data);
Mike Fiore 1:71125aa00e33 180 _sam.updateAccelerationX(_data.accel_data._x);
Mike Fiore 1:71125aa00e33 181 _sam.updateAccelerationY(_data.accel_data._y);
Mike Fiore 1:71125aa00e33 182 _sam.updateAccelerationZ(_data.accel_data._z);
Mike Fiore 1:71125aa00e33 183 _sam.updatePressure(_data.pressure);
Mike Fiore 1:71125aa00e33 184 _sam.updateAltitude(_data.altitude);
Mike Fiore 1:71125aa00e33 185 _sam.updateTemperature(_data.temperature);
Mike Fiore 1:71125aa00e33 186 _sam.updateLight(_data.light);
Mike Fiore 1:71125aa00e33 187 }
Mike Fiore 1:71125aa00e33 188 }
Mike Fiore 1:71125aa00e33 189 }
Mike Fiore 1:71125aa00e33 190
Mike Fiore 1:71125aa00e33 191 void ModeDemo::displayHelp() {
Mike Fiore 1:71125aa00e33 192 _help.display();
Mike Fiore 1:71125aa00e33 193 _help.updateMode("LoRa Demo");
Mike Fiore 1:71125aa00e33 194 _help.updateDescription("Select TX Method");
Mike Fiore 1:71125aa00e33 195 _help.updateSw1(" Trigger");
Mike Fiore 1:71125aa00e33 196 _help.updateSw2("Interval");
Mike Fiore 1:71125aa00e33 197 }
Mike Fiore 1:71125aa00e33 198