Skip to content

Commit 2ab2b1c

Browse files
committed
Add color range sensor
1 parent 7e94387 commit 2ab2b1c

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from component import Component, PortType, InvalidPortException
2+
from collections.abc import Callable, Protocol
3+
4+
class DistanceCallable(Protocol):
5+
def __call__(self, distance : float) -> None:
6+
pass
7+
class ColorCallable(Protocol):
8+
def __call__(self, hue : int, saturation : int, value : int) -> None:
9+
pass
10+
11+
class ColorRangeSensor(Component):
12+
# Required methods
13+
def __init__(self, ports : list[tuple[PortType, int]]):
14+
self.is_pressed = None
15+
portType, port = ports[0]
16+
if portType != PortType.I2C_PORT:
17+
raise InvalidPortException
18+
self.port = port
19+
def get_manufacturer(self) -> str:
20+
return "REV Robotics"
21+
def get_name(self) -> str:
22+
return "Color Sensor v3"
23+
def get_part_number(self) -> str:
24+
return "REV-31-1557"
25+
def get_url(self) -> str:
26+
return "https://www.revrobotics.com/rev-31-1557"
27+
def get_version(self) -> tuple[int, int, str]:
28+
return (1, 0, "")
29+
def stop(self) -> None:
30+
# send stop command to sensor
31+
pass
32+
def reset(self) -> None:
33+
pass
34+
def get_connection_port_type(self) -> list[PortType]:
35+
return [PortType.I2C_PORT]
36+
def periodic(self) -> None:
37+
pass
38+
39+
# Component specific methods
40+
def get_color_rgb(self) -> list[int, int, int]:
41+
'''gets the color in rgb (red, green, blue)'''
42+
pass
43+
def get_color_hsv(self) -> list[int, int, int]:
44+
'''gets the color in hsv (hue, saturation, value)'''
45+
pass
46+
def get_distance_mm(self) -> float:
47+
'''gets the distance of the object seen'''
48+
pass
49+
50+
def register_when_less_than_distance(self, distance : float, callback: DistanceCallable) -> None:
51+
'''Event when item is seen closer than a distance'''
52+
self.less_than_distance_callback = callback
53+
54+
def register_when_hue_in_range(self, min_hue : int, max_hue : int, callback: ColorCallable) -> None:
55+
'''Event when hue is in range'''
56+
self.hue_in_range_callback = callback
57+
58+
def register_when_saturation_in_range(self, min_saturation : int, max_saturation : int, callback : ColorCallable) -> None:
59+
'''Event when saturation is in range'''
60+
self.saturation_in_range_callback = callback
61+
62+
63+

0 commit comments

Comments
 (0)