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@14:88e212b7db59, 2018-08-05 (annotated)
- Committer:
- amateusz
- Date:
- Sun Aug 05 18:25:03 2018 +0000
- Revision:
- 14:88e212b7db59
- Parent:
- 13:0093792f2325
clean up
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 | 4:11438803516c | 20 | float values[48]; // 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 | 14:88e212b7db59 | 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 | 0:d58ebb2bed9a | 51 | |
amateusz | 14:88e212b7db59 | 52 | SDI12_device (SDI12 &inst, char address, EventQueue &mainDispatcher) : _sdi12(inst), parentQueue(mainDispatcher), sensorQueue(4*EVENTS_QUEUE_SIZE) { |
amateusz | 0:d58ebb2bed9a | 53 | setAddress(address); |
amateusz | 2:e2db05bc4708 | 54 | sensorQueue.chain(&mainDispatcher); |
amateusz | 1:6b1a21925a81 | 55 | if(getIdentification(_ident_struct)); |
amateusz | 0:d58ebb2bed9a | 56 | }; |
amateusz | 14:88e212b7db59 | 57 | |
amateusz | 14:88e212b7db59 | 58 | |
amateusz | 14:88e212b7db59 | 59 | SDI12_device & operator= (const SDI12_device & old) { |
amateusz | 14:88e212b7db59 | 60 | _address = old._address; |
amateusz | 14:88e212b7db59 | 61 | _ident_struct = old._ident_struct; |
amateusz | 14:88e212b7db59 | 62 | // _sdi12 = old._sdi12; // non-copyable -.- |
amateusz | 14:88e212b7db59 | 63 | |
amateusz | 14:88e212b7db59 | 64 | _measurementEventId = old._measurementEventId; |
amateusz | 14:88e212b7db59 | 65 | _measurement = old._measurement; |
amateusz | 14:88e212b7db59 | 66 | _valuesReadyCount = old._valuesReadyCount; |
amateusz | 14:88e212b7db59 | 67 | _measurementReady = old._measurementReady; |
amateusz | 14:88e212b7db59 | 68 | |
amateusz | 14:88e212b7db59 | 69 | parentQueue = old.parentQueue; |
amateusz | 14:88e212b7db59 | 70 | sensorQueue = old.sensorQueue; |
amateusz | 14:88e212b7db59 | 71 | } |
amateusz | 14:88e212b7db59 | 72 | |
amateusz | 10:efd1e313fe8c | 73 | ~SDI12_device () { |
amateusz | 10:efd1e313fe8c | 74 | // unchain event queue |
amateusz | 9:a3062b9e4324 | 75 | // sensorQueue.cancel(_measurementEventId); |
amateusz | 10:efd1e313fe8c | 76 | sensorQueue.chain(NULL); |
amateusz | 10:efd1e313fe8c | 77 | } |
amateusz | 0:d58ebb2bed9a | 78 | |
amateusz | 2:e2db05bc4708 | 79 | |
amateusz | 14:88e212b7db59 | 80 | // concurrent means that device under measurement won't signal its prempt readiness to the bus |
amateusz | 13:0093792f2325 | 81 | int measure(bool concurrent = true, char group = '0') { |
amateusz | 4:11438803516c | 82 | |
amateusz | 2:e2db05bc4708 | 83 | std::string question = string("?") + string(concurrent?"C":"M") + string("!"); |
amateusz | 2:e2db05bc4708 | 84 | question[0] = _address; |
amateusz | 13:0093792f2325 | 85 | if (group != '0') |
amateusz | 13:0093792f2325 | 86 | question.insert(2, 1, group); // e.g. 2M! -> 2M1! |
amateusz | 2:e2db05bc4708 | 87 | _sdi12.sendCommand(question); |
amateusz | 2:e2db05bc4708 | 88 | |
amateusz | 2:e2db05bc4708 | 89 | Timer timeout; |
amateusz | 2:e2db05bc4708 | 90 | timeout.start(); |
amateusz | 2:e2db05bc4708 | 91 | while (sdi12.RxBufferAvailable() == 0) { |
amateusz | 2:e2db05bc4708 | 92 | if (sdi12.RxInProgress()) timeout.reset(); |
amateusz | 5:e53104570fde | 93 | if(timeout.read_ms() > RESPONSE_TIMEOUT) { |
amateusz | 2:e2db05bc4708 | 94 | return false; |
amateusz | 2:e2db05bc4708 | 95 | } |
amateusz | 2:e2db05bc4708 | 96 | } |
amateusz | 10:efd1e313fe8c | 97 | |
amateusz | 2:e2db05bc4708 | 98 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 2:e2db05bc4708 | 99 | char buffer[bufferSize+1]; |
amateusz | 2:e2db05bc4708 | 100 | sdi12.getRxBuffer(buffer); |
amateusz | 2:e2db05bc4708 | 101 | buffer[bufferSize] = '\0'; |
amateusz | 2:e2db05bc4708 | 102 | std::string response(buffer); |
amateusz | 2:e2db05bc4708 | 103 | |
amateusz | 2:e2db05bc4708 | 104 | if (response[0] == _address) { |
amateusz | 2:e2db05bc4708 | 105 | char measurementsCount = std::atoi(response.substr(1+3,(concurrent?2:1)).c_str()); |
amateusz | 2:e2db05bc4708 | 106 | char waitFor = std::atoi(response.substr(1,3).c_str()); |
amateusz | 2:e2db05bc4708 | 107 | _measurement.count = measurementsCount; |
amateusz | 4:11438803516c | 108 | _measurement.readyIn = waitFor; |
amateusz | 2:e2db05bc4708 | 109 | // _measurement.timestampMeasurementReadyAt |
amateusz | 2:e2db05bc4708 | 110 | debug("wait for %d measurement for %d seconds...", _measurement.count, waitFor); |
amateusz | 11:e0ccc1072c42 | 111 | _valuesReadyCount = 0; |
amateusz | 13:0093792f2325 | 112 | _measurementReady = false; |
amateusz | 13:0093792f2325 | 113 | _measurementEventId = sensorQueue.call_in(waitFor * 1000 + 500, callback(this, &SDI12_device::read), '0'); // + some [ms] for good measure, not to be too early |
amateusz | 9:a3062b9e4324 | 114 | // debug("measurement scheduled"); |
amateusz | 4:11438803516c | 115 | // if (_measurement.values != NULL) delete[] _measurement.values; |
amateusz | 4:11438803516c | 116 | // _measurement.values = new float[measurementsCount]; |
amateusz | 2:e2db05bc4708 | 117 | return 0; |
amateusz | 2:e2db05bc4708 | 118 | } |
amateusz | 2:e2db05bc4708 | 119 | return -1; |
amateusz | 2:e2db05bc4708 | 120 | }; |
amateusz | 13:0093792f2325 | 121 | |
amateusz | 4:11438803516c | 122 | char readyIn() { |
amateusz | 4:11438803516c | 123 | return _measurement.readyIn; |
amateusz | 4:11438803516c | 124 | } |
amateusz | 4:11438803516c | 125 | |
amateusz | 13:0093792f2325 | 126 | bool isReady() { |
amateusz | 13:0093792f2325 | 127 | return _measurementReady; |
amateusz | 13:0093792f2325 | 128 | } |
amateusz | 13:0093792f2325 | 129 | |
amateusz | 11:e0ccc1072c42 | 130 | bool read(char group = '0') { |
amateusz | 11:e0ccc1072c42 | 131 | // Measurement (M), Continuous (R), and Concurrent (C) commands and subsequent Data (D) |
amateusz | 11:e0ccc1072c42 | 132 | std::string question = std::string("?") + std::string("D") + std::string(1,group)+ std::string("!"); |
amateusz | 2:e2db05bc4708 | 133 | question[0] = _address; |
amateusz | 2:e2db05bc4708 | 134 | _sdi12.sendCommand(question); |
amateusz | 2:e2db05bc4708 | 135 | |
amateusz | 2:e2db05bc4708 | 136 | Timer timeout; |
amateusz | 2:e2db05bc4708 | 137 | timeout.start(); |
amateusz | 2:e2db05bc4708 | 138 | while (sdi12.RxBufferAvailable() == 0) { |
amateusz | 2:e2db05bc4708 | 139 | if (sdi12.RxInProgress()) timeout.reset(); |
amateusz | 5:e53104570fde | 140 | if(timeout.read_ms() > RESPONSE_TIMEOUT) { |
amateusz | 2:e2db05bc4708 | 141 | return false; |
amateusz | 2:e2db05bc4708 | 142 | } |
amateusz | 2:e2db05bc4708 | 143 | } |
amateusz | 2:e2db05bc4708 | 144 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 2:e2db05bc4708 | 145 | char buffer[bufferSize+1]; |
amateusz | 2:e2db05bc4708 | 146 | sdi12.getRxBuffer(buffer); |
amateusz | 2:e2db05bc4708 | 147 | buffer[bufferSize] = '\0'; |
amateusz | 2:e2db05bc4708 | 148 | std::string response(buffer); |
amateusz | 2:e2db05bc4708 | 149 | |
amateusz | 4:11438803516c | 150 | // debug("parser. recv: %s\r\n", response); |
amateusz | 3:2847f7c543d3 | 151 | if (response[0] == _address) { |
amateusz | 3:2847f7c543d3 | 152 | // parser here |
amateusz | 3:2847f7c543d3 | 153 | response = response.substr(1); // to limit repeting this operation later. i.e. extract only values e.g. +21.3-123+123 |
amateusz | 3:2847f7c543d3 | 154 | // the only two possible delimeters of a value |
amateusz | 3:2847f7c543d3 | 155 | size_t next_start_index = response.find_first_of("-+", 0); |
amateusz | 3:2847f7c543d3 | 156 | size_t next_end_index; |
amateusz | 3:2847f7c543d3 | 157 | // ready, steady, parse!! |
amateusz | 3:2847f7c543d3 | 158 | while(next_start_index != std::string::npos) { |
amateusz | 3:2847f7c543d3 | 159 | // determine start index: |
amateusz | 3:2847f7c543d3 | 160 | next_end_index = response.find_first_of("-+", next_start_index + 1); // std::substr is prepared to take std::npos |
amateusz | 3:2847f7c543d3 | 161 | float value = std::atof(response.substr(next_start_index, next_end_index).c_str()); |
amateusz | 4:11438803516c | 162 | // debug("parsed: %f\r\n", value); |
amateusz | 11:e0ccc1072c42 | 163 | _measurement.values[_valuesReadyCount++] = value; |
amateusz | 3:2847f7c543d3 | 164 | next_start_index = response.find_first_of("-+", next_end_index); |
amateusz | 3:2847f7c543d3 | 165 | } |
amateusz | 11:e0ccc1072c42 | 166 | // if current parsing doesn't return all the expexted measurements, then press harder and poll further Dx commands. RECURSION HERE *.* |
amateusz | 11:e0ccc1072c42 | 167 | if (_valuesReadyCount < _measurement.count) |
amateusz | 11:e0ccc1072c42 | 168 | read(group + 1); |
amateusz | 13:0093792f2325 | 169 | else { |
amateusz | 13:0093792f2325 | 170 | _measurementReady = true; |
amateusz | 13:0093792f2325 | 171 | } |
amateusz | 3:2847f7c543d3 | 172 | } |
amateusz | 4:11438803516c | 173 | return true; |
amateusz | 2:e2db05bc4708 | 174 | } |
amateusz | 4:11438803516c | 175 | |
amateusz | 4:11438803516c | 176 | |
amateusz | 0:d58ebb2bed9a | 177 | // |
amateusz | 4:11438803516c | 178 | const unsigned char getMeasurementCount() { |
amateusz | 2:e2db05bc4708 | 179 | return _measurement.count; |
amateusz | 2:e2db05bc4708 | 180 | } |
amateusz | 2:e2db05bc4708 | 181 | |
amateusz | 4:11438803516c | 182 | const float getMeasurementValue(char which) { |
amateusz | 13:0093792f2325 | 183 | _measurementReady = false; // reading /any/ of the values resets this |
amateusz | 2:e2db05bc4708 | 184 | return _measurement.values[which]; |
amateusz | 2:e2db05bc4708 | 185 | } |
amateusz | 2:e2db05bc4708 | 186 | |
amateusz | 0:d58ebb2bed9a | 187 | void setAddress(char address) { |
amateusz | 0:d58ebb2bed9a | 188 | _address = address; |
amateusz | 0:d58ebb2bed9a | 189 | }; |
amateusz | 0:d58ebb2bed9a | 190 | // |
amateusz | 0:d58ebb2bed9a | 191 | // |
amateusz | 0:d58ebb2bed9a | 192 | private: |
amateusz | 13:0093792f2325 | 193 | static const int RESPONSE_TIMEOUT = 15+1; // device should start to respond within [ms] |
amateusz | 5:e53104570fde | 194 | static const std::string ADDRRICES; // as in index -> indices, |
amateusz | 5:e53104570fde | 195 | |
amateusz | 0:d58ebb2bed9a | 196 | SDI12 &_sdi12; |
amateusz | 0:d58ebb2bed9a | 197 | char _address; |
amateusz | 5:e53104570fde | 198 | int _measurementEventId; |
amateusz | 0:d58ebb2bed9a | 199 | Measurement_struct _measurement; |
amateusz | 11:e0ccc1072c42 | 200 | size_t _valuesReadyCount; |
amateusz | 13:0093792f2325 | 201 | bool _measurementReady; |
amateusz | 0:d58ebb2bed9a | 202 | |
amateusz | 14:88e212b7db59 | 203 | EventQueue &parentQueue; |
amateusz | 2:e2db05bc4708 | 204 | EventQueue sensorQueue; |
amateusz | 2:e2db05bc4708 | 205 | |
amateusz | 1:6b1a21925a81 | 206 | bool getIdentification(Identification_struct &ident) { |
amateusz | 1:6b1a21925a81 | 207 | // _sdi12.sendCommand(std::string(_address) + "I!"); |
amateusz | 1:6b1a21925a81 | 208 | std::string question = "?I!"; |
amateusz | 1:6b1a21925a81 | 209 | question[0] = _address; |
amateusz | 1:6b1a21925a81 | 210 | _sdi12.sendCommand(question); |
amateusz | 0:d58ebb2bed9a | 211 | |
amateusz | 1:6b1a21925a81 | 212 | Timer timeout; |
amateusz | 1:6b1a21925a81 | 213 | timeout.start(); |
amateusz | 1:6b1a21925a81 | 214 | while (sdi12.RxBufferAvailable() == 0) { |
amateusz | 1:6b1a21925a81 | 215 | if (sdi12.RxInProgress()) timeout.reset(); |
amateusz | 5:e53104570fde | 216 | if(timeout.read_ms() > RESPONSE_TIMEOUT) { |
amateusz | 1:6b1a21925a81 | 217 | return false; |
amateusz | 1:6b1a21925a81 | 218 | } |
amateusz | 1:6b1a21925a81 | 219 | } |
amateusz | 1:6b1a21925a81 | 220 | unsigned char bufferSize = sdi12.RxBufferAvailable(); |
amateusz | 1:6b1a21925a81 | 221 | char buffer[bufferSize+1]; |
amateusz | 1:6b1a21925a81 | 222 | sdi12.getRxBuffer(buffer); |
amateusz | 1:6b1a21925a81 | 223 | buffer[bufferSize] = '\0'; |
amateusz | 1:6b1a21925a81 | 224 | std::string response(buffer); |
amateusz | 0:d58ebb2bed9a | 225 | |
amateusz | 1:6b1a21925a81 | 226 | if (response[0] == _address) { |
amateusz | 1:6b1a21925a81 | 227 | // e.g.: 113DECAGON GS3 402 |
amateusz | 2:e2db05bc4708 | 228 | |
amateusz | 1:6b1a21925a81 | 229 | ident.sdi_version = std::atoi(response.substr(1, 2).c_str()); |
amateusz | 1:6b1a21925a81 | 230 | std::string tempStr = response.substr(3, 8); |
amateusz | 1:6b1a21925a81 | 231 | strcpy(ident.company, tempStr.c_str()); |
amateusz | 1:6b1a21925a81 | 232 | |
amateusz | 1:6b1a21925a81 | 233 | tempStr = response.substr(3+8, 6); |
amateusz | 1:6b1a21925a81 | 234 | strcpy(ident.model, tempStr.c_str()); |
amateusz | 1:6b1a21925a81 | 235 | |
amateusz | 1:6b1a21925a81 | 236 | tempStr = response.substr(3+8+6, 3); |
amateusz | 1:6b1a21925a81 | 237 | strcpy(ident.version, tempStr.c_str()); |
amateusz | 1:6b1a21925a81 | 238 | return true; |
amateusz | 0:d58ebb2bed9a | 239 | } |
amateusz | 1:6b1a21925a81 | 240 | return false; |
amateusz | 1:6b1a21925a81 | 241 | } |
amateusz | 0:d58ebb2bed9a | 242 | |
amateusz | 5:e53104570fde | 243 | }; |
amateusz | 5:e53104570fde | 244 | |
amateusz | 5:e53104570fde | 245 | const std::string SDI12_device::ADDRRICES = "0123456789abcdefgijklmnoprstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ"; |