Skip to content

Commit 8969907

Browse files
committed
Change float32 to float64
1 parent 54438c2 commit 8969907

File tree

4 files changed

+57
-56
lines changed

4 files changed

+57
-56
lines changed

decoder.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ var ErrInvalidChannelType = errors.New("cayennelpp: unknown type")
1616
type UplinkTarget interface {
1717
DigitalInput(channel, value uint8)
1818
DigitalOutput(channel, value uint8)
19-
AnalogInput(channel uint8, value float32)
20-
AnalogOutput(channel uint8, value float32)
19+
AnalogInput(channel uint8, value float64)
20+
AnalogOutput(channel uint8, value float64)
2121
Luminosity(channel uint8, value uint16)
2222
Presence(channel, value uint8)
23-
Temperature(channel uint8, celcius float32)
24-
RelativeHumidity(channel uint8, rh float32)
25-
Accelerometer(channel uint8, x, y, z float32)
26-
BarometricPressure(channel uint8, hpa float32)
27-
Gyrometer(channel uint8, x, y, z float32)
28-
GPS(channel uint8, latitude, longitude, altitude float32)
23+
Temperature(channel uint8, celcius float64)
24+
RelativeHumidity(channel uint8, rh float64)
25+
Accelerometer(channel uint8, x, y, z float64)
26+
BarometricPressure(channel uint8, hpa float64)
27+
Gyrometer(channel uint8, x, y, z float64)
28+
GPS(channel uint8, latitude, longitude, altitude float64)
2929
}
3030

3131
// DownlinkTarget represents a target that processes decoded downlink values.
3232
type DownlinkTarget interface {
33-
Port(channel uint8, value float32)
33+
Port(channel uint8, value float64)
3434
}
3535

3636
// Decoder decodes CayenneLPP encoded values.
@@ -110,7 +110,7 @@ func (d *decoder) DecodeDownlink(target DownlinkTarget) error {
110110
if err := binary.Read(d.r, binary.BigEndian, &val); err != nil {
111111
return err
112112
}
113-
target.Port(buf[0], float32(val)/100)
113+
target.Port(buf[0], float64(val)/100)
114114
}
115115
return nil
116116
}
@@ -138,7 +138,7 @@ func (d *decoder) decodeAnalogInput(channel uint8, target UplinkTarget) error {
138138
if err := binary.Read(d.r, binary.BigEndian, &val); err != nil {
139139
return err
140140
}
141-
target.AnalogInput(channel, float32(val)/100)
141+
target.AnalogInput(channel, float64(val)/100)
142142
return nil
143143
}
144144

@@ -147,7 +147,7 @@ func (d *decoder) decodeAnalogOutput(channel uint8, target UplinkTarget) error {
147147
if err := binary.Read(d.r, binary.BigEndian, &val); err != nil {
148148
return err
149149
}
150-
target.AnalogOutput(channel, float32(val)/100)
150+
target.AnalogOutput(channel, float64(val)/100)
151151
return nil
152152
}
153153

@@ -174,7 +174,7 @@ func (d *decoder) decodeTemperature(channel uint8, target UplinkTarget) error {
174174
if err := binary.Read(d.r, binary.BigEndian, &val); err != nil {
175175
return err
176176
}
177-
target.Temperature(channel, float32(val)/10)
177+
target.Temperature(channel, float64(val)/10)
178178
return nil
179179
}
180180

@@ -183,7 +183,7 @@ func (d *decoder) decodeRelativeHumidity(channel uint8, target UplinkTarget) err
183183
if err := binary.Read(d.r, binary.BigEndian, &val); err != nil {
184184
return err
185185
}
186-
target.RelativeHumidity(channel, float32(val)/2)
186+
target.RelativeHumidity(channel, float64(val)/2)
187187
return nil
188188
}
189189

@@ -198,7 +198,7 @@ func (d *decoder) decodeAccelerometer(channel uint8, target UplinkTarget) error
198198
if err := binary.Read(d.r, binary.BigEndian, &valZ); err != nil {
199199
return err
200200
}
201-
target.Accelerometer(channel, float32(valX)/1000, float32(valY)/1000, float32(valZ)/1000)
201+
target.Accelerometer(channel, float64(valX)/1000, float64(valY)/1000, float64(valZ)/1000)
202202
return nil
203203
}
204204

@@ -207,7 +207,7 @@ func (d *decoder) decodeBarometricPressure(channel uint8, target UplinkTarget) e
207207
if err := binary.Read(d.r, binary.BigEndian, &val); err != nil {
208208
return err
209209
}
210-
target.BarometricPressure(channel, float32(val)/10)
210+
target.BarometricPressure(channel, float64(val)/10)
211211
return nil
212212
}
213213

@@ -222,7 +222,7 @@ func (d *decoder) decodeGyrometer(channel uint8, target UplinkTarget) error {
222222
if err := binary.Read(d.r, binary.BigEndian, &valZ); err != nil {
223223
return err
224224
}
225-
target.Gyrometer(channel, float32(valX)/100, float32(valY)/100, float32(valZ)/100)
225+
target.Gyrometer(channel, float64(valX)/100, float64(valY)/100, float64(valZ)/100)
226226
return nil
227227
}
228228

@@ -238,8 +238,9 @@ func (d *decoder) decodeGPS(channel uint8, target UplinkTarget) error {
238238
altitude := make([]byte, 4)
239239
copy(altitude, buf[6:9])
240240
target.GPS(channel,
241-
float32(int32(binary.BigEndian.Uint32(latitude))>>8)/10000,
242-
float32(int32(binary.BigEndian.Uint32(longitude))>>8)/10000,
243-
float32(int32(binary.BigEndian.Uint32(altitude))>>8)/100)
241+
float64(int32(binary.BigEndian.Uint32(latitude))>>8)/10000,
242+
float64(int32(binary.BigEndian.Uint32(longitude))>>8)/10000,
243+
float64(int32(binary.BigEndian.Uint32(altitude))>>8)/100,
244+
)
244245
return nil
245246
}

decoder_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type target struct {
1616
values map[uint8]interface{}
1717
}
1818

19-
func (t *target) Port(channel uint8, value float32) {
19+
func (t *target) Port(channel uint8, value float64) {
2020
t.values[channel] = value
2121
}
2222

@@ -28,11 +28,11 @@ func (t *target) DigitalOutput(channel, value uint8) {
2828
t.values[channel] = value
2929
}
3030

31-
func (t *target) AnalogInput(channel uint8, value float32) {
31+
func (t *target) AnalogInput(channel uint8, value float64) {
3232
t.values[channel] = value
3333
}
3434

35-
func (t *target) AnalogOutput(channel uint8, value float32) {
35+
func (t *target) AnalogOutput(channel uint8, value float64) {
3636
t.values[channel] = value
3737
}
3838

@@ -44,28 +44,28 @@ func (t *target) Presence(channel, value uint8) {
4444
t.values[channel] = value
4545
}
4646

47-
func (t *target) Temperature(channel uint8, celcius float32) {
47+
func (t *target) Temperature(channel uint8, celcius float64) {
4848
t.values[channel] = celcius
4949
}
5050

51-
func (t *target) RelativeHumidity(channel uint8, rh float32) {
51+
func (t *target) RelativeHumidity(channel uint8, rh float64) {
5252
t.values[channel] = rh
5353
}
5454

55-
func (t *target) Accelerometer(channel uint8, x, y, z float32) {
56-
t.values[channel] = []float32{x, y, z}
55+
func (t *target) Accelerometer(channel uint8, x, y, z float64) {
56+
t.values[channel] = []float64{x, y, z}
5757
}
5858

59-
func (t *target) BarometricPressure(channel uint8, hpa float32) {
59+
func (t *target) BarometricPressure(channel uint8, hpa float64) {
6060
t.values[channel] = hpa
6161
}
6262

63-
func (t *target) Gyrometer(channel uint8, x, y, z float32) {
64-
t.values[channel] = []float32{x, y, z}
63+
func (t *target) Gyrometer(channel uint8, x, y, z float64) {
64+
t.values[channel] = []float64{x, y, z}
6565
}
6666

67-
func (t *target) GPS(channel uint8, latitude, longitude, altitude float32) {
68-
t.values[channel] = []float32{latitude, longitude, altitude}
67+
func (t *target) GPS(channel uint8, latitude, longitude, altitude float64) {
68+
t.values[channel] = []float64{latitude, longitude, altitude}
6969
}
7070

7171
func TestDecode(t *testing.T) {
@@ -100,10 +100,10 @@ func TestDecode(t *testing.T) {
100100
a.So(target.values[6], ShouldEqual, 50)
101101
a.So(target.values[7], ShouldEqual, -15.6)
102102
a.So(target.values[8], ShouldEqual, 80)
103-
a.So(target.values[9], ShouldResemble, []float32{-0.424, 0.015, 1.666})
103+
a.So(target.values[9], ShouldResemble, []float64{-0.424, 0.015, 1.666})
104104
a.So(target.values[10], ShouldEqual, 1073.5)
105-
a.So(target.values[11], ShouldResemble, []float32{3.55, 5.61, -4.10})
106-
a.So(target.values[12], ShouldResemble, []float32{52.3655, 4.8885, 21.54})
105+
a.So(target.values[11], ShouldResemble, []float64{3.55, 5.61, -4.10})
106+
a.So(target.values[12], ShouldResemble, []float64{52.3655, 4.8885, 21.54})
107107
}
108108

109109
// Happy flow: downlink
@@ -166,6 +166,6 @@ func TestDecode(t *testing.T) {
166166

167167
err := decoder.DecodeUplink(target)
168168
a.So(err, ShouldBeNil)
169-
a.So(target.values[1], ShouldResemble, []float32{42.3519, -87.9094, 10})
169+
a.So(target.values[1], ShouldResemble, []float64{42.3519, -87.9094, 10})
170170
}
171171
}

encoder.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ type Encoder interface {
1515
Bytes() []byte
1616
Reset()
1717
WriteTo(w io.Writer) (int64, error)
18-
AddPort(channel uint8, value float32)
18+
AddPort(channel uint8, value float64)
1919
AddDigitalInput(channel, value uint8)
2020
AddDigitalOutput(channel, value uint8)
21-
AddAnalogInput(channel uint8, value float32)
22-
AddAnalogOutput(channel uint8, value float32)
21+
AddAnalogInput(channel uint8, value float64)
22+
AddAnalogOutput(channel uint8, value float64)
2323
AddLuminosity(channel uint8, value uint16)
2424
AddPresence(channel, value uint8)
25-
AddTemperature(channel uint8, celcius float32)
26-
AddRelativeHumidity(channel uint8, rh float32)
27-
AddAccelerometer(channel uint8, x, y, z float32)
28-
AddBarometricPressure(channel uint8, hpa float32)
29-
AddGyrometer(channel uint8, x, y, z float32)
30-
AddGPS(channel uint8, latitude, longitude, meters float32)
25+
AddTemperature(channel uint8, celcius float64)
26+
AddRelativeHumidity(channel uint8, rh float64)
27+
AddAccelerometer(channel uint8, x, y, z float64)
28+
AddBarometricPressure(channel uint8, hpa float64)
29+
AddGyrometer(channel uint8, x, y, z float64)
30+
AddGPS(channel uint8, latitude, longitude, meters float64)
3131
}
3232

3333
type encoder struct {
@@ -57,7 +57,7 @@ func (e *encoder) WriteTo(w io.Writer) (int64, error) {
5757
return e.buf.WriteTo(w)
5858
}
5959

60-
func (e *encoder) AddPort(channel uint8, value float32) {
60+
func (e *encoder) AddPort(channel uint8, value float64) {
6161
val := uint16(value * 100)
6262
e.buf.WriteByte(channel)
6363
binary.Write(e.buf, binary.BigEndian, val)
@@ -75,14 +75,14 @@ func (e *encoder) AddDigitalOutput(channel, value uint8) {
7575
e.buf.WriteByte(value)
7676
}
7777

78-
func (e *encoder) AddAnalogInput(channel uint8, value float32) {
78+
func (e *encoder) AddAnalogInput(channel uint8, value float64) {
7979
val := uint16(value * 100)
8080
e.buf.WriteByte(channel)
8181
e.buf.WriteByte(AnalogInput)
8282
binary.Write(e.buf, binary.BigEndian, val)
8383
}
8484

85-
func (e *encoder) AddAnalogOutput(channel uint8, value float32) {
85+
func (e *encoder) AddAnalogOutput(channel uint8, value float64) {
8686
val := uint16(value * 100)
8787
e.buf.WriteByte(channel)
8888
e.buf.WriteByte(AnalogOutput)
@@ -101,20 +101,20 @@ func (e *encoder) AddPresence(channel, value uint8) {
101101
e.buf.WriteByte(value)
102102
}
103103

104-
func (e *encoder) AddTemperature(channel uint8, celcius float32) {
104+
func (e *encoder) AddTemperature(channel uint8, celcius float64) {
105105
val := uint16(celcius * 10)
106106
e.buf.WriteByte(channel)
107107
e.buf.WriteByte(Temperature)
108108
binary.Write(e.buf, binary.BigEndian, val)
109109
}
110110

111-
func (e *encoder) AddRelativeHumidity(channel uint8, rh float32) {
111+
func (e *encoder) AddRelativeHumidity(channel uint8, rh float64) {
112112
e.buf.WriteByte(channel)
113113
e.buf.WriteByte(RelativeHumidity)
114114
e.buf.WriteByte(uint8(rh * 2))
115115
}
116116

117-
func (e *encoder) AddAccelerometer(channel uint8, x, y, z float32) {
117+
func (e *encoder) AddAccelerometer(channel uint8, x, y, z float64) {
118118
valX := uint16(x * 1000)
119119
valY := uint16(y * 1000)
120120
valZ := uint16(z * 1000)
@@ -125,14 +125,14 @@ func (e *encoder) AddAccelerometer(channel uint8, x, y, z float32) {
125125
binary.Write(e.buf, binary.BigEndian, valZ)
126126
}
127127

128-
func (e *encoder) AddBarometricPressure(channel uint8, hpa float32) {
128+
func (e *encoder) AddBarometricPressure(channel uint8, hpa float64) {
129129
val := uint16(hpa * 10)
130130
e.buf.WriteByte(channel)
131131
e.buf.WriteByte(BarometricPressure)
132132
binary.Write(e.buf, binary.BigEndian, val)
133133
}
134134

135-
func (e *encoder) AddGyrometer(channel uint8, x, y, z float32) {
135+
func (e *encoder) AddGyrometer(channel uint8, x, y, z float64) {
136136
valX := uint16(x * 100)
137137
valY := uint16(y * 100)
138138
valZ := uint16(z * 100)
@@ -143,7 +143,7 @@ func (e *encoder) AddGyrometer(channel uint8, x, y, z float32) {
143143
binary.Write(e.buf, binary.BigEndian, valZ)
144144
}
145145

146-
func (e *encoder) AddGPS(channel uint8, latitude, longitude, meters float32) {
146+
func (e *encoder) AddGPS(channel uint8, latitude, longitude, meters float64) {
147147
valLat := uint32(latitude * 10000)
148148
valLon := uint32(longitude * 10000)
149149
valAlt := uint32(meters * 100)

encoder_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func TestEncode(t *testing.T) {
4747
a.So(e.Bytes()[36:40], ShouldResemble, []byte{10, BarometricPressure, 41, 239})
4848

4949
e.AddGyrometer(11, 3.55, 5.61, -4.10)
50-
a.So(e.Bytes()[40:48], ShouldResemble, []byte{11, Gyrometer, 1, 99, 2, 49, 254, 102})
50+
a.So(e.Bytes()[40:48], ShouldResemble, []byte{11, Gyrometer, 1, 99, 2, 49, 254, 103})
5151

5252
e.AddGPS(12, 52.3655, 4.8885, 21.54)
53-
a.So(e.Bytes()[48:59], ShouldResemble, []byte{12, GPS, 7, 253, 135, 0, 190, 245, 0, 8, 106})
53+
a.So(e.Bytes()[48:59], ShouldResemble, []byte{12, GPS, 7, 253, 135, 0, 190, 244, 0, 8, 106})
5454
}
5555

5656
// Downlink encoding

0 commit comments

Comments
 (0)