2018-03-18 16:04:38 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import ev3dev.ev3 as ev3
|
2018-03-20 15:11:19 +01:00
|
|
|
from enum import Enum, unique
|
|
|
|
|
2018-03-19 00:44:57 +01:00
|
|
|
'''
|
|
|
|
add enum for color definitions (type should be tupel of int's).
|
|
|
|
later onwards compare the idividual readouts like this to determine the color:
|
2018-03-19 02:46:25 +01:00
|
|
|
RED = (>90, <40, <25)
|
|
|
|
BLUE = (<25, >75, >90)
|
2018-03-19 05:14:58 +01:00
|
|
|
|
|
|
|
Example:
|
|
|
|
color = (100, 30, 20)
|
|
|
|
(color[0] > 90 and (color[1], color[2]) < (40, 25))
|
2018-03-19 00:44:57 +01:00
|
|
|
'''
|
2018-03-18 16:04:38 +01:00
|
|
|
|
|
|
|
|
2018-03-22 04:36:37 +01:00
|
|
|
class Color(tuple, Enum):
|
|
|
|
RED = (90, 40, 25)
|
|
|
|
BLUE = (25, 75, 90)
|
|
|
|
|
|
|
|
|
2018-03-18 16:04:38 +01:00
|
|
|
class Sensor:
|
|
|
|
def __init__(self):
|
|
|
|
self._sensor = ev3.ColorSensor()
|
2018-03-18 16:19:12 +01:00
|
|
|
self._sensor.mode = 'COL-REFLECT'
|
2018-03-22 04:36:37 +01:00
|
|
|
self.lastcolor = None
|
2018-03-18 16:04:38 +01:00
|
|
|
|
2018-03-20 15:11:19 +01:00
|
|
|
def getbrightness(self):
|
2018-03-22 04:36:37 +01:00
|
|
|
if(self._sensor.mode == 'COL-REFLECT'):
|
|
|
|
return self._sensor.value()
|
2018-03-18 16:04:38 +01:00
|
|
|
else:
|
2018-03-22 04:36:37 +01:00
|
|
|
print("ERROR: Wrong Sensor Mode.")
|
|
|
|
|
|
|
|
def getcolor(self):
|
|
|
|
if(self._sensor.mode == 'RGB-RAW'):
|
|
|
|
RGB = self._sensor.bin_data("hhh")
|
|
|
|
if(RGB[0] > Color.RED[0] and (RGB[1], RGB[2]) < (Color.RED[1], Color.RED[2])):
|
|
|
|
self.lastcolor = Color.RED
|
|
|
|
if(RGB[0] < Color.BLUE[0] and (RGB[1], RGB[2]) > (Color.BLUE[1], Color.BLUE[2])):
|
|
|
|
self.lastcolor = Color.BLUE
|
2018-03-18 16:04:38 +01:00
|
|
|
|
|
|
|
def setmode(self, newmode):
|
|
|
|
self._sensor.mode = newmode
|