summaryrefslogtreecommitdiff
path: root/main.cpp
blob: fdbd3e2b43b4a276f30f95b1fd478fec19fb2428 (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
/* Copyright (c) <2016> <MultiTech Systems>, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

// mbed headers
#include "mbed.h"
#include "rtos.h"
// MTS headers
#include "mDot.h"
#include "MTSLog.h"
// display headers
#include "DOGS102.h"
#include "NCP5623B.h"
#include "LayoutStartup.h"
#include "LayoutScrollSelect.h"
#include "LayoutConfig.h"
#include "LayoutHelp.h"
// button header
#include "ButtonHandler.h"
// LoRa header
#include "LoRaHandler.h"
// Sensor header
#include "SensorHandler.h"
// mode objects
#include "ModeJoin.h"
#include "ModeSingle.h"
#include "ModeSweep.h"
#include "ModeDemo.h"
#include "ModeConfig.h"
#include "ModeGps.h"
#include "ModeData.h"
// misc heders
#include "FileName.h"
#include <string>

#define DISABLE_DUTY_CYCLE true


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

// Thread informaiton
osThreadId main_id;

// Button controller
ButtonHandler* buttons;

// LoRa controller
LoRaHandler* lora_handler;

mDot* dot;

// GPS
GPSPARSER* gps;
MTSSerial gps_serial(XBEE_DOUT, XBEE_DIN, 256, 2048);

// Sensors
SensorHandler* sensors;

// Modes
ModeJoin* modeJoin;
ModeSingle* modeSingle;
ModeSweep* modeSweep;
ModeDemo* modeDemo;
ModeConfig* modeConfig;
ModeGps* modeGps;
ModeData* modeData;

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

// Survey Data File
char file_name[] = "SurveyData.txt";

// Prototypes
void mainMenu();

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

    lcd = new DOGS102(lcd_spi, lcd_spi_cs, lcd_cd);
    // NCP5623B::LEDs 1 & 2 are the screen backlight - not used on default build
    // NCP5623B::LED3 is EVB LED2
    led_cont = new NCP5623B(led_i2c);

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

    dot->setDisableDutyCycle(DISABLE_DUTY_CYCLE);
    dot->setLinkCheckThreshold(0);
    dot->setLinkCheckCount(0);

    // Seed the RNG
    srand(dot->getRadioRandom());

    gps = new GPSPARSER(&gps_serial, led_cont);
    sensors = new SensorHandler();

    led_cont->setLEDCurrent(16);

    MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);

    modeJoin = new ModeJoin(lcd, buttons, dot, lora_handler, gps, sensors);
    modeSingle = new ModeSingle(lcd, buttons, dot, lora_handler, gps, sensors);
    modeSweep = new ModeSweep(lcd, buttons, dot, lora_handler, gps, sensors);
    modeDemo = new ModeDemo(lcd, buttons, dot, lora_handler, gps, sensors);
    modeConfig = new ModeConfig(lcd, buttons, dot, lora_handler, gps, sensors);
    modeGps = new ModeGps(lcd, buttons, dot, lora_handler, gps, sensors, modeJoin);
    modeData = new ModeData(lcd, buttons, dot, lora_handler, gps, sensors);


    osDelay(1000);
    logInfo("%sGPS detected", gps->gpsDetected() ? "" : "no ");

    // display startup screen for 3 seconds
    LayoutStartup ls(lcd, dot);
    ls.display();
    ls.updateGPS(gps->gpsDetected());
    osDelay(3000);

    logInfo("displaying main menu");
    mainMenu();

    return 0;
}

void mainMenu() {
    bool mode_selected = false;
    std::string selected;
    std::string product;
    
    typedef enum {
        demo = 1,
        config,
        single,
        sweep,
        gps,
        data

    } menu_items;

    std::string menu_strings[] = {
        "Select Mode",
        "LoRa Demo",
        "Configuration",
        "Survey Single",
        "Survey Sweep",
        "Survey GPS",
        "View Data"
    };
    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]);
    items.push_back(menu_strings[gps]);
    items.push_back(menu_strings[data]);

    while (true) {
        product = "DOT-BOX/EVB ";
        product += mDot::FrequencyBandStr(dot->getFrequencyBand());

        // reset session between modes
        dot->resetNetworkSession();
        lora_handler->resetActivityLed();

        LayoutScrollSelect menu(lcd, items, product, menu_strings[0]);
        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]) {
            if (modeJoin->start())
                modeDemo->start();
        } else if (selected == menu_strings[config]) {
            modeConfig->start();
        } else if (selected == menu_strings[single]) {
            if (modeJoin->start())
                modeSingle->start();
        } else if (selected == menu_strings[sweep]) {
            if (modeJoin->start())
                modeSweep->start();
        } else if (selected == menu_strings[gps]) {
            if(dot->getFrequencyBand() == mDot::FB_EU868) {
                modeJoin->start();
            }
            modeGps->start();
        } else if (selected == menu_strings[data]) {
            modeData->start();
        } 

        mode_selected = false;
    }
}