Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3ecb369

Browse filesBrowse files
committed
Some optimizing.
1 parent adac2c1 commit 3ecb369
Copy full SHA for 3ecb369

File tree

Expand file treeCollapse file tree

1 file changed

+16
-16
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-16
lines changed

‎cores/esp8266/core_esp8266_waveform.cpp

Copy file name to clipboardExpand all lines: cores/esp8266/core_esp8266_waveform.cpp
+16-16Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -300,47 +300,47 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
300300

301301
Waveform& wave = waveform.pins[pin];
302302

303-
int32_t overshootCcys = now - wave.nextEventCcy;
304-
if (overshootCcys >= 0) {
303+
if (static_cast<int32_t>(now - wave.nextEventCcy) >= 0) {
305304
if (WaveformMode::EXPIRES == wave.mode && wave.nextEventCcy == wave.expiryCcy) {
306305
// Disable any waveforms that are done
307306
waveform.enabled ^= 1UL << pin;
308307
}
309308
else {
310309
const uint32_t idleCcys = wave.periodCcys - wave.dutyCcys;
311-
// 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 ?
314313
((overshootCcys + wave.dutyCcys) / wave.periodCcys) : 0;
314+
const uint32_t fwdPeriodCcys = fwdPeriods * wave.periodCcys;
315315
uint32_t nextEdgeCcy;
316316
if (waveform.states & (1UL << pin)) {
317-
if (!wave.autoPwm) {
318-
overshootCcys = 0;
319-
}
320317
// up to and including this period 100% duty
321318
const bool endOfPeriod = wave.nextPeriodCcy == wave.endDutyCcy;
322319
// active configuration and forward 100% duty
323320
if (!idleCcys) {
324-
wave.nextPeriodCcy += (fwdPeriods + 1) * wave.periodCcys;
321+
wave.nextPeriodCcy += fwdPeriodCcys + wave.periodCcys;
325322
wave.endDutyCcy = wave.nextPeriodCcy;
326323
nextEdgeCcy = wave.nextPeriodCcy;
327324
}
328325
else if (endOfPeriod) {
329326
// preceeding period had zero idle cycle, continue direct into new duty cycle
330327
if (fwdPeriods) {
331-
wave.nextPeriodCcy += fwdPeriods * wave.periodCcys;
328+
wave.nextPeriodCcy += fwdPeriodCcys;
332329
// adapt expiry such that it occurs during intended cycle
333330
if (WaveformMode::EXPIRES == wave.mode)
334-
wave.expiryCcy += fwdPeriods * wave.periodCcys;
331+
wave.expiryCcy += fwdPeriodCcys;
335332
}
336333
wave.endDutyCcy = wave.nextPeriodCcy + wave.dutyCcys;
337334
wave.nextPeriodCcy += wave.periodCcys;
338335
nextEdgeCcy = wave.endDutyCcy;
339336
}
340337
else {
341338
waveform.states ^= 1UL << pin;
339+
nextEdgeCcy = wave.nextPeriodCcy;
342340
// 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+
}
344344
if (pin == 16) {
345345
GP16O &= ~1; // GPIO16 write slow as it's RMW
346346
}
@@ -351,7 +351,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
351351
}
352352
else {
353353
if (!wave.dutyCcys) {
354-
wave.nextPeriodCcy += (fwdPeriods + 1) * wave.periodCcys;
354+
wave.nextPeriodCcy += fwdPeriodCcys + wave.periodCcys;
355355
wave.endDutyCcy = wave.nextPeriodCcy;
356356
}
357357
else {
@@ -362,12 +362,12 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
362362
{
363363
if (wave.autoPwm) {
364364
// 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;
366366
}
367-
wave.nextPeriodCcy += fwdPeriods * wave.periodCcys;
367+
wave.nextPeriodCcy += fwdPeriodCcys;
368368
// adapt expiry such that it occurs during intended cycle
369369
if (WaveformMode::EXPIRES == wave.mode)
370-
wave.expiryCcy += fwdPeriods * wave.periodCcys;
370+
wave.expiryCcy += fwdPeriodCcys;
371371
}
372372
if (pin == 16) {
373373
GP16O |= 1; // GPIO16 write slow as it's RMW

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.