Skip to content

Commit dbaacff

Browse files
committed
Formatting
1 parent bb39797 commit dbaacff

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

rpi-colorsensor.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,32 @@
7474

7575
pi = pigpio.pi()
7676

77+
7778
class Color:
79+
7880
def __init__(self, red: float, green: float, blue: float):
7981
self.red = red
8082
self.green = green
8183
self.blue = blue
8284

85+
8386
class RawColor:
87+
8488
def __init__(self, red: int, green: int, blue: int, ir: int):
8589
self.red = red
8690
self.green = green
8791
self.blue = blue
8892
self.ir = ir
8993

94+
9095
class CIEColor:
96+
9197
def __init__(self, x: float, y: float, z: float):
9298
self.x = x
9399
self.y = y
94100
self.z = z
95101

102+
96103
class ColorSensorV3:
97104
"""REV Robotics Color Sensor V3"""
98105

@@ -193,8 +200,7 @@ class ColorSensorMeasurementRate(enum.IntEnum):
193200
kColorRate2000ms = 7
194201

195202
def configureProximitySensorLED(self, freq: LEDPulseFrequency,
196-
curr: LEDCurrent,
197-
pulses: int):
203+
curr: LEDCurrent, pulses: int):
198204
"""
199205
Configure the the IR LED used by the proximity sensor.
200206
@@ -213,7 +219,7 @@ def configureProximitySensorLED(self, freq: LEDPulseFrequency,
213219
self._write8(self.Register.kProximitySensorPulses, pulses)
214220

215221
def configureProximitySensor(self, res: ProximitySensorResolution,
216-
rate: ProximitySensorMeasurementRate):
222+
rate: ProximitySensorMeasurementRate):
217223
"""
218224
Configure the proximity sensor.
219225
@@ -228,8 +234,8 @@ def configureProximitySensor(self, res: ProximitySensorResolution,
228234
self._write8(self.Register.kProximitySensorRate, res | rate)
229235

230236
def configureColorSensor(self, res: ColorSensorResolution,
231-
rate: ColorSensorMeasurementRate,
232-
gain: GainFactor):
237+
rate: ColorSensorMeasurementRate,
238+
gain: GainFactor):
233239
"""
234240
Configure the color sensor.
235241
@@ -275,7 +281,8 @@ def getRawColor(self) -> RawColor:
275281
276282
Returns Color containing red, green, blue and IR values
277283
"""
278-
return RawColor(self.getRed(), self.getGreen(), self.getBlue(), self.getIR())
284+
return RawColor(self.getRed(), self.getGreen(), self.getBlue(),
285+
self.getIR())
279286

280287
def getRed(self) -> int:
281288
"""
@@ -312,9 +319,8 @@ def getIR(self) -> int:
312319
# This is a transformation matrix given by the chip
313320
# manufacturer to transform the raw RGB to CIE XYZ
314321
_Cmatrix = [
315-
0.048112847, 0.289453437, -0.084950826,
316-
-0.030754752, 0.339680186, -0.071569905,
317-
-0.093947499, 0.072838494, 0.34024948
322+
0.048112847, 0.289453437, -0.084950826, -0.030754752, 0.339680186,
323+
-0.071569905, -0.093947499, 0.072838494, 0.34024948
318324
]
319325

320326
def getCIEColor(self) -> CIEColor:
@@ -327,9 +333,12 @@ def getCIEColor(self) -> CIEColor:
327333
Returns CIEColor value from sensor
328334
"""
329335
raw = self.getRawColor()
330-
return CIEColor(self._Cmatrix[0] * raw.red + self._Cmatrix[1] * raw.green + self._Cmatrix[2] * raw.blue,
331-
self._Cmatrix[3] * raw.red + self._Cmatrix[4] * raw.green + self._Cmatrix[5] * raw.blue,
332-
self._Cmatrix[6] * raw.red + self._Cmatrix[7] * raw.green + self._Cmatrix[8] * raw.blue)
336+
return CIEColor(
337+
self._Cmatrix[0] * raw.red + self._Cmatrix[1] * raw.green +
338+
self._Cmatrix[2] * raw.blue, self._Cmatrix[3] * raw.red +
339+
self._Cmatrix[4] * raw.green + self._Cmatrix[5] * raw.blue,
340+
self._Cmatrix[6] * raw.red + self._Cmatrix[7] * raw.green +
341+
self._Cmatrix[8] * raw.blue)
333342

334343
def hasReset(self) -> bool:
335344
"""
@@ -352,18 +361,20 @@ def _checkDeviceID(self) -> bool:
352361
raw = pi.i2c_read_byte_data(self.i2c, self.Register.kPartID)
353362

354363
if self.kPartID != raw:
355-
print("Unknown device found with same I2C addres as REV color sensor")
364+
print(
365+
"Unknown device found with same I2C addres as REV color sensor")
356366
return False
357367

358368
return True
359369

360370
def _initializeDevice(self):
361-
self._write8(self.Register.kMainCtrl,
362-
self.MainControl.kRGBMode |
363-
self.MainControl.kLightSensorEnable |
371+
self._write8(
372+
self.Register.kMainCtrl,
373+
self.MainControl.kRGBMode | self.MainControl.kLightSensorEnable |
364374
self.MainControl.kProximitySensorEnable)
365375

366-
self._write8(self.Register.kProximitySensorRate,
376+
self._write8(
377+
self.Register.kProximitySensorRate,
367378
self.ProximitySensorResolution.kProxRes11bit |
368379
self.ProximitySensorMeasurementRate.kProxRate100ms)
369380

@@ -407,7 +418,8 @@ def readConfig():
407418
with open(configFile, "rt", encoding="utf-8") as f:
408419
j = json.load(f)
409420
except OSError as err:
410-
print("could not open '{}': {}".format(configFile, err), file=sys.stderr)
421+
print("could not open '{}': {}".format(configFile, err),
422+
file=sys.stderr)
411423
return False
412424

413425
# top level must be an object
@@ -469,7 +481,8 @@ def readConfig():
469481
# read sensor and send to NT
470482
rawcolor = sensor1.getRawColor()
471483
prox = sensor1.getProximity()
472-
colorEntry1.setDoubleArray([rawcolor.red, rawcolor.green, rawcolor.blue, rawcolor.ir])
484+
colorEntry1.setDoubleArray(
485+
[rawcolor.red, rawcolor.green, rawcolor.blue, rawcolor.ir])
473486
proxEntry1.setDouble(prox)
474487

475488
# flush NT

0 commit comments

Comments
 (0)