From 89256ee9bd890de8f478559a728de1c1ce14cd92 Mon Sep 17 00:00:00 2001 From: Ben Rogers Date: Sun, 17 Jul 2022 17:29:25 +0100 Subject: [PATCH 1/5] First attempt at getting distance --- buildhat/colordistance.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/buildhat/colordistance.py b/buildhat/colordistance.py index 90f0f94..c10fd13 100644 --- a/buildhat/colordistance.py +++ b/buildhat/colordistance.py @@ -115,6 +115,17 @@ def get_reflected_light(self): readings.append(self.get()[0]) return int(sum(readings) / len(readings)) + def get_distance(self): + """Return the reflected light + + :return: Reflected light + :rtype: int + """ + self.mode(6) + distance = self.get() + print("distance", distance) + return + def _clamp(self, val, small, large): return max(small, min(val, large)) From 05853955244618c66e6fae7b10e542360067c0bc Mon Sep 17 00:00:00 2001 From: Ben Rogers Date: Sun, 17 Jul 2022 17:34:45 +0100 Subject: [PATCH 2/5] Removed debugging, return distance --- buildhat/colordistance.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/buildhat/colordistance.py b/buildhat/colordistance.py index c10fd13..782fc60 100644 --- a/buildhat/colordistance.py +++ b/buildhat/colordistance.py @@ -116,15 +116,14 @@ def get_reflected_light(self): return int(sum(readings) / len(readings)) def get_distance(self): - """Return the reflected light + """Return the distance - :return: Reflected light + :return: Distance :rtype: int """ self.mode(6) distance = self.get() - print("distance", distance) - return + return distance def _clamp(self, val, small, large): return max(small, min(val, large)) From 80c971775a1970344161cc05726b9f87425bddae Mon Sep 17 00:00:00 2001 From: Ben Rogers Date: Sun, 17 Jul 2022 17:43:11 +0100 Subject: [PATCH 3/5] Add get_distance() to example code --- docs/buildhat/colordistance.py | 1 + 1 file changed, 1 insertion(+) 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()) From 08c10a658cb598e71afd7b9e3f9dfb72181b53d6 Mon Sep 17 00:00:00 2001 From: Ben Rogers Date: Sun, 17 Jul 2022 17:46:01 +0100 Subject: [PATCH 4/5] Only return first element of array in get_distance() --- buildhat/colordistance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildhat/colordistance.py b/buildhat/colordistance.py index 782fc60..c60290a 100644 --- a/buildhat/colordistance.py +++ b/buildhat/colordistance.py @@ -122,7 +122,7 @@ def get_distance(self): :rtype: int """ self.mode(6) - distance = self.get() + distance = self.get()[0] return distance def _clamp(self, val, small, large): From b37f1b95b024de981dbb743f234147b9eb1a96d1 Mon Sep 17 00:00:00 2001 From: Ben Rogers Date: Sun, 17 Jul 2022 19:35:44 +0100 Subject: [PATCH 5/5] Use mode 1 for distance --- buildhat/colordistance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildhat/colordistance.py b/buildhat/colordistance.py index 782fc60..0dd41ab 100644 --- a/buildhat/colordistance.py +++ b/buildhat/colordistance.py @@ -121,7 +121,7 @@ def get_distance(self): :return: Distance :rtype: int """ - self.mode(6) + self.mode(1) distance = self.get() return distance