@@ -310,31 +310,31 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
310
310
waveform.enabled ^= 1UL << pin;
311
311
}
312
312
else {
313
+ // get true accumulated overshoot
314
+ overshootCcys = now - ((waveform.states & (1UL << pin)) ? wave.nextOffCcy : wave.nextPhaseCcy );
313
315
const uint32_t idleCcys = wave.periodCcys - wave.dutyCcys ;
314
- const uint32_t fwdPeriods = static_cast <uint32_t >(overshootCcys) >= idleCcys ?
316
+ uint32_t fwdPeriods = static_cast <uint32_t >(overshootCcys) >= idleCcys ?
315
317
((overshootCcys + wave.dutyCcys ) / wave.periodCcys ) : 0 ;
316
318
uint32_t nextEdgeCcy;
317
319
if (waveform.states & (1UL << pin)) {
318
320
const bool endOfPeriod = wave.nextPhaseCcy == wave.nextOffCcy ;
319
- if (fwdPeriods) {
320
- wave.nextPhaseCcy += fwdPeriods * wave.periodCcys ;
321
- }
322
321
if (!idleCcys) {
322
+ wave.nextPhaseCcy += (fwdPeriods + 1 ) * wave.periodCcys ;
323
323
wave.nextOffCcy = wave.nextPhaseCcy ;
324
324
nextEdgeCcy = wave.nextPhaseCcy ;
325
325
}
326
326
else if (endOfPeriod) {
327
327
// preceeding period had zero idle cycle, continue direct into new duty cycle
328
+ wave.nextPhaseCcy += fwdPeriods * wave.periodCcys ;
328
329
wave.nextOffCcy = wave.nextPhaseCcy + wave.dutyCcys ;
329
330
wave.nextPhaseCcy += wave.periodCcys ;
330
331
nextEdgeCcy = wave.nextOffCcy ;
331
332
}
332
333
else if (fwdPeriods) {
333
334
// maintain phase, maintain duty/idle ratio, temporarily reduce frequency by skipPeriods
334
- // plus dynamically scale frequency
335
- wave.nextOffCcy = wave.nextPhaseCcy - (fwdPeriods + 1 ) * idleCcys;
336
- wave.dutyCcys *= 2 ;
337
- wave.periodCcys *= 2 ;
335
+ fwdPeriods = (now + wave.periodCcys - wave.nextOffCcy ) / wave.dutyCcys ;
336
+ wave.nextOffCcy += fwdPeriods * wave.dutyCcys ;
337
+ wave.nextPhaseCcy += fwdPeriods * wave.periodCcys ;
338
338
nextEdgeCcy = wave.nextOffCcy ;
339
339
}
340
340
else {
@@ -350,21 +350,17 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
350
350
}
351
351
else {
352
352
if (!wave.dutyCcys ) {
353
- wave.nextPhaseCcy += wave. periodCcys + fwdPeriods * wave.periodCcys ;
353
+ wave.nextPhaseCcy += (fwdPeriods + 1 ) * wave.periodCcys ;
354
354
wave.nextOffCcy = wave.nextPhaseCcy ;
355
355
}
356
356
else {
357
357
waveform.states ^= 1UL << pin;
358
358
if (fwdPeriods)
359
359
{
360
- const uint32_t fwdPeriodsCcys = fwdPeriods * wave.periodCcys ;
361
360
// maintain phase, maintain duty/idle ratio, temporarily reduce frequency by skipPeriods
362
- // plus dynamically scale frequency
363
361
wave.nextOffCcy =
364
- wave.nextPhaseCcy + fwdPeriods * wave.dutyCcys + (overshootCcys + wave.dutyCcys - fwdPeriodsCcys);
365
- wave.nextPhaseCcy += fwdPeriodsCcys;
366
- wave.dutyCcys *= 2 ;
367
- wave.periodCcys *= 2 ;
362
+ wave.nextPhaseCcy + (1 * fwdPeriods + 1 ) * wave.dutyCcys + (overshootCcys + wave.dutyCcys - fwdPeriods * wave.periodCcys );
363
+ wave.nextPhaseCcy += (1 * fwdPeriods + 1 ) * wave.periodCcys ;
368
364
}
369
365
else
370
366
{
0 commit comments