-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pwm.hpp
89 lines (70 loc) · 2.79 KB
/
pwm.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2013-2023 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <[email protected]>
//
// This file is part of p44utils.
//
// p44utils is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// p44utils is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with p44utils. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef __p44utils__pwm__
#define __p44utils__pwm__
#include "p44utils_common.hpp"
#include "iopin.hpp"
#ifdef ESP_PLATFORM
#include "driver/ledc.h"
#include "driver/gpio.h"
#endif
using namespace std;
namespace p44 {
/// Wrapper for PWM output accessed via
/// generic Linux kernel SysFS support for PWMs
class PWMPin : public AnalogIOPin
{
typedef AnalogIOPin inherited;
uint32_t activeNs; ///< active time in nanoseconds
uint32_t periodNs; ///< PWM period in nanoseconds
bool inverted; ///< pwm inverted
#ifdef ESP_PLATFORM
gpio_num_t gpioNo; ///< the GPIO to use as PWM output
ledc_channel_t ledcChannel; ///< ledc channel number
#else
int pwmChip; ///< chip number
int pwmChannel; ///< channel number
int pwmFD; ///< file descriptor for the "value" file
#endif
public:
/// Create PWM output pin
/// @param aPwmChip PWM chip number (0,1,...), for ESP32: GPIO number
/// @param aPwmChannel number (0,1,...), for ESP32: ledc channel number
/// @param aInitialValue initial duty cycle value (0..100)
/// @param aPeriodInNs PWM period in nanoseconds, 0 = default
PWMPin(int aPwmChip, int aPwmChannel, bool aInverted, double aInitialValue, uint32_t aPeriodInNs);
virtual ~PWMPin();
/// get value of pin
/// @return current value (from actual pin for inputs, from last set state for outputs)
virtual double getValue() P44_OVERRIDE;
/// set value of pin (NOP for inputs)
/// @param aValue new value to set output to
virtual void setValue(double aValue) P44_OVERRIDE;
/// get range and resolution of this pin
/// @param aMin minimum value
/// @param aMax maximum value
/// @param aResolution resolution (LSBit value)
/// @return false if no range information is available (arguments are not touched then)
virtual bool getRange(double &aMin, double &aMax, double &aResolution) P44_OVERRIDE;
};
} // namespace p44
#endif /* defined(__p44utils__pwm__) */