From 8686681fd34b95372043d43c0a8810a5cb2897e5 Mon Sep 17 00:00:00 2001 From: Asedem Date: Sun, 18 Sep 2022 22:45:53 +0200 Subject: [PATCH 1/4] Added basic support for the HiTechColorSensor --- ev3dev2/sensor/lego.py | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/ev3dev2/sensor/lego.py b/ev3dev2/sensor/lego.py index 281d983..1d5b47d 100644 --- a/ev3dev2/sensor/lego.py +++ b/ev3dev2/sensor/lego.py @@ -411,6 +411,73 @@ def blue(self): return self.value(2) +class HiTechColorSensor(Sensor): + + SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME + SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION + + #: Reflected light. Red LED on. + MODE_COLOR = 'COLOR' + + #: Ambient light. Blue LEDs on. + MODE_RGB = 'RGB' + + #: Color. All LEDs rapidly cycling, appears white. + MODE_NORM = 'NORM' + + #: Raw reflected. Red LED on + MODE_PASSIVE = 'PASSIVE' + + #: Raw Color Components. All LEDs rapidly cycling, appears white. + MODE_RAW = 'RAW' + + MODES = (MODE_COLOR, MODE_RGB, MODE_NORM, MODE_PASSIVE, MODE_RAW) + + def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, name_exact=False, **kwargs): + super(HiTechColorSensor, self)\ + .__init__(address, name_pattern, name_exact, driver_name='lego-ev3-color', **kwargs) + + @property + def color(self): + self._ensure_mode(self.MODE_COLOR) + return self.value(0) + + @property + def rgb(self): + self._ensure_mode(self.MODE_RGB) + return self.value(0), self.value(1), self.value(2) + + @property + def red(self): + self._ensure_mode(self.MODE_RGB) + return self.value(0) + + @property + def green(self): + self._ensure_mode(self.MODE_RGB) + return self.value(1) + + @property + def blue(self): + self._ensure_mode(self.MODE_RGB) + return self.value(2) + + @property + def norm(self): + self._ensure_mode(self.MODE_NORM) + return self.value(0) + + @property + def passive(self): + self._ensure_mode(self.MODE_PASSIVE) + return self.value(0) + + @property + def raw(self): + self._ensure_mode(self.MODE_RAW) + return self.value(0) + + class UltrasonicSensor(Sensor): """ LEGO EV3 ultrasonic sensor. From 81c8cd0d847f6d2cd7b51ca36db08f19b7826c28 Mon Sep 17 00:00:00 2001 From: Asedem Date: Sun, 18 Sep 2022 23:04:58 +0200 Subject: [PATCH 2/4] Added first documentation and fixed driver issue --- ev3dev2/sensor/lego.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ev3dev2/sensor/lego.py b/ev3dev2/sensor/lego.py index 1d5b47d..ab3767f 100644 --- a/ev3dev2/sensor/lego.py +++ b/ev3dev2/sensor/lego.py @@ -412,30 +412,33 @@ def blue(self): class HiTechColorSensor(Sensor): + """ + HT NXT color sensor V2. + """ SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION - #: Reflected light. Red LED on. + # Color values 0 - 15. MODE_COLOR = 'COLOR' - #: Ambient light. Blue LEDs on. + # RGB Color components. MODE_RGB = 'RGB' - #: Color. All LEDs rapidly cycling, appears white. + # Normal value of the reflected light. MODE_NORM = 'NORM' - #: Raw reflected. Red LED on + # Passive value of the reflected light. MODE_PASSIVE = 'PASSIVE' - #: Raw Color Components. All LEDs rapidly cycling, appears white. + # Raw Color Components. MODE_RAW = 'RAW' MODES = (MODE_COLOR, MODE_RGB, MODE_NORM, MODE_PASSIVE, MODE_RAW) def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, name_exact=False, **kwargs): super(HiTechColorSensor, self)\ - .__init__(address, name_pattern, name_exact, driver_name='lego-ev3-color', **kwargs) + .__init__(address, name_pattern, name_exact, driver_name='ht-nxt-color-v2', **kwargs) @property def color(self): From 18d969d7c04b34594db4a0f342103a0738f90730 Mon Sep 17 00:00:00 2001 From: Asedem Date: Mon, 19 Sep 2022 23:46:23 +0200 Subject: [PATCH 3/4] Added the color indexes and a color list --- ev3dev2/sensor/lego.py | 85 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 5 deletions(-) diff --git a/ev3dev2/sensor/lego.py b/ev3dev2/sensor/lego.py index ab3767f..d5f4691 100644 --- a/ev3dev2/sensor/lego.py +++ b/ev3dev2/sensor/lego.py @@ -419,23 +419,98 @@ class HiTechColorSensor(Sensor): SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION - # Color values 0 - 15. + #: Color values 0 - 15. MODE_COLOR = 'COLOR' - # RGB Color components. + #: RGB Color components. MODE_RGB = 'RGB' - # Normal value of the reflected light. + #: Normal value of the reflected light. MODE_NORM = 'NORM' - # Passive value of the reflected light. + #: Passive value of the reflected light. MODE_PASSIVE = 'PASSIVE' - # Raw Color Components. + #: Raw Color Components. MODE_RAW = 'RAW' + #: Black color. + COLOR_BLACK = 0 + + #: Purple color. + COLOR_PURPLE = 1 + + #: Purple Blue color. + COLOR_PURPLE_BLUE = 2 + + #: Blue Green color. + COLOR_BLUE_GREEN = 3 + + #: Green color. + COLOR_GREEN = 4 + + #: Green Yellow color. + COLOR_GREEN_YELLOW = 5 + + #: Yellow color. + COLOR_YELLOW = 6 + + #: Orange color. + COLOR_ORANGE = 7 + + #: Red color. + COLOR_RED = 8 + + #: Red Pink color. + COLOR_RED_PINK = 9 + + #: Pink color. + COLOR_PINK = 10 + + #: Light Purple color. + COLOR_LIGHT_PURPLE = 11 + + #: Light Green color. + COLOR_LIGHT_GREEN = 12 + + #: Light Yellow color. + COLOR_LIGHT_YELLOW = 13 + + #: Light Yellow Red color. + COLOR_LIGHT_YELLOW_RED = 14 + + #: Red color. + COLOR_LIGHT_RED = 15 + + #: Light Pink color. + COLOR_LIGHT_PINK = 16 + + #: White color. + COLOR_WHITE = 17 + MODES = (MODE_COLOR, MODE_RGB, MODE_NORM, MODE_PASSIVE, MODE_RAW) + COLORS = ( + 'Black', + 'Purple', + 'PurpleBlue', + 'BlueGreen', + 'Green', + 'GreenYellow', + 'Yellow', + 'Orange', + 'Red', + 'RedPink', + 'Pink', + 'LightPurple', + 'LightGreen', + 'LightYellow', + 'LightYellowRed', + 'LightRed', + 'LightPink', + 'LightWhite', + ) + def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, name_exact=False, **kwargs): super(HiTechColorSensor, self)\ .__init__(address, name_pattern, name_exact, driver_name='ht-nxt-color-v2', **kwargs) From 2979255864ba7a02d88c40fc01c63e58037533fd Mon Sep 17 00:00:00 2001 From: Asedem Date: Tue, 27 Dec 2022 02:42:24 +0100 Subject: [PATCH 4/4] Fixed error in a code comment --- ev3dev2/sensor/lego.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ev3dev2/sensor/lego.py b/ev3dev2/sensor/lego.py index d5f4691..c466252 100644 --- a/ev3dev2/sensor/lego.py +++ b/ev3dev2/sensor/lego.py @@ -419,7 +419,7 @@ class HiTechColorSensor(Sensor): SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION - #: Color values 0 - 15. + #: Color values 0 - 17. MODE_COLOR = 'COLOR' #: RGB Color components.