@@ -300,47 +300,47 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
300
300
301
301
Waveform& wave = waveform.pins [pin];
302
302
303
- int32_t overshootCcys = now - wave.nextEventCcy ;
304
- if (overshootCcys >= 0 ) {
303
+ if (static_cast <int32_t >(now - wave.nextEventCcy ) >= 0 ) {
305
304
if (WaveformMode::EXPIRES == wave.mode && wave.nextEventCcy == wave.expiryCcy ) {
306
305
// Disable any waveforms that are done
307
306
waveform.enabled ^= 1UL << pin;
308
307
}
309
308
else {
310
309
const uint32_t idleCcys = wave.periodCcys - wave.dutyCcys ;
311
-
8000
// get true accumulated overshoot
312
- overshootCcys = now - ((waveform.states & (1UL << pin)) ? wave.endDutyCcy : wave.nextPeriodCcy );
313
- uint32_t fwdPeriods = static_cast <uint32_t >(overshootCcys) >= idleCcys ?
310
+ // get true accumulated overshoot, guaranteed >= 0 in this spot
311
+ const uint32_t overshootCcys = now - ((waveform.states & (1UL << pin)) ? wave.endDutyCcy : wave.nextPeriodCcy );
312
+ const uint32_t fwdPeriods = static_cast <uint32_t >(overshootCcys) >= idleCcys ?
314
313
((overshootCcys + wave.dutyCcys ) / wave.periodCcys ) : 0 ;
314
+ const uint32_t fwdPeriodCcys = fwdPeriods * wave.periodCcys ;
315
315
uint32_t nextEdgeCcy;
316
316
if (waveform.states & (1UL << pin)) {
317
- if (!wave.autoPwm ) {
318
- overshootCcys = 0 ;
319
- }
320
317
// up to and including this period 100% duty
321
318
const bool endOfPeriod = wave.nextPeriodCcy == wave.endDutyCcy ;
322
319
// active configuration and forward 100% duty
323
320
if (!idleCcys) {
324
- wave.nextPeriodCcy += (fwdPeriods + 1 ) * wave.periodCcys ;
321
+ wave.nextPeriodCcy += fwdPeriodCcys + wave.periodCcys ;
325
322
wave.endDutyCcy = wave.nextPeriodCcy ;
326
323
nextEdgeCcy = wave.nextPeriodCcy ;
327
324
}
328
325
else if (endOfPeriod) {
329
326
// preceeding period had zero idle cycle, continue direct into new duty cycle
330
327
if (fwdPeriods) {
331
- wave.nextPeriodCcy += fwdPeriods * wave. periodCcys ;
328
+ wave.nextPeriodCcy += fwdPeriodCcys ;
332
329
// adapt expiry such that it occurs during intended cycle
333
330
if (WaveformMode::EXPIRES == wave.mode )
334
- wave.expiryCcy += fwdPeriods * wave. periodCcys ;
331
+ wave.expiryCcy += fwdPeriodCcys ;
335
332
}
336
333
wave.endDutyCcy = wave.nextPeriodCcy + wave.dutyCcys ;
337
334
wave.nextPeriodCcy += wave.periodCcys ;
338
335
nextEdgeCcy = wave.endDutyCcy ;
339
336
}
340
337
else {
341
338
waveform.states ^= 1UL << pin;
339
+ nextEdgeCcy = wave.nextPeriodCcy ;
342
340
// the idle cycle code updating for the next period will approximate the duty/idle ratio.
343
- nextEdgeCcy = wave.nextPeriodCcy + overshootCcys;
341
+ if (wave.autoPwm ) {
342
+ nextEdgeCcy += overshootCcys;
343
+ }
344
344
if (pin == 16 ) {
345
345
GP16O &= ~1 ; // GPIO16 write slow as it's RMW
346
346
}
@@ -351,7 +351,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
351
351
}
352
352
else {
353
353
if (!wave.dutyCcys ) {
354
- wave.nextPeriodCcy += (fwdPeriods + 1 ) * wave.periodCcys ;
354
+ wave.nextPeriodCcy += fwdPeriodCcys + wave.periodCcys ;
355
355
wave.endDutyCcy = wave.nextPeriodCcy ;
356
356
}
357
357
else {
@@ -362,12 +362,12 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
362
362
{
363
363
if (wave.autoPwm ) {
364
364
// maintain phase, maintain duty/idle ratio, temporarily reduce frequency by fwdPeriods
365
- wave.endDutyCcy += (fwdPeriods + 1 ) * wave.dutyCcys - fwdPeriods * wave. periodCcys ;
365
+ wave.endDutyCcy += wave.dutyCcys - fwdPeriods * idleCcys ;
366
366
}
367
- wave.nextPeriodCcy += fwdPeriods * wave. periodCcys ;
367
+ wave.nextPeriodCcy += fwdPeriodCcys ;
368
368
// adapt expiry such that it occurs during intended cycle
369
369
if (WaveformMode::EXPIRES == wave.mode )
370
- wave.expiryCcy += fwdPeriods * wave. periodCcys ;
370
+ wave.expiryCcy += fwdPeriodCcys ;
371
371
}
372
372
if (pin == 16 ) {
373
373
GP16O |= 1 ; // GPIO16 write slow as it's RMW
0 commit comments