gobot.io/x/gobot/v2@v2.1.0/platforms/parrot/minidrone/helpers_test.go (about)

     1  package minidrone
     2  
     3  import (
     4  	"sync"
     5  
     6  	"gobot.io/x/gobot/v2/platforms/ble"
     7  )
     8  
     9  var _ ble.BLEConnector = (*bleTestClientAdaptor)(nil)
    10  
    11  type bleTestClientAdaptor struct {
    12  	name                    string
    13  	address                 string
    14  	mtx                     sync.Mutex
    15  	withoutResponses        bool
    16  	testReadCharacteristic  func(string) ([]byte, error)
    17  	testWriteCharacteristic func(string, []byte) error
    18  }
    19  
    20  func (t *bleTestClientAdaptor) Connect() (err error)      { return }
    21  func (t *bleTestClientAdaptor) Reconnect() (err error)    { return }
    22  func (t *bleTestClientAdaptor) Disconnect() (err error)   { return }
    23  func (t *bleTestClientAdaptor) Finalize() (err error)     { return }
    24  func (t *bleTestClientAdaptor) Name() string              { return t.name }
    25  func (t *bleTestClientAdaptor) SetName(n string)          { t.name = n }
    26  func (t *bleTestClientAdaptor) Address() string           { return t.address }
    27  func (t *bleTestClientAdaptor) WithoutResponses(use bool) { t.withoutResponses = use }
    28  
    29  func (t *bleTestClientAdaptor) ReadCharacteristic(cUUID string) (data []byte, err error) {
    30  	t.mtx.Lock()
    31  	defer t.mtx.Unlock()
    32  	return t.testReadCharacteristic(cUUID)
    33  }
    34  
    35  func (t *bleTestClientAdaptor) WriteCharacteristic(cUUID string, data []byte) (err error) {
    36  	t.mtx.Lock()
    37  	defer t.mtx.Unlock()
    38  	return t.testWriteCharacteristic(cUUID, data)
    39  }
    40  
    41  func (t *bleTestClientAdaptor) Subscribe(cUUID string, f func([]byte, error)) (err error) {
    42  	// TODO: implement this...
    43  	return
    44  }
    45  
    46  func (t *bleTestClientAdaptor) TestReadCharacteristic(f func(cUUID string) (data []byte, err error)) {
    47  	t.mtx.Lock()
    48  	defer t.mtx.Unlock()
    49  	t.testReadCharacteristic = f
    50  }
    51  
    52  func (t *bleTestClientAdaptor) TestWriteCharacteristic(f func(cUUID string, data []byte) (err error)) {
    53  	t.mtx.Lock()
    54  	defer t.mtx.Unlock()
    55  	t.testWriteCharacteristic = f
    56  }
    57  
    58  func NewBleTestAdaptor() *bleTestClientAdaptor {
    59  	return &bleTestClientAdaptor{
    60  		address: "01:02:03:04:05:06",
    61  		testReadCharacteristic: func(cUUID string) (data []byte, e error) {
    62  			return
    63  		},
    64  		testWriteCharacteristic: func(cUUID string, data []byte) (e error) {
    65  			return
    66  		},
    67  	}
    68  }