Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/oliviamiller/rdk
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviamiller committed Apr 17, 2024
2 parents 28ff656 + c5f8e8b commit 24cf7eb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions components/board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
commonpb "go.viam.com/api/common/v1"
pb "go.viam.com/api/component/board/v1"

"go.viam.com/rdk/components/board/pinwrappers"
"go.viam.com/rdk/data"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/robot"
Expand Down
16 changes: 16 additions & 0 deletions components/board/pinwrappers/digital_interrupts.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,19 @@ func (i *BasicDigitalInterrupt) Reconfigure(conf board.DigitalInterruptConfig) e
i.cfg = conf
return nil
}

// RemoveCallback removes a listener for interrupts.
func RemoveCallback(di board.DigitalInterrupt, c chan board.Tick) {
i := di.(*BasicDigitalInterrupt)
i.mu.Lock()
defer i.mu.Unlock()
for id := range i.callbacks {
if i.callbacks[id] == c {
// To remove this item, we replace it with the last item in the list, then truncate the
// list by 1.
i.callbacks[id] = i.callbacks[len(i.callbacks)-1]
i.callbacks = i.callbacks[:len(i.callbacks)-1]
break
}
}
}
6 changes: 3 additions & 3 deletions components/board/pinwrappers/digital_interrupts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestBasicDigitalInterrupt1(t *testing.T) {
test.That(t, v.High, test.ShouldBeTrue)
test.That(t, v.TimestampNanosec, test.ShouldEqual, timeNanoSec)

i.RemoveCallback(c)
RemoveCallback(i, c)

c = make(chan board.Tick, 2)
i.AddCallback(c)
Expand Down Expand Up @@ -111,8 +111,8 @@ func TestRemoveCallbackDigitalInterrupt(t *testing.T) {
i.AddCallback(c2)
test.That(t, ret, test.ShouldBeTrue)

i.RemoveCallback(c1)
i.RemoveCallback(c1)
RemoveCallback(i, c1)
RemoveCallback(i, c1)

ret2 := false
result := make(chan bool, 1)
Expand Down

0 comments on commit 24cf7eb

Please sign in to comment.