Add ability to check if sensor had an error
This commit is contained in:
parent
f2a8f47958
commit
3a88377f1d
@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ SensorDriver::Sensor::~Sensor()
|
||||
qDebug(ltr("Destroying sensor on serial port %1").arg(m_serialPortName));
|
||||
}
|
||||
|
||||
std::pair<int, int> SensorDriver::Sensor::readRange() const
|
||||
std::pair<int, int> SensorDriver::Sensor::readRange()
|
||||
{
|
||||
qDebug(ltr("Reading range of sensor on port %1").arg(m_serialPortName));
|
||||
|
||||
@ -26,7 +26,7 @@ std::pair<int, int> SensorDriver::Sensor::readRange() const
|
||||
return parseRangeResponse(response);
|
||||
}
|
||||
|
||||
std::vector<int> SensorDriver::Sensor::readValues() const
|
||||
std::vector<int> 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<int>(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;
|
||||
}
|
||||
|
||||
|
@ -12,8 +12,10 @@ class SensorDriver {
|
||||
public:
|
||||
~Sensor();
|
||||
|
||||
std::pair<int, int> readRange() const;
|
||||
std::vector<int> readValues() const;
|
||||
std::pair<int, int> readRange();
|
||||
std::vector<int> 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<int, int> parseRangeResponse(const QByteArray& response) const;
|
||||
|
Loading…
Reference in New Issue
Block a user