Skip to content

Commit 6c670c9

Browse files
committed
encoder: change casting for float64 values to handle negative values on different architectures than amd64.
This is required in order to handle running Go on ARM processors, as well as for other compilers such as TinyGo, which is specifically where I initially ran into it. See golang/go#43047 for a description of the underlying Go issue. Signed-off-by: deadprogram <[email protected]>
1 parent 8969907 commit 6c670c9

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

decoder_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func TestDecode(t *testing.T) {
8686
10, BarometricPressure, 41, 239,
8787
11, Gyrometer, 1, 99, 2, 49, 254, 102,
8888
12, GPS, 7, 253, 135, 0, 190, 245, 0, 8, 106,
89+
13, GPS, 7, 253, 135, 255, 65, 12, 0, 8, 106,
8990
}
9091
decoder := NewDecoder(bytes.NewBuffer(buf))
9192
target := &target{make(map[uint8]interface{})}
@@ -104,6 +105,7 @@ func TestDecode(t *testing.T) {
104105
a.So(target.values[10], ShouldEqual, 1073.5)
105106
a.So(target.values[11], ShouldResemble, []float64{3.55, 5.61, -4.10})
106107
a.So(target.values[12], ShouldResemble, []float64{52.3655, 4.8885, 21.54})
108+
a.So(target.values[13], ShouldResemble, []float64{52.3655, -4.8884, 21.54})
107109
}
108110

109111
// Happy flow: downlink

encoder.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (e *encoder) WriteTo(w io.Writer) (int64, error) {
5858
}
5959

6060
func (e *encoder) AddPort(channel uint8, value float64) {
61-
val := uint16(value * 100)
61+
val := int16(value * 100)
6262
e.buf.WriteByte(channel)
6363
binary.Write(e.buf, binary.BigEndian, val)
6464
}
@@ -76,14 +76,14 @@ func (e *encoder) AddDigitalOutput(channel, value uint8) {
7676
}
7777

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

8585
func (e *encoder) AddAnalogOutput(channel uint8, value float64) {
86-
val := uint16(value * 100)
86+
val := int16(value * 100)
8787
e.buf.WriteByte(channel)
8888
e.buf.WriteByte(AnalogOutput)
8989
binary.Write(e.buf, binary.BigEndian, val)
@@ -102,7 +102,7 @@ func (e *encoder) AddPresence(channel, value uint8) {
102102
}
103103

104104
func (e *encoder) AddTemperature(channel uint8, celcius float64) {
105-
val := uint16(celcius * 10)
105+
val := int16(celcius * 10)
106106
e.buf.WriteByte(channel)
107107
e.buf.WriteByte(Temperature)
108108
binary.Write(e.buf, binary.BigEndian, val)
@@ -115,9 +115,9 @@ func (e *encoder) AddRelativeHumidity(channel uint8, rh float64) {
115115
}
116116

117117
func (e *encoder) AddAccelerometer(channel uint8, x, y, z float64) {
118-
valX := uint16(x * 1000)
119-
valY := uint16(y * 1000)
120-
valZ := uint16(z * 1000)
118+
valX := int16(x * 1000)
119+
valY := int16(y * 1000)
120+
valZ := int16(z * 1000)
121121
e.buf.WriteByte(channel)
122122
e.buf.WriteByte(Accelerometer)
123123
binary.Write(e.buf, binary.BigEndian, valX)
@@ -126,16 +126,16 @@ func (e *encoder) AddAccelerometer(channel uint8, x, y, z float64) {
126126
}
127127

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

135135
func (e *encoder) AddGyrometer(channel uint8, x, y, z float64) {
136-
valX := uint16(x * 100)
137-
valY := uint16(y * 100)
138-
valZ := uint16(z * 100)
136+
valX := int16(x * 100)
137+
valY := int16(y * 100)
138+
valZ := int16(z * 100)
139139
e.buf.WriteByte(channel)
140140
e.buf.WriteByte(Gyrometer)
141141
binary.Write(e.buf, binary.BigEndian, valX)
@@ -144,18 +144,18 @@ func (e *encoder) AddGyrometer(channel uint8, x, y, z float64) {
144144
}
145145

146146
func (e *encoder) AddGPS(channel uint8, latitude, longitude, meters float64) {
147-
valLat := uint32(latitude * 10000)
148-
valLon := uint32(longitude * 10000)
149-
valAlt := uint32(meters * 100)
147+
valLat := int32(latitude * 10000)
148+
valLon := int32(longitude * 10000)
149+
valAlt := int32(meters * 100)
150150
e.buf.WriteByte(channel)
151151
e.buf.WriteByte(GPS)
152-
e.buf.WriteByte(uint8(valLat >> 16))
153-
e.buf.WriteByte(uint8(valLat >> 8))
154-
e.buf.WriteByte(uint8(valLat))
155-
e.buf.WriteByte(uint8(valLon >> 16))
156-
e.buf.WriteByte(uint8(valLon >> 8))
157-
e.buf.WriteByte(uint8(valLon))
158-
e.buf.WriteByte(uint8(valAlt >> 16))
159-
e.buf.WriteByte(uint8(valAlt >> 8))
160-
e.buf.WriteByte(uint8(valAlt))
152+
e.buf.WriteByte(byte(valLat >> 16))
153+
e.buf.WriteByte(byte(valLat >> 8))
154+
e.buf.WriteByte(byte(valLat))
155+
e.buf.WriteByte(byte(valLon >> 16))
156+
e.buf.WriteByte(byte(valLon >> 8))
157+
e.buf.WriteByte(byte(valLon))
158+
e.buf.WriteByte(byte(valAlt >> 16))
159+
e.buf.WriteByte(byte(valAlt >> 8))
160+
e.buf.WriteByte(byte(valAlt))
161161
}

encoder_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func TestEncode(t *testing.T) {
5151

5252
e.AddGPS(12, 52.3655, 4.8885, 21.54)
5353
a.So(e.Bytes()[48:59], ShouldResemble, []byte{12, GPS, 7, 253, 135, 0, 190, 244, 0, 8, 106})
54+
55+
e.AddGPS(13, 43.43382, -5.865784, 179.0000)
56+
a.So(e.Bytes()[59:70], ShouldResemble, []byte{13, GPS, 0x06, 160, 162, 255, 26, 223, 0, 69, 236})
5457
}
5558

5659
// Downlink encoding

0 commit comments

Comments
 (0)