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
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
2015-06-08 10:31:00 +08:00
|
|
|
)
|
|
|
|
|
2016-12-09 19:15:33 +08:00
|
|
|
// BatteryDriver represents the Battery Service for a BLE Peripheral
|
2016-09-26 02:14:05 +08:00
|
|
|
type BatteryDriver struct {
|
2015-06-08 10:31:00 +08:00
|
|
|
name string
|
|
|
|
connection gobot.Connection
|
|
|
|
gobot.Eventer
|
|
|
|
}
|
|
|
|
|
2016-09-26 02:14:05 +08:00
|
|
|
// NewBatteryDriver creates a BatteryDriver
|
2017-04-05 17:47:28 +08:00
|
|
|
func NewBatteryDriver(a BLEConnector) *BatteryDriver {
|
2016-09-26 02:14:05 +08:00
|
|
|
n := &BatteryDriver{
|
2017-02-02 22:56:26 +08:00
|
|
|
name: gobot.DefaultName("Battery"),
|
2015-06-08 10:31:00 +08:00
|
|
|
connection: a,
|
|
|
|
Eventer: gobot.NewEventer(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
2016-12-09 19:15:33 +08:00
|
|
|
|
|
|
|
// Connection returns the Driver's Connection to the associated Adaptor
|
2016-09-26 02:14:05 +08:00
|
|
|
func (b *BatteryDriver) Connection() gobot.Connection { return b.connection }
|
2016-12-09 19:15:33 +08:00
|
|
|
|
|
|
|
// Name returns the Driver name
|
|
|
|
func (b *BatteryDriver) Name() string { return b.name }
|
|
|
|
|
|
|
|
// SetName sets the Driver name
|
|
|
|
func (b *BatteryDriver) SetName(n string) { b.name = n }
|
2015-06-08 10:31:00 +08:00
|
|
|
|
|
|
|
// adaptor returns BLE adaptor
|
2017-04-05 17:47:28 +08:00
|
|
|
func (b *BatteryDriver) adaptor() BLEConnector {
|
|
|
|
return b.Connection().(BLEConnector)
|
2015-06-08 10:31:00 +08:00
|
|
|
}
|
|
|
|
|
2015-06-30 06:25:59 +08:00
|
|
|
// Start tells driver to get ready to do work
|
2016-11-08 00:38:14 +08:00
|
|
|
func (b *BatteryDriver) Start() (err error) {
|
2015-06-08 10:31:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Halt stops battery driver (void)
|
2016-11-08 00:38:14 +08:00
|
|
|
func (b *BatteryDriver) Halt() (err error) { return }
|
2015-06-08 10:31:00 +08:00
|
|
|
|
2016-12-09 19:15:33 +08:00
|
|
|
// GetBatteryLevel reads and returns the current battery level
|
2016-09-26 02:14:05 +08:00
|
|
|
func (b *BatteryDriver) GetBatteryLevel() (level uint8) {
|
2015-06-30 06:25:59 +08:00
|
|
|
var l uint8
|
2016-12-29 00:53:41 +08:00
|
|
|
c, _ := b.adaptor().ReadCharacteristic("2a19")
|
2016-03-03 14:43:41 +08:00
|
|
|
buf := bytes.NewBuffer(c)
|
2015-06-30 06:25:59 +08:00
|
|
|
val, _ := buf.ReadByte()
|
|
|
|
l = uint8(val)
|
|
|
|
return l
|
|
|
|
}
|