MTDOT-BOX-EVB-Factory-Firmware

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

Committer:
Mike Fiore
Date:
Fri Nov 04 17:27:05 2016 -0500
Revision:
7:a31236c2e75c
update from git revision 2.1.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mike Fiore 7:a31236c2e75c 1 /* Copyright (c) <2016> <MultiTech Systems>, MIT License
Mike Fiore 7:a31236c2e75c 2 *
Mike Fiore 7:a31236c2e75c 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Mike Fiore 7:a31236c2e75c 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Mike Fiore 7:a31236c2e75c 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Mike Fiore 7:a31236c2e75c 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Mike Fiore 7:a31236c2e75c 7 * furnished to do so, subject to the following conditions:
Mike Fiore 7:a31236c2e75c 8 *
Mike Fiore 7:a31236c2e75c 9 * The above copyright notice and this permission notice shall be included in all copies or
Mike Fiore 7:a31236c2e75c 10 * substantial portions of the Software.
Mike Fiore 7:a31236c2e75c 11 *
Mike Fiore 7:a31236c2e75c 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Mike Fiore 7:a31236c2e75c 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Mike Fiore 7:a31236c2e75c 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Mike Fiore 7:a31236c2e75c 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Mike Fiore 7:a31236c2e75c 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Mike Fiore 7:a31236c2e75c 17 */
Mike Fiore 7:a31236c2e75c 18
Mike Fiore 7:a31236c2e75c 19 #include "ModeData.h"
Mike Fiore 7:a31236c2e75c 20 #include "MTSText.h"
Mike Fiore 7:a31236c2e75c 21 #define ONELINEMAX 93
Mike Fiore 7:a31236c2e75c 22
Mike Fiore 7:a31236c2e75c 23 ModeData::ModeData(DOGS102* lcd, ButtonHandler* _buttons, mDot* _dot, LoRaHandler* lora, GPSPARSER* gps, SensorHandler* sensors)
Mike Fiore 7:a31236c2e75c 24 :Mode(lcd,_buttons,_dot,lora,gps,sensors),
Mike Fiore 7:a31236c2e75c 25 _data(lcd),
Mike Fiore 7:a31236c2e75c 26 _help(lcd),
Mike Fiore 7:a31236c2e75c 27 _buf_size(ONELINEMAX)
Mike Fiore 7:a31236c2e75c 28 {}
Mike Fiore 7:a31236c2e75c 29
Mike Fiore 7:a31236c2e75c 30 ModeData::~ModeData() {}
Mike Fiore 7:a31236c2e75c 31
Mike Fiore 7:a31236c2e75c 32 bool ModeData::checkFile(){
Mike Fiore 7:a31236c2e75c 33 bool exists = false;
Mike Fiore 7:a31236c2e75c 34 //get all files and see if file exists
Mike Fiore 7:a31236c2e75c 35 vector<mDot::mdot_file> files = _dot->listUserFiles();
Mike Fiore 7:a31236c2e75c 36 for (vector<mDot::mdot_file>::iterator it = files.begin(); it != files.end(); it++) {
Mike Fiore 7:a31236c2e75c 37 if (strcmp(file_name,it->name)==0) {
Mike Fiore 7:a31236c2e75c 38 exists = true;
Mike Fiore 7:a31236c2e75c 39 break;
Mike Fiore 7:a31236c2e75c 40 }
Mike Fiore 7:a31236c2e75c 41 }
Mike Fiore 7:a31236c2e75c 42 //if file doesnt exist exit to main menu
Mike Fiore 7:a31236c2e75c 43 if(!exists) {
Mike Fiore 7:a31236c2e75c 44 _data.noData();
Mike Fiore 7:a31236c2e75c 45 osDelay(3000);
Mike Fiore 7:a31236c2e75c 46 return true;
Mike Fiore 7:a31236c2e75c 47 }
Mike Fiore 7:a31236c2e75c 48 _file = _dot->openUserFile(file_name, mDot::FM_RDONLY);
Mike Fiore 7:a31236c2e75c 49 //if nothing is in file exit to main menu
Mike Fiore 7:a31236c2e75c 50 if (_file.fd < 0) {
Mike Fiore 7:a31236c2e75c 51 _data.errorData();
Mike Fiore 7:a31236c2e75c 52 osDelay(3000);
Mike Fiore 7:a31236c2e75c 53 _dot->closeUserFile(_file);
Mike Fiore 7:a31236c2e75c 54 return true;
Mike Fiore 7:a31236c2e75c 55 }
Mike Fiore 7:a31236c2e75c 56 return false;
Mike Fiore 7:a31236c2e75c 57 }
Mike Fiore 7:a31236c2e75c 58
Mike Fiore 7:a31236c2e75c 59 bool ModeData::start(){
Mike Fiore 7:a31236c2e75c 60 if(checkFile())
Mike Fiore 7:a31236c2e75c 61 return true;
Mike Fiore 7:a31236c2e75c 62 _help.display();
Mike Fiore 7:a31236c2e75c 63 osDelay(3000);
Mike Fiore 7:a31236c2e75c 64 readFile();
Mike Fiore 7:a31236c2e75c 65 }
Mike Fiore 7:a31236c2e75c 66
Mike Fiore 7:a31236c2e75c 67 void ModeData::displayData(){
Mike Fiore 7:a31236c2e75c 68 std::vector<std::string> data = mts::Text::split(_str, ',');
Mike Fiore 7:a31236c2e75c 69 _line.id = data.at(0);
Mike Fiore 7:a31236c2e75c 70 _line.status = data.at(1);
Mike Fiore 7:a31236c2e75c 71 _line.lock = data.at(2);
Mike Fiore 7:a31236c2e75c 72 _line.lat = data.at(3);
Mike Fiore 7:a31236c2e75c 73 _line.lng = data.at(4);
Mike Fiore 7:a31236c2e75c 74 _line.alt = data.at(5);
Mike Fiore 7:a31236c2e75c 75 _line.time = data.at(6);
Mike Fiore 7:a31236c2e75c 76 _line.gateways = data.at(7);
Mike Fiore 7:a31236c2e75c 77 _line.margin = data.at(8);
Mike Fiore 7:a31236c2e75c 78 _line.rssiD = data.at(9);
Mike Fiore 7:a31236c2e75c 79 _line.snrD = data.at(10);
Mike Fiore 7:a31236c2e75c 80 _line.dataRate = data.at(11);
Mike Fiore 7:a31236c2e75c 81 _line.power = data.at(12);
Mike Fiore 7:a31236c2e75c 82 _data.updateAll(_line);
Mike Fiore 7:a31236c2e75c 83 }
Mike Fiore 7:a31236c2e75c 84
Mike Fiore 7:a31236c2e75c 85 //get the current line out of the buffer into str
Mike Fiore 7:a31236c2e75c 86 void ModeData::getLine(){
Mike Fiore 7:a31236c2e75c 87 _prev = 0;
Mike Fiore 7:a31236c2e75c 88 _indexUpdate = 0;
Mike Fiore 7:a31236c2e75c 89 _str = "";
Mike Fiore 7:a31236c2e75c 90 _dot->readUserFile(_file, (void*)_buf, _buf_size);
Mike Fiore 7:a31236c2e75c 91 //only gets called when going back
Mike Fiore 7:a31236c2e75c 92 if(_last) {
Mike Fiore 7:a31236c2e75c 93 //-3 puts it back to one before new line
Mike Fiore 7:a31236c2e75c 94 _indexUpdate = _buf_size - 3;
Mike Fiore 7:a31236c2e75c 95 //check from back of buffer for new line
Mike Fiore 7:a31236c2e75c 96 while(_buf[_indexUpdate] != '\n' && _indexUpdate >= 0){
Mike Fiore 7:a31236c2e75c 97 _indexUpdate--;
Mike Fiore 7:a31236c2e75c 98 }
Mike Fiore 7:a31236c2e75c 99 _indexUpdate++;
Mike Fiore 7:a31236c2e75c 100 }
Mike Fiore 7:a31236c2e75c 101 //go from indexUpdate to new line to ge the line
Mike Fiore 7:a31236c2e75c 102 //prev keeps track of how long line read was
Mike Fiore 7:a31236c2e75c 103 while(_buf[_indexUpdate]!='\n') {
Mike Fiore 7:a31236c2e75c 104 _str += _buf[_indexUpdate];
Mike Fiore 7:a31236c2e75c 105 _indexUpdate++;
Mike Fiore 7:a31236c2e75c 106 _prev++;
Mike Fiore 7:a31236c2e75c 107 }
Mike Fiore 7:a31236c2e75c 108 //push index past newline
Mike Fiore 7:a31236c2e75c 109 _index += _indexUpdate + 1;
Mike Fiore 7:a31236c2e75c 110 displayData();
Mike Fiore 7:a31236c2e75c 111 }
Mike Fiore 7:a31236c2e75c 112
Mike Fiore 7:a31236c2e75c 113 void ModeData::back(){
Mike Fiore 7:a31236c2e75c 114 if(_index >= (_buf_size + _prev)) {
Mike Fiore 7:a31236c2e75c 115 _index -= (_prev + _buf_size);
Mike Fiore 7:a31236c2e75c 116 } else {
Mike Fiore 7:a31236c2e75c 117 //special case for beginning of file
Mike Fiore 7:a31236c2e75c 118 if(_index > 0){
Mike Fiore 7:a31236c2e75c 119 _buf_size = _index-1;
Mike Fiore 7:a31236c2e75c 120 }
Mike Fiore 7:a31236c2e75c 121 _buf_size -= _prev;
Mike Fiore 7:a31236c2e75c 122 _index = 0;
Mike Fiore 7:a31236c2e75c 123 }
Mike Fiore 7:a31236c2e75c 124 _last = true;
Mike Fiore 7:a31236c2e75c 125 _dot->seekUserFile(_file, _index, SEEK_SET);
Mike Fiore 7:a31236c2e75c 126 getLine();
Mike Fiore 7:a31236c2e75c 127 }
Mike Fiore 7:a31236c2e75c 128
Mike Fiore 7:a31236c2e75c 129 void ModeData::forward(){
Mike Fiore 7:a31236c2e75c 130 _last = false;
Mike Fiore 7:a31236c2e75c 131 if(_index < _file.size) {
Mike Fiore 7:a31236c2e75c 132 _buf_size = ONELINEMAX;
Mike Fiore 7:a31236c2e75c 133 _dot->seekUserFile(_file, _index, SEEK_SET);
Mike Fiore 7:a31236c2e75c 134 getLine();
Mike Fiore 7:a31236c2e75c 135 }
Mike Fiore 7:a31236c2e75c 136 }
Mike Fiore 7:a31236c2e75c 137
Mike Fiore 7:a31236c2e75c 138 //update switch labels
Mike Fiore 7:a31236c2e75c 139 void ModeData::configSw(){
Mike Fiore 7:a31236c2e75c 140 if(_index - (_prev+1) <= 0){
Mike Fiore 7:a31236c2e75c 141 _data.updateSw2("");
Mike Fiore 7:a31236c2e75c 142 } else {
Mike Fiore 7:a31236c2e75c 143 _data.updateSw2("Back");
Mike Fiore 7:a31236c2e75c 144 }
Mike Fiore 7:a31236c2e75c 145 if(_index<_file.size){
Mike Fiore 7:a31236c2e75c 146 _data.updateSw1("Next");
Mike Fiore 7:a31236c2e75c 147 } else {
Mike Fiore 7:a31236c2e75c 148 _data.updateSw1("");
Mike Fiore 7:a31236c2e75c 149 }
Mike Fiore 7:a31236c2e75c 150 }
Mike Fiore 7:a31236c2e75c 151
Mike Fiore 7:a31236c2e75c 152 bool ModeData::readFile(){
Mike Fiore 7:a31236c2e75c 153 _index = 0;
Mike Fiore 7:a31236c2e75c 154 _last = false;
Mike Fiore 7:a31236c2e75c 155 _prev = 0;
Mike Fiore 7:a31236c2e75c 156 _indexUpdate = 0;
Mike Fiore 7:a31236c2e75c 157 //called to start on page one
Mike Fiore 7:a31236c2e75c 158 forward();
Mike Fiore 7:a31236c2e75c 159 configSw();
Mike Fiore 7:a31236c2e75c 160 ButtonHandler::ButtonEvent be;
Mike Fiore 7:a31236c2e75c 161 while (true) {
Mike Fiore 7:a31236c2e75c 162 be = _buttons->getButtonEvent();
Mike Fiore 7:a31236c2e75c 163 switch(be) {
Mike Fiore 7:a31236c2e75c 164 case ButtonHandler::sw1_press:
Mike Fiore 7:a31236c2e75c 165 if(_index!=_file.size) {
Mike Fiore 7:a31236c2e75c 166 forward();
Mike Fiore 7:a31236c2e75c 167 configSw();
Mike Fiore 7:a31236c2e75c 168 }
Mike Fiore 7:a31236c2e75c 169 break;
Mike Fiore 7:a31236c2e75c 170
Mike Fiore 7:a31236c2e75c 171 case ButtonHandler::sw2_press:
Mike Fiore 7:a31236c2e75c 172 if(_index - (_prev+1) > 0) {
Mike Fiore 7:a31236c2e75c 173 back();
Mike Fiore 7:a31236c2e75c 174 configSw();
Mike Fiore 7:a31236c2e75c 175 }
Mike Fiore 7:a31236c2e75c 176 break;
Mike Fiore 7:a31236c2e75c 177
Mike Fiore 7:a31236c2e75c 178 case ButtonHandler::sw1_hold:
Mike Fiore 7:a31236c2e75c 179 _dot->closeUserFile(_file);
Mike Fiore 7:a31236c2e75c 180 return true;
Mike Fiore 7:a31236c2e75c 181
Mike Fiore 7:a31236c2e75c 182 default:
Mike Fiore 7:a31236c2e75c 183 break;
Mike Fiore 7:a31236c2e75c 184 }
Mike Fiore 7:a31236c2e75c 185 }
Mike Fiore 7:a31236c2e75c 186 }
Mike Fiore 7:a31236c2e75c 187