From e8f02b09e9f18a8449f66f6a106e6cf65e5962fe Mon Sep 17 00:00:00 2001 From: Justin Cichra <1342149+jrcichra@users.noreply.github.com> Date: Mon, 10 Feb 2025 19:31:42 -0500 Subject: [PATCH] chore(sonar): remove count from reflections Because go makes it simple to len() slices omit the count but make sure that the lengths are the same. --- phidgets/sonar.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/phidgets/sonar.go b/phidgets/sonar.go index 87a73fc..5e9fccd 100644 --- a/phidgets/sonar.go +++ b/phidgets/sonar.go @@ -12,6 +12,7 @@ void creflectioncallback(void* handle, void* ctx, const uint32_t distances[8], c */ import "C" import ( + "fmt" "unsafe" ) @@ -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++ { @@ -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 distances, amplitudes, 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 {