-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformat_v3.go
113 lines (96 loc) · 2.66 KB
/
format_v3.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
package ruuvitag
import (
"encoding/binary"
"fmt"
"time"
)
// dataFormat3 (also known as RAWv1) measurement
// @see https://github.com/ruuvi/ruuvi-sensor-protocols/blob/master/dataformat_03.md
type dataFormat3 struct {
deviceID string
format uint8
humidity uint8
temperature int8
temperatureFraction uint8
pressure uint16
accelerationX int16
accelerationY int16
accelerationZ int16
batteryVoltage uint16
timestamp time.Time
}
func (f *dataFormat3) DeviceID() string {
return f.deviceID
}
func (f *dataFormat3) Format() uint8 {
return f.format
}
func (f *dataFormat3) Humidity() float32 {
return float32(f.humidity) / 2
}
func (f *dataFormat3) Temperature() float32 {
if f.temperature < 0 {
return float32(f.temperature) - (float32(f.temperatureFraction) / 100)
}
return float32(f.temperature) + (float32(f.temperatureFraction) / 100)
}
func (f *dataFormat3) Pressure() uint32 {
return uint32(f.pressure) + 50000
}
func (f *dataFormat3) AccelerationX() float32 {
return float32(f.accelerationX) / 1000
}
func (f *dataFormat3) AccelerationY() float32 {
return float32(f.accelerationY) / 1000
}
func (f *dataFormat3) AccelerationZ() float32 {
return float32(f.accelerationZ) / 1000
}
func (f *dataFormat3) BatteryVoltage() float32 {
return float32(f.batteryVoltage) / 1000
}
func (f *dataFormat3) Timestamp() time.Time {
return f.timestamp
}
func (f *dataFormat3) TXPower() int8 {
return 0
}
func (f *dataFormat3) MovementCounter() uint8 {
return 0
}
func (f *dataFormat3) Sequence() uint16 {
return 0
}
// NewDataFormat3 https://github.com/ruuvi/ruuvi-sensor-protocols/blob/master/broadcast_formats.md
func NewDataFormat3(ID string, data []byte) (Measurement, error) {
if len(data) != 16 {
return nil, fmt.Errorf("manufacturer data lenght mismatch")
}
if data[2] != 3 {
return nil, fmt.Errorf("data format mismatch (%d)", data[2])
}
m := dataFormat3{
deviceID: ID,
format: data[2],
humidity: uint8(data[3]),
temperature: msbSignedByteToInt8(data[4]),
temperatureFraction: uint8(data[5]),
pressure: binary.BigEndian.Uint16(data[6:8]),
accelerationX: int16(binary.BigEndian.Uint16(data[8:10])),
accelerationY: int16(binary.BigEndian.Uint16(data[10:12])),
accelerationZ: int16(binary.BigEndian.Uint16(data[12:14])),
batteryVoltage: binary.BigEndian.Uint16(data[14:16]),
timestamp: time.Now(),
}
return &m, nil
}
func msbSignedByteToInt8(value byte) int8 {
sign := value >> 7
var v int8
if sign == 1 {
v = -int8(value >> 1)
} else {
v = int8(value >> 0)
}
return v
}