-
Notifications
You must be signed in to change notification settings - Fork 0
/
REVColorSensorV3.py
180 lines (159 loc) · 5.22 KB
/
REVColorSensorV3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""REV ControlHub Interface for the REV Color Sensor"""
import logging
from REVI2C import I2CDevice
from REVConstants import *
def getDominantColor(r: int, g: int, b: int) -> str:
""" return dominant color as a string """
m = max(r, g, b)
if m == r:
return 'red'
if m == g:
return 'green'
if m == b:
return 'blue'
return 'none'
def rgbi2c(r: int, g: int, b: int, ir: int) -> float:
""" convert red, green, blue and ir value to clear """
return r + g + b - 2 * ir
class REVColorSensorV3(I2CDevice):
"""For use with the REV Color Sensor V3"""
def __init__(self, commObj, channel, destinationModule):
I2CDevice.__init__(self, commObj, channel, destinationModule, self._REV_COLOR_SENSOR_ADDRESS)
self.logger = logging.getLogger(__name__)
self.setType('REVColorSensorV3')
def initSensor(self):
try:
ident = self.readRegister(self._PART_ID)
except TypeError:
self.logger.debug('Error reading device ID')
return False
self.logger.debug('Device ID: 0x%0X', ident)
if ident != self._REV_COLOR_SENSOR_ID:
return False
self.writeRegister(self._MAIN_CTRL, self._RGB_MODE | self._LS_EN | self._PS_EN)
self.printRegister(self._MAIN_CTRL)
self.writeRegister(self._PS_PULSES, 32)
self.printRegister(self._PS_PULSES)
self.writeRegister(self._PS_MEAS_RATE, self._PS_RES_11_BIT | self._PS_MEAS_RATE_100ms)
self.printRegister(self._PS_MEAS_RATE)
self.writeRegister(self._LS_GAIN, self._LS_GAIN_9)
self.printRegister(self._LS_GAIN)
return True
def getAll(self):
""" Returns raw proximity, red, green, blue and ir values. This is read as
a block which is more efficient than getting each value individually. """
self.writeByte(self._PS_DATA)
tmp = self.readMultipleBytes(14)
d = tmp & 2047
ir = tmp >> 16 & 16777215
g = tmp >> 40 & 16777215
b = tmp >> 64 & 16777215
r = tmp >> 88 & 16777215
self.logger.debug('R: %d, G: %d, B: %d, I: %d, Prox: %d', r, g, b, ir, d)
return (
r, g, b, ir, d)
def getProxValue(self):
""" Returns raw proximity value """
tmp = self.readWord(self._PS_DATA)
ovf = (tmp & 2048) >> 11
prox = tmp & 2047
self.logger.debug('Prox: %d, Ovf: %d', prox, ovf)
return prox
def getRGBC(self):
""" Returns red, green, blue and (calculated) clear value """
self.writeByte(self._LS_DATA_IR)
tmp = self.readMultipleBytes(12)
ir = tmp & 16777215
g = tmp >> 24 & 16777215
b = tmp >> 48 & 16777215
r = tmp >> 72 & 16777215
c = rgbi2c(r, g, b, ir)
self.logger.debug('R: %d, G: %d, B: %d, C: %d', r, g, b, c)
return (
r, g, b, c)
def getIrValue(self):
"""Get just the range"""
self.writeByte(self._LS_DATA_IR)
ir = self.readMultipleBytes(3)
self.logger.debug('IR: %d', ir)
return ir
def getGreenValue(self):
"""Get just the green"""
self.writeByte(self._LS_DATA_GREEN)
green = self.readMultipleBytes(3)
self.logger.debug('G: %d', green)
return green
def getRedValue(self):
"""Get just the red"""
self.writeByte(self._LS_DATA_RED)
red = self.readMultipleBytes(3)
self.logger.debug('R: %d', red)
return red
def getBlueValue(self):
"""Get just the blue"""
self.writeByte(self._LS_DATA_BLUE)
blue = self.readMultipleBytes(3)
self.logger.debug('B: %d', blue)
return blue
def readRegister(self, addr):
"""Read the whole register"""
self.writeByte(addr)
tmp = self.readByte()
self.logger.debug('Reading 0x%02X 0x%02X', addr, tmp)
return tmp
def printRegister(self, addr):
"""Print Register if debug is enabled"""
tmp = self.readRegister(addr)
self.logger.debug('Register 0x%02X: 0x%02X', addr, tmp)
def readWord(self, addr):
"""Read the word value for the color (?)"""
self.writeByte(addr)
tmp = self.readMultipleBytes(2)
self.logger.debug('Reading 0x%02X 0x%04X', addr, tmp)
return tmp
def writeRegister(self, register, value):
"""write to the i2c register"""
self.logger.debug('Writing 0x%02X 0x%02X', register, value)
self.writeMultipleBytes(2, register & 255 | value << 8)
_REV_COLOR_SENSOR_ADDRESS = 82
_REV_COLOR_SENSOR_ID = 194
_MAIN_CTRL = 0
_PS_LED = 1
_PS_PULSES = 2
_PS_MEAS_RATE = 3
_LS_MEAS_RATE = 4
_LS_GAIN = 5
_PART_ID = 6
_MAIN_STATUS = 7
_PS_DATA = 8
_LS_DATA_IR = 10
_LS_DATA_GREEN = 13
_LS_DATA_BLUE = 16
_LS_DATA_RED = 19
_INT_CFG = 25
_INT_PST = 26
_PS_THRES_UP = 27
_PS_THRES_LOW = 29
_PS_CAN = 31
_LS_THRES_UP = 33
_LS_THRES_LOW = 36
_LS_THRES_VAR = 39
_PS_EN = 1
_LS_EN = 2
_RGB_MODE = 4
_PS_MEAS_RATE_6_25ms = 1
_PS_MEAS_RATE_12_5ms = 2
_PS_MEAS_RATE_25ms = 3
_PS_MEAS_RATE_50ms = 4
_PS_MEAS_RATE_100ms = 5
_PS_MEAS_RATE_200ms = 6
_PS_MEAS_RATE_400ms = 7
_PS_RES_8_BIT = 0
_PS_RES_9_BIT = 8
_PS_RES_10_BIT = 16
_PS_RES_11_BIT = 24
_LS_GAIN_1 = 0
_LS_GAIN_3 = 1
_LS_GAIN_6 = 2
_LS_GAIN_9 = 3
_LS_GAIN_18 = 4