-
Notifications
You must be signed in to change notification settings - Fork 1
/
wind.go
52 lines (46 loc) · 1.09 KB
/
wind.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
package gorfxtrx
import (
"encoding/binary"
"fmt"
)
// Struct for the Wind packets.
type Wind struct {
data []byte
typeId byte
SequenceNumber byte
id uint16
Direction uint16
AverageSpeed float64
Gust float64
Battery byte
Rssi byte
}
var windTypes = map[byte]string{
0x01: "WTGR800",
0x02: "WGR800",
0x03: "STR918, WGR918",
0x04: "TFA",
}
func (self *Wind) Receive(data []byte) {
self.data = data
self.typeId = data[2]
self.SequenceNumber = data[3]
self.id = binary.BigEndian.Uint16(data[4:6])
self.Direction = binary.BigEndian.Uint16(data[6:8])
self.AverageSpeed = float64(binary.BigEndian.Uint16(data[8:10])) / 10
self.Gust = float64(binary.BigEndian.Uint16(data[10:12])) / 10
if self.typeId == 0x03 {
self.Battery = (data[16] + 1) * 10
} else {
self.Battery = (data[16] & 0x0f) * 10
self.Rssi = data[16] >> 4
}
}
// Id of the device.
func (self *Wind) Id() string {
return fmt.Sprintf("%02x:%02x", self.id>>8, self.id&0xff)
}
// Type of the device.
func (self *Wind) Type() string {
return windTypes[self.typeId]
}