Torc  0.1
torcpwmoutput.cpp
Go to the documentation of this file.
1 /* Class TorcPWMOutput
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2015-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Torc
24 #include "torclogging.h"
25 #include "torcpwmoutput.h"
26 
27 TorcPWMOutput::TorcPWMOutput(double Value, const QString &ModelId, const QVariantMap &Details, uint MaxResolution)
28  : TorcOutput(TorcOutput::PWM, Value, ModelId, Details),
29  m_resolution(MaxResolution),
30  m_maxResolution(MaxResolution)
31 {
32  // disallow inputs
33  if (ModelId.startsWith(DEVICE_CONSTANT))
34  SetOwner(this);
35 
36  // check for user defined resolution
37  // the XSD limits this to 7 to 24bit resolution
38  QVariantMap::const_iterator it = Details.begin();
39  for ( ; it != Details.constEnd(); ++it)
40  {
41  if (it.key() == QStringLiteral("resolution"))
42  {
43  bool ok = false;
44  uint resolution = it.value().toUInt(&ok);
45  if (ok)
46  {
47  if (resolution >= 128 && resolution <= 16777215)
48  {
49  if (resolution > m_maxResolution)
50  {
51  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Requested resolution of %1 for '%2' exceeds maximum - defaulting to %3")
52  .arg(resolution).arg(uniqueId).arg(m_maxResolution));
53  }
54  else
55  {
56  m_resolution = resolution;
57  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Set resolution to %1 for '%2'").arg(m_resolution).arg(uniqueId));
58  }
59  }
60  else
61  {
62  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Requested resolution of %1 for '%2' is out of range - defaulting to %3")
63  .arg(resolution).arg(uniqueId).arg(m_maxResolution));
64  }
65  }
66  else
67  {
68  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Failed to parse resolution for '%1'").arg(uniqueId));
69  }
70  }
71  }
72 }
73 
75 {
76  return QStringList() << tr("Constant PWM");
77 }
78 
80 {
81  return TorcOutput::PWM;
82 }
83 
85 {
86  return m_resolution;
87 }
88 
90 {
91  return m_maxResolution;
92 }
93 
94 bool TorcPWMOutput::ValueIsDifferent(double &NewValue)
95 {
96  // range check
97  double newvalue = qBound(0.0, NewValue, 1.0);
98 
99  // resolution check
100  double threshhold = 1.0 / (double)m_resolution;
101  // ensure we get maximum resolution at the extremes
102  if (newvalue <= threshhold || newvalue >= (1 - threshhold))
103  threshhold = 0;
104 
105  if (qAbs(value - newvalue) < threshhold)
106  return false;
107 
108  // set new value in case range check caught out of bounds
109  NewValue = newvalue;
110  return true;
111 }
QString uniqueId
Definition: torcdevice.h:63
TorcOutput::Type GetType(void) override
uint GetMaxResolution(void)
QStringList GetDescription(void) override
#define DEVICE_CONSTANT
Definition: torcdevice.h:17
bool ValueIsDifferent(double &NewValue)
uint GetResolution(void)
double value
Definition: torcdevice.h:60
TorcPWMOutput(double Value, const QString &ModelId, const QVariantMap &Details, uint MaxResolution=DEFAULT_PWM_RESOLUTION)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
bool SetOwner(QObject *Owner)
Definition: torcoutput.cpp:66
uint m_maxResolution
Definition: torcpwmoutput.h:31