7 struct analog_write_channel
14 #define ANALOG_WRITE_DEFAULT_FREQUENCY 1000
15 #define ANALOG_WRITE_DEFAULT_RESOLUTION 8
17 analog_write_channel _analog_write_channels[LEDC_CHANNELS];
36 int analogWriteChannel(uint8_t pin)
41 for (uint8_t i = 0; i <
SizeOfArray(_analog_write_channels); i++)
43 if (_analog_write_channels[i].pin == pin)
53 for (
int i =
SizeOfArray(_analog_write_channels)-1; i >= 0; i--)
55 analog_write_channel& channel = _analog_write_channels[i];
61 if (channel.frequency == 0 && channel.resolution == 0)
63 channel.frequency = ANALOG_WRITE_DEFAULT_FREQUENCY;
64 channel.resolution = ANALOG_WRITE_DEFAULT_RESOLUTION;
67 ledcAttachPin(pin, channelNum);
68 ledcSetup(channelNum, ANALOG_WRITE_DEFAULT_FREQUENCY, ANALOG_WRITE_DEFAULT_RESOLUTION);
69 if (ledcChangeFrequency(i, channel.frequency, channel.resolution) != channel.frequency)
72 channel.frequency = ANALOG_WRITE_DEFAULT_FREQUENCY;
73 channel.resolution = ANALOG_WRITE_DEFAULT_RESOLUTION;
82 void analogWriteFrequencyResolution(uint32_t frequency, uint8_t resolution = 8)
84 for (uint8_t i = 0; i <
SizeOfArray(_analog_write_channels); i++)
86 bool updateChannel =
true;
87 analog_write_channel& channel = _analog_write_channels[i];
89 updateChannel = (ledcChangeFrequency(i, frequency, resolution) == frequency);
92 channel.frequency = frequency;
93 channel.resolution = resolution;
98 bool analogWriteFrequencyResolution(uint8_t pin, uint32_t frequency, uint8_t resolution = 8)
100 unsigned channelNum = analogWriteChannel(pin);
103 if (channelNum <
SizeOfArray(_analog_write_channels))
105 bool updateChannel =
true;
106 analog_write_channel& channel = _analog_write_channels[channelNum];
107 if (channel.pin != 0)
111 updateChannel = (ledcChangeFrequency(channelNum, frequency, resolution) == frequency);
115 channel.frequency = frequency;
116 channel.resolution = resolution;
123 void analogWriteFrequency(uint32_t frequency)
125 for (uint8_t i = 0; i <
SizeOfArray(_analog_write_channels); i++)
127 bool updateChannel =
true;
128 analog_write_channel& channel = _analog_write_channels[i];
129 if (channel.pin != 0)
130 updateChannel = (ledcChangeFrequency(i, frequency, channel.resolution) == frequency);
132 channel.frequency = frequency;
136 void analogWriteFrequency(uint8_t pin, uint32_t frequency)
138 unsigned channelNum = analogWriteChannel(pin);
141 if (channelNum <
SizeOfArray(_analog_write_channels))
143 bool updateChannel =
true;
144 analog_write_channel& channel = _analog_write_channels[channelNum];
145 if (channel.pin != 0)
146 updateChannel = (ledcChangeFrequency(channelNum, frequency, channel.resolution) == frequency);
148 channel.frequency = frequency;
152 void analogWriteResolution(uint8_t resolution)
154 for (uint8_t i = 0; i <
SizeOfArray(_analog_write_channels); i++)
156 bool updateChannel =
true;
157 analog_write_channel& channel = _analog_write_channels[i];
158 if (channel.pin != 0)
159 updateChannel = (ledcChangeFrequency(i, channel.frequency, resolution) == channel.frequency);
161 channel.resolution = resolution;
165 void analogWriteResolution(uint8_t pin, uint8_t resolution)
167 unsigned channelNum = analogWriteChannel(pin);
170 if (channelNum <
SizeOfArray(_analog_write_channels))
172 bool updateChannel =
true;
173 analog_write_channel& channel = _analog_write_channels[channelNum];
174 if (channel.pin != 0)
175 updateChannel = (ledcChangeFrequency(channelNum, channel.frequency, resolution) == channel.frequency);
177 channel.resolution = resolution;
181 void analogWrite(uint8_t pin, uint32_t value, uint32_t valueMax)
183 int channel = analogWriteChannel(pin);
186 if (channel != -1 && channel <
SizeOfArray(_analog_write_channels))
188 uint8_t resolution = _analog_write_channels[channel].resolution;
189 uint32_t levels = pow(2, resolution);
190 uint32_t duty = ((levels - 1) / valueMax) * min(value, valueMax);
194 ledcWrite(channel, duty);
198 void analogWrite(uint8_t pin,
float value)
200 value = min(max(0.0f, value), 1.0f);
201 int channel = analogWriteChannel(pin);
204 if (channel != -1 && channel <
SizeOfArray(_analog_write_channels))
206 uint8_t resolution = _analog_write_channels[channel].resolution;
207 uint32_t levels = pow(2, resolution);
208 uint32_t duty = (levels - 1) * value;
212 ledcWrite(channel, duty);