Skip to content

Commit 88fc14a

Browse files
committed
optimize left edge finding in autotune
1 parent dd1ac88 commit 88fc14a

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

autotune.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,18 @@ func (tune *autoTune) Sample(bit bool, seq uint32) {
2727
// Period
2828
// Falling Edge Rising Edge
2929
func (tune *autoTune) FindPeriod(bit bool) int {
30-
// find left and right edges for a bit
30+
// last pulse and initial index setup
3131
lastPulse := tune.pulses[0]
3232
idx := 1
3333

3434
// left edge
3535
var leftEdge int
3636
for ; idx < len(tune.pulses); idx++ {
37-
if lastPulse.seq+1 == tune.pulses[idx].seq {
38-
if lastPulse.bit != bit && tune.pulses[idx].bit == bit {
37+
if lastPulse.bit != bit && tune.pulses[idx].bit == bit { // edge found
38+
if lastPulse.seq+1 == tune.pulses[idx].seq { // ensure edge continuity
3939
leftEdge = idx
4040
break
4141
}
42-
} else {
43-
return -1
4442
}
4543
lastPulse = tune.pulses[idx]
4644
}
@@ -51,8 +49,8 @@ func (tune *autoTune) FindPeriod(bit bool) int {
5149
idx = leftEdge + 1
5250

5351
for ; idx < len(tune.pulses); idx++ {
54-
if lastPulse.seq+1 == tune.pulses[idx].seq {
55-
if lastPulse.bit == bit && tune.pulses[idx].bit != bit {
52+
if lastPulse.seq+1 == tune.pulses[idx].seq { // ensure pulses in this level monotonic
53+
if lastPulse.bit == bit && tune.pulses[idx].bit != bit { // edge found
5654
rightEdge = idx
5755
break
5856
}

0 commit comments

Comments
 (0)