summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 63d04710a1932d573068cc9ede1387368036ab5c (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// mbed headers
#include "mbed.h"
#include "rtos.h"
// MTS headers
#include "mDot.h"
#include "MTSLog.h"
#include "MTSText.h"
// sensor headers
#include "ISL29011.h"
#include "MMA845x.h"
#include "MPL3115A2.h"
#include "GPSPARSER.h"
// display headers
#include "DOGS102.h"
#include "NCP5623B.h"
#include "LayoutStartup.h"
#include "LayoutScrollSelect.h"
#include "LayoutJoin.h"
#include "LayoutConfig.h"
// button header
#include "ButtonHandler.h"
// LoRa header
#include "LoRaHandler.h"
// misc heders
#include <string>

// LCD and backlight controllers
SPI lcd_spi(SPI1_MOSI, SPI1_MISO, SPI1_SCK);
I2C backlight_i2c(I2C_SDA, I2C_SCL);
DigitalOut lcd_spi_cs(SPI1_CS, 1);
DigitalOut lcd_cd(XBEE_ON_SLEEP, 1);
DOGS102* lcd;
NCP5623B* lcd_backlight;

// Thread informaiton
osThreadId main_id;

// Button controller
ButtonHandler* buttons;

// LoRa controller
LoRaHandler* lora;
mDot* dot;

// Serial debug port
Serial debug(USBTX, USBRX);

// Prototypes
void mainMenu();
void join();
void configuration();

int main() {
    debug.baud(115200);

    lcd = new DOGS102(lcd_spi, lcd_spi_cs, lcd_cd);
    lcd_backlight = new NCP5623B(backlight_i2c);

    main_id = Thread::gettid();
    buttons = new ButtonHandler(main_id);
    dot = mDot::getInstance();

    // display startup screen for 3 seconds
    LayoutStartup ls(lcd);
    ls.display();
    osDelay(3000);

    //MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
    MTSLog::setLogLevel(MTSLog::INFO_LEVEL);
    logInfo("displaying main menu");
    mainMenu();

    return 0;
}

void mainMenu() {
    bool mode_selected = false;
    std::string selected;

    typedef enum {
        demo = 2,
        config,
        single,
        sweep
    } menu_items;

    std::string menu_strings[] = {
        "MultiTech EVB",
        "Select Mode",
        "LoRa Demo",
        "Configuration",
        "Survey Single",
        "Survey Sweep"
    };

    std::vector<std::string> items;
    items.push_back(menu_strings[demo]);
    items.push_back(menu_strings[config]);
    items.push_back(menu_strings[single]);
    items.push_back(menu_strings[sweep]);

    while (true) {
        LayoutScrollSelect menu(lcd, items, menu_strings[0], menu_strings[1]);
        menu.display();

        while (! mode_selected) {
            osEvent e = Thread::signal_wait(buttonSignal);
            if (e.status == osEventSignal) {
                ButtonHandler::ButtonEvent ev = buttons->getButtonEvent();
                switch (ev) {
                    case ButtonHandler::sw1_press:
                        selected = menu.select();
                        mode_selected = true;
                        break;
                    case ButtonHandler::sw2_press:
                        menu.scroll();
                        break;
                    case ButtonHandler::sw1_hold:
                        break;
                    default:
                        break;
                }
            }
        }

        if (selected == menu_strings[demo]) {
            join();
        } else if (selected == menu_strings[config]) {
            configuration();
        } else if (selected == menu_strings[single]) {
            join();
        } else if (selected == menu_strings[sweep]) {
            join();
        }

        mode_selected = false;
    }
}

void join() {
    uint32_t next_tx;
    uint8_t rate;
    uint8_t power;
    uint8_t band;
    bool joined = false;
    ButtonHandler::ButtonEvent ev;
    LoRaHandler::LoRaStatus status;

    // 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());
    lj.updateRate(dot->DataRateStr(rate));
    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.updateStatus("Joining...");
            if (! lora->join())
                logError("cannot join - LoRa layer busy");
        }

        osEvent e = Thread::signal_wait(0, 250);
        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);
    lc.display();

    logInfo("config mode");

    while (true) {
        osEvent e = Thread::signal_wait(buttonSignal);
        if (e.status == osEventSignal) {
            ButtonHandler::ButtonEvent ev = buttons->getButtonEvent();
            switch (ev) {
                case ButtonHandler::sw1_press:
                    break;
                case ButtonHandler::sw2_press:
                    break;
                case ButtonHandler::sw1_hold:
                    return;
                default:
                    break;
            }
        }
    }
}