From efe5557f4b72125721e4c69a849cea0569896e98 Mon Sep 17 00:00:00 2001 From: Ben Rogers Date: Mon, 18 Jul 2022 10:04:33 +0100 Subject: [PATCH] Added get_distance() to ColourDistanceSensor (#157) * Add get_distance() to example code * Only return first element of array in get_distance() * Use mode 1 for distance --- buildhat/colordistance.py | 10 ++++++++++ docs/buildhat/colordistance.py | 1 + 2 files changed, 11 insertions(+) diff --git a/buildhat/colordistance.py b/buildhat/colordistance.py index 90f0f94..da85c3a 100644 --- a/buildhat/colordistance.py +++ b/buildhat/colordistance.py @@ -115,6 +115,16 @@ def get_reflected_light(self): readings.append(self.get()[0]) return int(sum(readings) / len(readings)) + def get_distance(self): + """Return the distance + + :return: Distance + :rtype: int + """ + self.mode(1) + distance = self.get()[0] + return distance + def _clamp(self, val, small, large): return max(small, min(val, large)) diff --git a/docs/buildhat/colordistance.py b/docs/buildhat/colordistance.py index 8a73964..d8a3466 100755 --- a/docs/buildhat/colordistance.py +++ b/docs/buildhat/colordistance.py @@ -4,6 +4,7 @@ color = ColorDistanceSensor('C') +print("Distance", color.get_distance()) print("RGBI", color.get_color_rgb()) print("Ambient", color.get_ambient_light()) print("Reflected", color.get_reflected_light())