MTDOT-BOX-EVB-Factory-Firmware
Dependencies: NCP5623B GpsParser ISL29011 libmDot-mbed5 MTS-Serial MMA845x DOGS102 MPL3115A2
Mode/ModeDemo.cpp@16:e76cec0eec43, 2019-03-14 (annotated)
- Committer:
- jenkins@jenkinsdm1
- Date:
- Thu Mar 14 08:23:20 2019 -0500
- Revision:
- 16:e76cec0eec43
- Parent:
- 12:05435282f899
dotbox-firmware revision 3.1.2
Who changed what in which revision?
User | Revision | Line number | New 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 | // 10 s, 30 s, 1 min, 5 min, 10 min, 15 min, 30 min 1 hour |
Mike Fiore |
1:71125aa00e33 | 22 | const uint32_t ModeDemo::_intervals[] = { 10, 30, 60, 5 * 60, 10 * 60, 15 * 60, 30 * 60 }; |
Mike Fiore |
1:71125aa00e33 | 23 | |
Mike Fiore |
1:71125aa00e33 | 24 | ModeDemo::ModeDemo(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, GPSPARSER* gps, SensorHandler* sensors) |
Mike Fiore |
1:71125aa00e33 | 25 | : Mode(lcd, buttons, dot, lora, gps, sensors), |
Mike Fiore |
1:71125aa00e33 | 26 | _help(lcd), |
Mike Fiore |
1:71125aa00e33 | 27 | _sam(lcd), |
Mike Fiore |
1:71125aa00e33 | 28 | _interval(0) |
Mike Fiore |
1:71125aa00e33 | 29 | {} |
Mike Fiore |
1:71125aa00e33 | 30 | |
Mike Fiore |
1:71125aa00e33 | 31 | ModeDemo::~ModeDemo() {} |
Mike Fiore |
1:71125aa00e33 | 32 | |
Mike Fiore |
1:71125aa00e33 | 33 | bool ModeDemo::start() { |
Mike Fiore |
1:71125aa00e33 | 34 | bool send = false; |
Mike Fiore |
1:71125aa00e33 | 35 | bool no_channel = false; |
Mike Fiore |
1:71125aa00e33 | 36 | |
Mike Fiore |
1:71125aa00e33 | 37 | // clear any stale signals |
Mike Fiore |
1:71125aa00e33 | 38 | osSignalClear(_main_id, buttonSignal | loraSignal); |
Mike Fiore |
1:71125aa00e33 | 39 | |
Mike Fiore |
1:71125aa00e33 | 40 | _initial_data_rate = _dot->getTxDataRate(); |
jenkins@jenkinsdm1 | 12:05435282f899 | 41 | std::vector<uint8_t> s_data = formatSensorData(_data); |
jenkins@jenkinsdm1 | 12:05435282f899 | 42 | for(int i = 0; i < _dot->getMaxDatarate(); i++) { |
jenkins@jenkinsdm1 | 12:05435282f899 | 43 | _dot->setTxDataRate(i); |
jenkins@jenkinsdm1 | 12:05435282f899 | 44 | if(s_data.size() < _dot->getMaxPacketLength()) { |
jenkins@jenkinsdm1 | 12:05435282f899 | 45 | i = _dot->getMaxDatarate(); |
jenkins@jenkinsdm1 | 12:05435282f899 | 46 | } |
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(); |
jenkins@jenkinsdm1 | 12:05435282f899 | 106 | if (lora::ChannelPlan::IsPlanFixed(_band)) { |
Mike Fiore |
1:71125aa00e33 | 107 | _dot->setTxDataRate(_initial_data_rate); |
Mike Fiore |
7:a31236c2e75c | 108 | } |
Mike Fiore |
1:71125aa00e33 | 109 | return true; |
Mike Fiore |
1:71125aa00e33 | 110 | } |
Mike Fiore |
1:71125aa00e33 | 111 | } |
Mike Fiore |
1:71125aa00e33 | 112 | if (e.value.signals & loraSignal) { |
Mike Fiore |
1:71125aa00e33 | 113 | _ls = _lora->getStatus(); |
Mike Fiore |
1:71125aa00e33 | 114 | switch (_ls) { |
Mike Fiore |
1:71125aa00e33 | 115 | case LoRaHandler::send_success: |
Mike Fiore |
1:71125aa00e33 | 116 | switch (_state) { |
Mike Fiore |
1:71125aa00e33 | 117 | case sampling: |
Mike Fiore |
1:71125aa00e33 | 118 | if (_mode == trigger) { |
Mike Fiore |
1:71125aa00e33 | 119 | _sam.updateSw1(" Send"); |
Mike Fiore |
1:71125aa00e33 | 120 | _sam.updateInfo(" "); |
Mike Fiore |
1:71125aa00e33 | 121 | } else { |
Mike Fiore |
1:71125aa00e33 | 122 | _sam.updateSw1("Interval"); |
Mike Fiore |
1:71125aa00e33 | 123 | _sam.updateInterval(_intervals[_interval]); |
Mike Fiore |
1:71125aa00e33 | 124 | } |
Mike Fiore |
1:71125aa00e33 | 125 | _sam.updateSw2("Back"); |
Mike Fiore |
1:71125aa00e33 | 126 | break; |
Mike Fiore |
1:71125aa00e33 | 127 | } |
Mike Fiore |
1:71125aa00e33 | 128 | break; |
Mike Fiore |
1:71125aa00e33 | 129 | |
Mike Fiore |
1:71125aa00e33 | 130 | case LoRaHandler::send_failure: |
Mike Fiore |
1:71125aa00e33 | 131 | switch (_state) { |
Mike Fiore |
1:71125aa00e33 | 132 | case sampling: |
Mike Fiore |
1:71125aa00e33 | 133 | if (_mode == trigger) { |
Mike Fiore |
1:71125aa00e33 | 134 | _sam.updateSw1(" Send"); |
Mike Fiore |
1:71125aa00e33 | 135 | _sam.updateInfo(" "); |
Mike Fiore |
1:71125aa00e33 | 136 | } else { |
Mike Fiore |
1:71125aa00e33 | 137 | _sam.updateSw1("Interval"); |
Mike Fiore |
1:71125aa00e33 | 138 | _sam.updateInterval(_intervals[_interval]); |
Mike Fiore |
1:71125aa00e33 | 139 | } |
Mike Fiore |
1:71125aa00e33 | 140 | _sam.updateSw2("Back"); |
Mike Fiore |
1:71125aa00e33 | 141 | break; |
Mike Fiore |
1:71125aa00e33 | 142 | } |
Mike Fiore |
1:71125aa00e33 | 143 | break; |
Mike Fiore |
1:71125aa00e33 | 144 | } |
Mike Fiore |
1:71125aa00e33 | 145 | } |
Mike Fiore |
1:71125aa00e33 | 146 | } |
Mike Fiore |
1:71125aa00e33 | 147 | |
Mike Fiore |
1:71125aa00e33 | 148 | if (_send_timer.read_ms() > _intervals[_interval] * 1000) { |
Mike Fiore |
1:71125aa00e33 | 149 | _send_timer.reset(); |
Mike Fiore |
1:71125aa00e33 | 150 | if (_dot->getNextTxMs() > 0) |
Mike Fiore |
1:71125aa00e33 | 151 | no_channel = true; |
Mike Fiore |
1:71125aa00e33 | 152 | else |
Mike Fiore |
1:71125aa00e33 | 153 | send = true; |
Mike Fiore |
1:71125aa00e33 | 154 | } |
Mike Fiore |
1:71125aa00e33 | 155 | if (no_channel) { |
Mike Fiore |
1:71125aa00e33 | 156 | uint32_t t = _dot->getNextTxMs(); |
Mike Fiore |
1:71125aa00e33 | 157 | if (t > 0) { |
Mike Fiore |
1:71125aa00e33 | 158 | logInfo("next tx %lu ms", t); |
Mike Fiore |
1:71125aa00e33 | 159 | _sam.updateCountdown(t / 1000); |
Mike Fiore |
1:71125aa00e33 | 160 | } else { |
Mike Fiore |
1:71125aa00e33 | 161 | no_channel = false; |
Mike Fiore |
1:71125aa00e33 | 162 | send = true; |
Mike Fiore |
1:71125aa00e33 | 163 | } |
Mike Fiore |
1:71125aa00e33 | 164 | } |
Mike Fiore |
1:71125aa00e33 | 165 | if (send) { |
jenkins@jenkinsdm1 | 12:05435282f899 | 166 | s_data.clear(); |
jenkins@jenkinsdm1 | 12:05435282f899 | 167 | s_data = formatSensorData(_data); |
Mike Fiore |
1:71125aa00e33 | 168 | logInfo("sending data %s %d", _dot->DataRateStr(_dot->getTxDataRate()).c_str(), _dot->getTxPower()); |
Mike Fiore |
1:71125aa00e33 | 169 | _sam.updateInfo("Sending..."); |
Mike Fiore |
1:71125aa00e33 | 170 | _sam.updateSw1(" "); |
Mike Fiore |
1:71125aa00e33 | 171 | _sam.updateSw2(" "); |
Mike Fiore |
1:71125aa00e33 | 172 | send = false; |
Mike Fiore |
1:71125aa00e33 | 173 | // we don't care if the server actually gets this packet or not |
Mike Fiore |
1:71125aa00e33 | 174 | // we won't retry anyway |
Mike Fiore |
1:71125aa00e33 | 175 | _dot->setAck(0); |
Mike Fiore |
1:71125aa00e33 | 176 | _dot->setTxWait(false); |
Mike Fiore |
1:71125aa00e33 | 177 | _lora->send(s_data); |
Mike Fiore |
1:71125aa00e33 | 178 | osDelay(500); |
Mike Fiore |
1:71125aa00e33 | 179 | } |
Mike Fiore |
1:71125aa00e33 | 180 | if(_state != show_help){ |
Mike Fiore |
1:71125aa00e33 | 181 | updateSensorData(_data); |
Mike Fiore |
1:71125aa00e33 | 182 | _sam.updateAccelerationX(_data.accel_data._x); |
Mike Fiore |
1:71125aa00e33 | 183 | _sam.updateAccelerationY(_data.accel_data._y); |
Mike Fiore |
1:71125aa00e33 | 184 | _sam.updateAccelerationZ(_data.accel_data._z); |
Mike Fiore |
1:71125aa00e33 | 185 | _sam.updatePressure(_data.pressure); |
Mike Fiore |
1:71125aa00e33 | 186 | _sam.updateAltitude(_data.altitude); |
Mike Fiore |
1:71125aa00e33 | 187 | _sam.updateTemperature(_data.temperature); |
Mike Fiore |
1:71125aa00e33 | 188 | _sam.updateLight(_data.light); |
Mike Fiore |
1:71125aa00e33 | 189 | } |
Mike Fiore |
1:71125aa00e33 | 190 | } |
Mike Fiore |
1:71125aa00e33 | 191 | } |
Mike Fiore |
1:71125aa00e33 | 192 | |
Mike Fiore |
1:71125aa00e33 | 193 | void ModeDemo::displayHelp() { |
Mike Fiore |
1:71125aa00e33 | 194 | _help.display(); |
Mike Fiore |
1:71125aa00e33 | 195 | _help.updateMode("LoRa Demo"); |
Mike Fiore |
1:71125aa00e33 | 196 | _help.updateDescription("Select TX Method"); |
Mike Fiore |
1:71125aa00e33 | 197 | _help.updateSw1(" Trigger"); |
Mike Fiore |
1:71125aa00e33 | 198 | _help.updateSw2("Interval"); |
Mike Fiore |
1:71125aa00e33 | 199 | } |
Mike Fiore |
1:71125aa00e33 | 200 |