gobot.io/x/gobot@v1.16.0/platforms/microbit/helpers_test.go (about)

     1  package microbit
     2  
     3  import (
     4  	"sync"
     5  
     6  	"gobot.io/x/gobot/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  
    17  	testSubscribe           func([]byte, error)
    18  	testReadCharacteristic  func(string) ([]byte, error)
    19  	testWriteCharacteristic func(string, []byte) error
    20  }
    21  
    22  func (t *bleTestClientAdaptor) Connect() (err error)      { return }
    23  func (t *bleTestClientAdaptor) Reconnect() (err error)    { return }
    24  func (t *bleTestClientAdaptor) Disconnect() (err error)   { return }
    25  func (t *bleTestClientAdaptor) Finalize() (err error)     { return }
    26  func (t *bleTestClientAdaptor) Name() string              { return t.name }
    27  func (t *bleTestClientAdaptor) SetName(n string)          { t.name = n }
    28  func (t *bleTestClientAdaptor) Address() string           { return t.address }
    29  func (t *bleTestClientAdaptor) WithoutResponses(use bool) { t.withoutResponses = use }
    30  
    31  func (t *bleTestClientAdaptor) ReadCharacteristic(cUUID string) (data []byte, err error) {
    32  	t.mtx.Lock()
    33  	defer t.mtx.Unlock()
    34  	return t.testReadCharacteristic(cUUID)
    35  }
    36  
    37  func (t *bleTestClientAdaptor) WriteCharacteristic(cUUID string, data []byte) (err error) {
    38  	t.mtx.Lock()
    39  	defer t.mtx.Unlock()
    40  	return t.testWriteCharacteristic(cUUID, data)
    41  }
    42  
    43  func (t *bleTestClientAdaptor) Subscribe(cUUID string, f func([]byte, error)) (err error) {
    44  	t.testSubscribe = f
    45  	return
    46  }
    47  
    48  func (t *bleTestClientAdaptor) TestReadCharacteristic(f func(cUUID string) (data []byte, err error)) {
    49  	t.mtx.Lock()
    50  	defer t.mtx.Unlock()
    51  	t.testReadCharacteristic = f
    52  }
    53  
    54  func (t *bleTestClientAdaptor) TestWriteCharacteristic(f func(cUUID string, data []byte) (err error)) {
    55  	t.mtx.Lock()
    56  	defer t.mtx.Unlock()
    57  	t.testWriteCharacteristic = f
    58  }
    59  
    60  func (t *bleTestClientAdaptor) TestReceiveNotification(data []byte, err error) {
    61  	t.mtx.Lock()
    62  	defer t.mtx.Unlock()
    63  	t.testSubscribe(data, err)
    64  }
    65  
    66  func NewBleTestAdaptor() *bleTestClientAdaptor {
    67  	return &bleTestClientAdaptor{
    68  		address: "01:02:03:04:05:06",
    69  		testReadCharacteristic: func(cUUID string) (data []byte, e error) {
    70  			return
    71  		},
    72  		testWriteCharacteristic: func(cUUID string, data []byte) (e error) {
    73  			return
    74  		},
    75  		testSubscribe: func([]byte, error) {
    76  			return
    77  		},
    78  	}
    79  }