2021-08-09 15:04:49 -04:00
|
|
|
/*
|
|
|
|
sensor.cpp
|
|
|
|
|
|
|
|
Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sensor.h"
|
2021-08-10 00:56:43 -04:00
|
|
|
#include "rdm.h"
|
2021-08-09 15:04:49 -04:00
|
|
|
|
|
|
|
namespace RDM {
|
|
|
|
|
2021-08-10 00:56:43 -04:00
|
|
|
/**
|
|
|
|
* @brief Sensor::Sensor
|
|
|
|
*/
|
2021-08-09 15:04:49 -04:00
|
|
|
Sensor::Sensor()
|
2021-08-10 00:56:43 -04:00
|
|
|
: type(SENS_OTHER)
|
|
|
|
, unit(UNITS_NONE)
|
|
|
|
, unitPrefix(PREFIX_NONE)
|
|
|
|
, minLimit(-32768)
|
|
|
|
, maxLimit(32767)
|
|
|
|
, minNominal(-32768)
|
|
|
|
, maxNominal(32767)
|
|
|
|
, recordedValue(true)
|
|
|
|
, minMaxDetectedValue(true)
|
|
|
|
, reserved(0)
|
|
|
|
, description("Null Sensor")
|
|
|
|
, min_(32767)
|
|
|
|
, max_(-32768)
|
|
|
|
, memory_(0)
|
|
|
|
, value_(0)
|
2021-08-09 15:04:49 -04:00
|
|
|
{
|
2021-08-10 00:56:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sensor::setValue
|
|
|
|
* @param val
|
|
|
|
*/
|
|
|
|
void Sensor::setValue(int16_t &val)
|
|
|
|
{
|
|
|
|
if (val < min_)
|
|
|
|
min_ = val;
|
|
|
|
if (val > max_)
|
|
|
|
max_ = val;
|
|
|
|
value_ = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief Sensor::save
|
|
|
|
*/
|
|
|
|
void Sensor::save()
|
|
|
|
{
|
|
|
|
memory_ = value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-10 00:56:43 -04:00
|
|
|
/**
|
|
|
|
* @brief Sensor::minimum
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
int16_t Sensor::minimum() const
|
|
|
|
{
|
|
|
|
if (!minMaxDetectedValue)
|
|
|
|
return 0;
|
|
|
|
return min_;
|
|
|
|
}
|
|
|
|
|
2021-08-09 15:04:49 -04:00
|
|
|
|
2021-08-10 00:56:43 -04:00
|
|
|
/**
|
|
|
|
* @brief Sensor::maximum
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
int16_t Sensor::maximum() const
|
|
|
|
{
|
|
|
|
if (!minMaxDetectedValue)
|
|
|
|
return 0;
|
|
|
|
return max_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief Sensor::value
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
int16_t Sensor::value() const
|
|
|
|
{
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-10 00:56:43 -04:00
|
|
|
/**
|
|
|
|
* @brief Sensor::saved
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
int16_t Sensor::saved() const
|
|
|
|
{
|
|
|
|
if (!recordedValue)
|
|
|
|
return 0;
|
|
|
|
return memory_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sensor::resetMinMax
|
|
|
|
*/
|
|
|
|
void Sensor::clearMemory()
|
|
|
|
{
|
|
|
|
min_ = value_;
|
|
|
|
max_ = value_;
|
|
|
|
memory_ = value_;
|
2021-08-09 15:04:49 -04:00
|
|
|
}
|
|
|
|
|
2021-08-10 14:50:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sensor::actionGetSensorDefinition
|
2021-08-25 17:20:33 -04:00
|
|
|
* @param index
|
2021-08-10 14:50:53 -04:00
|
|
|
* @param response
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
void Sensor::actionGetSensorDefinition(uint8_t index, MsgPtr response)
|
2021-08-10 14:50:53 -04:00
|
|
|
{
|
|
|
|
response->responseType = RESPONSE_TYPE_ACK;
|
2023-04-28 20:46:13 -04:00
|
|
|
response->appendParameterData(index);
|
|
|
|
response->appendParameterData(type);
|
|
|
|
response->appendParameterData(unit);
|
|
|
|
response->appendParameterData(unitPrefix);
|
|
|
|
response->appendParameterData(minLimit);
|
|
|
|
response->appendParameterData(maxLimit);
|
|
|
|
response->appendParameterData(minNominal);
|
|
|
|
response->appendParameterData(maxNominal);
|
|
|
|
response->appendParameterData(recordedValueSupport);
|
2021-08-10 14:50:53 -04:00
|
|
|
for (size_t i = 0; i < description.size(); i++)
|
|
|
|
{
|
|
|
|
if (i > 32)
|
|
|
|
break;
|
2023-04-28 20:46:13 -04:00
|
|
|
response->appendParameterData(description.at(i));
|
2021-08-10 14:50:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sensor::actionGetSensorValue
|
2021-08-25 17:20:33 -04:00
|
|
|
* @param index
|
2021-08-10 14:50:53 -04:00
|
|
|
* @param response
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
void Sensor::actionGetSensorValue(uint8_t index, MsgPtr response)
|
2021-08-10 14:50:53 -04:00
|
|
|
{
|
|
|
|
response->responseType = RESPONSE_TYPE_ACK;
|
2023-04-28 20:46:13 -04:00
|
|
|
response->appendParameterData(index);
|
|
|
|
response->appendParameterData(value());
|
|
|
|
response->appendParameterData(minimum());
|
|
|
|
response->appendParameterData(maximum());
|
|
|
|
response->appendParameterData(saved());
|
2021-08-10 14:50:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sensor::actionSetSensorValue
|
2021-08-25 17:20:33 -04:00
|
|
|
* @param index
|
2021-08-10 14:50:53 -04:00
|
|
|
* @param response
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
void Sensor::actionSetSensorValue(uint8_t index, MsgPtr response)
|
2021-08-10 14:50:53 -04:00
|
|
|
{
|
|
|
|
clearMemory();
|
|
|
|
|
2023-04-28 20:46:13 -04:00
|
|
|
if (response->mdb.pdl() != 0)
|
2021-08-10 14:50:53 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
int16_t val=0, min=0, max=0, mem=0;
|
|
|
|
if (index != 0xff)
|
|
|
|
{
|
|
|
|
val = value();
|
|
|
|
min = minimum();
|
|
|
|
max = maximum();
|
|
|
|
mem = saved();
|
|
|
|
}
|
|
|
|
|
|
|
|
response->responseType = RESPONSE_TYPE_ACK;
|
2023-04-28 20:46:13 -04:00
|
|
|
response->appendParameterData(index);
|
|
|
|
response->appendParameterData(val);
|
|
|
|
response->appendParameterData(min);
|
|
|
|
response->appendParameterData(max);
|
|
|
|
response->appendParameterData(mem);
|
2021-08-10 14:50:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sensor::actionSetRecordSensors
|
|
|
|
* @param response
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
void Sensor::actionSetRecordSensors(MsgPtr response)
|
2021-08-10 14:50:53 -04:00
|
|
|
{
|
|
|
|
save();
|
|
|
|
response->responseType = RESPONSE_TYPE_ACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-09 15:04:49 -04:00
|
|
|
} // namespace RDM
|