gobot.io/x/gobot@v1.16.0/platforms/firmata/ble_firmata_adaptor.go (about)

     1  // +build !windows
     2  
     3  package firmata
     4  
     5  import (
     6  	"io"
     7  
     8  	"gobot.io/x/gobot"
     9  	"gobot.io/x/gobot/platforms/ble"
    10  )
    11  
    12  const (
    13  	// ReceiveID is the BLE characteristic ID for receiving serial data
    14  	ReceiveID = "6e400003b5a3f393e0a9e50e24dcca9e"
    15  
    16  	// TransmitID is the BLE characteristic ID for transmitting serial data
    17  	TransmitID = "6e400002b5a3f393e0a9e50e24dcca9e"
    18  )
    19  
    20  // BLEAdaptor represents a Bluetooth LE based connection to a
    21  // microcontroller running FirmataBLE
    22  type BLEAdaptor struct {
    23  	*Adaptor
    24  }
    25  
    26  // NewBLEAdaptor opens and uses a BLE connection to a
    27  // microcontroller running FirmataBLE
    28  func NewBLEAdaptor(args ...interface{}) *BLEAdaptor {
    29  	address := args[0].(string)
    30  	rid := ReceiveID
    31  	wid := TransmitID
    32  
    33  	if len(args) >= 3 {
    34  		rid = args[1].(string)
    35  		wid = args[2].(string)
    36  	}
    37  
    38  	a := NewAdaptor(address)
    39  	a.SetName(gobot.DefaultName("BLEFirmata"))
    40  	a.PortOpener = func(port string) (io.ReadWriteCloser, error) {
    41  		sp := ble.NewSerialPort(address, rid, wid)
    42  		sp.Open()
    43  		return sp, nil
    44  	}
    45  
    46  	return &BLEAdaptor{
    47  		Adaptor: a,
    48  	}
    49  }