MTDOT-BOX-EVB-Factory-Firmware
Dependencies: NCP5623B GpsParser ISL29011 libmDot-mbed5 MTS-Serial MMA845x DOGS102 MPL3115A2
Mode/Mode.cpp@16:e76cec0eec43, 2019-03-14 (annotated)
- Committer:
- jenkins@jenkinsdm1
- Date:
- Thu Mar 14 08:23:20 2019 -0500
- Revision:
- 16:e76cec0eec43
- Parent:
- 12:05435282f899
dotbox-firmware revision 3.1.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
1:71125aa00e33 | 1 | /* Copyright (c) <2016> <MultiTech Systems>, MIT License |
Mike Fiore |
1:71125aa00e33 | 2 | * |
Mike Fiore |
1:71125aa00e33 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Mike Fiore |
1:71125aa00e33 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Mike Fiore |
1:71125aa00e33 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Mike Fiore |
1:71125aa00e33 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Mike Fiore |
1:71125aa00e33 | 7 | * furnished to do so, subject to the following conditions: |
Mike Fiore |
1:71125aa00e33 | 8 | * |
Mike Fiore |
1:71125aa00e33 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Mike Fiore |
1:71125aa00e33 | 10 | * substantial portions of the Software. |
Mike Fiore |
1:71125aa00e33 | 11 | * |
Mike Fiore |
1:71125aa00e33 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Mike Fiore |
1:71125aa00e33 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Mike Fiore |
1:71125aa00e33 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Mike Fiore |
1:71125aa00e33 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Mike Fiore |
1:71125aa00e33 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Mike Fiore |
1:71125aa00e33 | 17 | */ |
Mike Fiore |
1:71125aa00e33 | 18 | |
Mike Fiore |
1:71125aa00e33 | 19 | #include "Mode.h" |
Mike Fiore |
1:71125aa00e33 | 20 | #include "MTSLog.h" |
Mike Fiore |
1:71125aa00e33 | 21 | |
Mike Fiore |
1:71125aa00e33 | 22 | /* |
Mike Fiore |
1:71125aa00e33 | 23 | * union for converting from 32-bit to 4 8-bit values |
Mike Fiore |
1:71125aa00e33 | 24 | */ |
Mike Fiore |
1:71125aa00e33 | 25 | union convert32 { |
Mike Fiore |
1:71125aa00e33 | 26 | int32_t f_s; // convert from signed 32 bit int |
Mike Fiore |
1:71125aa00e33 | 27 | uint32_t f_u; // convert from unsigned 32 bit int |
Mike Fiore |
1:71125aa00e33 | 28 | uint8_t t_u[4]; // convert to 8 bit unsigned array |
Mike Fiore |
1:71125aa00e33 | 29 | }convertL; |
Mike Fiore |
1:71125aa00e33 | 30 | |
Mike Fiore |
1:71125aa00e33 | 31 | /* |
Mike Fiore |
1:71125aa00e33 | 32 | * union for converting from 16- bit to 2 8-bit values |
Mike Fiore |
1:71125aa00e33 | 33 | */ |
Mike Fiore |
1:71125aa00e33 | 34 | union convert16 { |
Mike Fiore |
1:71125aa00e33 | 35 | int16_t f_s; // convert from signed 16 bit int |
Mike Fiore |
1:71125aa00e33 | 36 | uint16_t f_u; // convert from unsigned 16 bit int |
Mike Fiore |
1:71125aa00e33 | 37 | uint8_t t_u[2]; // convert to 8 bit unsigned array |
Mike Fiore |
1:71125aa00e33 | 38 | } convertS; |
Mike Fiore |
1:71125aa00e33 | 39 | |
Mike Fiore |
1:71125aa00e33 | 40 | |
Mike Fiore |
1:71125aa00e33 | 41 | Mode::Mode(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, GPSPARSER* gps, SensorHandler* sensors) |
Mike Fiore |
1:71125aa00e33 | 42 | : _lcd(lcd), |
Mike Fiore |
1:71125aa00e33 | 43 | _buttons(buttons), |
Mike Fiore |
1:71125aa00e33 | 44 | _dot(dot), |
Mike Fiore |
1:71125aa00e33 | 45 | _lora(lora), |
Mike Fiore |
1:71125aa00e33 | 46 | _gps(gps), |
Mike Fiore |
1:71125aa00e33 | 47 | _sensors(sensors), |
Mike Fiore |
1:71125aa00e33 | 48 | _main_id(Thread::gettid()), |
Mike Fiore |
1:71125aa00e33 | 49 | _index(0), |
Mike Fiore |
1:71125aa00e33 | 50 | _band(_dot->getFrequencyBand()), |
Mike Fiore |
1:71125aa00e33 | 51 | _sub_band(_dot->getFrequencySubBand()), |
Mike Fiore |
7:a31236c2e75c | 52 | _data_rate(mDot::DR0), |
jenkins@jenkinsdm1 | 12:05435282f899 | 53 | _power(20), |
Mike Fiore |
1:71125aa00e33 | 54 | _next_tx(0), |
Mike Fiore |
1:71125aa00e33 | 55 | _send_data(false), |
Mike Fiore |
1:71125aa00e33 | 56 | _gps_available(_gps->gpsDetected()) |
Mike Fiore |
1:71125aa00e33 | 57 | {} |
Mike Fiore |
1:71125aa00e33 | 58 | |
Mike Fiore |
1:71125aa00e33 | 59 | Mode::~Mode() {} |
Mike Fiore |
1:71125aa00e33 | 60 | |
Mike Fiore |
1:71125aa00e33 | 61 | bool Mode::deleteDataFile() { |
Mike Fiore |
1:71125aa00e33 | 62 | bool ret = true; |
Mike Fiore |
1:71125aa00e33 | 63 | |
Mike Fiore |
1:71125aa00e33 | 64 | // if survey data file exists, attempt to delete it |
Mike Fiore |
1:71125aa00e33 | 65 | std::vector<mDot::mdot_file> files = _dot->listUserFiles(); |
Mike Fiore |
1:71125aa00e33 | 66 | for (std::vector<mDot::mdot_file>::iterator it = files.begin(); it != files.end(); it++) |
Mike Fiore |
1:71125aa00e33 | 67 | if (it->name == file_name) { |
Mike Fiore |
1:71125aa00e33 | 68 | if (! _dot->deleteUserFile(file_name)) |
Mike Fiore |
1:71125aa00e33 | 69 | ret = false; |
Mike Fiore |
1:71125aa00e33 | 70 | break; |
Mike Fiore |
1:71125aa00e33 | 71 | } |
Mike Fiore |
1:71125aa00e33 | 72 | |
Mike Fiore |
1:71125aa00e33 | 73 | return ret; |
Mike Fiore |
1:71125aa00e33 | 74 | } |
Mike Fiore |
1:71125aa00e33 | 75 | |
Mike Fiore |
1:71125aa00e33 | 76 | // ID, Status, Lock, Lat, Long, Alt, Time, RSSIup, SNRup, RSSIdown, SNRdown, DataRate, Power |
Mike Fiore |
1:71125aa00e33 | 77 | bool Mode::appendDataFile(const DataItem& data) { |
Mike Fiore |
1:71125aa00e33 | 78 | char main_buf[256]; |
Mike Fiore |
1:71125aa00e33 | 79 | char id_buf[16]; |
Mike Fiore |
1:71125aa00e33 | 80 | char lat_buf[32]; |
Mike Fiore |
1:71125aa00e33 | 81 | char lon_buf[32]; |
Mike Fiore |
1:71125aa00e33 | 82 | char alt_buf[16]; |
Mike Fiore |
1:71125aa00e33 | 83 | char time_buf[32]; |
Mike Fiore |
1:71125aa00e33 | 84 | char stats_buf[32]; |
Mike Fiore |
1:71125aa00e33 | 85 | size_t size; |
Mike Fiore |
1:71125aa00e33 | 86 | |
Mike Fiore |
1:71125aa00e33 | 87 | snprintf(id_buf, sizeof(id_buf), "%c%ld", (data.type == single) ? 'L' : 'S', data.index); |
Mike Fiore |
1:71125aa00e33 | 88 | |
Mike Fiore |
1:71125aa00e33 | 89 | // if we had GPS lock, format GPS data |
Mike Fiore |
1:71125aa00e33 | 90 | if (data.gps_lock) { |
Mike Fiore |
1:71125aa00e33 | 91 | snprintf(lat_buf, sizeof(lat_buf), "%d %d %d.%03d %c", |
jenkins@jenkinsdm1 | 12:05435282f899 | 92 | (int) abs(data.gps_latitude.degrees), |
jenkins@jenkinsdm1 | 12:05435282f899 | 93 | (int) data.gps_latitude.minutes, |
jenkins@jenkinsdm1 | 12:05435282f899 | 94 | (int) (data.gps_latitude.seconds * 6) / 1000, |
jenkins@jenkinsdm1 | 12:05435282f899 | 95 | (int) (data.gps_latitude.seconds * 6) % 1000, |
Mike Fiore |
1:71125aa00e33 | 96 | (data.gps_latitude.degrees > 0) ? 'N' : 'S'); |
Mike Fiore |
1:71125aa00e33 | 97 | snprintf(lon_buf, sizeof(lon_buf), "%d %d %d.%03d %c", |
jenkins@jenkinsdm1 | 12:05435282f899 | 98 | (int) abs(data.gps_longitude.degrees), |
jenkins@jenkinsdm1 | 12:05435282f899 | 99 | (int) data.gps_longitude.minutes, |
jenkins@jenkinsdm1 | 12:05435282f899 | 100 | (int) (data.gps_longitude.seconds * 6) / 1000, |
jenkins@jenkinsdm1 | 12:05435282f899 | 101 | (int) (data.gps_longitude.seconds * 6) % 1000, |
Mike Fiore |
1:71125aa00e33 | 102 | (data.gps_longitude.degrees > 0) ? 'E' : 'W'); |
Mike Fiore |
1:71125aa00e33 | 103 | snprintf(alt_buf, sizeof(alt_buf), "%d", |
Mike Fiore |
1:71125aa00e33 | 104 | data.gps_altitude); |
Mike Fiore |
1:71125aa00e33 | 105 | snprintf(time_buf, sizeof(time_buf), "%02d:%02d:%02d %02d/%02d/%04d", |
Mike Fiore |
1:71125aa00e33 | 106 | data.gps_time.tm_hour, |
Mike Fiore |
1:71125aa00e33 | 107 | data.gps_time.tm_min, |
Mike Fiore |
1:71125aa00e33 | 108 | data.gps_time.tm_sec, |
Mike Fiore |
1:71125aa00e33 | 109 | data.gps_time.tm_mon + 1, |
Mike Fiore |
1:71125aa00e33 | 110 | data.gps_time.tm_mday, |
Mike Fiore |
1:71125aa00e33 | 111 | data.gps_time.tm_year + 1900); |
Mike Fiore |
1:71125aa00e33 | 112 | } |
Mike Fiore |
1:71125aa00e33 | 113 | |
Mike Fiore |
1:71125aa00e33 | 114 | if (data.status) { |
Mike Fiore |
1:71125aa00e33 | 115 | float down_snr = (float)data.link.down.snr / 10.0; |
Mike Fiore |
1:71125aa00e33 | 116 | snprintf(stats_buf, sizeof(stats_buf), "%lu,%ld,%d,%2.1f", |
Mike Fiore |
1:71125aa00e33 | 117 | data.link.up.gateways, |
Mike Fiore |
1:71125aa00e33 | 118 | data.link.up.dBm, |
jenkins@jenkinsdm1 | 12:05435282f899 | 119 | (int)abs(data.link.down.rssi), |
Mike Fiore |
1:71125aa00e33 | 120 | down_snr); |
Mike Fiore |
1:71125aa00e33 | 121 | } |
Mike Fiore |
1:71125aa00e33 | 122 | |
Mike Fiore |
1:71125aa00e33 | 123 | size = snprintf(main_buf, sizeof(main_buf), "%s,%c,%ld,%s,%s,%s,%s,%s,%s,%lu\n", |
Mike Fiore |
1:71125aa00e33 | 124 | id_buf, |
Mike Fiore |
1:71125aa00e33 | 125 | data.status ? 'S' : 'F', |
Mike Fiore |
1:71125aa00e33 | 126 | data.gps_lock ? data.gps_sats : 0, |
Mike Fiore |
1:71125aa00e33 | 127 | (data.gps_lock) ? lat_buf : "", |
Mike Fiore |
1:71125aa00e33 | 128 | (data.gps_lock) ? lon_buf : "", |
Mike Fiore |
1:71125aa00e33 | 129 | (data.gps_lock) ? alt_buf : "", |
Mike Fiore |
1:71125aa00e33 | 130 | (data.gps_lock) ? time_buf : "", |
Mike Fiore |
1:71125aa00e33 | 131 | data.status ? stats_buf : ",,,", |
Mike Fiore |
7:a31236c2e75c | 132 | _dot->DataRateStr(data.data_rate).substr(2).c_str(), |
Mike Fiore |
1:71125aa00e33 | 133 | data.power); |
Mike Fiore |
1:71125aa00e33 | 134 | |
Mike Fiore |
1:71125aa00e33 | 135 | if (size < 0) { |
Mike Fiore |
1:71125aa00e33 | 136 | logError("failed to format survey data"); |
Mike Fiore |
1:71125aa00e33 | 137 | return false; |
Mike Fiore |
1:71125aa00e33 | 138 | } |
Mike Fiore |
1:71125aa00e33 | 139 | |
Mike Fiore |
1:71125aa00e33 | 140 | if (! _dot->appendUserFile(file_name, (void*)main_buf, size)) { |
Mike Fiore |
1:71125aa00e33 | 141 | logError("failed to write survey data to file"); |
Mike Fiore |
1:71125aa00e33 | 142 | return false; |
Mike Fiore |
1:71125aa00e33 | 143 | } else { |
Mike Fiore |
1:71125aa00e33 | 144 | logInfo("successfully wrote survey data to file\r\n\t%s", main_buf); |
Mike Fiore |
1:71125aa00e33 | 145 | } |
Mike Fiore |
1:71125aa00e33 | 146 | |
Mike Fiore |
1:71125aa00e33 | 147 | return true; |
Mike Fiore |
1:71125aa00e33 | 148 | } |
Mike Fiore |
1:71125aa00e33 | 149 | |
Mike Fiore |
1:71125aa00e33 | 150 | void Mode::updateData(DataItem& data, DataType type, bool status) { |
Mike Fiore |
1:71125aa00e33 | 151 | data.type = type; |
Mike Fiore |
1:71125aa00e33 | 152 | data.index = _index; |
Mike Fiore |
1:71125aa00e33 | 153 | data.status = status; |
Mike Fiore |
1:71125aa00e33 | 154 | data.gps_lock = _gps->getLockStatus(); |
Mike Fiore |
1:71125aa00e33 | 155 | data.gps_sats = _gps->getNumSatellites(); |
Mike Fiore |
1:71125aa00e33 | 156 | data.gps_longitude = _gps->getLongitude(); |
Mike Fiore |
1:71125aa00e33 | 157 | data.gps_latitude = _gps->getLatitude(); |
Mike Fiore |
1:71125aa00e33 | 158 | data.gps_altitude = _gps->getAltitude(); |
Mike Fiore |
1:71125aa00e33 | 159 | data.gps_time = _gps->getTimestamp(); |
Mike Fiore |
1:71125aa00e33 | 160 | data.link = _link_check_result; |
Mike Fiore |
1:71125aa00e33 | 161 | data.data_rate = _data_rate; |
Mike Fiore |
1:71125aa00e33 | 162 | data.power = _power; |
Mike Fiore |
1:71125aa00e33 | 163 | } |
Mike Fiore |
1:71125aa00e33 | 164 | |
Mike Fiore |
1:71125aa00e33 | 165 | void Mode::updateSensorData(SensorItem& data) { |
Mike Fiore |
1:71125aa00e33 | 166 | data.accel_data = _sensors->getAcceleration(); |
Mike Fiore |
1:71125aa00e33 | 167 | data.baro_data = _sensors->getBarometer(); |
Mike Fiore |
1:71125aa00e33 | 168 | data.lux_data_raw = _sensors->getLightRaw(); |
Mike Fiore |
1:71125aa00e33 | 169 | data.pressure_raw = _sensors->getPressureRaw(); |
Mike Fiore |
1:71125aa00e33 | 170 | data.light = _sensors->getLight(); |
Mike Fiore |
1:71125aa00e33 | 171 | data.pressure = _sensors->getPressure(); |
Mike Fiore |
1:71125aa00e33 | 172 | data.altitude = _sensors->getAltitude(); |
Mike Fiore |
1:71125aa00e33 | 173 | data.temperature = _sensors->getTemp(SensorHandler::CELSIUS); |
Mike Fiore |
1:71125aa00e33 | 174 | } |
Mike Fiore |
1:71125aa00e33 | 175 | |
Mike Fiore |
1:71125aa00e33 | 176 | uint32_t Mode::getIndex(DataType type) { |
Mike Fiore |
1:71125aa00e33 | 177 | uint32_t index = 0; |
Mike Fiore |
1:71125aa00e33 | 178 | mDot::mdot_file file; |
Mike Fiore |
1:71125aa00e33 | 179 | size_t buf_size = 128; |
Mike Fiore |
1:71125aa00e33 | 180 | char buf[buf_size]; |
Mike Fiore |
1:71125aa00e33 | 181 | bool done = false; |
Mike Fiore |
1:71125aa00e33 | 182 | char search; |
Mike Fiore |
1:71125aa00e33 | 183 | |
Mike Fiore |
1:71125aa00e33 | 184 | int read_offset; |
Mike Fiore |
1:71125aa00e33 | 185 | int read_size; |
Mike Fiore |
1:71125aa00e33 | 186 | int reduce = buf_size - 32; |
Mike Fiore |
1:71125aa00e33 | 187 | int bytes_read; |
Mike Fiore |
1:71125aa00e33 | 188 | int ret; |
Mike Fiore |
1:71125aa00e33 | 189 | int current; |
Mike Fiore |
1:71125aa00e33 | 190 | |
Mike Fiore |
1:71125aa00e33 | 191 | if (type == single) |
Mike Fiore |
1:71125aa00e33 | 192 | search = 'L'; |
Mike Fiore |
1:71125aa00e33 | 193 | else |
Mike Fiore |
1:71125aa00e33 | 194 | search = 'S'; |
Mike Fiore |
1:71125aa00e33 | 195 | |
Mike Fiore |
1:71125aa00e33 | 196 | file = _dot->openUserFile(file_name, mDot::FM_RDONLY); |
Mike Fiore |
1:71125aa00e33 | 197 | if (file.fd < 0) { |
Mike Fiore |
1:71125aa00e33 | 198 | logError("failed to open survey data file"); |
Mike Fiore |
1:71125aa00e33 | 199 | } else { |
Mike Fiore |
1:71125aa00e33 | 200 | //logInfo("file size %d", file.size); |
Mike Fiore |
1:71125aa00e33 | 201 | if (file.size > buf_size) { |
Mike Fiore |
1:71125aa00e33 | 202 | read_offset = file.size - buf_size - 1; |
Mike Fiore |
1:71125aa00e33 | 203 | read_size = buf_size; |
Mike Fiore |
1:71125aa00e33 | 204 | } else { |
Mike Fiore |
1:71125aa00e33 | 205 | read_offset = 0; |
Mike Fiore |
1:71125aa00e33 | 206 | read_size = file.size; |
Mike Fiore |
1:71125aa00e33 | 207 | } |
Mike Fiore |
1:71125aa00e33 | 208 | |
Mike Fiore |
1:71125aa00e33 | 209 | while (! done) { |
Mike Fiore |
1:71125aa00e33 | 210 | if (read_offset == 0) |
Mike Fiore |
1:71125aa00e33 | 211 | done = true; |
Mike Fiore |
1:71125aa00e33 | 212 | |
Mike Fiore |
1:71125aa00e33 | 213 | //logInfo("reading from index %d, %d bytes", read_offset, read_size); |
Mike Fiore |
1:71125aa00e33 | 214 | |
Mike Fiore |
1:71125aa00e33 | 215 | if (! _dot->seekUserFile(file, read_offset, SEEK_SET)) { |
Mike Fiore |
1:71125aa00e33 | 216 | logError("failed to seek %d/%d", read_offset, file.size); |
Mike Fiore |
1:71125aa00e33 | 217 | return 0; |
Mike Fiore |
1:71125aa00e33 | 218 | } |
Mike Fiore |
1:71125aa00e33 | 219 | memset(buf, 0, buf_size); |
Mike Fiore |
1:71125aa00e33 | 220 | ret = _dot->readUserFile(file, (void*)buf, read_size); |
Mike Fiore |
1:71125aa00e33 | 221 | if (ret != read_size) { |
Mike Fiore |
1:71125aa00e33 | 222 | logError("failed to read"); |
Mike Fiore |
1:71125aa00e33 | 223 | return 0; |
Mike Fiore |
1:71125aa00e33 | 224 | } |
Mike Fiore |
1:71125aa00e33 | 225 | //logInfo("read %d bytes [%s]", ret, buf); |
Mike Fiore |
1:71125aa00e33 | 226 | bytes_read = file.size - read_offset; |
Mike Fiore |
1:71125aa00e33 | 227 | //logInfo("read %d total bytes", bytes_read); |
Mike Fiore |
1:71125aa00e33 | 228 | |
Mike Fiore |
1:71125aa00e33 | 229 | // read_size - 1 is the last byte in the buffer |
Mike Fiore |
1:71125aa00e33 | 230 | for (current = read_size - 1; current >= 0; current--) { |
Mike Fiore |
1:71125aa00e33 | 231 | // generic case where a preceding newline exists |
Mike Fiore |
1:71125aa00e33 | 232 | if (buf[current] == '\n' && current != read_size - 1) { |
Mike Fiore |
1:71125aa00e33 | 233 | int test = current; |
Mike Fiore |
1:71125aa00e33 | 234 | //logInfo("found potential %d, %c", read_offset + current, buf[test + 1]); |
Mike Fiore |
1:71125aa00e33 | 235 | if (buf[test + 1] == search) { |
Mike Fiore |
1:71125aa00e33 | 236 | sscanf(&buf[test + 2], "%ld", &index); |
Mike Fiore |
1:71125aa00e33 | 237 | done = true; |
Mike Fiore |
1:71125aa00e33 | 238 | break; |
Mike Fiore |
1:71125aa00e33 | 239 | } |
Mike Fiore |
1:71125aa00e33 | 240 | // special case where the index we're looking for is in the first entry - no newline |
Mike Fiore |
1:71125aa00e33 | 241 | } else if (current == 0 && bytes_read >= file.size) { |
Mike Fiore |
1:71125aa00e33 | 242 | int test = current; |
Mike Fiore |
1:71125aa00e33 | 243 | //logInfo("found potential %d, %c", read_offset + current, buf[test + 1]); |
Mike Fiore |
1:71125aa00e33 | 244 | if (buf[test] == search) { |
Mike Fiore |
1:71125aa00e33 | 245 | sscanf(&buf[test + 1], "%ld", &index); |
Mike Fiore |
1:71125aa00e33 | 246 | done = true; |
Mike Fiore |
1:71125aa00e33 | 247 | break; |
Mike Fiore |
1:71125aa00e33 | 248 | } |
Mike Fiore |
1:71125aa00e33 | 249 | } |
Mike Fiore |
1:71125aa00e33 | 250 | } |
Mike Fiore |
1:71125aa00e33 | 251 | |
Mike Fiore |
1:71125aa00e33 | 252 | read_offset = (read_offset - reduce > 0) ? read_offset - reduce : 0; |
Mike Fiore |
1:71125aa00e33 | 253 | } |
Mike Fiore |
1:71125aa00e33 | 254 | _dot->closeUserFile(file); |
Mike Fiore |
1:71125aa00e33 | 255 | } |
Mike Fiore |
1:71125aa00e33 | 256 | |
Mike Fiore |
1:71125aa00e33 | 257 | logInfo("returning index %d", index); |
Mike Fiore |
1:71125aa00e33 | 258 | |
Mike Fiore |
1:71125aa00e33 | 259 | return index; |
Mike Fiore |
1:71125aa00e33 | 260 | } |
Mike Fiore |
1:71125aa00e33 | 261 | |
Mike Fiore |
1:71125aa00e33 | 262 | std::vector<uint8_t> Mode::formatSurveyData(DataItem& data) { |
Mike Fiore |
1:71125aa00e33 | 263 | std::vector<uint8_t> send_data; |
Mike Fiore |
1:71125aa00e33 | 264 | uint8_t satfix; |
Mike Fiore |
1:71125aa00e33 | 265 | |
Mike Fiore |
1:71125aa00e33 | 266 | send_data.clear(); |
Mike Fiore |
1:71125aa00e33 | 267 | send_data.push_back(0x1D); // key for start of data structure |
Mike Fiore |
1:71125aa00e33 | 268 | send_data.push_back(0x1A); // key for uplink QOS + RF Pwr |
Mike Fiore |
1:71125aa00e33 | 269 | convertS.f_s = data.link.up.gateways; |
Mike Fiore |
1:71125aa00e33 | 270 | send_data.push_back(convertS.t_u[1]); |
Mike Fiore |
1:71125aa00e33 | 271 | send_data.push_back(convertS.t_u[0]); |
Mike Fiore |
1:71125aa00e33 | 272 | send_data.push_back((data.link.up.dBm) & 0xFF); |
Mike Fiore |
1:71125aa00e33 | 273 | send_data.push_back(data.power); |
Mike Fiore |
1:71125aa00e33 | 274 | |
Mike Fiore |
1:71125aa00e33 | 275 | send_data.push_back(0x1B); // key for downlink QOS |
Mike Fiore |
1:71125aa00e33 | 276 | convertS.f_s=data.link.down.rssi; |
Mike Fiore |
1:71125aa00e33 | 277 | send_data.push_back(convertS.t_u[1]); |
Mike Fiore |
1:71125aa00e33 | 278 | send_data.push_back(convertS.t_u[0]); |
Mike Fiore |
1:71125aa00e33 | 279 | send_data.push_back((data.link.down.snr/10) & 0xFF); |
Mike Fiore |
1:71125aa00e33 | 280 | |
Mike Fiore |
1:71125aa00e33 | 281 | // collect GPS data if GPS device detected |
jenkins@jenkinsdm1 | 12:05435282f899 | 282 | if (_gps->gpsDetected() && ((_data_rate != lora::SF_10) || (lora::ChannelPlan::IsPlanDynamic(_band)))) { |
Mike Fiore |
1:71125aa00e33 | 283 | send_data.push_back(0x19); // key for GPS Lock Status |
Mike Fiore |
1:71125aa00e33 | 284 | satfix = (_gps->getNumSatellites() << 4 ) | (_gps->getFixStatus() & 0x0F ); |
Mike Fiore |
1:71125aa00e33 | 285 | send_data.push_back(satfix); |
Mike Fiore |
1:71125aa00e33 | 286 | |
Mike Fiore |
1:71125aa00e33 | 287 | if (_gps->getLockStatus()){ // if gps has a lock |
Mike Fiore |
1:71125aa00e33 | 288 | // Send GPS data if GPS device locked |
Mike Fiore |
1:71125aa00e33 | 289 | send_data.push_back(0x15); // key for GPS Latitude |
Mike Fiore |
1:71125aa00e33 | 290 | send_data.push_back(data.gps_latitude.degrees); |
Mike Fiore |
1:71125aa00e33 | 291 | send_data.push_back(data.gps_latitude.minutes); |
Mike Fiore |
1:71125aa00e33 | 292 | convertS.f_s = data.gps_latitude.seconds; |
Mike Fiore |
1:71125aa00e33 | 293 | send_data.push_back(convertS.t_u[1]); |
Mike Fiore |
1:71125aa00e33 | 294 | send_data.push_back(convertS.t_u[0]); |
Mike Fiore |
1:71125aa00e33 | 295 | |
Mike Fiore |
1:71125aa00e33 | 296 | send_data.push_back(0x16); // key for GPS Longitude |
Mike Fiore |
1:71125aa00e33 | 297 | convertS.f_s = data.gps_longitude.degrees; |
Mike Fiore |
1:71125aa00e33 | 298 | send_data.push_back(convertS.t_u[1]); |
Mike Fiore |
1:71125aa00e33 | 299 | send_data.push_back(convertS.t_u[0]); |
Mike Fiore |
1:71125aa00e33 | 300 | |
Mike Fiore |
1:71125aa00e33 | 301 | send_data.push_back(data.gps_longitude.minutes); |
Mike Fiore |
1:71125aa00e33 | 302 | convertS.f_s = data.gps_longitude.seconds; |
Mike Fiore |
1:71125aa00e33 | 303 | send_data.push_back(convertS.t_u[1]); |
Mike Fiore |
1:71125aa00e33 | 304 | send_data.push_back(convertS.t_u[0]); |
Mike Fiore |
1:71125aa00e33 | 305 | } |
Mike Fiore |
1:71125aa00e33 | 306 | } |
Mike Fiore |
1:71125aa00e33 | 307 | // key for end of data structure |
Mike Fiore |
1:71125aa00e33 | 308 | send_data.push_back(0x1D); |
Mike Fiore |
1:71125aa00e33 | 309 | |
Mike Fiore |
1:71125aa00e33 | 310 | return send_data; |
Mike Fiore |
1:71125aa00e33 | 311 | } |
Mike Fiore |
1:71125aa00e33 | 312 | |
Mike Fiore |
1:71125aa00e33 | 313 | std::vector<uint8_t> Mode::formatSensorData(SensorItem& data) { |
Mike Fiore |
1:71125aa00e33 | 314 | std::vector<uint8_t> send_data; |
Mike Fiore |
1:71125aa00e33 | 315 | send_data.clear(); |
Mike Fiore |
1:71125aa00e33 | 316 | send_data.push_back(0x0E); // key for Current Acceleration 3-Axis Value |
Mike Fiore |
1:71125aa00e33 | 317 | convertS.f_s = data.accel_data._x *4; // shift data 2 bits while retaining sign |
Mike Fiore |
1:71125aa00e33 | 318 | send_data.push_back(convertS.t_u[1]); // get 8 MSB of 14 bit value |
Mike Fiore |
1:71125aa00e33 | 319 | convertS.f_s = data.accel_data._y * 4; // shift data 2 bits while retaining sign |
Mike Fiore |
1:71125aa00e33 | 320 | send_data.push_back(convertS.t_u[1]); // get 8 MSB of 14 bit value |
Mike Fiore |
1:71125aa00e33 | 321 | convertS.f_s = data.accel_data._z * 4; // shift data 2 bits while retaining sign |
Mike Fiore |
1:71125aa00e33 | 322 | send_data.push_back(convertS.t_u[1]); // get 8 MSB of 14 bit value |
Mike Fiore |
1:71125aa00e33 | 323 | send_data.push_back(0x08); // key for Current Pressure Value |
Mike Fiore |
1:71125aa00e33 | 324 | convertL.f_u = data.pressure_raw; // pressure data is 20 bits unsigned |
Mike Fiore |
1:71125aa00e33 | 325 | send_data.push_back(convertL.t_u[2]); |
Mike Fiore |
1:71125aa00e33 | 326 | send_data.push_back(convertL.t_u[1]); |
Mike Fiore |
1:71125aa00e33 | 327 | send_data.push_back(convertL.t_u[0]); |
Mike Fiore |
1:71125aa00e33 | 328 | send_data.push_back(0x05); // key for Current Ambient Light Value |
Mike Fiore |
1:71125aa00e33 | 329 | convertS.f_u = data.lux_data_raw; // data is 16 bits unsigned |
Mike Fiore |
1:71125aa00e33 | 330 | send_data.push_back(convertS.t_u[1]); |
Mike Fiore |
1:71125aa00e33 | 331 | send_data.push_back(convertS.t_u[0]); |
Mike Fiore |
1:71125aa00e33 | 332 | send_data.push_back(0x0B); // key for Current Temperature Value |
Mike Fiore |
1:71125aa00e33 | 333 | convertS.f_s = data.baro_data._temp; // temperature is signed 12 bit |
Mike Fiore |
1:71125aa00e33 | 334 | send_data.push_back(convertS.t_u[1]); |
Mike Fiore |
1:71125aa00e33 | 335 | send_data.push_back(convertS.t_u[0]); |
Mike Fiore |
1:71125aa00e33 | 336 | |
Mike Fiore |
1:71125aa00e33 | 337 | return send_data; |
Mike Fiore |
1:71125aa00e33 | 338 | } |
Mike Fiore |
1:71125aa00e33 | 339 |