1+ # @license
2+ # Copyright 2025 Porpoiseful LLC
3+ #
4+ # Licensed under the Apache License, Version 2.0 (the "License");
5+ # you may not use this file except in compliance with the License.
6+ # You may obtain a copy of the License at
7+ #
8+ # https://www.apache.org/licenses/LICENSE-2.0
9+ #
10+ # Unless required by applicable law or agreed to in writing, software
11+ # distributed under the License is distributed on an "AS IS" BASIS,
12+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ # See the License for the specific language governing permissions and
14+ # limitations under the License.
15+
16+ # @fileoverview This is a sample for a color/range sensor
17+ # @author [email protected] (Alan Smith) 18+
19+ from component import Component , PortType , InvalidPortException
20+ from collections .abc import Protocol
21+
22+ class DistanceCallable (Protocol ):
23+ def __call__ (self , distance : float ) -> None :
24+ pass
25+ class ColorCallable (Protocol ):
26+ def __call__ (self , hue : int , saturation : int , value : int ) -> None :
27+ pass
28+
29+ class ColorRangeSensor (Component ):
30+ def __init__ (self , ports : list [tuple [PortType , int ]]):
31+ portType , port = ports [0 ]
32+ if portType != PortType .I2C_PORT :
33+ raise InvalidPortException
34+ self .port = port
35+ def get_manufacturer (self ) -> str :
36+ return "REV Robotics"
37+ def get_name (self ) -> str :
38+ return "Color Sensor v3"
39+ def get_part_number (self ) -> str :
40+ return "REV-31-1557"
41+ def get_url (self ) -> str :
42+ return "https://www.revrobotics.com/rev-31-1557"
43+ def get_version (self ) -> tuple [int , int , str ]:
44+ return (1 , 0 , "" )
45+ def stop (self ) -> None :
46+ # send stop command to sensor
47+ pass
48+ def reset (self ) -> None :
49+ pass
50+ def get_connection_port_type (self ) -> list [PortType ]:
51+ return [PortType .I2C_PORT ]
52+ def periodic (self ) -> None :
53+ pass
54+
55+ # Component specific methods
56+ def get_color_rgb (self ) -> tuple [int , int , int ]:
57+ '''gets the color in rgb (red, green, blue)'''
58+ pass
59+ def get_color_hsv (self ) -> tuple [int , int , int ]:
60+ '''gets the color in hsv (hue, saturation, value)'''
61+ pass
62+ def get_distance_mm (self ) -> float :
63+ '''gets the distance of the object seen'''
64+ pass
65+
66+ def register_when_less_than_distance (self , distance : float ,
67+ callback : DistanceCallable ) -> None :
68+ '''Event when item is seen closer than a distance'''
69+ self .less_than_distance_callback = callback
70+
71+ def register_when_hue_in_range (self , min_hue : int ,
72+ max_hue : int ,
73+ callback : ColorCallable ) -> None :
74+ '''Event when hue is in range'''
75+ self .hue_in_range_callback = callback
76+
77+ def register_when_saturation_in_range (self , min_saturation : int ,
78+ max_saturation : int ,
79+ callback : ColorCallable ) -> None :
80+ '''Event when saturation is in range'''
81+ self .saturation_in_range_callback = callback
0 commit comments