SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
pwm_output.cpp
Go to the documentation of this file.
1#include "pwm_output.h"
2
3#include <algorithm>
4
5namespace sensesp {
6
7// For info on frequency and resolution for ESP32, see
8// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/ledc.html#ledc-api-supported-range-frequency-duty-resolution
9#define CHANNEL_FREQUENCY 5000
10#define CHANNEL_RESOLUTION 13
11#define PWMRANGE 4095
12
13std::map<uint8_t, int8_t> PWMOutput::channel_to_pin_;
14
16 if (pin >= 0) {
18 }
19}
20
22 if (pwm_channel == 0) {
23 // Use the default channel, as zero is the SensESP default
24 // input_channel for ValueConsumers
26 }
28}
29
31 if (pwm_channel == -1) {
32 // Do a search for the next available channel
33 std::map<uint8_t, int8_t>::iterator it;
34 pwm_channel = 0;
35 do {
38 } while (it != channel_to_pin_.end());
39 }
40
42
43 debugD("PWM channel %d assigned to pin %d", pwm_channel, pin);
44
45 pinMode(pin, OUTPUT);
46#ifdef ESP32
49#endif
50
51 return pwm_channel;
52}
53
54void PWMOutput::set_pwm(int pwm_channel, float value) {
55 std::map<uint8_t, int8_t>::iterator it;
57 if (it != channel_to_pin_.end()) {
58 int pin = it->second;
59 int output_val = value * PWMRANGE;
60 debugD("Outputting %d to pwm channel %d (pin %d)", output_val, pwm_channel,
61 pin);
62
63#ifdef ESP32
66 ((levels - 1) / PWMRANGE) * std::min(output_val, (int)PWMRANGE);
68#else
70#endif
71 } else {
72 debugW("No pin assigned to channel %d. Ignoring set_pwm()", pwm_channel);
73 }
74}
75
76} // namespace sensesp
Construct a new transform based on a single function.
static std::map< uint8_t, int8_t > channel_to_pin_
Definition pwm_output.h:73
virtual void set_input(float new_value, uint8_t pwm_channel=0) override
static int assign_channel(int pin, int pwm_channel=-1)
PWMOutput(int pin=-1, int pwm_channel=-1)
static void set_pwm(int pwm_channel, float value)
uint8_t default_channel_
Definition pwm_output.h:74
#define debugD(fmt,...)
Definition local_debug.h:47
#define debugW(fmt,...)
Definition local_debug.h:49
#define CHANNEL_FREQUENCY
Definition pwm_output.cpp:9
#define PWMRANGE
#define CHANNEL_RESOLUTION