-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery_driver.go
57 lines (45 loc) · 1.33 KB
/
battery_driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package ble
import (
"bytes"
"gobot.io/x/gobot"
)
// BatteryDriver represents the Battery Service for a BLE Peripheral
type BatteryDriver struct {
name string
connection gobot.Connection
gobot.Eventer
}
// NewBatteryDriver creates a BatteryDriver
func NewBatteryDriver(a *ClientAdaptor) *BatteryDriver {
n := &BatteryDriver{
name: gobot.DefaultName("Battery"),
connection: a,
Eventer: gobot.NewEventer(),
}
return n
}
// Connection returns the Driver's Connection to the associated Adaptor
func (b *BatteryDriver) Connection() gobot.Connection { return b.connection }
// 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 }
// adaptor returns BLE adaptor
func (b *BatteryDriver) adaptor() *ClientAdaptor {
return b.Connection().(*ClientAdaptor)
}
// Start tells driver to get ready to do work
func (b *BatteryDriver) Start() (err error) {
return
}
// Halt stops battery driver (void)
func (b *BatteryDriver) Halt() (err error) { return }
// GetBatteryLevel reads and returns the current battery level
func (b *BatteryDriver) GetBatteryLevel() (level uint8) {
var l uint8
c, _ := b.adaptor().ReadCharacteristic("2a19")
buf := bytes.NewBuffer(c)
val, _ := buf.ReadByte()
l = uint8(val)
return l
}