forked from gbionescu/go-idasen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbledesk.go
127 lines (104 loc) · 3 KB
/
bledesk.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"encoding/binary"
"log"
"math"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/ble"
)
// This is the desks minimum height
const baseHeight = 63.00
// Absolute difference between desired height and current height
// used when moving the desk.
const positionDiff = 0.2
// Interval to sleep between issuing command and measuring the position
const sleepInterval = 100 * time.Millisecond
const deskBLEPosition = "99fa0021-338a-1024-8a49-009c0215f78a"
const deskBleControl = "99fa0002-338a-1024-8a49-009c0215f78a"
var deskBLEUp = uint16(71)
var deskBLEDown = uint16(70)
var deskBLEStop = uint16(255)
// BLE desk driver stucture that plugs in to gobot
type deskDriver struct {
name string
connection gobot.Connection
gobot.Eventer
}
// Create a new Idasen BLE driver
func newDeskDriver(a ble.BLEConnector) *deskDriver {
n := &deskDriver{
name: gobot.DefaultName("IdasenDriver"),
connection: a,
Eventer: gobot.NewEventer(),
}
return n
}
// Connection returns the Driver's Connection to the associated Adaptor
func (b *deskDriver) Connection() gobot.Connection { return b.connection }
// adaptor returns BLE adaptor
func (b *deskDriver) adaptor() ble.BLEConnector {
return b.Connection().(ble.BLEConnector)
}
// Start tells driver to get ready to do work
func (b *deskDriver) Start() (err error) {
return
}
// Halt stops battery driver (void)
func (b *deskDriver) Halt() (err error) { return }
// Gets current desk position and returns a float between 65 and 128
func (b *deskDriver) getPosition() (level float64) {
c, err := b.adaptor().ReadCharacteristic(deskBLEPosition)
if err != nil {
log.Println(err)
return
}
return baseHeight + float64(binary.LittleEndian.Uint16(c))/100
}
// Moves desk up by sending a BLE command
// Does not have any control on how much to move
func (b *deskDriver) moveUp() {
moveCmd := make([]byte, 2)
binary.LittleEndian.PutUint16(moveCmd, deskBLEUp)
err := b.adaptor().WriteCharacteristic(deskBleControl, moveCmd)
if err != nil {
log.Println(err)
return
}
}
// Moves desk down by sending a BLE command
// Does not have any control on how much to move
func (b *deskDriver) moveDown() {
moveCmd := make([]byte, 2)
binary.LittleEndian.PutUint16(moveCmd, deskBLEDown)
err := b.adaptor().WriteCharacteristic(deskBleControl, moveCmd)
if err != nil {
log.Println(err)
return
}
}
// Stop the movement of th desk by sending a BLE command
func (b *deskDriver) moveStop() {
moveCmd := make([]byte, 2)
binary.LittleEndian.PutUint16(moveCmd, deskBLEStop)
err := b.adaptor().WriteCharacteristic(deskBleControl, moveCmd)
if err != nil {
log.Println(err)
return
}
}
// Moves the desk to a given position
func (b *deskDriver) move(position float64) {
for {
crtPosition := b.getPosition()
// If current position is within range, break - we're done
if math.Abs(crtPosition-position) <= positionDiff {
b.moveStop()
return
} else if crtPosition < position {
b.moveUp()
} else if crtPosition >= position {
b.moveDown()
}
}
}