Add total number of measurements

This commit is contained in:
Christoph Hagen 2023-06-08 14:58:12 +02:00
parent da3cf8d4fd
commit 7f43cfedf2
3 changed files with 54 additions and 28 deletions

View File

@ -12,7 +12,7 @@ constexpr size_t maximumStorageDurationInHours = (storageSize / TEMPERATURE_SENS
// Max size: 7664 // Max size: 7664
constexpr size_t maxRtcStorageSize = 7664; constexpr size_t maxRtcStorageSize = 7664;
constexpr size_t rtcStorageSize = 7600; constexpr size_t rtcStorageSize = 7550;
constexpr size_t maxEepromSize = 13350; constexpr size_t maxEepromSize = 13350;
constexpr size_t eepromSize = 13350; constexpr size_t eepromSize = 13350;
@ -44,6 +44,8 @@ uint16_t getTotalNumberOfStoredBytes();
uint16_t getNumberOfMeasurements(); uint16_t getNumberOfMeasurements();
uint32_t getTotalNumberOfMeasurements();
uint8_t getLastTemperature(uint8_t sensorIndex); uint8_t getLastTemperature(uint8_t sensorIndex);
uint16_t getTimeSinceValidTemperature(uint8_t sensorIndex); uint16_t getTimeSinceValidTemperature(uint8_t sensorIndex);

View File

@ -235,6 +235,7 @@ uint16_t readNumberFromReceivedBuffer(uint8_t* buffer) {
void fillInfo() { void fillInfo() {
uint16_t value; uint16_t value;
uint32_t value32;
// the number of bytes as a uint16_t (2 bytes) // the number of bytes as a uint16_t (2 bytes)
value = getTotalNumberOfStoredBytes(); value = getTotalNumberOfStoredBytes();
@ -256,35 +257,13 @@ void fillInfo() {
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t); bluetoothDataCount += sizeof(uint16_t);
// the maximum number of bytes that can be copied // Total number of measurements since start
value = bluetoothMaxDataSize; value32 = getTotalNumberOfMeasurements();
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); memcpy(bluetoothDataBuffer + bluetoothDataCount, &value32, sizeof(uint32_t));
bluetoothDataCount += sizeof(uint16_t);
// the number of seconds since power on as a uint32_t (4 bytes)
uint32_t currentTime = time(NULL);
memcpy(bluetoothDataBuffer + bluetoothDataCount, &currentTime, sizeof(uint32_t));
bluetoothDataCount += sizeof(uint32_t); bluetoothDataCount += sizeof(uint32_t);
// The last temperatures // the maximum number of bytes that can be copied
bluetoothDataBuffer[bluetoothDataCount] = getLastTemperature(0); value = bluetoothMaxDataSize;
bluetoothDataCount += sizeof(uint8_t);
bluetoothDataBuffer[bluetoothDataCount] = getLastTemperature(1);
bluetoothDataCount += sizeof(uint8_t);
// Temperature sensor addresses
copySensorAddress(0, bluetoothDataBuffer + bluetoothDataCount);
bluetoothDataCount += TEMPERATURE_SENSOR_ADDRESS_SIZE;
copySensorAddress(1, bluetoothDataBuffer + bluetoothDataCount);
bluetoothDataCount += TEMPERATURE_SENSOR_ADDRESS_SIZE;
// Time since measurement of sensor 0
value = getTimeSinceValidTemperature(0);
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t);
// Time since measurement of sensor 1
value = getTimeSinceValidTemperature(1);
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t); bluetoothDataCount += sizeof(uint16_t);
@ -293,10 +272,43 @@ void fillInfo() {
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t); bluetoothDataCount += sizeof(uint16_t);
// the number of seconds since power on as a uint32_t (4 bytes)
value32 = time(NULL);
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value32, sizeof(uint32_t));
bluetoothDataCount += sizeof(uint32_t);
// Configured device start time // Configured device start time
memcpy(bluetoothDataBuffer + bluetoothDataCount, &deviceStartTime, sizeof(uint32_t)); memcpy(bluetoothDataBuffer + bluetoothDataCount, &deviceStartTime, sizeof(uint32_t));
bluetoothDataCount += sizeof(uint32_t); bluetoothDataCount += sizeof(uint32_t);
// Sensor 0
// Temperature sensor addresses
copySensorAddress(0, bluetoothDataBuffer + bluetoothDataCount);
bluetoothDataCount += TEMPERATURE_SENSOR_ADDRESS_SIZE;
// The last temperatures
bluetoothDataBuffer[bluetoothDataCount] = getLastTemperature(0);
bluetoothDataCount += sizeof(uint8_t);
// Time since measurement of sensor 0
value = getTimeSinceValidTemperature(0);
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t);
// Sensor 1
copySensorAddress(1, bluetoothDataBuffer + bluetoothDataCount);
bluetoothDataCount += TEMPERATURE_SENSOR_ADDRESS_SIZE;
bluetoothDataBuffer[bluetoothDataCount] = getLastTemperature(1);
bluetoothDataCount += sizeof(uint8_t);
// Time since measurement of sensor 1
value = getTimeSinceValidTemperature(1);
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t);
setResponse(BluetoothResponse::success); setResponse(BluetoothResponse::success);
} }
@ -341,9 +353,15 @@ void setDeviceStartTime(uint8_t* buffer, uint16_t count) {
return; return;
} }
memcpy(&deviceStartTime, buffer, sizeof(uint32_t)); memcpy(&deviceStartTime, buffer, sizeof(uint32_t));
Serial.print("Device start time: ");
Serial.println(deviceStartTime);
setResponseWithoutData(BluetoothResponse::success); setResponseWithoutData(BluetoothResponse::success);
} }
1694498815
1686227379
void bluetoothDidReceiveData(uint8_t* buffer, uint16_t count) { void bluetoothDidReceiveData(uint8_t* buffer, uint16_t count) {
if (count < 1) { if (count < 1) {
setResponseWithoutData(BluetoothResponse::invalidCommand); setResponseWithoutData(BluetoothResponse::invalidCommand);

View File

@ -12,6 +12,7 @@ constexpr uint32_t eepromChunkSize = 100;
RTC_DATA_ATTR uint8_t data[rtcStorageSize]; RTC_DATA_ATTR uint8_t data[rtcStorageSize];
RTC_DATA_ATTR uint16_t dataIndex = 0; RTC_DATA_ATTR uint16_t dataIndex = 0;
RTC_DATA_ATTR uint16_t numberOfMeasurements = 0; RTC_DATA_ATTR uint16_t numberOfMeasurements = 0;
RTC_DATA_ATTR uint32_t numberOfDiscardedMeasurements = 0;
// The index into EEPROM storage where the next data should be written // The index into EEPROM storage where the next data should be written
RTC_DATA_ATTR uint32_t eepromIndex = 0; RTC_DATA_ATTR uint32_t eepromIndex = 0;
@ -193,6 +194,10 @@ uint16_t getNumberOfMeasurements() {
return numberOfMeasurements; return numberOfMeasurements;
} }
uint32_t getTotalNumberOfMeasurements() {
return numberOfMeasurements + numberOfDiscardedMeasurements;
}
uint8_t getLastTemperature(uint8_t sensorIndex) { uint8_t getLastTemperature(uint8_t sensorIndex) {
if (lastTemperatures[sensorIndex].status != TemperatureStatus::temperatureIsValid) { if (lastTemperatures[sensorIndex].status != TemperatureStatus::temperatureIsValid) {
return static_cast<uint8_t>(lastTemperatures[sensorIndex].status); return static_cast<uint8_t>(lastTemperatures[sensorIndex].status);
@ -227,6 +232,7 @@ uint16_t getRecordedBytesAtOffset(uint8_t* buffer, uint16_t offset, uint16_t cou
void discardAllRecordedBytes() { void discardAllRecordedBytes() {
eepromIsConsideredEmpty = true; eepromIsConsideredEmpty = true;
dataIndex = 0; dataIndex = 0;
numberOfDiscardedMeasurements += numberOfMeasurements;
numberOfMeasurements = 0; numberOfMeasurements = 0;
resetLastMeasurements(); resetLastMeasurements();
} }