Skip to content

Commit

Permalink
chore(sonar): remove count from reflections
Browse files Browse the repository at this point in the history
Because go makes it simple to len() slices omit the count
but make sure that the lengths are the same.
  • Loading branch information
jrcichra committed Feb 11, 2025
1 parent 4f45cd1 commit 0e5f6cc
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions phidgets/sonar.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void creflectioncallback(void* handle, void* ctx, const uint32_t distances[8], c
*/
import "C"
import (
"fmt"
"unsafe"
)

Expand Down Expand Up @@ -69,17 +70,16 @@ func (p *PhidgetDistanceSensor) SetDistanceChangeTrigger(distance uint32) error
// }

// GetSonarReflections - The most recent reflection values that the channel has reported.
func (p *PhidgetDistanceSensor) GetSonarReflections() ([]uint32, []uint32, uint32, error) {
func (p *PhidgetDistanceSensor) GetSonarReflections() ([]uint32, []uint32, error) {
var cDistances [8]C.uint
var cAmplitudes [8]C.uint
var count C.uint
cerr := C.PhidgetDistanceSensor_getSonarReflections(p.handle, &cDistances, &cAmplitudes, &count)
if cerr != C.EPHIDGET_OK {
return nil, nil, 0, p.phidgetError(cerr)
return nil, nil, p.phidgetError(cerr)
}

iCount := int(count)

// trim arrays to count size (removes 2^32 − 1 values)
distances := make([]uint32, 0, iCount)
for i := 0; i < iCount; i++ {
Expand All @@ -90,7 +90,11 @@ func (p *PhidgetDistanceSensor) GetSonarReflections() ([]uint32, []uint32, uint3
amplitudes = append(amplitudes, uint32(cAmplitudes[i]))
}

return distances, amplitudes, uint32(count), nil
if len(distances) != len(amplitudes) {
return nil, nil, fmt.Errorf("length of distances: %d does not match the length of amplitudes: %d", len(distances), len(amplitudes))
}

return distances, amplitudes, nil
}

func (p *PhidgetDistanceSensor) SetSonarQuietMode(val bool) error {
Expand Down

0 comments on commit 0e5f6cc

Please sign in to comment.