Skip to content

Commit

Permalink
[RSDK-7321] Rename AnalogReaders -> Analogs (#3804)
Browse files Browse the repository at this point in the history
  • Loading branch information
randhid committed Apr 16, 2024
1 parent 9313d94 commit 78e1625
Show file tree
Hide file tree
Showing 25 changed files with 223 additions and 219 deletions.
16 changes: 8 additions & 8 deletions components/board/board.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package board defines the interfaces that typically live on a single-board computer
// such as a Raspberry Pi.
//
// Besides the board itself, some other interfaces it defines are analog readers and digital interrupts.
// Besides the board itself, some other interfaces it defines are analog pins and digital interrupts.
package board

import (
Expand Down Expand Up @@ -49,21 +49,21 @@ func Named(name string) resource.Name {
}

// A Board represents a physical general purpose board that contains various
// components such as analog readers, and digital interrupts.
// components such as analogs, and digital interrupts.
type Board interface {
resource.Resource

// AnalogReaderByName returns an analog reader by name.
AnalogReaderByName(name string) (AnalogReader, bool)
// AnalogByName returns an analog pin by name.
AnalogByName(name string) (Analog, bool)

// DigitalInterruptByName returns a digital interrupt by name.
DigitalInterruptByName(name string) (DigitalInterrupt, bool)

// GPIOPinByName returns a GPIOPin by name.
GPIOPinByName(name string) (GPIOPin, error)

// AnalogReaderNames returns the names of all known analog readers.
AnalogReaderNames() []string
// AnalogNames returns the names of all known analog pins.
AnalogNames() []string

// DigitalInterruptNames returns the names of all known digital interrupts.
DigitalInterruptNames() []string
Expand All @@ -85,8 +85,8 @@ type Board interface {
StreamTicks(ctx context.Context, interrupts []string, ch chan Tick, extra map[string]interface{}) error
}

// An AnalogReader represents an analog pin reader that resides on a board.
type AnalogReader interface {
// An Analog represents an analog pin that resides on a board.
type Analog interface {
// Read reads off the current value.
Read(ctx context.Context, extra map[string]interface{}) (int, error)
Close(ctx context.Context) error
Expand Down
34 changes: 18 additions & 16 deletions components/board/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type client struct {

type boardInfo struct {
name string
analogReaderNames []string
analogNames []string
digitalInterruptNames []string
}

Expand All @@ -68,11 +68,11 @@ func NewClientFromConn(
return c, nil
}

func (c *client) AnalogReaderByName(name string) (AnalogReader, bool) {
return &analogReaderClient{
client: c,
boardName: c.info.name,
analogReaderName: name,
func (c *client) AnalogByName(name string) (Analog, bool) {
return &analogClient{
client: c,
boardName: c.info.name,
analogName: name,
}, true
}

Expand All @@ -92,12 +92,12 @@ func (c *client) GPIOPinByName(name string) (GPIOPin, error) {
}, nil
}

func (c *client) AnalogReaderNames() []string {
func (c *client) AnalogNames() []string {
if c.getCachedStatus() == nil {
c.logger.Debugw("no cached status")
return []string{}
}
return copyStringSlice(c.info.analogReaderNames)
return copyStringSlice(c.info.analogNames)
}

func (c *client) DigitalInterruptNames() []string {
Expand Down Expand Up @@ -133,9 +133,9 @@ func (c *client) refresh(ctx context.Context) error {
}
c.storeStatus(status)

c.info.analogReaderNames = []string{}
c.info.analogNames = []string{}
for name := range status.Analogs {
c.info.analogReaderNames = append(c.info.analogReaderNames, name)
c.info.analogNames = append(c.info.analogNames, name)
}
c.info.digitalInterruptNames = []string{}
for name := range status.DigitalInterrupts {
Expand Down Expand Up @@ -197,22 +197,24 @@ func (c *client) WriteAnalog(ctx context.Context, pin string, value int32, extra
return err
}

// analogReaderClient satisfies a gRPC based board.AnalogReader. Refer to the interface
// analogClient satisfies a gRPC based board.AnalogReader. Refer to the interface
// for descriptions of its methods.
type analogReaderClient struct {
type analogClient struct {
*client
boardName string
analogReaderName string
boardName string
analogName string
}

func (arc *analogReaderClient) Read(ctx context.Context, extra map[string]interface{}) (int, error) {
func (arc *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.analogReaderName,
AnalogReaderName: arc.analogName,
Extra: ext,
})
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions components/board/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,17 @@ func TestWorkingClient(t *testing.T) {
test.That(t, actualExtra, test.ShouldResemble, expectedExtra)
actualExtra = nil

// Analog Reader
injectAnalogReader := &inject.AnalogReader{}
injectBoard.AnalogReaderByNameFunc = func(name string) (board.AnalogReader, bool) {
return injectAnalogReader, true
// Analog
injectAnalog := &inject.Analog{}
injectBoard.AnaloByNameFunc = func(name string) (board.Analog, bool) {
return injectAnalog, true
}
analog1, ok := injectBoard.AnalogReaderByName("analog1")
analog1, ok := injectBoard.AnalogByName("analog1")
test.That(t, ok, test.ShouldBeTrue)
test.That(t, injectBoard.AnalogReaderByNameCap(), test.ShouldResemble, []interface{}{"analog1"})
test.That(t, injectBoard.AnalogByNameCap(), test.ShouldResemble, []interface{}{"analog1"})

// Analog Reader:Read
injectAnalogReader.ReadFunc = func(ctx context.Context, extra map[string]interface{}) (int, error) {
// Analog: Read
injectAnalog.ReadFunc = func(ctx context.Context, extra map[string]interface{}) (int, error) {
actualExtra = extra
return 6, nil
}
Expand Down Expand Up @@ -258,8 +258,8 @@ func TestClientWithStatus(t *testing.T) {

test.That(t, injectBoard.StatusCap()[1:], test.ShouldResemble, []interface{}{})

respAnalogReaders := client.AnalogReaderNames()
test.That(t, respAnalogReaders, test.ShouldResemble, []string{"analog1"})
respAnalogs := client.AnalogNames()
test.That(t, respAnalogs, test.ShouldResemble, []string{"analog1"})

respDigitalInterrupts := client.DigitalInterruptNames()
test.That(t, respDigitalInterrupts, test.ShouldResemble, []string{"digital1"})
Expand Down Expand Up @@ -296,7 +296,7 @@ func TestClientWithoutStatus(t *testing.T) {

test.That(t, injectBoard.StatusCap()[1:], test.ShouldResemble, []interface{}{})

test.That(t, rClient.AnalogReaderNames(), test.ShouldResemble, []string{})
test.That(t, rClient.AnalogNames(), test.ShouldResemble, []string{})
test.That(t, rClient.DigitalInterruptNames(), test.ShouldResemble, []string{})

err = rClient.Close(context.Background())
Expand Down
4 changes: 3 additions & 1 deletion components/board/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
type method int64

const (
// we need analog pins that are readers, not writers,
// to collect data from.
analogReaderNameKey = "reader_name"
gpioPinNameKey = "pin_name"
analogs method = iota
Expand Down Expand Up @@ -43,7 +45,7 @@ func newAnalogCollector(resource interface{}, params data.CollectorParams) (data
return nil, data.FailedToReadErr(params.ComponentName, analogs.String(),
errors.New("Must supply reader_name in additional_params for analog collector"))
}
if reader, ok := board.AnalogReaderByName(arg[analogReaderNameKey].String()); ok {
if reader, ok := board.AnalogByName(arg[analogReaderNameKey].String()); ok {
value, err = reader.Read(ctx, data.FromDMExtraMap)
if err != nil {
// A modular filter component can be created to filter the readings from a component. The error ErrNoCaptureToStore
Expand Down
8 changes: 4 additions & 4 deletions components/board/collectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ func TestCollectors(t *testing.T) {

func newBoard() board.Board {
b := &inject.Board{}
analogReader := &inject.AnalogReader{}
analogReader.ReadFunc = func(ctx context.Context, extra map[string]interface{}) (int, error) {
analog := &inject.Analog{}
analog.ReadFunc = func(ctx context.Context, extra map[string]interface{}) (int, error) {
return 1, nil
}
b.AnalogReaderByNameFunc = func(name string) (board.AnalogReader, bool) {
return analogReader, true
b.AnaloByNameFunc = func(name string) (board.Analog, bool) {
return analog, true
}
gpioPin := &inject.GPIOPin{}
gpioPin.GetFunc = func(ctx context.Context, extra map[string]interface{}) (bool, error) {
Expand Down
66 changes: 33 additions & 33 deletions components/board/fake/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func init() {
// NewBoard returns a new fake board.
func NewBoard(ctx context.Context, conf resource.Config, logger logging.Logger) (*Board, error) {
b := &Board{
Named: conf.ResourceName().AsNamed(),
AnalogReaders: map[string]*AnalogReader{},
Digitals: map[string]*DigitalInterruptWrapper{},
GPIOPins: map[string]*GPIOPin{},
logger: logger,
Named: conf.ResourceName().AsNamed(),
Analogs: map[string]*Analog{},
Digitals: map[string]*DigitalInterruptWrapper{},
GPIOPins: map[string]*GPIOPin{},
logger: logger,
}

if err := b.processConfig(conf); err != nil {
Expand All @@ -99,19 +99,19 @@ func (b *Board) processConfig(conf resource.Config) error {

for _, c := range newConf.AnalogReaders {
stillExists[c.Name] = struct{}{}
if curr, ok := b.AnalogReaders[c.Name]; ok {
if curr, ok := b.Analogs[c.Name]; ok {
if curr.pin != c.Pin {
curr.reset(c.Pin)
}
continue
}
b.AnalogReaders[c.Name] = newAnalogReader(c.Pin)
b.Analogs[c.Name] = newAnalogReader(c.Pin)
}
for name := range b.AnalogReaders {
for name := range b.Analogs {
if _, ok := stillExists[name]; ok {
continue
}
delete(b.AnalogReaders, name)
delete(b.Analogs, name)
}
stillExists = map[string]struct{}{}

Expand Down Expand Up @@ -140,7 +140,7 @@ func (b *Board) processConfig(conf resource.Config) error {
return nil
}

// Reconfigure atomically reconfigures this board© in place based on the new config.
// Reconfigure atomically reconfigures this board in place based on the new config.
func (b *Board) Reconfigure(ctx context.Context, deps resource.Dependencies, conf resource.Config) error {
return b.processConfig(conf)
}
Expand All @@ -149,19 +149,19 @@ func (b *Board) Reconfigure(ctx context.Context, deps resource.Dependencies, con
type Board struct {
resource.Named

mu sync.RWMutex
AnalogReaders map[string]*AnalogReader
Digitals map[string]*DigitalInterruptWrapper
GPIOPins map[string]*GPIOPin
logger logging.Logger
CloseCount int
mu sync.RWMutex
Analogs map[string]*Analog
Digitals map[string]*DigitalInterruptWrapper
GPIOPins map[string]*GPIOPin
logger logging.Logger
CloseCount int
}

// AnalogReaderByName returns the analog reader by the given name if it exists.
func (b *Board) AnalogReaderByName(name string) (board.AnalogReader, bool) {
// AnalogByName returns the analog pin by the given name if it exists.
func (b *Board) AnalogByName(name string) (board.Analog, bool) {
b.mu.RLock()
defer b.mu.RUnlock()
a, ok := b.AnalogReaders[name]
a, ok := b.Analogs[name]
return a, ok
}

Expand All @@ -186,12 +186,12 @@ func (b *Board) GPIOPinByName(name string) (board.GPIOPin, error) {
return p, nil
}

// AnalogReaderNames returns the names of all known analog readers.
func (b *Board) AnalogReaderNames() []string {
// AnalogNames returns the names of all known analog pins.
func (b *Board) AnalogNames() []string {
b.mu.RLock()
defer b.mu.RUnlock()
names := []string{}
for k := range b.AnalogReaders {
for k := range b.Analogs {
names = append(names, k)
}
return names
Expand Down Expand Up @@ -222,8 +222,8 @@ func (b *Board) SetPowerMode(ctx context.Context, mode pb.PowerMode, duration *t

// WriteAnalog writes the value to the given pin, which can be read back by adding it to AnalogReaders.
func (b *Board) WriteAnalog(ctx context.Context, pin string, value int32, extra map[string]interface{}) error {
alg := &AnalogReader{pin: pin, Value: int(value)}
b.AnalogReaders[pin] = alg
alg := &Analog{pin: pin, Value: int(value)}
b.Analogs[pin] = alg
return nil
}

Expand Down Expand Up @@ -252,7 +252,7 @@ func (b *Board) Close(ctx context.Context) error {
b.CloseCount++
var err error

for _, analog := range b.AnalogReaders {
for _, analog := range b.Analogs {
err = multierr.Combine(err, analog.Close(ctx))
}
for _, digital := range b.Digitals {
Expand All @@ -261,40 +261,40 @@ func (b *Board) Close(ctx context.Context) error {
return err
}

// An AnalogReader reads back the same set value.
type AnalogReader struct {
// An Analog reads back the same set value.
type Analog struct {
pin string
Value int
CloseCount int
Mu sync.RWMutex
}

func newAnalogReader(pin string) *AnalogReader {
return &AnalogReader{pin: pin}
func newAnalogReader(pin string) *Analog {
return &Analog{pin: pin}
}

func (a *AnalogReader) reset(pin string) {
func (a *Analog) reset(pin string) {
a.Mu.Lock()
a.pin = pin
a.Value = 0
a.Mu.Unlock()
}

func (a *AnalogReader) Read(ctx context.Context, extra map[string]interface{}) (int, error) {
func (a *Analog) Read(ctx context.Context, extra map[string]interface{}) (int, error) {
a.Mu.RLock()
defer a.Mu.RUnlock()
return a.Value, nil
}

// Set is used during testing.
func (a *AnalogReader) Set(value int) {
func (a *Analog) Set(value int) {
a.Mu.Lock()
defer a.Mu.Unlock()
a.Value = value
}

// Close does nothing.
func (a *AnalogReader) Close(ctx context.Context) error {
func (a *Analog) Close(ctx context.Context) error {
a.CloseCount++
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion components/board/fake/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestFakeBoard(t *testing.T) {
b, err := NewBoard(context.Background(), cfg, logger)
test.That(t, err, test.ShouldBeNil)

_, ok := b.AnalogReaderByName("blue")
_, ok := b.AnalogByName("blue")
test.That(t, ok, test.ShouldBeTrue)

_, ok = b.DigitalInterruptByName("i1")
Expand Down
Loading

0 comments on commit 78e1625

Please sign in to comment.