Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SDI12_device.h@18:365e8aa4002c, 2018-08-07 (annotated)
- Committer:
- amateusz
- Date:
- Tue Aug 07 16:36:17 2018 +0000
- Revision:
- 18:365e8aa4002c
- Parent:
- 17:5cabfc047b40
- Child:
- 19:a29b98ae0005
got rid of event queue
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amateusz | 0:d58ebb2bed9a | 1 | #include "SDI12.h" |
amateusz | 0:d58ebb2bed9a | 2 | #include <string> |
amateusz | 2:e2db05bc4708 | 3 | #include "mbed.h" |
amateusz | 0:d58ebb2bed9a | 4 | |
amateusz | 0:d58ebb2bed9a | 5 | class SDI12_device |
amateusz | 0:d58ebb2bed9a | 6 | { |
amateusz | 0:d58ebb2bed9a | 7 | public: |
amateusz | 0:d58ebb2bed9a | 8 | struct Identification_struct { |
amateusz | 0:d58ebb2bed9a | 9 | unsigned char sdi_version; |
amateusz | 0:d58ebb2bed9a | 10 | char company[8+1]; |
amateusz | 0:d58ebb2bed9a | 11 | char model[6+1]; |
amateusz | 0:d58ebb2bed9a | 12 | char version[3+1]; |
amateusz | 0:d58ebb2bed9a | 13 | }; |
amateusz | 0:d58ebb2bed9a | 14 | |
amateusz | 0:d58ebb2bed9a | 15 | struct Measurement_struct { |
amateusz | 0:d58ebb2bed9a | 16 | unsigned long timestampMeasurementReadyAt; |
amateusz | 4:11438803516c | 17 | char readyIn; |
amateusz | 0:d58ebb2bed9a | 18 | bool _measurementAlreadyRead; |
amateusz | 0:d58ebb2bed9a | 19 | char count; |
amateusz | 15:530bf2e09814 | 20 | float * values; // not float values [] !! :O |
amateusz | 0:d58ebb2bed9a | 21 | }; |
amateusz | 0:d58ebb2bed9a | 22 | |
amateusz | 0:d58ebb2bed9a | 23 | Identification_struct _ident_struct; |
amateusz | 0:d58ebb2bed9a | 24 | |
amateusz | 10:efd1e313fe8c | 25 | static int detect(SDI12 &sdi12, char indices[], int stop = -1, int start = -1) { |
amateusz | 0:d58ebb2bed9a | 26 | char found = 0; |
amateusz | 10:efd1e313fe8c | 27 | for (size_t i = (start==-1?0:start); i < (stop==-1?ADDRRICES.size():stop+1); ++i) { |
amateusz | 0:d58ebb2bed9a | 28 | string question = "?!"; |
amateusz | 5:e53104570fde | 29 | question[0] = ADDRRICES[i]; |
amateusz | 0:d58ebb2bed9a | 30 | sdi12.sendCommand(question); |
amateusz | 0:d58ebb2bed9a | 31 | std::string response; |
amateusz | 0:d58ebb2bed9a | 32 | Timer detectTimer; |
amateusz | 0:d58ebb2bed9a | 33 | detectTimer.start(); |
amateusz | 15:530bf2e09814 | 34 | while (detectTimer.read_ms() < RESPONSE_TIMEOUT * 4) { |
amateusz | 5:e53104570fde | 35 | if (sdi12.RxInProgress()) detectTimer.reset(); |
amateusz | 7:1f506c65c4e8 | 36 | if (sdi12.RxBufferAvailable()) { |
amateusz | 7:1f506c65c4e8 | 37 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 7:1f506c65c4e8 | 38 | char buffer[bufferSize]; |
amateusz | 7:1f506c65c4e8 | 39 | sdi12.getRxBuffer(buffer); |
amateusz | 7:1f506c65c4e8 | 40 | // if first char is valid address char |
amateusz | 7:1f506c65c4e8 | 41 | if (ADDRRICES.find(buffer[0])) { |
amateusz | 7:1f506c65c4e8 | 42 | indices[found++] = buffer[0]; |
amateusz | 13:0093792f2325 | 43 | debug("FOUND: %d -> %c\r\n", i, indices[found-1]); |
amateusz | 7:1f506c65c4e8 | 44 | } |
amateusz | 7:1f506c65c4e8 | 45 | } |
amateusz | 0:d58ebb2bed9a | 46 | } |
amateusz | 13:0093792f2325 | 47 | osDelay(100); |
amateusz | 0:d58ebb2bed9a | 48 | } |
amateusz | 0:d58ebb2bed9a | 49 | return found; |
amateusz | 0:d58ebb2bed9a | 50 | }; |
amateusz | 17:5cabfc047b40 | 51 | |
amateusz | 17:5cabfc047b40 | 52 | SDI12_device() { |
amateusz | 15:530bf2e09814 | 53 | debug("DEFAULT\r\n"); |
amateusz | 17:5cabfc047b40 | 54 | } |
amateusz | 17:5cabfc047b40 | 55 | |
amateusz | 18:365e8aa4002c | 56 | SDI12_device (SDI12 *inst, char address) : _sdi12(inst) { |
amateusz | 15:530bf2e09814 | 57 | setAddress(address); |
amateusz | 1:6b1a21925a81 | 58 | if(getIdentification(_ident_struct)); |
amateusz | 0:d58ebb2bed9a | 59 | }; |
amateusz | 15:530bf2e09814 | 60 | |
amateusz | 15:530bf2e09814 | 61 | |
amateusz | 15:530bf2e09814 | 62 | SDI12_device & operator= (const SDI12_device & old) { |
amateusz | 15:530bf2e09814 | 63 | _address = old._address; |
amateusz | 15:530bf2e09814 | 64 | _ident_struct = old._ident_struct; |
amateusz | 15:530bf2e09814 | 65 | // _sdi12 = old._sdi12; // non-copyable -.- |
amateusz | 15:530bf2e09814 | 66 | |
amateusz | 15:530bf2e09814 | 67 | _measurement = old._measurement; |
amateusz | 15:530bf2e09814 | 68 | _valuesReadyCount = old._valuesReadyCount; |
amateusz | 15:530bf2e09814 | 69 | _measurementReady = old._measurementReady; |
amateusz | 15:530bf2e09814 | 70 | } |
amateusz | 15:530bf2e09814 | 71 | |
amateusz | 10:efd1e313fe8c | 72 | ~SDI12_device () { |
amateusz | 15:530bf2e09814 | 73 | delete [] _measurement.values; |
amateusz | 10:efd1e313fe8c | 74 | } |
amateusz | 0:d58ebb2bed9a | 75 | |
amateusz | 15:530bf2e09814 | 76 | // concurrent means that device under measurement won't signal its prempt readiness to the bus |
amateusz | 13:0093792f2325 | 77 | int measure(bool concurrent = true, char group = '0') { |
amateusz | 4:11438803516c | 78 | |
amateusz | 2:e2db05bc4708 | 79 | std::string question = string("?") + string(concurrent?"C":"M") + string("!"); |
amateusz | 2:e2db05bc4708 | 80 | question[0] = _address; |
amateusz | 13:0093792f2325 | 81 | if (group != '0') |
amateusz | 13:0093792f2325 | 82 | question.insert(2, 1, group); // e.g. 2M! -> 2M1! |
amateusz | 15:530bf2e09814 | 83 | _sdi12->sendCommand(question); |
amateusz | 2:e2db05bc4708 | 84 | |
amateusz | 2:e2db05bc4708 | 85 | Timer timeout; |
amateusz | 2:e2db05bc4708 | 86 | timeout.start(); |
amateusz | 2:e2db05bc4708 | 87 | while (sdi12.RxBufferAvailable() == 0) { |
amateusz | 2:e2db05bc4708 | 88 | if (sdi12.RxInProgress()) timeout.reset(); |
amateusz | 5:e53104570fde | 89 | if(timeout.read_ms() > RESPONSE_TIMEOUT) { |
amateusz | 2:e2db05bc4708 | 90 | return false; |
amateusz | 2:e2db05bc4708 | 91 | } |
amateusz | 2:e2db05bc4708 | 92 | } |
amateusz | 10:efd1e313fe8c | 93 | |
amateusz | 2:e2db05bc4708 | 94 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 2:e2db05bc4708 | 95 | char buffer[bufferSize+1]; |
amateusz | 2:e2db05bc4708 | 96 | sdi12.getRxBuffer(buffer); |
amateusz | 2:e2db05bc4708 | 97 | buffer[bufferSize] = '\0'; |
amateusz | 2:e2db05bc4708 | 98 | std::string response(buffer); |
amateusz | 2:e2db05bc4708 | 99 | |
amateusz | 2:e2db05bc4708 | 100 | if (response[0] == _address) { |
amateusz | 2:e2db05bc4708 | 101 | char measurementsCount = std::atoi(response.substr(1+3,(concurrent?2:1)).c_str()); |
amateusz | 2:e2db05bc4708 | 102 | char waitFor = std::atoi(response.substr(1,3).c_str()); |
amateusz | 2:e2db05bc4708 | 103 | _measurement.count = measurementsCount; |
amateusz | 15:530bf2e09814 | 104 | _measurement.values = new float[measurementsCount]; |
amateusz | 4:11438803516c | 105 | _measurement.readyIn = waitFor; |
amateusz | 2:e2db05bc4708 | 106 | // _measurement.timestampMeasurementReadyAt |
amateusz | 2:e2db05bc4708 | 107 | debug("wait for %d measurement for %d seconds...", _measurement.count, waitFor); |
amateusz | 11:e0ccc1072c42 | 108 | _valuesReadyCount = 0; |
amateusz | 13:0093792f2325 | 109 | _measurementReady = false; |
amateusz | 9:a3062b9e4324 | 110 | // debug("measurement scheduled"); |
amateusz | 4:11438803516c | 111 | // if (_measurement.values != NULL) delete[] _measurement.values; |
amateusz | 4:11438803516c | 112 | // _measurement.values = new float[measurementsCount]; |
amateusz | 2:e2db05bc4708 | 113 | return 0; |
amateusz | 2:e2db05bc4708 | 114 | } |
amateusz | 2:e2db05bc4708 | 115 | return -1; |
amateusz | 2:e2db05bc4708 | 116 | }; |
amateusz | 13:0093792f2325 | 117 | |
amateusz | 4:11438803516c | 118 | char readyIn() { |
amateusz | 4:11438803516c | 119 | return _measurement.readyIn; |
amateusz | 4:11438803516c | 120 | } |
amateusz | 4:11438803516c | 121 | |
amateusz | 13:0093792f2325 | 122 | bool isReady() { |
amateusz | 13:0093792f2325 | 123 | return _measurementReady; |
amateusz | 13:0093792f2325 | 124 | } |
amateusz | 13:0093792f2325 | 125 | |
amateusz | 11:e0ccc1072c42 | 126 | bool read(char group = '0') { |
amateusz | 11:e0ccc1072c42 | 127 | // Measurement (M), Continuous (R), and Concurrent (C) commands and subsequent Data (D) |
amateusz | 11:e0ccc1072c42 | 128 | std::string question = std::string("?") + std::string("D") + std::string(1,group)+ std::string("!"); |
amateusz | 2:e2db05bc4708 | 129 | question[0] = _address; |
amateusz | 15:530bf2e09814 | 130 | _sdi12->sendCommand(question); |
amateusz | 2:e2db05bc4708 | 131 | |
amateusz | 2:e2db05bc4708 | 132 | Timer timeout; |
amateusz | 2:e2db05bc4708 | 133 | timeout.start(); |
amateusz | 2:e2db05bc4708 | 134 | while (sdi12.RxBufferAvailable() == 0) { |
amateusz | 2:e2db05bc4708 | 135 | if (sdi12.RxInProgress()) timeout.reset(); |
amateusz | 5:e53104570fde | 136 | if(timeout.read_ms() > RESPONSE_TIMEOUT) { |
amateusz | 2:e2db05bc4708 | 137 | return false; |
amateusz | 2:e2db05bc4708 | 138 | } |
amateusz | 2:e2db05bc4708 | 139 | } |
amateusz | 2:e2db05bc4708 | 140 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 2:e2db05bc4708 | 141 | char buffer[bufferSize+1]; |
amateusz | 2:e2db05bc4708 | 142 | sdi12.getRxBuffer(buffer); |
amateusz | 2:e2db05bc4708 | 143 | buffer[bufferSize] = '\0'; |
amateusz | 17:5cabfc047b40 | 144 | const std::string response(buffer); |
amateusz | 2:e2db05bc4708 | 145 | |
amateusz | 4:11438803516c | 146 | // debug("parser. recv: %s\r\n", response); |
amateusz | 3:2847f7c543d3 | 147 | if (response[0] == _address) { |
amateusz | 3:2847f7c543d3 | 148 | // parser here |
amateusz | 17:5cabfc047b40 | 149 | // const!!! response = response.substr(1); // to limit repeting this operation later. i.e. extract only values e.g. +21.3-123+123 |
amateusz | 3:2847f7c543d3 | 150 | // the only two possible delimeters of a value |
amateusz | 17:5cabfc047b40 | 151 | size_t next_start_index = response.find_first_of("-+", 0 + 1); // address offset! |
amateusz | 3:2847f7c543d3 | 152 | size_t next_end_index; |
amateusz | 3:2847f7c543d3 | 153 | // ready, steady, parse!! |
amateusz | 3:2847f7c543d3 | 154 | while(next_start_index != std::string::npos) { |
amateusz | 3:2847f7c543d3 | 155 | // determine start index: |
amateusz | 3:2847f7c543d3 | 156 | next_end_index = response.find_first_of("-+", next_start_index + 1); // std::substr is prepared to take std::npos |
amateusz | 17:5cabfc047b40 | 157 | |
amateusz | 3:2847f7c543d3 | 158 | float value = std::atof(response.substr(next_start_index, next_end_index).c_str()); |
amateusz | 4:11438803516c | 159 | // debug("parsed: %f\r\n", value); |
amateusz | 11:e0ccc1072c42 | 160 | _measurement.values[_valuesReadyCount++] = value; |
amateusz | 3:2847f7c543d3 | 161 | next_start_index = response.find_first_of("-+", next_end_index); |
amateusz | 3:2847f7c543d3 | 162 | } |
amateusz | 11:e0ccc1072c42 | 163 | // if current parsing doesn't return all the expexted measurements, then press harder and poll further Dx commands. RECURSION HERE *.* |
amateusz | 11:e0ccc1072c42 | 164 | if (_valuesReadyCount < _measurement.count) |
amateusz | 11:e0ccc1072c42 | 165 | read(group + 1); |
amateusz | 13:0093792f2325 | 166 | else { |
amateusz | 13:0093792f2325 | 167 | _measurementReady = true; |
amateusz | 13:0093792f2325 | 168 | } |
amateusz | 3:2847f7c543d3 | 169 | } |
amateusz | 4:11438803516c | 170 | return true; |
amateusz | 2:e2db05bc4708 | 171 | } |
amateusz | 4:11438803516c | 172 | |
amateusz | 4:11438803516c | 173 | |
amateusz | 0:d58ebb2bed9a | 174 | // |
amateusz | 4:11438803516c | 175 | const unsigned char getMeasurementCount() { |
amateusz | 2:e2db05bc4708 | 176 | return _measurement.count; |
amateusz | 2:e2db05bc4708 | 177 | } |
amateusz | 2:e2db05bc4708 | 178 | |
amateusz | 4:11438803516c | 179 | const float getMeasurementValue(char which) { |
amateusz | 13:0093792f2325 | 180 | _measurementReady = false; // reading /any/ of the values resets this |
amateusz | 2:e2db05bc4708 | 181 | return _measurement.values[which]; |
amateusz | 2:e2db05bc4708 | 182 | } |
amateusz | 2:e2db05bc4708 | 183 | |
amateusz | 0:d58ebb2bed9a | 184 | void setAddress(char address) { |
amateusz | 0:d58ebb2bed9a | 185 | _address = address; |
amateusz | 0:d58ebb2bed9a | 186 | }; |
amateusz | 0:d58ebb2bed9a | 187 | // |
amateusz | 0:d58ebb2bed9a | 188 | // |
amateusz | 0:d58ebb2bed9a | 189 | private: |
amateusz | 13:0093792f2325 | 190 | static const int RESPONSE_TIMEOUT = 15+1; // device should start to respond within [ms] |
amateusz | 5:e53104570fde | 191 | static const std::string ADDRRICES; // as in index -> indices, |
amateusz | 5:e53104570fde | 192 | |
amateusz | 15:530bf2e09814 | 193 | SDI12 *_sdi12; |
amateusz | 0:d58ebb2bed9a | 194 | char _address; |
amateusz | 17:5cabfc047b40 | 195 | |
amateusz | 0:d58ebb2bed9a | 196 | Measurement_struct _measurement; |
amateusz | 11:e0ccc1072c42 | 197 | size_t _valuesReadyCount; |
amateusz | 13:0093792f2325 | 198 | bool _measurementReady; |
amateusz | 0:d58ebb2bed9a | 199 | |
amateusz | 1:6b1a21925a81 | 200 | bool getIdentification(Identification_struct &ident) { |
amateusz | 15:530bf2e09814 | 201 | // _sdi12->sendCommand(std::string(_address) + "I!"); |
amateusz | 1:6b1a21925a81 | 202 | std::string question = "?I!"; |
amateusz | 1:6b1a21925a81 | 203 | question[0] = _address; |
amateusz | 15:530bf2e09814 | 204 | _sdi12->sendCommand(question); |
amateusz | 0:d58ebb2bed9a | 205 | |
amateusz | 1:6b1a21925a81 | 206 | Timer timeout; |
amateusz | 1:6b1a21925a81 | 207 | timeout.start(); |
amateusz | 1:6b1a21925a81 | 208 | while (sdi12.RxBufferAvailable() == 0) { |
amateusz | 1:6b1a21925a81 | 209 | if (sdi12.RxInProgress()) timeout.reset(); |
amateusz | 5:e53104570fde | 210 | if(timeout.read_ms() > RESPONSE_TIMEOUT) { |
amateusz | 1:6b1a21925a81 | 211 | return false; |
amateusz | 1:6b1a21925a81 | 212 | } |
amateusz | 1:6b1a21925a81 | 213 | } |
amateusz | 1:6b1a21925a81 | 214 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 1:6b1a21925a81 | 215 | char buffer[bufferSize+1]; |
amateusz | 1:6b1a21925a81 | 216 | sdi12.getRxBuffer(buffer); |
amateusz | 1:6b1a21925a81 | 217 | buffer[bufferSize] = '\0'; |
amateusz | 17:5cabfc047b40 | 218 | const std::string response(buffer); |
amateusz | 0:d58ebb2bed9a | 219 | |
amateusz | 1:6b1a21925a81 | 220 | if (response[0] == _address) { |
amateusz | 1:6b1a21925a81 | 221 | // e.g.: 113DECAGON GS3 402 |
amateusz | 2:e2db05bc4708 | 222 | |
amateusz | 1:6b1a21925a81 | 223 | ident.sdi_version = std::atoi(response.substr(1, 2).c_str()); |
amateusz | 17:5cabfc047b40 | 224 | const std::string tempStr = response.substr(3, 8); |
amateusz | 1:6b1a21925a81 | 225 | strcpy(ident.company, tempStr.c_str()); |
amateusz | 1:6b1a21925a81 | 226 | |
amateusz | 17:5cabfc047b40 | 227 | strcpy(ident.model, response.substr(3+8, 6).c_str()); |
amateusz | 1:6b1a21925a81 | 228 | |
amateusz | 17:5cabfc047b40 | 229 | strcpy(ident.version, response.substr(3+8+6, 3).c_str()); |
amateusz | 1:6b1a21925a81 | 230 | return true; |
amateusz | 0:d58ebb2bed9a | 231 | } |
amateusz | 1:6b1a21925a81 | 232 | return false; |
amateusz | 1:6b1a21925a81 | 233 | } |
amateusz | 5:e53104570fde | 234 | }; |
amateusz | 5:e53104570fde | 235 | |
amateusz | 5:e53104570fde | 236 | const std::string SDI12_device::ADDRRICES = "0123456789abcdefgijklmnoprstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ"; |