roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
esp32_ledc.h
Go to the documentation of this file.
1#pragma once
2
5
6#if defined(ESP_PLATFORM)
7#include "driver/ledc.h"
8#include "esp_err.h"
9
10namespace roo_display {
11
12#if defined(ARDUINO)
13#if (defined ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 3)
14
15class LedcBacklit : public Backlit {
16 public:
17 LedcBacklit(uint8_t pin) : pin_(pin) {}
18
19 public:
20 void begin(uint16_t intensity = kMaxIntensity) {
21 DefaultGpio::setOutput(pin_);
22 // Use 50 kHz frequency to avoid interference with audio.
23 CHECK(ledcAttach(pin_, 50000, 8));
24 CHECK(ledcWrite(pin_, intensity));
25 }
26
27 void setIntensity(uint16_t intensity) override {
28 CHECK(ledcWrite(pin_, intensity));
29 }
30
31 private:
32 uint8_t pin_;
33};
34
35class LedcBacklitAtChannel : public Backlit {
36 public:
37 LedcBacklitAtChannel(uint8_t pin, uint8_t channel)
38 : pin_(pin), channel_(channel) {}
39
40 public:
41 void begin(uint16_t intensity = kMaxIntensity) {
42 DefaultGpio::setOutput(pin_);
43 // Use 50 kHz frequency to avoid interference with audio.
44 CHECK(ledcAttachChannel(pin_, 50000, 8, channel_));
45 CHECK(ledcWriteChannel(channel_, intensity));
46 }
47
48 void setIntensity(uint16_t intensity) override {
49 CHECK(ledcWriteChannel(channel_, intensity));
50 }
51
52 private:
53 uint8_t pin_;
54 int8_t channel_;
55};
56
57#else
58
59// With Arduino < 3, the channel must be specified explicitly.
60
61class LedcBacklitAtChannel : public Backlit {
62 public:
63 LedcBacklitAtChannel(uint8_t pin, uint8_t channel)
64 : pin_(pin), channel_(channel) {}
65
66 public:
67 void begin(uint16_t intensity = kMaxIntensity) {
68 DefaultGpio::setOutput(pin_);
69 // Use 50 kHz frequency to avoid interference with audio.
70 ledcSetup(channel_, 50000, 8);
71 ledcAttachPin(pin_, channel_);
72 ledcWrite(channel_, intensity);
73 }
74
75 void setIntensity(uint16_t intensity) override {
76 ledcWrite(channel_, intensity);
77 }
78
79 private:
80 uint8_t pin_;
81 uint8_t channel_;
82};
83
84#endif
85#else // defined(ARDUINO)
86
87// Note: always uses LEDC_CHANNEL_0 and LEDC_TIMER_0. Use LedcBacklitAtChannel
88// to specify a different channel or timer.
89class LedcBacklit : public Backlit {
90 public:
91 explicit LedcBacklit(uint8_t pin)
92 : pin_(pin), channel_(LEDC_CHANNEL_0), timer_(LEDC_TIMER_0) {}
93
94 void begin(uint16_t intensity = kMaxIntensity) {
95 DefaultGpio::setOutput(pin_);
96 ledc_timer_config_t timer_config = {
97 .speed_mode = LEDC_LOW_SPEED_MODE,
98 .duty_resolution = LEDC_TIMER_8_BIT,
99 .timer_num = timer_,
100 .freq_hz = 50000,
101 .clk_cfg = LEDC_AUTO_CLK,
102 };
103 ESP_ERROR_CHECK(ledc_timer_config(&timer_config));
104
105 ledc_channel_config_t channel_config = {
106 .gpio_num = pin_,
107 .speed_mode = LEDC_LOW_SPEED_MODE,
108 .channel = channel_,
109 .intr_type = LEDC_INTR_DISABLE,
110 .timer_sel = timer_,
111 .duty = 0,
112 .hpoint = 0,
113 };
114 ESP_ERROR_CHECK(ledc_channel_config(&channel_config));
115 setIntensity(intensity);
116 }
117
118 void setIntensity(uint16_t intensity) override {
119 ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, channel_, intensity));
120 ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, channel_));
121 }
122
123 private:
124 uint8_t pin_;
125 ledc_channel_t channel_;
126 ledc_timer_t timer_;
127};
128
129class LedcBacklitAtChannel : public Backlit {
130 public:
131 LedcBacklitAtChannel(uint8_t pin, uint8_t channel)
132 : pin_(pin),
133 channel_(static_cast<ledc_channel_t>(channel)),
134 timer_(LEDC_TIMER_0) {}
135
136 // ESP-IDF style constructor allowing timer selection.
137 LedcBacklitAtChannel(uint8_t pin, ledc_channel_t channel,
138 ledc_timer_t timer = LEDC_TIMER_0)
139 : pin_(pin),
140 channel_(static_cast<ledc_channel_t>(channel)),
141 timer_(timer) {}
142
143 void begin(uint16_t intensity = kMaxIntensity) {
144 DefaultGpio::setOutput(pin_);
145 ledc_timer_config_t timer_config = {
146 .speed_mode = LEDC_LOW_SPEED_MODE,
147 .duty_resolution = LEDC_TIMER_8_BIT,
148 .timer_num = timer_,
149 .freq_hz = 50000,
150 .clk_cfg = LEDC_AUTO_CLK,
151 };
152 ESP_ERROR_CHECK(ledc_timer_config(&timer_config));
153
154 ledc_channel_config_t channel_config = {
155 .gpio_num = pin_,
156 .speed_mode = LEDC_LOW_SPEED_MODE,
157 .channel = channel_,
158 .intr_type = LEDC_INTR_DISABLE,
159 .timer_sel = timer_,
160 .duty = 0,
161 .hpoint = 0,
162 };
163 ESP_ERROR_CHECK(ledc_channel_config(&channel_config));
164 setIntensity(intensity);
165 }
166
167 void setIntensity(uint16_t intensity) override {
168 ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, channel_, intensity));
169 ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, channel_));
170 }
171
172 private:
173 uint8_t pin_;
174 ledc_channel_t channel_;
175 ledc_timer_t timer_;
176};
177
178#endif // defined(ARDUINO)
179
180} // namespace roo_display
181
182#endif // defined(ESP_PLATFORM)
@ CHECK
Definition inflate.h:47
Defines 140 opaque HTML named colors.