Factory firmware for the MultiTech Dotbox (MTDOT-BOX) and EVB (MTDOT-EVB) products.

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ModeData.cpp Source File

ModeData.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 #include "ModeData.h"
00020 #include "MTSText.h"
00021 #define ONELINEMAX 93
00022 
00023 ModeData::ModeData(DOGS102* lcd, ButtonHandler* _buttons, mDot* _dot, LoRaHandler* lora, GPSPARSER* gps, SensorHandler* sensors)
00024     :Mode(lcd,_buttons,_dot,lora,gps,sensors),
00025      _data(lcd),
00026      _help(lcd),
00027      _buf_size(ONELINEMAX)
00028 {}
00029 
00030 ModeData::~ModeData() {}
00031 
00032 bool ModeData::checkFile(){
00033     bool exists = false;
00034     //get all files and see if file exists
00035     vector<mDot::mdot_file> files = _dot->listUserFiles();
00036     for (vector<mDot::mdot_file>::iterator it = files.begin(); it != files.end(); it++) {
00037         if (strcmp(file_name,it->name)==0) {
00038             exists = true;
00039             break;
00040         }
00041     }
00042     //if file doesnt exist exit to main menu
00043     if(!exists) {
00044         _data.noData();
00045         osDelay(3000);
00046         return true;
00047     }
00048     _file = _dot->openUserFile(file_name, mDot::FM_RDONLY);
00049     //if nothing is in file exit to main menu
00050     if (_file.fd < 0) {
00051         _data.errorData();
00052         osDelay(3000);
00053         _dot->closeUserFile(_file);
00054         return true;
00055     }
00056     return false;
00057 }
00058 
00059 bool ModeData::start(){
00060     if(checkFile())
00061         return true;
00062     _help.display();
00063     osDelay(3000);
00064     readFile();
00065 }
00066 
00067 void ModeData::displayData(){
00068     std::vector<std::string> data = mts::Text::split(_str, ',');
00069     _line.id = data.at(0);
00070     _line.status = data.at(1);
00071     _line.lock = data.at(2);
00072     _line.lat = data.at(3);
00073     _line.lng = data.at(4);
00074     _line.alt = data.at(5);
00075     _line.time = data.at(6);
00076     _line.gateways = data.at(7);
00077     _line.margin = data.at(8);
00078     _line.rssiD = data.at(9);
00079     _line.snrD = data.at(10);
00080     _line.dataRate = data.at(11);
00081     _line.power = data.at(12);
00082     _data.updateAll(_line);
00083 }
00084 
00085 //get the current line out of the buffer into str
00086 void ModeData::getLine(){
00087     _prev = 0;
00088     _indexUpdate = 0;
00089     _str = "";
00090     _dot->readUserFile(_file, (void*)_buf, _buf_size);
00091     //only gets called when going back
00092     if(_last) {
00093         //-3 puts it back to one before new line
00094         _indexUpdate = _buf_size - 3;
00095         //check from back of buffer for new line
00096         while(_buf[_indexUpdate] != '\n' && _indexUpdate >= 0){
00097              _indexUpdate--;
00098         }
00099         _indexUpdate++;
00100     }
00101     //go from indexUpdate to new line to ge the line
00102     //prev keeps track of how long line read was
00103     while(_buf[_indexUpdate]!='\n') {
00104         _str += _buf[_indexUpdate];
00105         _indexUpdate++;
00106         _prev++;
00107     }
00108     //push index past newline
00109     _index += _indexUpdate + 1;
00110     displayData();
00111 }
00112 
00113 void ModeData::back(){
00114     if(_index >= (_buf_size + _prev)) {
00115         _index -= (_prev + _buf_size);
00116     } else {
00117         //special case for beginning of file
00118         if(_index > 0){
00119             _buf_size = _index-1;
00120         }
00121         _buf_size -= _prev;
00122         _index = 0;
00123     }
00124     _last = true;
00125     _dot->seekUserFile(_file, _index, SEEK_SET);
00126     getLine();
00127 }
00128 
00129 void ModeData::forward(){
00130     _last = false;
00131     if(_index < _file.size) {
00132         _buf_size = ONELINEMAX;
00133         _dot->seekUserFile(_file, _index, SEEK_SET);
00134         getLine();
00135     }
00136 }
00137 
00138 //update switch labels
00139 void ModeData::configSw(){
00140     if(_index - (_prev+1) <= 0){
00141          _data.updateSw2("");
00142     } else {
00143          _data.updateSw2("Back");
00144     }
00145     if(_index<_file.size){
00146          _data.updateSw1("Next");
00147     } else {
00148          _data.updateSw1("");
00149     }
00150 }
00151 
00152 bool ModeData::readFile(){
00153     _index = 0;
00154     _last = false;
00155     _prev = 0;
00156     _indexUpdate = 0;
00157     //called to start on page one
00158     forward();
00159     configSw();
00160     ButtonHandler::ButtonEvent be;
00161     while (true) {
00162         be = _buttons->getButtonEvent();
00163         switch(be) {
00164             case ButtonHandler::sw1_press:
00165                 if(_index!=_file.size) {
00166                     forward();
00167                     configSw();
00168                 }
00169                 break;
00170 
00171             case ButtonHandler::sw2_press:
00172                 if(_index - (_prev+1) > 0) {
00173                     back();
00174                     configSw();
00175                 }
00176                 break;
00177 
00178             case ButtonHandler::sw1_hold:
00179                 _dot->closeUserFile(_file);
00180                 return true;
00181 
00182             default:
00183                 break;
00184         }
00185     }
00186 }
00187