Torc  0.1
torcpipwmoutput.cpp
Go to the documentation of this file.
1 /* Class TorcPiPWMOutput
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 "torclogging.h"
25 #include "torcpigpio.h"
26 #include "torcpipwmoutput.h"
27 
28 // wiringPi
29 #include "wiringPi.h"
30 #include "softPwm.h"
31 
32 // for lround
33 #include <math.h>
34 
35 #define DEFAULT_VALUE 0
36 #define PI_PWM_RESOLUTION 1024
37 
46 TorcPiPWMOutput::TorcPiPWMOutput(int Pin, const QVariantMap &Details)
47  : TorcPWMOutput(DEFAULT_VALUE, QStringLiteral("PiGPIOPWMOutput"), Details, PI_PWM_RESOLUTION),
48  m_pin(Pin)
49 {
50  // hardware PWM only operates at default 10bit accuracy
51  if ((m_resolution != m_maxResolution) && (m_pin == TORC_HWPWM_PIN))
52  {
54  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("Ignoring user defined resolution for hardware PWM - defaulting to %1").arg(m_maxResolution));
55  }
56 
57  int value = lround(DEFAULT_VALUE * (double)m_resolution);
58 
59  if (m_pin == TORC_HWPWM_PIN)
60  {
61  pinMode(m_pin, PWM_OUTPUT);
62  pwmWrite(m_pin, value);
63  }
64  else
65  {
66  if (softPwmCreate(m_pin, value, m_resolution) == 0)
67  {
68  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("Using software PWM on pin %1: It MIGHT flicker...").arg(m_pin));
69  }
70  else
71  {
72  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Failed to setup software PWM on pin %1").arg(m_pin));
73  }
74  }
75 }
76 
78 {
79  // always return the pin to its default state.
80  // we must do this here to trigger the correct SetValue implementation
82 
83  // shutdown the soft pwm thread
84  if (m_pin != TORC_HWPWM_PIN)
85  softPwmStop(m_pin);
86 }
87 
88 
90 {
91  return QStringList() << tr("Pin %1 PWM").arg(m_pin) << tr("Resolution %1").arg(m_resolution);
92 }
93 
94 void TorcPiPWMOutput::SetValue(double Value)
95 {
96  QMutexLocker locker(&lock);
97 
98  double newdouble = Value;
99  if (!ValueIsDifferent(newdouble))
100  return;
101 
102  int newvalue = lround(newdouble * (double)m_resolution);
103 
104  if (m_pin == TORC_HWPWM_PIN)
105  pwmWrite(m_pin, newvalue);
106  else
107  softPwmWrite(m_pin, newvalue);
108 
110 }
111 
#define TORC_HWPWM_PIN
bool ValueIsDifferent(double &NewValue)
QStringList GetDescription(void)
#define PI_PWM_RESOLUTION
double defaultValue
Definition: torcdevice.h:61
virtual void SetValue(double Value)
Definition: torcdevice.cpp:115
QMutex lock
Definition: torcdevice.h:66
#define DEFAULT_VALUE
double value
Definition: torcdevice.h:60
TorcPiPWMOutput(int Pin, const QVariantMap &Details)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
uint m_maxResolution
Definition: torcpwmoutput.h:31
void SetValue(double Value)