gobot.io/x/gobot/v2@v2.1.0/platforms/firmata/ble_firmata_adaptor.go (about)

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