Torc  0.1
torcpiswitchoutput.cpp
Go to the documentation of this file.
1 /* Class TorcPiSwitchOutput
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 "torcpigpio.h"
26 #include "torcpiswitchoutput.h"
27 
28 // wiringPi
29 #include "wiringPi.h"
30 
31 #define DEFAULT_VALUE 0
32 
33 TorcPiSwitchOutput::TorcPiSwitchOutput(int Pin, const QVariantMap &Details)
34  : TorcSwitchOutput(DEFAULT_VALUE, QStringLiteral("PiGPIOSwitchOutput"), Details),
35  m_pin(Pin)
36 {
37  // setup and initialise pin for output
38  pinMode(m_pin, OUTPUT);
39  digitalWrite(m_pin, DEFAULT_VALUE);
40 }
41 
43 {
44  // always return the pin to its default state.
45  // we must do this here to trigger the correct SetValue implementation
47 }
48 
50 {
51  return QStringList() << tr("Pin %1 Switch").arg(m_pin);
52 }
53 
54 void TorcPiSwitchOutput::SetValue(double Value)
55 {
56  QMutexLocker locker(&lock);
57 
58  // as in TorcSwitchOutput::SetValue
59  double newvalue = Value == 0.0 ? 0 : 1;
60 
61  // ignore same value updates
62  if (qFuzzyCompare(newvalue + 1.0f, value + 1.0f))
63  return;
64 
65  digitalWrite(m_pin, newvalue);
67 }
virtual void SetValue(double Value) override
TorcPiSwitchOutput(int Pin, const QVariantMap &Details)
QStringList GetDescription(void)
double defaultValue
Definition: torcdevice.h:61
#define DEFAULT_VALUE
void SetValue(double Value)
QMutex lock
Definition: torcdevice.h:66
double value
Definition: torcdevice.h:60