summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--LoRaHandler/LoRaHandler.cpp2
-rw-r--r--Mode/Mode.cpp19
-rw-r--r--Mode/Mode.h47
-rw-r--r--Mode/ModeJoin.cpp86
-rw-r--r--Mode/ModeJoin.h30
-rw-r--r--main.cpp129
6 files changed, 207 insertions, 106 deletions
diff --git a/LoRaHandler/LoRaHandler.cpp b/LoRaHandler/LoRaHandler.cpp
index 0ecb59e..27eb266 100644
--- a/LoRaHandler/LoRaHandler.cpp
+++ b/LoRaHandler/LoRaHandler.cpp
@@ -25,6 +25,7 @@ void l_worker(void const* argument) {
while (true) {
e = Thread::signal_wait(signal);
if (e.status == osEventSignal) {
+ l->_status = LoRaHandler::busy;
switch (cmd) {
case l_ping:
l->_mutex.lock();
@@ -69,6 +70,7 @@ void l_worker(void const* argument) {
break;
default:
+ l->_status = LoRaHandler::none;
break;
}
}
diff --git a/Mode/Mode.cpp b/Mode/Mode.cpp
new file mode 100644
index 0000000..eea6d3c
--- /dev/null
+++ b/Mode/Mode.cpp
@@ -0,0 +1,19 @@
+#include "Mode.h"
+
+Mode::Mode(DOGS102* lcd, ButtonHandler* buttons)
+ : _lcd(lcd),
+ _buttons(buttons),
+ _index(0),
+ _main_id(Thread::gettid())
+{}
+
+Mode::~Mode() {}
+
+bool Mode::deleteDataFile() {
+ return true;
+}
+
+bool Mode::appendDataFile(const DataItem& data) {
+ return true;
+}
+
diff --git a/Mode/Mode.h b/Mode/Mode.h
new file mode 100644
index 0000000..1254a9e
--- /dev/null
+++ b/Mode/Mode.h
@@ -0,0 +1,47 @@
+#ifndef __MODE_H__
+#define __MODE_H__
+
+#include "DOGS102.h"
+#include "ButtonHandler.h"
+#include "GPSPARSER.h"
+
+class Mode {
+ public:
+ typedef enum {
+ single = 0,
+ sweep
+ } DataType;
+
+ typedef struct {
+ DataType type;
+ int32_t index;
+ bool status;
+ int32_t lock;
+ GPSPARSER::longitude gps_longitude;
+ GPSPARSER::latitude gps_latitude;
+ int16_t gps_altitude;
+ struct tm gps_time;
+ int16_t rssi_up;
+ int16_t snr_up;
+ int16_t rssi_down;
+ int16_t snr_down;
+ uint8_t data_rate;
+ uint8_t power;
+ } DataItem;
+
+ Mode(DOGS102* lcd, ButtonHandler* buttons);
+ ~Mode();
+
+ virtual bool start() = 0;
+
+ protected:
+ bool deleteDataFile();
+ bool appendDataFile(const DataItem& data);
+
+ DOGS102* _lcd;
+ ButtonHandler* _buttons;
+ uint32_t _index;
+ osThreadId _main_id;
+};
+
+#endif
diff --git a/Mode/ModeJoin.cpp b/Mode/ModeJoin.cpp
new file mode 100644
index 0000000..f1f0e9a
--- /dev/null
+++ b/Mode/ModeJoin.cpp
@@ -0,0 +1,86 @@
+#include "ModeJoin.h"
+#include "MTSLog.h"
+#include "MTSText.h"
+
+ModeJoin::ModeJoin(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, uint8_t band)
+ : Mode(lcd, buttons),
+ _lj(lcd, band),
+ _dot(dot),
+ _lora(lora),
+ _band(band),
+ _data_rate(band == mDot::FB_915 ? mDot::SF_10 : mDot::SF_12),
+ _power(20),
+ _next_tx(0),
+ _joined(false)
+{}
+
+ModeJoin::~ModeJoin() {}
+
+bool ModeJoin::start() {
+ // clear any stale signals
+ osSignalClear(_main_id, buttonSignal | loraSignal);
+
+ _lj.display();
+ _lj.updateStatus("Joining...");
+ if (_dot->getJoinMode() == mDot::MANUAL) {
+ _lj.updateId(mts::Text::bin2hexString(_dot->getNetworkId()));
+ _lj.updateKey(mts::Text::bin2hexString(_dot->getNetworkKey()));
+ } else {
+ _lj.updateId(_dot->getNetworkName());
+ _lj.updateKey(_dot->getNetworkPassphrase());
+ }
+ if (_band == mDot::FB_915) {
+ _sub_band = _dot->getFrequencySubBand();
+ _lj.updateFsb(_sub_band);
+ }
+ // mDot::DataRateStr returns format SF_XX - we only want to display the XX part
+ _lj.updateRate(_dot->DataRateStr(_data_rate).substr(3));
+ _lj.updatePower(_power);
+
+ _lora->setDataRate(_data_rate);
+ _lora->setPower(_power);
+
+ while (! _joined) {
+ _next_tx = _lora->getNextTx();
+ if (_next_tx) {
+ _lj.updateCountdown(_next_tx * 1000);
+ } else {
+ _lj.updateAttempt(++_index);
+ _lj.updateStatus("Joining...");
+ _lora->join();
+ }
+
+ osEvent e = Thread::signal_wait(0);
+ if (e.status == osEventSignal) {
+ if (e.value.signals & buttonSignal) {
+ _be = _buttons->getButtonEvent();
+ switch (_be) {
+ case ButtonHandler::sw1_press:
+ return false;
+ case ButtonHandler::sw2_press:
+ break;
+ case ButtonHandler::sw1_hold:
+ return false;
+ }
+ }
+ if (e.value.signals & loraSignal) {
+ _ls = _lora->getStatus();
+ switch (_ls) {
+ case LoRaHandler::join_success:
+ _lj.updateStatus("Join Success!");
+ _lj.displayCancel(false);
+ logInfo("joined");
+ _joined = true;
+ osDelay(2000);
+ return true;
+
+ case LoRaHandler::join_failure:
+ logInfo("failed to join");
+ break;
+ }
+ }
+ }
+ }
+
+ return false;
+}
diff --git a/Mode/ModeJoin.h b/Mode/ModeJoin.h
new file mode 100644
index 0000000..5b88e25
--- /dev/null
+++ b/Mode/ModeJoin.h
@@ -0,0 +1,30 @@
+#ifndef __MODEJOIN_H__
+#define __MODEJOIN_H__
+
+#include "Mode.h"
+#include "LayoutJoin.h"
+#include "mDot.h"
+#include "LoRaHandler.h"
+
+class ModeJoin : public Mode {
+ public:
+ ModeJoin(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, uint8_t band);
+ ~ModeJoin();
+
+ bool start();
+
+ private:
+ LayoutJoin _lj;
+ mDot* _dot;
+ LoRaHandler* _lora;
+ uint8_t _band;
+ uint8_t _sub_band;
+ uint8_t _data_rate;
+ uint8_t _power;
+ uint32_t _next_tx;
+ bool _joined;
+ ButtonHandler::ButtonEvent _be;
+ LoRaHandler::LoRaStatus _ls;
+};
+
+#endif
diff --git a/main.cpp b/main.cpp
index fce9e10..e02deb4 100644
--- a/main.cpp
+++ b/main.cpp
@@ -15,7 +15,6 @@
#include "NCP5623B.h"
#include "LayoutStartup.h"
#include "LayoutScrollSelect.h"
-#include "LayoutJoin.h"
#include "LayoutConfig.h"
#include "LayoutDemoHelp.h"
#include "LayoutSingleHelp.h"
@@ -24,6 +23,8 @@
#include "ButtonHandler.h"
// LoRa header
#include "LoRaHandler.h"
+// mode objects
+#include "ModeJoin.h"
// misc heders
#include <string>
@@ -45,6 +46,9 @@ ButtonHandler* buttons;
LoRaHandler* lora;
mDot* dot;
+// Modes
+ModeJoin* modeJoin;
+
// Serial debug port
Serial debug(USBTX, USBRX);
@@ -65,12 +69,24 @@ int main() {
main_id = Thread::gettid();
buttons = new ButtonHandler(main_id);
dot = mDot::getInstance();
+ lora = new LoRaHandler(main_id);
+
+ modeJoin = new ModeJoin(lcd, buttons, dot, lora, dot->getFrequencyBand());
// display startup screen for 3 seconds
LayoutStartup ls(lcd);
ls.display();
osDelay(3000);
+ // start of temporary stuff!
+ if (dot->getFrequencyBand() == mDot::FB_915)
+ dot->setFrequencySubBand(mDot::FSB_7);
+ dot->setJoinMode(mDot::OTA);
+ dot->setNetworkName("mikes_lora_network");
+ dot->setNetworkPassphrase("password_123");
+ dot->setAck(1);
+ // end of temporary stuff!
+
//MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
MTSLog::setLogLevel(MTSLog::INFO_LEVEL);
logInfo("displaying main menu");
@@ -130,121 +146,22 @@ void mainMenu() {
}
if (selected == menu_strings[demo]) {
- join();
- loraDemo();
+ if (modeJoin->start())
+ loraDemo();
} else if (selected == menu_strings[config]) {
configuration();
} else if (selected == menu_strings[single]) {
- join();
- surveySingle();
+ if (modeJoin->start())
+ surveySingle();
} else if (selected == menu_strings[sweep]) {
- join();
- surveySweep();
+ if (modeJoin->start())
+ surveySweep();
}
mode_selected = false;
}
}
-void join() {
- uint32_t attempts = 1;
- uint32_t next_tx;
- uint8_t rate;
- uint8_t power;
- uint8_t band;
- bool joined = false;
- ButtonHandler::ButtonEvent ev;
- LoRaHandler::LoRaStatus status;
-
- // clear any stale signals
- osSignalClear(main_id, buttonSignal | loraSignal);
-
- // start of temporary stuff!
- if (dot->getFrequencyBand() == mDot::FB_915)
- dot->setFrequencySubBand(mDot::FSB_7);
- dot->setJoinMode(mDot::OTA);
- dot->setNetworkName("mikes_lora_network");
- dot->setNetworkPassphrase("password_123");
- dot->setAck(1);
- // end of temporary stuff!
-
- power = 20;
- band = dot->getFrequencyBand();
- if (band == mDot::FB_915)
- rate = mDot::SF_10;
- else
- rate = mDot::SF_12;
-
- logInfo("joining");
- LayoutJoin lj(lcd, band);
- lj.display();
- lj.updateStatus("Joining...");
-
- if (dot->getJoinMode() == mDot::MANUAL) {
- lj.updateId(mts::Text::bin2hexString(dot->getNetworkId()));
- lj.updateKey(mts::Text::bin2hexString(dot->getNetworkKey()));
- } else {
- lj.updateId(dot->getNetworkName());
- lj.updateKey(dot->getNetworkPassphrase());
- }
- if (band == mDot::FB_915)
- lj.updateFsb(dot->getFrequencySubBand());
- // mDot::DataRateStr returns format SF_XX - we only want to display the XX part
- lj.updateRate(dot->DataRateStr(rate).substr(3));
- lj.updatePower(power);
-
- if (! lora) {
- lora = new LoRaHandler(main_id);
- // give the LoRa worker thread some time to start up
- osDelay(100);
- }
- lora->setDataRate(rate);
- lora->setPower(power);
-
- while (! joined) {
- next_tx = lora->getNextTx();
- if (next_tx) {
- lj.updateCountdown(next_tx * 1000);
- } else {
- lj.updateAttempt(attempts++);
- lj.updateStatus("Joining...");
- if (! lora->join())
- logError("cannot join - LoRa layer busy");
- }
-
- osEvent e = Thread::signal_wait(0);
- if (e.status == osEventSignal) {
- if (e.value.signals & buttonSignal) {
- ev = buttons->getButtonEvent();
- switch (ev) {
- case ButtonHandler::sw1_press:
- return;
- case ButtonHandler::sw2_press:
- break;
- case ButtonHandler::sw1_hold:
- return;
- }
- }
- if (e.value.signals & loraSignal) {
- status = lora->getStatus();
- switch (status) {
- case LoRaHandler::join_success:
- lj.updateStatus("Join Success!");
- lj.displayCancel(false);
- logInfo("joined");
- joined = true;
- osDelay(2000);
- break;
-
- case LoRaHandler::join_failure:
- logInfo("failed to join");
- break;
- }
- }
- }
- }
-}
-
void configuration() {
LayoutConfig lc(lcd);