Represents a single device. Take measurements, send commands. Needs SDI12 bus instance as argument. Allows to detect devices on the bus
Diff: SDI12_device.h
- Revision:
- 4:11438803516c
- Parent:
- 3:2847f7c543d3
- Child:
- 5:e53104570fde
--- a/SDI12_device.h Fri Jul 27 17:24:38 2018 +0000 +++ b/SDI12_device.h Mon Jul 30 13:48:52 2018 +0000 @@ -14,9 +14,10 @@ struct Measurement_struct { unsigned long timestampMeasurementReadyAt; + char readyIn; bool _measurementAlreadyRead; char count; - float *values; // not float values [] !! :O + float values[48]; // not float values [] !! :O }; Identification_struct _ident_struct; @@ -65,6 +66,7 @@ // // returns after how many seconds a measurement will be ready int measure(bool concurrent = true) { + std::string question = string("?") + string(concurrent?"C":"M") + string("!"); question[0] = _address; _sdi12.sendCommand(question); @@ -87,11 +89,12 @@ char measurementsCount = std::atoi(response.substr(1+3,(concurrent?2:1)).c_str()); char waitFor = std::atoi(response.substr(1,3).c_str()); _measurement.count = measurementsCount; + _measurement.readyIn = waitFor; // _measurement.timestampMeasurementReadyAt debug("wait for %d measurement for %d seconds...", _measurement.count, waitFor); sensorQueue.call_in(waitFor * 1000, callback(this, &SDI12_device::read)); - if (_measurement.values != NULL) delete[] _measurement.values; - _measurement.values = new float[measurementsCount]; +// if (_measurement.values != NULL) delete[] _measurement.values; +// _measurement.values = new float[measurementsCount]; return 0; } return -1; @@ -109,6 +112,10 @@ // } // }; // + char readyIn() { + return _measurement.readyIn; + } + bool read() { char valuesAlreadyRead = 0; std::string question = string("?") + string("D0") + string("!"); @@ -129,9 +136,8 @@ buffer[bufferSize] = '\0'; std::string response(buffer); - debug("parser. recv: %s\r\n", response); +// debug("parser. recv: %s\r\n", response); if (response[0] == _address) { - debug("parser"); // parser here response = response.substr(1); // to limit repeting this operation later. i.e. extract only values e.g. +21.3-123+123 // the only two possible delimeters of a value @@ -139,26 +145,24 @@ size_t next_end_index; // ready, steady, parse!! while(next_start_index != std::string::npos) { - debug("_entre\r\n"); - osDelay(1000); // determine start index: next_end_index = response.find_first_of("-+", next_start_index + 1); // std::substr is prepared to take std::npos float value = std::atof(response.substr(next_start_index, next_end_index).c_str()); - debug("_parsed: %f\r\n", value); -// _measurement.values[valuesAlreadyRead++] = value; - - debug("(start %d) (end %d)", next_start_index, next_end_index); +// debug("parsed: %f\r\n", value); + _measurement.values[valuesAlreadyRead++] = value; next_start_index = response.find_first_of("-+", next_end_index); - debug("(new start %d) [hard stop: %d]\r\n", next_start_index, std::string::npos); } } + return true; } + + // - unsigned char getMeasurementCount() { + const unsigned char getMeasurementCount() { return _measurement.count; } - float getMeasurementValue(char which) { + const float getMeasurementValue(char which) { return _measurement.values[which]; }