diff --git a/autotune.go b/autotune.go index e850ad3f..1f85be33 100644 --- a/autotune.go +++ b/autotune.go @@ -27,20 +27,18 @@ func (tune *autoTune) Sample(bit bool, seq uint32) { // Period // Falling Edge Rising Edge func (tune *autoTune) FindPeriod(bit bool) int { - // find left and right edges for a bit + // last pulse and initial index setup lastPulse := tune.pulses[0] idx := 1 // left edge var leftEdge int for ; idx < len(tune.pulses); idx++ { - if lastPulse.seq+1 == tune.pulses[idx].seq { - if lastPulse.bit != bit && tune.pulses[idx].bit == bit { + if lastPulse.bit != bit && tune.pulses[idx].bit == bit { // edge found + if lastPulse.seq+1 == tune.pulses[idx].seq { // ensure edge continuity leftEdge = idx break } - } else { - return -1 } lastPulse = tune.pulses[idx] } @@ -51,8 +49,8 @@ func (tune *autoTune) FindPeriod(bit bool) int { idx = leftEdge + 1 for ; idx < len(tune.pulses); idx++ { - if lastPulse.seq+1 == tune.pulses[idx].seq { - if lastPulse.bit == bit && tune.pulses[idx].bit != bit { + if lastPulse.seq+1 == tune.pulses[idx].seq { // ensure pulses in this level monotonic + if lastPulse.bit == bit && tune.pulses[idx].bit != bit { // edge found rightEdge = idx break }