summaryrefslogtreecommitdiff
path: root/Mode/ModeDemo.cpp
blob: d1dc793e745f89e435670c960fcd7d894f51263d (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#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 };

ModeDemo::ModeDemo(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, GPSPARSER* gps, SensorHandler* sensors)
  : Mode(lcd, buttons, dot, lora, gps, sensors),
    _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);

    _initial_data_rate = _dot->getTxDataRate();

    // use configured data rate and power if possible
    if (_band == mDot::FB_915 && _initial_data_rate == mDot::SF_10) {
        logInfo("using SF_9 instead of SF_10 - SF_10 max packet size is too small for data");
        _dot->setTxDataRate(mDot::SF_9);
    }

    _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.updateSw1("    Send");
                                _sam.updateSw2("Back");
                                break;
                            case sampling:
                                if (_mode == trigger) {
                                    if (_dot->getNextTxMs() > 0)
                                        no_channel = true;
                                    else
                                        send = true;
                                } else {
                                    _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.updateSw2("Back");
                                _sam.updateInterval(_intervals[_interval]);
                                break;
                            case sampling:
                                _send_timer.stop();
                                _send_timer.reset();
                                _state = show_help;
                                displayHelp();
                                break;
                        }
                        break;
                    case ButtonHandler::sw1_hold:
                        _send_timer.stop();
                        _send_timer.reset();
                        _dot->setTxDataRate(_initial_data_rate);
                        return true;
                }
            }
            if (e.value.signals & loraSignal) {
                _ls = _lora->getStatus();
                switch (_ls) {
                    case LoRaHandler::send_success:
                        switch (_state) {
                            case sampling:
                                if (_mode == trigger) {
                                    _sam.updateSw1("    Send");
                                    _sam.updateInfo("                 ");
                                } else {
                                    _sam.updateSw1("Interval");
                                    _sam.updateInterval(_intervals[_interval]);
                                }
                                _sam.updateSw2("Back");
                                break;
                        }
                        break;

                    case LoRaHandler::send_failure:
                        switch (_state) {
                            case sampling:
                                if (_mode == trigger) {
                                    _sam.updateSw1("    Send");
                                    _sam.updateInfo("                 ");
                                } else {
                                    _sam.updateSw1("Interval");
                                    _sam.updateInterval(_intervals[_interval]);
                                }
                                _sam.updateSw2("Back");
                                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);
        }
        if(_state != show_help){
            updateSensorData(_data);
            _sam.updateAccelerationX(_data.accel_data._x);
            _sam.updateAccelerationY(_data.accel_data._y);
            _sam.updateAccelerationZ(_data.accel_data._z);
    		_sam.updatePressure(_data.pressure);
    		_sam.updateAltitude(_data.altitude);
    		_sam.updateTemperature(_data.temperature);
    		_sam.updateLight(_data.light);        
        }
    }
}

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