2015-06-08 10:31:00 +08:00
|
|
|
package ble
|
|
|
|
|
|
|
|
import (
|
2015-06-30 06:25:59 +08:00
|
|
|
"bytes"
|
2015-06-08 10:31:00 +08:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*BLEBatteryDriver)(nil)
|
|
|
|
|
|
|
|
type BLEBatteryDriver struct {
|
|
|
|
name string
|
|
|
|
connection gobot.Connection
|
|
|
|
gobot.Eventer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBLEBatteryDriver creates a BLEBatteryDriver by name
|
|
|
|
func NewBLEBatteryDriver(a *BLEAdaptor, name string) *BLEBatteryDriver {
|
|
|
|
n := &BLEBatteryDriver{
|
|
|
|
name: name,
|
|
|
|
connection: a,
|
|
|
|
Eventer: gobot.NewEventer(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// n.AddEvent("extended")
|
|
|
|
// n.AddEvent("signal")
|
|
|
|
// n.AddEvent("attention")
|
|
|
|
// n.AddEvent("meditation")
|
|
|
|
// n.AddEvent("blink")
|
|
|
|
// n.AddEvent("wave")
|
|
|
|
// n.AddEvent("eeg")
|
|
|
|
// n.AddEvent("error")
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
func (b *BLEBatteryDriver) Connection() gobot.Connection { return b.connection }
|
|
|
|
func (b *BLEBatteryDriver) Name() string { return b.name }
|
|
|
|
|
|
|
|
// adaptor returns BLE adaptor
|
|
|
|
func (b *BLEBatteryDriver) adaptor() *BLEAdaptor {
|
|
|
|
return b.Connection().(*BLEAdaptor)
|
|
|
|
}
|
|
|
|
|
2015-06-30 06:25:59 +08:00
|
|
|
// Start tells driver to get ready to do work
|
2015-06-08 10:31:00 +08:00
|
|
|
func (b *BLEBatteryDriver) Start() (errs []error) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Halt stops battery driver (void)
|
|
|
|
func (b *BLEBatteryDriver) Halt() (errs []error) { return }
|
|
|
|
|
2015-06-30 06:25:59 +08:00
|
|
|
func (b *BLEBatteryDriver) GetBatteryLevel() (level uint8) {
|
|
|
|
var l uint8
|
|
|
|
c, _ := b.adaptor().ReadCharacteristic("180f", "2a19")
|
|
|
|
buf := bytes.NewBuffer(<-c)
|
|
|
|
val, _ := buf.ReadByte()
|
|
|
|
l = uint8(val)
|
|
|
|
return l
|
|
|
|
}
|