Torc  0.1
torcnetworkbuttoninput.cpp
Go to the documentation of this file.
1 /* Class TorcNetworkButtonInput
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2016-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 "torcnetworkbuttoninput.h"
25 
40 TorcNetworkButtonInput::TorcNetworkButtonInput(double Default, const QVariantMap &Details)
41  : TorcNetworkSwitchInput(Default, Details),
42  m_pulseTimer()
43 {
44  // timers cannot be started from other threads, so use some signalling
45  connect(this, &TorcNetworkButtonInput::Pushed, this, &TorcNetworkButtonInput::StartTimer);
46 
47  // setup the timer
48  m_pulseTimer.setSingleShot(true);
49  m_pulseTimer.setInterval(5);
50  connect(&m_pulseTimer, &QTimer::timeout, this, &TorcNetworkButtonInput::EndPulse);
51 }
52 
54 {
55  return QStringList() << tr("Network button");
56 }
57 
59 {
60  SetValid(true);
63 }
64 
66 {
67  return TorcInput::Button;
68 }
69 
77 {
78  (void)Value;
79  QMutexLocker locker(&lock);
80 
81  // don't re-trigger until current pulse is finished
82  if (m_pulseTimer.isActive())
83  return;
84 
85  // only trigger on a 'push' - not release
86  if (qFuzzyCompare(Value + 1.0, 1.0))
87  return;
88 
89  TorcNetworkSwitchInput::SetValue(value < 1.0 ? 1.0 : 0.0);
90  emit Pushed();
91 }
92 
93 void TorcNetworkButtonInput::StartTimer(void)
94 {
95  if (!m_pulseTimer.isActive())
96  m_pulseTimer.start();
97 }
98 
99 void TorcNetworkButtonInput::EndPulse(void)
100 {
101  QMutexLocker locker(&lock);
102 
103  TorcSwitchInput::SetValue(value < 1.0 ? 1.0 : 0.0);
104 }
105 
TorcNetworkButtonInput(double Default, const QVariantMap &Details)
void SetValid(bool Valid) final
Definition: torcinput.cpp:143
TorcInput::Type GetType(void) override
double defaultValue
Definition: torcdevice.h:61
void SetValue(double Value) override
Update the inputs value.
Definition: torcinput.cpp:90
QMutex lock
Definition: torcdevice.h:66
double value
Definition: torcdevice.h:60
void SetValue(double Value) final
Toggle the value of the button.
QStringList GetDescription(void) override