Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for the Hi-Tech Color Sensor (HT NXT color sensor V2) #786

Open
wants to merge 4 commits into
base: ev3dev-stretch
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions ev3dev2/sensor/lego.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,151 @@ def blue(self):
return self.value(2)


class HiTechColorSensor(Sensor):
"""
HT NXT color sensor V2.
"""

SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME
SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION

#: Color values 0 - 17.
MODE_COLOR = 'COLOR'

#: RGB Color components.
MODE_RGB = 'RGB'

#: Normal value of the reflected light.
MODE_NORM = 'NORM'

#: Passive value of the reflected light.
MODE_PASSIVE = 'PASSIVE'

#: 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)

@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.
Expand Down