-
Notifications
You must be signed in to change notification settings - Fork 15
/
pwmWrite.cpp
478 lines (432 loc) · 15.5 KB
/
pwmWrite.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
ESP32 PWM, SERVO and TONE Library, Version 5.0.2
by dlloydev https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite
License: MIT
*/
#include <Arduino.h>
#include "pwmWrite.h"
Pwm::Pwm() {}
float Pwm::write(int pin, uint32_t duty) {
uint8_t ch = attach(pin);
if (ch < chMax) { // write PWM
wr_duty(ch, duty);
}
return mem[ch].frequency;
}
float Pwm::write(int pin, uint32_t duty, uint32_t frequency) {
uint8_t ch = attach(pin);
if (ch < chMax) { // write PWM
wr_freq_res(ch, frequency, mem[ch].resolution);
wr_duty(ch, duty);
}
return mem[ch].frequency;
}
float Pwm::write(int pin, uint32_t duty, uint32_t frequency, uint8_t resolution) {
uint8_t ch = attach(pin);
if (ch < chMax) { // write PWM
wr_freq_res(ch, frequency, resolution);
wr_duty(ch, duty);
}
return mem[ch].frequency;
}
float Pwm::write(int pin, uint32_t duty, uint32_t frequency, uint8_t resolution, uint32_t phase) {
uint8_t ch = attach(pin);
if (ch < chMax) { // write PWM
wr_freq_res(ch, frequency, resolution);
wr_phase(ch, duty, phase);
wr_duty(ch, duty);
}
return mem[ch].frequency;
}
uint8_t Pwm::attach(int pin) {
uint8_t ch = attached(pin);
if (ch == 253) { // free channels exist
for (uint8_t c = 0; c < chMax; c++) {
if (mem[c].pin == 255 && ch == 253) { //first free ch
mem[c].pin = pin;
ch = c;
ledcSetup(ch, mem[ch].frequency, mem[ch].resolution);
if (sync) pause(ch);
ledcAttachPin(pin, ch);
return ch;
}
}
}
return ch;
}
uint8_t Pwm::attach(int pin, int ch) {
if (mem[ch].pin == 255) {
mem[ch].pin = pin;
ledcSetup(ch, mem[ch].frequency, mem[ch].resolution);
if (sync) pause(ch);
ledcAttachPin(pin, ch);
}
return ch;
}
uint8_t Pwm::attachInvert(int pin) {
uint8_t ch = firstFreeCh();
if (ch < chMax) mem[ch].pin = pin;
ledcSetup(ch, mem[ch].frequency, mem[ch].resolution);
if (sync) pause(ch);
ledc_attach_with_invert(pin, ch);
return ch;
}
uint8_t Pwm::attachInvert(int pin, int ch) {
if (mem[ch].pin == 255) {
mem[ch].pin = pin;
ledcSetup(ch, mem[ch].frequency, mem[ch].resolution);
if (sync) pause(ch);
ledc_attach_with_invert(pin, ch);
}
return ch;
}
uint8_t Pwm::attachServo(int pin) {
uint8_t ch = firstFreeCh();
if (ch < chMax) config_servo(ch, mem[ch].servoMinUs, mem[ch].servoMaxUs);
return attach(pin, ch);
}
uint8_t Pwm::attachServo(int pin, bool invert) {
uint8_t ch = firstFreeCh();
if (ch < chMax) config_servo(ch, mem[ch].servoMinUs, mem[ch].servoMaxUs);
return (invert) ? attachInvert(pin, ch) : attach(pin, ch);
}
uint8_t Pwm::attachServo(int pin, int ch) {
if (ch < chMax) config_servo(ch, mem[ch].servoMinUs, mem[ch].servoMaxUs);
return attach(pin, ch);
}
uint8_t Pwm::attachServo(int pin, int ch, bool invert) {
if (ch < chMax) config_servo(ch, mem[ch].servoMinUs, mem[ch].servoMaxUs);
return (invert) ? attachInvert(pin, ch) : attach(pin, ch);
}
uint8_t Pwm::attachServo(int pin, int minUs, int maxUs) {
uint8_t ch = firstFreeCh();
if (ch < chMax) config_servo(ch, minUs, maxUs);
return attach(pin, ch);
}
uint8_t Pwm::attachServo(int pin, int ch, int minUs, int maxUs) {
config_servo(ch, minUs, maxUs);
return attach(pin, ch);
}
uint8_t Pwm::attachServo(int pin, int ch, int minUs, int maxUs, bool invert) {
config_servo(ch, minUs, maxUs);
return (invert) ? attachInvert(pin, ch) : attach(pin, ch);
}
uint8_t Pwm::attachServo(int pin, int minUs, int maxUs, double speed, double ke) {
uint8_t ch = firstFreeCh();
if (ch < chMax) config_servo(ch, minUs, maxUs, speed, ke);
return attach(pin, ch);
}
uint8_t Pwm::attachServo(int pin, int ch, int minUs, int maxUs, double speed, double ke) {
config_servo(ch, minUs, maxUs, speed, ke);
return attach(pin, ch);
}
uint8_t Pwm::attachServo(int pin, int ch, int minUs, int maxUs, double speed, double ke, bool invert) {
config_servo(ch, minUs, maxUs, speed, ke);
return (invert) ? attachInvert(pin, ch) : attach(pin, ch);
}
float Pwm::read(int pin) {
uint8_t ch = attached(pin);
if (ch < chMax) {
float deg = (readMicroseconds(pin) - mem[ch].servoMinUs) / (mem[ch].servoMaxUs - mem[ch].servoMinUs) * 180.0;
return deg < 0 ? 0 : deg;
}
else return 0;
}
float Pwm::readMicroseconds(int pin) {
uint8_t ch = attached(pin);
if (ch < chMax) return mem[ch].duty * ((1000000.0 / mem[ch].frequency) / ((1 << mem[ch].resolution) - 1.0)); // μs
else return 0;
}
float Pwm::writeServo(int pin, float value, double speed, double ke) {
uint8_t ch = attached(pin);
wr_servo(pin, value, speed, ke);
return mem[ch].ye; // normalized easing position (0.0 - 1.0)
}
float Pwm::writeServo(int pin, float value) {
uint8_t ch = attached(pin);
if (ch == 253) { // free channels exist
for (uint8_t c = 0; c < chMax; c++) {
if (mem[c].pin == 255 && ch == 253) { //first free ch
mem[c].pin = pin;
ch = c;
if (mem[ch].frequency < 40 || mem[ch].frequency > 900) mem[ch].frequency = 50;
if (mem[ch].resolution > widthMax) mem[ch].resolution = widthMax;
else if (mem[ch].resolution < 14 && widthMax == 20) mem[ch].resolution = 16;
else if (mem[ch].resolution < 14) mem[ch].resolution = 14;
ledcSetup(ch, mem[ch].frequency, mem[ch].resolution);
if (sync) pause(ch);
ledcAttachPin(pin, ch);
}
}
}
wr_servo(pin, value, mem[ch].speed, mem[ch].ke);
return mem[ch].ye; // normalized easing position (0.0 - 1.0)
}
void Pwm::tone(int pin, uint32_t frequency, uint16_t duration, uint16_t interval) {
uint8_t ch = attach(pin);
if (ch < chMax) {
uint32_t ms = millis();
static bool durDone = false;
#if defined(CONFIG_IDF_TARGET_ESP32C3)
if (frequency < 153) frequency = 153;
#else
if (frequency < 4) frequency = 4;
#endif
if (!durDone) {
if (frequency != mem[ch].frequency && (ms - mem[ch].startMs > interval)) {
mem[ch].startMs = ms;
mem[ch].frequency = frequency;
ledcChangeFrequency(ch, frequency, mem[ch].resolution);
write(pin, 127, frequency, 8);
resume(ch);
}
if ((duration && ((ms - mem[ch].startMs) > duration)) || (duration == 0)) {
mem[ch].startMs = ms;
durDone = true;
if (duration < 0xffff) pause(ch);
}
} else if (ms - mem[ch].startMs > interval) durDone = false;
}
}
void Pwm::note(int pin, note_t note, uint8_t octave, uint16_t duration, uint16_t interval) {
const uint16_t noteFrequencyBase[12] = {
// C C# D Eb E F F# G G# A Bb B
4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902
};
uint32_t noteFreq = (uint32_t)noteFrequencyBase[note] / (uint32_t)(1 << (8 - octave));
if (octave <= 8 || note <= NOTE_MAX) tone(pin, noteFreq, duration, interval);
}
uint8_t Pwm::attached(int pin) {
if (!((pinMask >> pin) & 1)) return 254; // not a pwm pin
bool freeCh = false;
for (uint8_t ch = 0; ch < chMax; ch++) {
if (mem[ch].pin == pin) return ch;
if (mem[ch].pin == 255) freeCh = true; // free channel(s) exist
}
return (freeCh) ? 253 : 255; // freeCh : not attached
}
uint8_t Pwm::attachedPin(int ch) {
return mem[ch].pin;
}
uint8_t Pwm::firstFreeCh(void) {
for (uint8_t ch = 0; ch < chMax; ch++) {
if (mem[ch].pin == 255) return ch;
}
return 255;
}
void Pwm::detach(int pin) {
uint8_t ch = attached(pin);
if (ch < chMax) {
reset_fields(ch);
if (digitalRead(pin) == HIGH) delayMicroseconds(mem[ch].servoMaxUs); // wait until LOW
ledcWrite(ch, 4); // set minimal duty
ledcDetachPin(mem[ch].pin); // jitterless
REG_SET_FIELD(GPIO_PIN_MUX_REG[pin], MCU_SEL, GPIO_MODE_DEF_DISABLE);
}
}
bool Pwm::detached(int pin) {
if ((REG_GET_FIELD(GPIO_PIN_MUX_REG[pin], MCU_SEL)) == 0) return true;
else return false;
}
void Pwm::pause(int ch) {
if (ch == 255) sync = true;
else ledc_timer_pause((ledc_mode_t)mem[ch].mode, (ledc_timer_t)mem[ch].timer);
}
void Pwm::resume(int ch) {
if (sync && ch == 255) {
for (uint8_t ch = 0; ch < chMax; ch++) {
if (mem[ch].pin < 48) ledc_timer_resume((ledc_mode_t)mem[ch].mode, (ledc_timer_t)mem[ch].timer);
}
sync = false;
} else {
ledc_timer_resume((ledc_mode_t)mem[ch].mode, (ledc_timer_t)mem[ch].timer);
}
}
float Pwm::setFrequency(int pin, uint32_t frequency) {
uint8_t ch = attach(pin);
if (ch < chMax) {
if (mem[ch].frequency != frequency) {
ledcSetup(ch, frequency, mem[ch].resolution);
if (sync) pause(ch);
ledcWrite(ch, mem[ch].duty);
mem[ch].frequency = frequency;
wr_ch_pair(ch, frequency, mem[ch].resolution);
}
}
return ledcReadFreq(ch);
}
uint8_t Pwm::setResolution(int pin, uint8_t resolution) {
uint8_t ch = attach(pin);
if (ch < chMax) {
if (mem[ch].resolution != resolution) {
ledcSetup(ch, mem[ch].frequency, resolution);
if (sync) pause(ch);
ledcWrite(ch, mem[ch].duty);
mem[ch].resolution = resolution;
wr_ch_pair(ch, mem[ch].frequency, resolution);
}
}
return mem[ch].resolution;
}
void Pwm::printDebug() {
Serial.printf("PWM pins:\n");
for (uint8_t i = 0; i < 48; i++) {
if ((pinMask >> i) & 1) {
Serial.printf("%d,", i);
}
}
Serial.printf("\n\nCh Pin Hz Res Duty Servo Speed ke\n");
for (uint8_t ch = 0; ch < chMax; ch++) {
Serial.printf ("%2d %3d %5.0f %2d %4d %d-%d %5.1f %1.1f\n", ch, mem[ch].pin, mem[ch].frequency, mem[ch].resolution,
mem[ch].duty, mem[ch].servoMinUs, mem[ch].servoMaxUs, mem[ch].speed, mem[ch].ke);
}
Serial.printf("\n");
}
/***** private functions *****/
void Pwm::ledc_attach_with_invert(int pin, int ch) {
if (ch >= chMax) return;
uint32_t ch_config = ch;
if (ch > 7) ch_config = ch - 8;
ledc_channel_config_t ledc_channel = {
.gpio_num = (mem[ch].pin),
.speed_mode = (ledc_mode_t) mem[ch].mode,
.channel = (ledc_channel_t) ch_config,
.intr_type = (ledc_intr_type_t) LEDC_INTR_DISABLE,
.timer_sel = (ledc_timer_t) mem[ch].timer,
.duty = 0,
.hpoint = 0,
.flags = {
.output_invert = 1
}
};
ledc_channel_config(&ledc_channel);
}
float Pwm::duty2deg(int ch, uint32_t duty, float countPerUs) {
return ((duty / countPerUs - mem[ch].servoMinUs) / (mem[ch].servoMaxUs - mem[ch].servoMinUs)) * 180.0;
}
void Pwm::config_servo(int ch, int minUs, int maxUs, double speed, double ke) {
if (minUs < 500) mem[ch].servoMinUs = 500;
else if (minUs > 2500) mem[ch].servoMinUs = 2500;
else mem[ch].servoMinUs = minUs;
if (maxUs < 500) mem[ch].servoMaxUs = 500;
else if (maxUs > 2500) mem[ch].servoMaxUs = 2500;
else mem[ch].servoMaxUs = maxUs;
if (mem[ch].frequency < 40 || mem[ch].frequency > 900) mem[ch].frequency = 50;
if (mem[ch].resolution > widthMax) mem[ch].resolution = widthMax;
else if (mem[ch].resolution < 14 && widthMax == 20) mem[ch].resolution = 16;
else if (mem[ch].resolution < 14) mem[ch].resolution = 14;
speed = (speed > 2000) ? 2000 : (speed < 0) ? 0 : speed;
mem[ch].speed = speed;
ke = (ke > 1.0) ? 1.0 : (ke < 0) ? 0 : ke;
mem[ch].ke = ke;
wr_ch_pair(ch, mem[ch].frequency, mem[ch].resolution);
}
void Pwm::wr_servo(int pin, float value, double speed, double ke) {
uint8_t ch = attached(pin);
if (ch == 253) { // free channels exist
for (uint8_t c = 0; c < chMax; c++) {
if (mem[c].pin == 255 && ch == 253) { //first free ch
mem[c].pin = pin;
ch = c;
if (mem[ch].frequency < 40 || mem[ch].frequency > 900) mem[ch].frequency = 50;
if (mem[ch].resolution > widthMax) mem[ch].resolution = widthMax;
else if (mem[ch].resolution < 14 && widthMax == 20) mem[ch].resolution = 16;
else if (mem[ch].resolution < 14) mem[ch].resolution = 14;
ledcSetup(ch, mem[ch].frequency, mem[ch].resolution);
if (sync) pause(ch);
ledcAttachPin(pin, ch);
}
}
}
uint32_t duty;
if (ch < chMax) { // write PWM
float countPerUs = ((1 << mem[ch].resolution) - 1) / (1000000.0 / mem[ch].frequency);
if (value < mem[ch].servoMinUs) { // degrees
if (value < 0) value = 0;
else if (value > 180 && value < 500) value = 180;
duty = (((value / 180.0) * (mem[ch].servoMaxUs - mem[ch].servoMinUs)) + mem[ch].servoMinUs) * countPerUs;
} else { // microseconds
if (value < mem[ch].servoMinUs) value = mem[ch].servoMinUs;
else if (value > mem[ch].servoMaxUs) value = mem[ch].servoMaxUs;
duty = value * countPerUs;
}
ke = (ke > 1.0) ? 1.0 : (ke < 0) ? 0 : ke;
if (ke < 1.0) { // easing enabled
float deltaDeg;
uint32_t easeDuty;
if (ke < 1.0 && duty != mem[ch].duty) { // init
mem[ch].startDuty = mem[ch].stopDuty;
mem[ch].stopDuty = duty;
mem[ch].deltaDuty = (mem[ch].startDuty < mem[ch].stopDuty) ? mem[ch].stopDuty - mem[ch].startDuty : mem[ch].startDuty - mem[ch].stopDuty;
mem[ch].startMs = millis();
speed = (speed > 2000) ? 2000 : (speed < 0) ? 0 : speed;
if (speed > 0) {
deltaDeg = fabsf(duty2deg(ch, mem[ch].stopDuty, countPerUs) - duty2deg(ch, mem[ch].startDuty, countPerUs));
mem[ch].stopMs = mem[ch].startMs + (deltaDeg / speed) * 1000;
mem[ch].deltaMs = mem[ch].stopMs - mem[ch].startMs;
}
}
uint32_t now = millis();
if (duty > mem[ch].startDuty) {
mem[ch].te = (float)(now - mem[ch].startMs) / mem[ch].deltaMs;
mem[ch].ye = (-(ke + 1.0f) * (2.0f * mem[ch].te - 1.0f) / (2.0f * (-4.0f * ke * fabsf(mem[ch].te - 0.5f) + ke - 1.0f))) + 0.5f;
if (isnan(mem[ch].ye) || mem[ch].ye < 0.0) mem[ch].ye = 0.0;
else if (mem[ch].ye > 1.0) mem[ch].ye = 1.0;
easeDuty = mem[ch].startDuty + (mem[ch].deltaDuty * mem[ch].ye);
} else {
mem[ch].te = 1 - ((float)(now - mem[ch].startMs) / mem[ch].deltaMs);
mem[ch].ye = 1 - (((ke + 1.0f) * (2.0f * mem[ch].te - 1.0f) / (2.0f * (-4.0f * ke * fabsf(mem[ch].te - 0.5f) + ke - 1.0f))) + 0.5f);
if (isnan(mem[ch].ye) || mem[ch].ye < 0.0) mem[ch].ye = 0.0;
else if (mem[ch].ye > 1.0) mem[ch].ye = 1.0;
easeDuty = duty + (mem[ch].deltaDuty * mem[ch].ye);
}
ledcWrite(ch, easeDuty);
} else ledcWrite(ch, duty);
mem[ch].duty = duty;
}
}
void Pwm::wr_ch_pair(int ch, uint32_t frequency, uint8_t bits) {
mem[ch].frequency = frequency;
mem[ch].resolution = bits;
if (ch % 2 == 0) { // even ch
mem[ch + 1].frequency = frequency;
mem[ch + 1].resolution = bits;
} else { // odd ch
mem[ch - 1].frequency = frequency;
mem[ch - 1].resolution = bits;
}
}
void Pwm::wr_duty(int ch, uint32_t duty) {
if (mem[ch].duty != duty) {
ledcWrite(ch, duty);
mem[ch].duty = duty;
}
}
void Pwm::wr_freq_res(int ch, uint32_t frequency, uint8_t resolution) {
if ((mem[ch].frequency != frequency) || (mem[ch].resolution != resolution)) {
ledcSetup(ch, frequency, resolution);
wr_ch_pair(ch, frequency, resolution);
mem[ch].frequency = frequency;
mem[ch].resolution = resolution;
}
}
void Pwm::wr_phase(int ch, uint32_t duty, uint32_t phase) {
if (mem[ch].phase != phase) {
uint32_t ch_config = ch;
if (ch > 7) ch_config = ch - 8;
ledc_set_duty_with_hpoint((ledc_mode_t)mem[ch].mode, (ledc_channel_t)ch_config, duty, phase);
if (sync) pause(ch);
mem[ch].phase = phase;
}
}
void Pwm::reset_fields(int ch) {
mem[ch].pin = 255;
mem[ch].duty = 0;
mem[ch].frequency = 1000;
mem[ch].resolution = 8;
mem[ch].phase = 0;
mem[ch].servoMinUs = 544;
mem[ch].servoMaxUs = 2400;
mem[ch].speed = 0;
mem[ch].ke = 1.0;
}