diff --git a/AdaptiveBrightness/main.cpp b/AdaptiveBrightness/main.cpp index 37bd397..88e4ded 100644 --- a/AdaptiveBrightness/main.cpp +++ b/AdaptiveBrightness/main.cpp @@ -17,16 +17,21 @@ int main(int argc, char* argv[]) SensorDriver sensorDriver; - const auto sensors = sensorDriver.enumerateSensors(); + auto sensors = sensorDriver.enumerateSensors(); - for(const auto& sensor: sensors) { - const auto sensorRange = sensor.readRange(); - const auto sensorValues = sensor.readValues(); + for(auto& sensor: sensors) { + if(sensor) { + const auto sensorRange = sensor.readRange(); + const auto sensorValues = sensor.readValues(); - qInfo(ltr("Sensor range: [%1,%2]").arg(sensorRange.first).arg(sensorRange.second)); + qInfo(ltr("Sensor range: [%1,%2]").arg(sensorRange.first).arg(sensorRange.second)); - for(size_t i = 0; i < sensorValues.size(); ++i) { - qInfo(ltr("Sensor value %1: %2").arg(i).arg(sensorValues[i])); + for(size_t i = 0; i < sensorValues.size(); ++i) { + qInfo(ltr("Sensor value %1: %2").arg(i).arg(sensorValues[i])); + } + } + else { + qWarning(ltr("Sensor is invalid")); } } diff --git a/AdaptiveBrightness/sensor_driver.cpp b/AdaptiveBrightness/sensor_driver.cpp index 89aa3e0..d534bcb 100644 --- a/AdaptiveBrightness/sensor_driver.cpp +++ b/AdaptiveBrightness/sensor_driver.cpp @@ -14,7 +14,7 @@ SensorDriver::Sensor::~Sensor() qDebug(ltr("Destroying sensor on serial port %1").arg(m_serialPortName)); } -std::pair SensorDriver::Sensor::readRange() const +std::pair SensorDriver::Sensor::readRange() { qDebug(ltr("Reading range of sensor on port %1").arg(m_serialPortName)); @@ -26,7 +26,7 @@ std::pair SensorDriver::Sensor::readRange() const return parseRangeResponse(response); } -std::vector SensorDriver::Sensor::readValues() const +std::vector SensorDriver::Sensor::readValues() { qDebug(ltr("Reading values of sensor on port %1").arg(m_serialPortName)); @@ -43,7 +43,7 @@ SensorDriver::Sensor::Sensor(const QString& serialPortName) : m_serialPortName(s qInfo(ltr("Creating sensor on serial port %1").arg(m_serialPortName)); } -bool SensorDriver::Sensor::isValidSensor() const +bool SensorDriver::Sensor::isValidSensor() { const auto versionCmd = QByteArray(VERSION_CMD.data(), static_cast(VERSION_CMD.size())); @@ -60,12 +60,14 @@ bool SensorDriver::Sensor::isValidSensor() const return version == "AdaptiveBrightness v1.2"; } -bool SensorDriver::Sensor::getSensorCommandResponse(const QString& serialPortName, QByteArray command, QByteArray& response) const +bool SensorDriver::Sensor::getSensorCommandResponse(const QString& serialPortName, QByteArray command, QByteArray& response) { QSerialPort serialPort; serialPort.setPortName(serialPortName); serialPort.setBaudRate(115200); // Not required, STM32 VCP works with any baud rate + m_errorOccurred = true; // Cleared on success + if(!serialPort.open(QIODevice::ReadWrite)) { qCritical(ltr("Unable to open serial port %1, error code: %2").arg(serialPortName).arg(serialPort.errorString())); return false; @@ -110,6 +112,7 @@ bool SensorDriver::Sensor::getSensorCommandResponse(const QString& serialPortNam return false; } + m_errorOccurred = false; return true; } diff --git a/AdaptiveBrightness/sensor_driver.hpp b/AdaptiveBrightness/sensor_driver.hpp index 7eda2b5..b946692 100644 --- a/AdaptiveBrightness/sensor_driver.hpp +++ b/AdaptiveBrightness/sensor_driver.hpp @@ -12,8 +12,10 @@ class SensorDriver { public: ~Sensor(); - std::pair readRange() const; - std::vector readValues() const; + std::pair readRange(); + std::vector readValues(); + + operator bool() const { return !m_errorOccurred; } private: friend SensorDriver; @@ -25,10 +27,11 @@ class SensorDriver { static constexpr auto READ_CMD = std::string_view{"read csv"}; const QString m_serialPortName; + bool m_errorOccurred = false; - bool isValidSensor() const; + bool isValidSensor(); - bool getSensorCommandResponse(const QString& serialPortName, QByteArray command, QByteArray& response) const; + bool getSensorCommandResponse(const QString& serialPortName, QByteArray command, QByteArray& response); std::string parseVersionResponse(const QByteArray& response) const; std::pair parseRangeResponse(const QByteArray& response) const;