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-09-26 02:14:05 +08:00
|
|
|
var _ gobot.Driver = (*BatteryDriver)(nil)
|
2015-06-08 10:31:00 +08:00
|
|
|
|
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
|
2016-10-04 01:06:37 +08:00
|
|
|
func NewBatteryDriver(a *ClientAdaptor) *BatteryDriver {
|
2016-09-26 02:14:05 +08:00
|
|
|
n := &BatteryDriver{
|
2016-10-04 01:06:37 +08:00
|
|
|
name: "Battery",
|
2015-06-08 10:31:00 +08:00
|
|
|
connection: a,
|
|
|
|
Eventer: gobot.NewEventer(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
2016-09-26 02:14:05 +08:00
|
|
|
func (b *BatteryDriver) Connection() gobot.Connection { return b.connection }
|
|
|
|
func (b *BatteryDriver) Name() string { return b.name }
|
|
|
|
func (b *BatteryDriver) SetName(n string) { b.name = n }
|
2015-06-08 10:31:00 +08:00
|
|
|
|
|
|
|
// adaptor returns BLE adaptor
|
2016-09-26 02:14:05 +08:00
|
|
|
func (b *BatteryDriver) adaptor() *ClientAdaptor {
|
|
|
|
return b.Connection().(*ClientAdaptor)
|
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-09-26 02:14:05 +08:00
|
|
|
func (b *BatteryDriver) GetBatteryLevel() (level uint8) {
|
2015-06-30 06:25:59 +08:00
|
|
|
var l uint8
|
|
|
|
c, _ := b.adaptor().ReadCharacteristic("180f", "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
|
|
|
|
}
|