gobot.io/x/gobot/v2@v2.1.0/drivers/aio/helpers_test.go (about)

     1  package aio
     2  
     3  import "sync"
     4  
     5  type aioTestBareAdaptor struct{}
     6  
     7  func (t *aioTestBareAdaptor) Connect() (err error)  { return }
     8  func (t *aioTestBareAdaptor) Finalize() (err error) { return }
     9  func (t *aioTestBareAdaptor) Name() string          { return "" }
    10  func (t *aioTestBareAdaptor) SetName(n string)      {}
    11  
    12  type aioTestAdaptor struct {
    13  	name                   string
    14  	port                   string
    15  	mtx                    sync.Mutex
    16  	testAdaptorAnalogRead  func() (val int, err error)
    17  	testAdaptorAnalogWrite func(val int) (err error)
    18  	written                []int
    19  }
    20  
    21  func (t *aioTestAdaptor) TestAdaptorAnalogRead(f func() (val int, err error)) {
    22  	t.mtx.Lock()
    23  	defer t.mtx.Unlock()
    24  	t.testAdaptorAnalogRead = f
    25  }
    26  
    27  func (t *aioTestAdaptor) TestAdaptorAnalogWrite(f func(val int) (err error)) {
    28  	t.mtx.Lock()
    29  	defer t.mtx.Unlock()
    30  	t.testAdaptorAnalogWrite = f
    31  }
    32  
    33  func (t *aioTestAdaptor) AnalogRead(pin string) (val int, err error) {
    34  	t.mtx.Lock()
    35  	defer t.mtx.Unlock()
    36  	return t.testAdaptorAnalogRead()
    37  }
    38  
    39  func (t *aioTestAdaptor) AnalogWrite(pin string, val int) (err error) {
    40  	t.mtx.Lock()
    41  	defer t.mtx.Unlock()
    42  	t.written = append(t.written, val)
    43  	return t.testAdaptorAnalogWrite(val)
    44  }
    45  
    46  func (t *aioTestAdaptor) Connect() (err error)  { return }
    47  func (t *aioTestAdaptor) Finalize() (err error) { return }
    48  func (t *aioTestAdaptor) Name() string          { return t.name }
    49  func (t *aioTestAdaptor) SetName(n string)      { t.name = n }
    50  func (t *aioTestAdaptor) Port() string          { return t.port }
    51  
    52  func newAioTestAdaptor() *aioTestAdaptor {
    53  	return &aioTestAdaptor{
    54  		port: "/dev/null",
    55  		testAdaptorAnalogRead: func() (val int, err error) {
    56  			return 99, nil
    57  		},
    58  		testAdaptorAnalogWrite: func(val int) (err error) {
    59  			return nil
    60  		},
    61  	}
    62  }