OpenLCP/protocol/esta/analog/receiver.h
2023-03-30 20:15:35 -04:00

82 lines
2.9 KiB
C++

/*
receiver.h
Copyright (c) 2023 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.
*/
#pragma once
#include <cstdint>
namespace ANALOG {
/**
* @brief A single-channel 0-10V receiver.
*/
struct Receiver
{
/**
* \cite ANALOG 6.2.1 Amplitude (Receiver)
* The dimmer or other receiving device shall be at “zero” (its specified minimum state,
* position, speed, etc.) with any control signal below 0.3 volts. The dimmer or other
* receiving device shall be at “full” (its specified maximum state, position, speed, etc.)
* with any control signal above 9.8 volts.
*/
static const int16_t zero = 300; //!< upper "Zero" threshold as mV, 0.3 V
static const int16_t full = 9800; //!< lower "Full" threshold as mV, 9.8 V
/**
* @brief Convert millivolts to an 8-bit level.
* @param mV
* @return \cite ANALOG 5.4 Scale: control is intended to be linear
*/
uint8_t level(const int16_t mV) const
{
if (mV <= zero)
return 0;
if ( mV >= full)
return UINT8_MAX;
return UINT8_MAX * ((mV - zero) / (full - zero));
}
/**
* @brief Determine the state of a non-dim switch.
* @param mV
* @param cur If incorporating hysteresis, the current state of the switch.
* @return
*
* \cite ANALOG It is suggested that receiving devices that switch between "off" and "on," such
* as relay packs, should consider incorporating hysteresis in switching between "off" and "on."
*/
bool state(const int16_t mV, const bool cur = false) const
{
/**
* \cite ANALOG In this case a device in the "off" state would not switch on until the
* control voltage exceeds 6 volts. A device in the "on" state would not switch off until
* the control voltage drops below 4 volts.
*/
if (!cur)
return mV > 6000; // 6V
return !(mV < 4000); // 4V
}
};
} // namespace ANALOG