summaryrefslogtreecommitdiff
path: root/Mode/ModeDemo.cpp
blob: e70ccbd5dc4b0074bae7b5817c95f902f0dce736 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#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;
                        }
                        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;
                        }
                        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 sampling:
                                if (_mode == trigger) {
                                    _sam.updateSw2("Send");
                                    _sam.updateInfo("                 ");
                                } else {
                                    _sam.updateSw1("Interval");
                                    _sam.updateInterval(_intervals[_interval]);
                                }
                                break;
                        }
                        break;

                    case LoRaHandler::send_failure:
                        switch (_state) {
                            case sampling:
                                if (_mode == trigger) {
                                    _sam.updateSw2("Send");
                                    _sam.updateInfo("                 ");
                                } else {
                                    _sam.updateSw1("Interval");
                                    _sam.updateInterval(_intervals[_interval]);
                                }
                                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(_dot->getTxDataRate()).c_str(), _dot->getTxPower());
            _sam.updateInfo("Sending...");
            _sam.updateSw1("        ");
            _sam.updateSw2("        ");
            send = false;
            // 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);
        }
    }
}

void ModeDemo::displayHelp() {
    _help.display();
    _help.updateMode("LoRa Demo");
    _help.updateDescription("Select TX Method");
    _help.updateSw1(" Trigger");
    _help.updateSw2("Interval");
}