Scott Hoppe / Mbed OS 4_Ecolab_RSSI_Checker

Dependencies:   DOGS102 GpsParser ISL29011 MMA845x MPL3115A2 MTS-Serial NCP5623B libmDot-dev-mbed5-deprecated

Fork of MTDOT-BOX-EVB-Factory-Firmware by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Copyright (c) <2016> <MultiTech Systems>, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 // mbed headers
00020 #include "mbed.h"
00021 #include "rtos.h"
00022 // MTS headers
00023 #include "mDot.h"
00024 #include "MTSLog.h"
00025 // display headers
00026 #include "DOGS102.h"
00027 #include "NCP5623B.h"
00028 #include "LayoutStartup.h"
00029 #include "LayoutScrollSelect.h"
00030 #include "LayoutConfig.h"
00031 #include "LayoutHelp.h"
00032 // button header
00033 #include "ButtonHandler.h"
00034 // LoRa header
00035 #include "LoRaHandler.h"
00036 // Sensor header
00037 #include "SensorHandler.h"
00038 // mode objects
00039 #include "ModeJoin.h"
00040 #include "ModeSingle.h"
00041 #include "ModeSweep.h"
00042 #include "ModeDemo.h"
00043 #include "ModeConfig.h"
00044 #include "ModeGps.h"
00045 #include "ModeData.h"
00046 // misc heders
00047 #include "FileName.h"
00048 #include <string>
00049 
00050 #define DISABLE_DUTY_CYCLE true
00051 
00052 
00053 // LCD and LED controllers
00054 SPI lcd_spi(SPI1_MOSI, SPI1_MISO, SPI1_SCK);
00055 I2C led_i2c(I2C_SDA, I2C_SCL);
00056 DigitalOut lcd_spi_cs(SPI1_CS, 1);
00057 DigitalOut lcd_cd(XBEE_ON_SLEEP, 1);
00058 DOGS102* lcd;
00059 NCP5623B* led_cont;
00060 
00061 // Thread informaiton
00062 osThreadId main_id;
00063 
00064 // Button controller
00065 ButtonHandler* buttons;
00066 
00067 // LoRa controller
00068 LoRaHandler* lora_handler;
00069 
00070 mDot* dot;
00071 
00072 // GPS
00073 GPSPARSER* gps;
00074 MTSSerial gps_serial(XBEE_DOUT, XBEE_DIN, 256, 2048);
00075 
00076 // Sensors
00077 SensorHandler* sensors;
00078 
00079 // Modes
00080 ModeJoin* modeJoin;
00081 ModeSingle* modeSingle;
00082 ModeSweep* modeSweep;
00083 ModeDemo* modeDemo;
00084 ModeConfig* modeConfig;
00085 ModeGps* modeGps;
00086 ModeData* modeData;
00087 
00088 // Serial debug port
00089 Serial debug(USBTX, USBRX);
00090 
00091 // Survey Data File
00092 char file_name[] = "SurveyData.txt";
00093 
00094 // Prototypes
00095 void mainMenu();
00096 
00097 int main() {
00098     debug.baud(115200);
00099 
00100     lcd = new DOGS102(lcd_spi, lcd_spi_cs, lcd_cd);
00101     // NCP5623B::LEDs 1 & 2 are the screen backlight - not used on default build
00102     // NCP5623B::LED3 is EVB LED2
00103     led_cont = new NCP5623B(led_i2c);
00104 
00105     main_id = Thread::gettid();
00106     buttons = new ButtonHandler(main_id);
00107     dot = mDot::getInstance();
00108     lora_handler = new LoRaHandler(main_id);
00109 
00110     dot->setDisableDutyCycle(DISABLE_DUTY_CYCLE);
00111     dot->setLinkCheckThreshold(0);
00112     dot->setLinkCheckCount(0);
00113 
00114     // Seed the RNG
00115     srand(dot->getRadioRandom());
00116 
00117     gps = new GPSPARSER(&gps_serial, led_cont);
00118     sensors = new SensorHandler();
00119 
00120     led_cont->setLEDCurrent(16);
00121 
00122     MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
00123 
00124     modeJoin = new ModeJoin(lcd, buttons, dot, lora_handler, gps, sensors);
00125     modeSingle = new ModeSingle(lcd, buttons, dot, lora_handler, gps, sensors);
00126     modeSweep = new ModeSweep(lcd, buttons, dot, lora_handler, gps, sensors);
00127     modeDemo = new ModeDemo(lcd, buttons, dot, lora_handler, gps, sensors);
00128     modeConfig = new ModeConfig(lcd, buttons, dot, lora_handler, gps, sensors);
00129     modeGps = new ModeGps(lcd, buttons, dot, lora_handler, gps, sensors, modeJoin);
00130     modeData = new ModeData(lcd, buttons, dot, lora_handler, gps, sensors);
00131 
00132 
00133     osDelay(1000);
00134     logInfo("%sGPS detected", gps->gpsDetected() ? "" : "no ");
00135 
00136     // display startup screen for 3 seconds
00137     LayoutStartup ls(lcd, dot);
00138     ls.display();
00139     ls.updateGPS(gps->gpsDetected());
00140     osDelay(3000);
00141 
00142     logInfo("displaying main menu");
00143     mainMenu();
00144 
00145     return 0;
00146 }
00147 
00148 void mainMenu() {
00149     bool mode_selected = false;
00150     std::string selected;
00151     std::string product;
00152     
00153     typedef enum {
00154         demo = 1,
00155         config,
00156         single,
00157         sweep,
00158         gps,
00159         data
00160 
00161     } menu_items;
00162 
00163     std::string menu_strings[] = {
00164         "Select Mode",
00165         "LoRa Demo",
00166         "Configuration",
00167         "Survey Single",
00168         "Survey Sweep",
00169         "Survey GPS",
00170         "View Data"
00171     };
00172     std::vector<std::string> items;
00173     items.push_back(menu_strings[demo]);
00174     items.push_back(menu_strings[config]);
00175     items.push_back(menu_strings[single]);
00176     items.push_back(menu_strings[sweep]);
00177     items.push_back(menu_strings[gps]);
00178     items.push_back(menu_strings[data]);
00179 
00180     while (true) {
00181         product = "DOT-BOX/EVB ";
00182         product += mDot::FrequencyBandStr(dot->getFrequencyBand());
00183 
00184         // reset session between modes
00185         dot->resetNetworkSession();
00186         lora_handler->resetActivityLed();
00187 
00188         LayoutScrollSelect menu(lcd, items, product, menu_strings[0]);
00189        // menu.display();
00190 
00191         /*while (! mode_selected) {
00192             osEvent e = Thread::signal_wait(buttonSignal);
00193             if (e.status == osEventSignal) {
00194                 ButtonHandler::ButtonEvent ev = buttons->getButtonEvent();
00195                 switch (ev) {
00196                     case ButtonHandler::sw1_press:
00197                         selected = menu.select();
00198                         mode_selected = true;
00199                         break;
00200                     case ButtonHandler::sw2_press:
00201                         menu.scroll();
00202                         break;
00203                     case ButtonHandler::sw1_hold:
00204                         break;
00205                     default:
00206                         break;
00207                 }
00208             }
00209         }*/
00210 
00211         selected = menu_strings[single];
00212         if (selected == menu_strings[demo]) {
00213             if (modeJoin->start())
00214                 modeDemo->start();
00215         } else if (selected == menu_strings[config]) {
00216             modeConfig->start();
00217         } else if (selected == menu_strings[single]) {
00218             if (modeJoin->start())
00219              
00220                 modeSingle->start();
00221         } else if (selected == menu_strings[sweep]) {
00222             if (modeJoin->start())
00223                 modeSweep->start();
00224         } else if (selected == menu_strings[gps]) {
00225             if(dot->getFrequencyBand() == mDot::FB_EU868) {
00226                 modeJoin->start();
00227             }
00228             modeGps->start();
00229         } else if (selected == menu_strings[data]) {
00230             modeData->start();
00231         } 
00232 
00233         mode_selected = false;
00234         
00235 
00236     }
00237 }
00238 
00239 
00240