Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RSDK-7335] Remove Close() from and Add Write() to Analog interface in board #3809

Merged
merged 6 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion components/board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ type Board interface {
type Analog interface {
// Read reads off the current value.
Read(ctx context.Context, extra map[string]interface{}) (int, error)
Close(ctx context.Context) error

// Write writes a value to the analog pin.
Write(ctx context.Context, value int, extra map[string]interface{}) error
}

// FromDependencies is a helper for getting the named board from a collection of
Expand Down
12 changes: 8 additions & 4 deletions components/board/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,16 @@ type analogClient struct {
analogName string
}

func (arc *analogClient) Read(ctx context.Context, extra map[string]interface{}) (int, error) {
func (ac *analogClient) Read(ctx context.Context, extra map[string]interface{}) (int, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return 0, err
}
// the api method is named ReadAnalogReader, it is named differenlty than
// the board interface functions.
resp, err := arc.client.client.ReadAnalogReader(ctx, &pb.ReadAnalogReaderRequest{
BoardName: arc.boardName,
AnalogReaderName: arc.analogName,
resp, err := ac.client.client.ReadAnalogReader(ctx, &pb.ReadAnalogReaderRequest{
BoardName: ac.boardName,
AnalogReaderName: ac.analogName,
Extra: ext,
})
if err != nil {
Expand All @@ -223,6 +223,10 @@ func (arc *analogClient) Read(ctx context.Context, extra map[string]interface{})
return int(resp.Value), nil
}

func (ac *analogClient) Write(ctx context.Context, value int, extra map[string]interface{}) error {
return errors.New("unimplemented")
randhid marked this conversation as resolved.
Show resolved Hide resolved
}

// digitalInterruptClient satisfies a gRPC based board.DigitalInterrupt. Refer to the
// interface for descriptions of its methods.
type digitalInterruptClient struct {
Expand Down
5 changes: 5 additions & 0 deletions components/board/fake/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ func (a *Analog) Read(ctx context.Context, extra map[string]interface{}) (int, e
return a.Value, nil
}

func (a *Analog) Write(ctx context.Context, value int, extra map[string]interface{}) error {
a.Set(value)
return nil
}

// Set is used during testing.
func (a *Analog) Set(value int) {
a.Mu.Lock()
Expand Down
4 changes: 4 additions & 0 deletions components/board/genericlinux/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ func (a *wrappedAnalogReader) reset(ctx context.Context, chipSelect string, read
a.chipSelect = chipSelect
}

func (a *wrappedAnalogReader) Write(ctx context.Context, value int, extra map[string]interface{}) error {
return grpc.UnimplementedError
}

// Board implements a component for a Linux machine.
type Board struct {
resource.Named
Expand Down
5 changes: 5 additions & 0 deletions components/board/mcp3008helper/mcp3008.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go.uber.org/multierr"

"go.viam.com/rdk/components/board/genericlinux/buses"
"go.viam.com/rdk/grpc"
"go.viam.com/rdk/resource"
)

Expand Down Expand Up @@ -65,3 +66,7 @@ func (mar *MCP3008AnalogReader) Read(ctx context.Context, extra map[string]inter
func (mar *MCP3008AnalogReader) Close(ctx context.Context) error {
return nil
}

func (mar *MCP3008AnalogReader) Write(ctx context.Context, value int, extra map[string]interface{}) error {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current flow for the pi/generic linux:

  • make a MCP3008 analog helper,
  • pass that into AnalogSMoother, it needs Close function implemented.
  • use that analog smoother to fulfill the Read and Write methods in analog for board's Analog struct.

This can probably be better. Probably by obliterating it.

return grpc.UnimplementedError
}
12 changes: 6 additions & 6 deletions components/board/numato/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type numatoBoard struct {
resource.Named
resource.AlwaysRebuild
pins int
analogs map[string]board.Analog
analogs map[string]*pinwrappers.AnalogSmoother
penguinland marked this conversation as resolved.
Show resolved Hide resolved

port io.ReadWriteCloser
closed int32
Expand Down Expand Up @@ -360,16 +360,16 @@ type analog struct {
pin string
}

func (ar *analog) Read(ctx context.Context, extra map[string]interface{}) (int, error) {
res, err := ar.b.doSendReceive(ctx, fmt.Sprintf("adc read %s", ar.pin))
func (a *analog) Read(ctx context.Context, extra map[string]interface{}) (int, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the numato code can either not use the analog smoother or implement a Close more gracefully. We'll handle this driver in our next bug bash.

res, err := a.b.doSendReceive(ctx, fmt.Sprintf("adc read %s", a.pin))
if err != nil {
return 0, err
}
return strconv.Atoi(res)
}

func (ar *analog) Close(ctx context.Context) error {
return nil
func (a *analog) Write(ctx context.Context, value int, extra map[string]interface{}) error {
return grpc.UnimplementedError
}

func connect(ctx context.Context, name resource.Name, conf *Config, logger logging.Logger) (board.Board, error) {
Expand Down Expand Up @@ -404,7 +404,7 @@ func connect(ctx context.Context, name resource.Name, conf *Config, logger loggi
logger: logger,
}

b.analogs = map[string]board.Analog{}
b.analogs = map[string]*pinwrappers.AnalogSmoother{}
for _, c := range conf.Analogs {
r := &analog{b, c.Pin}
b.analogs[c.Name] = pinwrappers.SmoothAnalogReader(r, c, logger)
Expand Down
6 changes: 3 additions & 3 deletions components/board/pi/impl/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type piPigpio struct {
cancelFunc context.CancelFunc
duty int // added for mutex
gpioConfigSet map[int]bool
analogReaders map[string]board.Analog
analogReaders map[string]*pinwrappers.AnalogSmoother
penguinland marked this conversation as resolved.
Show resolved Hide resolved
// `interrupts` maps interrupt names to the interrupts. `interruptsHW` maps broadcom addresses
// to these same values. The two should always have the same set of values.
interrupts map[string]ReconfigurableDigitalInterrupt
Expand Down Expand Up @@ -222,7 +222,7 @@ func (pi *piPigpio) StreamTicks(ctx context.Context, interruptNames []string, ch

func (pi *piPigpio) reconfigureAnalogReaders(ctx context.Context, cfg *Config) error {
// No need to reconfigure the old analog readers; just throw them out and make new ones.
pi.analogReaders = map[string]board.Analog{}
pi.analogReaders = map[string]*pinwrappers.AnalogSmoother{}
for _, ac := range cfg.AnalogReaders {
channel, err := strconv.Atoi(ac.Pin)
if err != nil {
Expand Down Expand Up @@ -734,7 +734,7 @@ func (pi *piPigpio) Close(ctx context.Context) error {
for _, analog := range pi.analogReaders {
err = multierr.Combine(err, analog.Close(ctx))
}
pi.analogReaders = map[string]board.Analog{}
pi.analogReaders = map[string]*pinwrappers.AnalogSmoother{}

for bcom, interrupt := range pi.interruptsHW {
err = multierr.Combine(err, interrupt.Close(ctx))
Expand Down
5 changes: 5 additions & 0 deletions components/board/pinwrappers/analog_smoother_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.viam.com/utils/testutils"

"go.viam.com/rdk/components/board"
"go.viam.com/rdk/grpc"
"go.viam.com/rdk/logging"
)

Expand All @@ -32,6 +33,10 @@ func (t *testReader) Read(ctx context.Context, extra map[string]interface{}) (in
return t.r.Intn(100), nil
}

func (t *testReader) Write(ctx context.Context, value int, extra map[string]interface{}) error {
randhid marked this conversation as resolved.
Show resolved Hide resolved
return grpc.UnimplementedError
}

func (t *testReader) Close(ctx context.Context) error {
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions components/board/pinwrappers/analogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
goutils "go.viam.com/utils"

"go.viam.com/rdk/components/board"
"go.viam.com/rdk/grpc"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/utils"
)
Expand Down Expand Up @@ -140,3 +141,7 @@ func (as *AnalogSmoother) Start(ctx context.Context) {
}
}, as.activeBackgroundWorkers.Done)
}

func (as *AnalogSmoother) Write(ctx context.Context, value int, extra map[string]interface{}) error {
return grpc.UnimplementedError
}
24 changes: 22 additions & 2 deletions testutils/inject/analog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
// Analog is an injected analog pin.
type Analog struct {
board.Analog
ReadFunc func(ctx context.Context, extra map[string]interface{}) (int, error)
readCap []interface{}
ReadFunc func(ctx context.Context, extra map[string]interface{}) (int, error)
readCap []interface{}
WriteFunc func(ctx context.Context, value int, extra map[string]interface{}) error
writeCap []interface{}
}

// Read calls the injected Read or the real version.
Expand All @@ -30,3 +32,21 @@ func (a *Analog) ReadCap() []interface{} {
defer func() { a.readCap = nil }()
return a.readCap
}

// Write calls the injected Write or the real version.
func (a *Analog) Write(ctx context.Context, value int, extra map[string]interface{}) error {
a.readCap = []interface{}{ctx}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the documentation you left for WriteCap, I suspect we should also store the value here.

...though TBH I don't understand when you'd ever need to call WriteCap() or ReadCap() and don't really get why this code exists at all. The one use of ReadCap in our codebase is used to assert that the context passed into a function really is the context passed in, which seems like kind of a useless test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not going to change the pattern of inject implementations as part of this pr. They could probably be better, but they do their job of injecting interfaces for testing, and I'm happy with that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not mean to change the pattern; I don't think you're following the pre-existing pattern here. For example, in testutils/inject/gpio_pin.go, the captures store all of the arguments except extra, which would include storing value here.

I wonder if there's some other reason not to store value and to set things up the way you did, but I don't see that reason myself, partly because I don't understand why the pattern has been set up this way in the first place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, no you're right, we should store value here. tbh I do think our injects could be better. The Cap functions are usually to capture the input arguments to the injected function to be checked against in server-client code tests. But to be frank our team almost never has a need to use the Cap functions in testing the drivers, because we care about testing the behaviour of inputs not validating that we've sent the inputs. The Cap functions are usually used in server tests for resource APIs.

... the inject package could be better and lessconfusing overall, maybe one day.

randhid marked this conversation as resolved.
Show resolved Hide resolved
if a.ReadFunc == nil {
randhid marked this conversation as resolved.
Show resolved Hide resolved
randhid marked this conversation as resolved.
Show resolved Hide resolved
return a.Analog.Write(ctx, value, extra)
}
return a.WriteFunc(ctx, value, extra)
}

// WriteCap returns the last parameters received by Write, and then clears them.
func (a *Analog) WriteCap() []interface{} {
if a == nil {
return nil
}
defer func() { a.writeCap = nil }()
return a.writeCap
}
Loading