-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcs3472.py
42 lines (30 loc) · 1.11 KB
/
tcs3472.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
# tcs3472.py
# A MicroPython library for the TCS3472 light sensing chip
# https://github.com/tti0/tcs3472-micropython
# Copyright (c) 2021 tti0
# Licensed under the MIT License
import struct
class tcs3472:
def __init__(self, bus, address=0x29):
self._bus = bus
self._i2c_address = address
self._bus.start()
self._bus.writeto(self._i2c_address, b'\x80\x03')
self._bus.writeto(self._i2c_address, b'\x81\x2b')
def scaled(self):
crgb = self.raw()
if crgb[0] > 0:
return tuple(float(x) / crgb[0] for x in crgb[1:])
return (0,0,0)
def rgb(self):
return tuple(int(x * 255) for x in self.scaled())
def light(self):
return self.raw()[0]
def brightness(self, level=65.535):
return int((self.light() / level))
def valid(self):
self._bus.writeto(self._i2c_address, b'\x93')
return self._bus.readfrom(self._i2c_address, 1)[0] & 1
def raw(self):
self._bus.writeto(self._i2c_address, b'\xb4')
return struct.unpack("<HHHH", self._bus.readfrom(self._i2c_address, 8))