SensESP 3.1.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
pwm_output.h
Go to the documentation of this file.
1#ifndef SENSESP_SYSTEM_PWM_OUTPUT_H
2#define SENSESP_SYSTEM_PWM_OUTPUT_H
3
4#include <Arduino.h>
5#include <map>
6
7#include "esp_arduino_version.h"
8#include "valueconsumer.h"
9
10namespace sensesp {
11
33class PWMOutput : public ValueConsumer<float> {
34 public:
45 PWMOutput(int pin = -1, int pwm_channel = -1, int channel_frequency = 5000,
46 int channel_resolution = 13) __attribute__((deprecated))
47 : ValueConsumer<float>(),
48 pwm_channel_{static_cast<uint8_t>(pwm_channel)},
49 channel_frequency_{channel_frequency},
50 channel_resolution_{channel_resolution},
51 pwmrange_{static_cast<int>((1 << channel_resolution) - 1)} {
52 if (pin >= 0) {
53 pwm_channel_ = assign_channel(pin, pwm_channel);
54 }
55 }
56
62 virtual void set(const float& new_value) override { set_pwm(new_value); }
63
64 protected:
68
69 static std::map<uint8_t, int8_t>& channel_map() {
70 static std::map<uint8_t, int8_t> channel_to_pin_;
71 return channel_to_pin_;
72 }
73
74 uint8_t pwm_channel_{};
75
85 int assign_channel(int pin, int pwm_channel = -1) {
86 if (pwm_channel == -1) {
87 // Do a search for the next available channel
88 std::map<uint8_t, int8_t>::iterator it;
89 pwm_channel = 0;
90 do {
91 pwm_channel++;
92 it = channel_map().find(pwm_channel);
93 } while (it != channel_map().end());
94 }
95
96 channel_map()[pwm_channel] = pin;
97
98 ESP_LOGD(__FILENAME__, "PWM channel %d assigned to pin %d", pwm_channel,
99 pin);
100
101 pinMode(pin, OUTPUT);
102#if ESP_ARDUINO_VERSION_MAJOR > 2
103 ledcAttachChannel(pin, channel_frequency_, channel_resolution_,
104 pwm_channel);
105#else
106 ledcSetup(pwm_channel, channel_frequency_, channel_resolution_);
107 ledcAttachPin(pin, pwm_channel);
108#endif
109
110 return pwm_channel;
111 }
112
118 void set_pwm(float value) {
119 std::map<uint8_t, int8_t>::iterator it;
120 it = channel_map().find(pwm_channel_);
121 if (it != channel_map().end()) {
122 int pin = it->second;
123 int const output_val = value * pwmrange_;
124 // ESP_LOGD(__FILENAME__, "Setting PWM channel %d to %d", pwm_channel_,
125 // output_val);
126#if ESP_ARDUINO_VERSION_MAJOR > 2
127 ledcWriteChannel(pwm_channel_, output_val);
128#else
129 ledcWrite(pwm_channel_, output_val);
130#endif
131 } else {
132 ESP_LOGW(__FILENAME__,
133 "No pin assigned to channel %d. Ignoring set_pwm()",
135 }
136 }
137};
138
139} // namespace sensesp
140
141#endif
void set_pwm(float value)
Definition pwm_output.h:118
int assign_channel(int pin, int pwm_channel=-1)
Definition pwm_output.h:85
PWMOutput(int pin=-1, int pwm_channel=-1, int channel_frequency=5000, int channel_resolution=13) __attribute__((deprecated))
Definition pwm_output.h:45
virtual void set(const float &new_value) override
Definition pwm_output.h:62
static std::map< uint8_t, int8_t > & channel_map()
Definition pwm_output.h:69
A base class for piece of code (like a transform) that accepts data for input. ValueConsumers can acc...