diff options
author | Mike Fiore <mfiore@multitech.com> | 2015-12-04 09:53:21 -0600 |
---|---|---|
committer | Mike Fiore <mfiore@multitech.com> | 2015-12-04 09:53:21 -0600 |
commit | 41a5c0de1343c4a4163c7096ea33bbfd6c3e84cb (patch) | |
tree | aa964ee70557217d409a2fb44546785a543aaeb0 /Mode | |
parent | c52f29331e76857b4bbab72829479a04e19318cf (diff) | |
download | mtdot-box-evb-factory-firmware-41a5c0de1343c4a4163c7096ea33bbfd6c3e84cb.tar.gz mtdot-box-evb-factory-firmware-41a5c0de1343c4a4163c7096ea33bbfd6c3e84cb.tar.bz2 mtdot-box-evb-factory-firmware-41a5c0de1343c4a4163c7096ea33bbfd6c3e84cb.zip |
implement demo mode logic - still need to add sensor data
Diffstat (limited to 'Mode')
-rw-r--r-- | Mode/ModeDemo.cpp | 172 | ||||
-rw-r--r-- | Mode/ModeDemo.h | 39 |
2 files changed, 211 insertions, 0 deletions
diff --git a/Mode/ModeDemo.cpp b/Mode/ModeDemo.cpp new file mode 100644 index 0000000..80d202c --- /dev/null +++ b/Mode/ModeDemo.cpp @@ -0,0 +1,172 @@ +#include "ModeDemo.h" +#include "MTSLog.h" + +// 10 s, 30 s, 1 min, 5 min, 10 min, 15 min, 30 min 1 hour +const uint32_t ModeDemo::_intervals[] = { 10, 30, 60, 5 * 60, 10 * 60, 15 * 60, 30 * 60, 60 * 60 }; + +ModeDemo::ModeDemo(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, GPSPARSER* gps) + : Mode(lcd, buttons, dot, lora, gps), + _help(lcd), + _sam(lcd), + _interval(0) +{} + +ModeDemo::~ModeDemo() {} + +bool ModeDemo::start() { + bool send = false; + bool no_channel = false; + + // clear any stale signals + osSignalClear(_main_id, buttonSignal | loraSignal); + + _state = show_help; + displayHelp(); + + while (true) { + osEvent e = Thread::signal_wait(0, 250); + if (e.status == osEventSignal) { + if (e.value.signals & buttonSignal) { + _be = _buttons->getButtonEvent(); + + switch (_be) { + case ButtonHandler::sw1_press: + switch (_state) { + case show_help: + _state = sampling; + _mode = trigger; + _sam.display(); + _sam.updateSw2("Send"); + break; + case sampling: + if (_mode == interval) { + _interval = (_interval + 1) % (sizeof(_intervals) / sizeof(uint32_t)); + _sam.updateInterval(_intervals[_interval]); + } + break; + case success: + break; + case failure: + break; + } + break; + + case ButtonHandler::sw2_press: + switch (_state) { + case show_help: + _state = sampling; + _mode = interval; + _send_timer.start(); + _sam.display(); + _sam.updateSw1("Interval"); + _sam.updateInterval(_intervals[_interval]); + break; + case sampling: + if (_mode == trigger) { + if (_dot->getNextTxMs() > 0) + no_channel = true; + else + send = true; + } + break; + case success: + break; + case failure: + break; + } + break; + case ButtonHandler::sw1_hold: + _send_timer.stop(); + _send_timer.reset(); + return true; + } + } + if (e.value.signals & loraSignal) { + _ls = _lora->getStatus(); + switch (_ls) { + case LoRaHandler::send_success: + switch (_state) { + case show_help: + break; + case sampling: + if (_mode == trigger) { + _sam.updateSw2("Send"); + } else { + _sam.updateSw1("Interval"); + _sam.updateInterval(_intervals[_interval]); + } + break; + case success: + break; + case failure: + break; + } + break; + + case LoRaHandler::send_failure: + switch (_state) { + case show_help: + break; + case sampling: + if (_mode == trigger) { + _sam.updateSw2("Send"); + } else { + _sam.updateSw1("Interval"); + _sam.updateInterval(_intervals[_interval]); + } + break; + case success: + break; + case failure: + break; + } + break; + } + } + } + + if (_send_timer.read_ms() > _intervals[_interval] * 1000) { + _send_timer.reset(); + if (_dot->getNextTxMs() > 0) + no_channel = true; + else + send = true; + } + if (no_channel) { + uint32_t t = _dot->getNextTxMs(); + if (t > 0) { + logInfo("next tx %lu ms", t); + _sam.updateCountdown(t / 1000); + } else { + no_channel = false; + send = true; + } + } + if (send) { + std::vector<uint8_t> s_data = formatSensorData(_data); + logInfo("sending data %s %d", _dot->DataRateStr(_data_rate).c_str(), _power); + _sam.updateInfo("Sending..."); + _sam.updateSw1(" "); + _sam.updateSw2(" "); + send = false; + _dot->setTxDataRate(_data_rate); + _dot->setTxPower(_power); + // we don't care if the server actually gets this packet or not + // we won't retry anyway + _dot->setAck(0); + _dot->setTxWait(false); + _lora->send(s_data); + osDelay(500); + _sam.updateInfo(" "); + } + } +} + +void ModeDemo::displayHelp() { + _help.display(); + _help.updateMode("LoRa Demo"); + _help.updateDescription("Select TX Method"); + _help.updateSw1(" Trigger"); + _help.updateSw2("Interval"); +} + diff --git a/Mode/ModeDemo.h b/Mode/ModeDemo.h new file mode 100644 index 0000000..b2aaf21 --- /dev/null +++ b/Mode/ModeDemo.h @@ -0,0 +1,39 @@ +#ifndef __MODEDEMO_H__ +#define __MODEDEMO_H__ + +#include "Mode.h" +#include "LayoutHelp.h" +#include "LayoutDemoSampling.h" + +class ModeDemo : public Mode { + public: + ModeDemo(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, GPSPARSER* gps); + ~ModeDemo(); + + bool start(); + + private: + void displayHelp(); + + typedef enum { + show_help = 0, + sampling, + success, + failure + } state; + + typedef enum { + trigger = 0, + interval + } mode; + + LayoutHelp _help; + LayoutDemoSampling _sam; + SensorItem _data; + mode _mode; + Timer _send_timer; + uint8_t _interval; + static const uint32_t _intervals[]; +}; + +#endif |