gobot.io/x/gobot/v2@v2.1.0/platforms/microbit/button_driver.go (about) 1 package microbit 2 3 import ( 4 "gobot.io/x/gobot/v2" 5 "gobot.io/x/gobot/v2/platforms/ble" 6 ) 7 8 // ButtonDriver is the Gobot driver for the Microbit's built-in buttons 9 type ButtonDriver struct { 10 name string 11 connection gobot.Connection 12 gobot.Eventer 13 } 14 15 const ( 16 // BLE services 17 buttonService = "e95d9882251d470aa062fa1922dfa9a8" 18 19 // BLE characteristics 20 buttonACharacteristic = "e95dda90251d470aa062fa1922dfa9a8" 21 buttonBCharacteristic = "e95dda91251d470aa062fa1922dfa9a8" 22 23 // ButtonA event 24 ButtonA = "buttonA" 25 26 // ButtonB event 27 ButtonB = "buttonB" 28 ) 29 30 // NewButtonDriver creates a Microbit ButtonDriver 31 func NewButtonDriver(a ble.BLEConnector) *ButtonDriver { 32 n := &ButtonDriver{ 33 name: gobot.DefaultName("Microbit Button"), 34 connection: a, 35 Eventer: gobot.NewEventer(), 36 } 37 38 n.AddEvent(ButtonA) 39 n.AddEvent(ButtonB) 40 41 return n 42 } 43 44 // Connection returns the BLE connection 45 func (b *ButtonDriver) Connection() gobot.Connection { return b.connection } 46 47 // Name returns the Driver Name 48 func (b *ButtonDriver) Name() string { return b.name } 49 50 // SetName sets the Driver Name 51 func (b *ButtonDriver) SetName(n string) { b.name = n } 52 53 // adaptor returns BLE adaptor 54 func (b *ButtonDriver) adaptor() ble.BLEConnector { 55 return b.Connection().(ble.BLEConnector) 56 } 57 58 // Start tells driver to get ready to do work 59 func (b *ButtonDriver) Start() (err error) { 60 // subscribe to button A notifications 61 b.adaptor().Subscribe(buttonACharacteristic, func(data []byte, e error) { 62 b.Publish(b.Event(ButtonA), data) 63 }) 64 65 // subscribe to button B notifications 66 b.adaptor().Subscribe(buttonBCharacteristic, func(data []byte, e error) { 67 b.Publish(b.Event(ButtonB), data) 68 }) 69 70 return 71 } 72 73 // Halt stops LED driver (void) 74 func (b *ButtonDriver) Halt() (err error) { 75 return 76 }