--[[ @module tcs3472 @summary tcs3472 颜色传感器 @version 1.0 @date 2022.03.14 @author Dozingfiretruck @usage --注意:因使用了sys.wait()所有api需要在协程中使用 -- 用法实例 local tcs3472 = require "tcs3472" i2cid = 0 i2c_speed = i2c.FAST sys.taskInit(function() i2c.setup(i2cid,i2c_speed) tcs3472.init(i2cid)--初始化,传入i2c_id while 1 do local rgb_data = tcs3472.get_rgb() log.info("rgb_data.R:",rgb_data.R) log.info("rgb_data.G:",rgb_data.G) log.info("rgb_data.B:",rgb_data.B) log.info("rgb_data.C:",rgb_data.C) if rgb_data.R then local lux_date = tcs3472.get_lux(rgb_data) log.info("lux_date:",lux_date) end sys.wait(1000) end end) ]] local tcs3472 = {} local sys = require "sys" local i2cid local TCS3472_ADDRESS_ADR = 0x29 ---器件所用地址 local TCS3472_CMD_BIT = 0x80 local TCS3472_CMD_Read_Byte = 0x00 local TCS3472_CMD_Read_Word = 0x20 local TCS3472_CMD_Clear_INT = 0x66 -- RGBC Interrupt flag clear local TCS3472_ENABLE = 0x00 local TCS3472_ENABLE_AIEN = 0x10 -- RGBC Interrupt Enable local TCS3472_ENABLE_WEN = 0x08 -- Wait enable - Writing 1 activates the wait timer local TCS3472_ENABLE_AEN = 0x02 -- RGBC Enable - Writing 1 actives the ADC, 0 disables it local TCS3472_ENABLE_PON = 0x01 -- Power on - Writing 1 activates the internal oscillator, 0 disables it local TCS3472_ATIME = 0x01 -- Integration time local TCS3472_WTIME = 0x03 -- Wait time (if TCS34725_ENABLE_WEN is asserted) local TCS3472_WTIME_2_4MS = 0xFF -- WLONG0 = 2.4ms WLONG1 = 0.029s local TCS3472_WTIME_204MS = 0xAB -- WLONG0 = 204ms WLONG1 = 2.45s local TCS3472_WTIME_614MS = 0x00 -- WLONG0 = 614ms WLONG1 = 7.4s local TCS3472_AILTL = 0x04 -- Clear channel lower interrupt threshold local TCS3472_AILTH = 0x05 local TCS3472_AIHTL = 0x06 -- Clear channel upper interrupt threshold local TCS3472_AIHTH = 0x07 local TCS3472_PERS = 0x0C -- Persistence register - basic SW filtering mechanism for interrupts local TCS3472_PERS_NONE = 0x00 -- Every RGBC cycle generates an interrupt local TCS3472_PERS_1_CYCLE = 0x01 -- 1 clean channel value outside threshold range generates an interrupt local TCS3472_PERS_2_CYCLE = 0x02 -- 2 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_3_CYCLE = 0x03 -- 3 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_5_CYCLE = 0x04 -- 5 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_10_CYCLE = 0x05 -- 10 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_15_CYCLE = 0x06 -- 15 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_20_CYCLE = 0x07 -- 20 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_25_CYCLE = 0x08 -- 25 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_30_CYCLE = 0x09 -- 30 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_35_CYCLE = 0x0a -- 35 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_40_CYCLE = 0x0b -- 40 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_45_CYCLE = 0x0c -- 45 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_50_CYCLE = 0x0d -- 50 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_55_CYCLE = 0x0e -- 55 clean channel values outside threshold range generates an interrupt local TCS3472_PERS_60_CYCLE = 0x0f -- 60 clean channel values outside threshold range generates an interrupt local TCS3472_CONFIG = 0x0D local TCS3472_CONFIG_WLONG = 0x02 -- Choose between short and long (12x) wait times via TCS34725_WTIME local TCS3472_CONTROL = 0x0F -- Set the gain level for the sensor local TCS3472_CHIP_ID_CHECK = 0x12 -- 0x44 = TCS34721/TCS34725, 0x4D = TCS34723/TCS34727 local TCS34721_TCS34725_CHIP_ID = 0x44 local TCS34723_TCS34727_CHIP_ID = 0x4D local TCS3472_STATUS = 0x13 local TCS3472_STATUS_AINT = 0x10 -- RGBC Clean channel interrupt local TCS3472_STATUS_AVALID = 0x01 -- Indicates that the RGBC channels have completed an integration cycle local TCS3472_CDATAL = 0x14 -- Clear channel data local TCS3472_CDATAH = 0x15 local TCS3472_RDATAL = 0x16 -- Red channel data local TCS3472_RDATAH = 0x17 local TCS3472_GDATAL = 0x18 -- Green channel data local TCS3472_GDATAH = 0x19 local TCS3472_BDATAL = 0x1A -- Blue channel data local TCS3472_BDATAH = 0x1B -- Offset and Compensated local TCS3472_R_Coef = 0.136 local TCS3472_G_Coef = 1.000 local TCS3472_B_Coef = -0.444 local TCS3472_GA = 1.0 local TCS3472_DF = 310.0 local TCS3472_CT_Coef = 3810.0 local TCS3472_CT_Offset = 1391.0 -- Integration Time local TCS3472_INTEGRATIONTIME_2_4MS = 0xFF -- 2.4ms - 1 cycle - Max Count: 1024 local TCS3472_INTEGRATIONTIME_24MS = 0xF6 -- 24ms - 10 cycles - Max Count: 10240 local TCS3472_INTEGRATIONTIME_50MS = 0xEB -- 50ms - 20 cycles - Max Count: 20480 local TCS3472_INTEGRATIONTIME_101MS = 0xD5 -- 101ms - 42 cycles - Max Count: 43008 local TCS3472_INTEGRATIONTIME_154MS = 0xC0 -- 154ms - 64 cycles - Max Count: 65535 local TCS3472_INTEGRATIONTIME_700MS = 0x00 -- 700ms - 256 cycles - Max Count: 65535 --Gain local TCS3472_GAIN_1X = 0x00 -- No gain local TCS3472_GAIN_4X = 0x01 -- 4x gain local TCS3472_GAIN_16X = 0x02 -- 16x gain local TCS3472_GAIN_60X = 0x03 -- 60x gain local integrationTime_t,gain_t --[[ Writes an 8-bit value to the specified register/address ]] local function tcs3472_writebyte(add, data) i2c.send(i2cid, TCS3472_ADDRESS_ADR, {bit.bor(add,TCS3472_CMD_BIT), data}) end --[[ Read an unsigned byte from the I2C device ]] local function tcs3472_readbyte(add) i2c.send(i2cid, TCS3472_ADDRESS_ADR, bit.bor(add,TCS3472_CMD_BIT)) local revData = i2c.recv(i2cid, TCS3472_ADDRESS_ADR, 1) return revData:byte() end --[[ Read an unsigned word from the I2C device ]] local function tcs3472_readword(add) i2c.send(i2cid, TCS3472_ADDRESS_ADR, bit.bor(add,TCS3472_CMD_BIT)) local _,revData = pack.unpack(i2c.recv(i2cid, TCS3472_ADDRESS_ADR, 2), " rgb.C then ir = (rgb.R + rgb.G + rgb.B - rgb.C) / 2 else ir = 0 end r_comp = rgb.R - ir g_comp = rgb.G - ir b_comp = rgb.B - ir if gain_t == TCS3472_GAIN_1X then gain_temp = 1 elseif gain_t == TCS3472_GAIN_4X then gain_temp = 4 elseif gain_t == TCS3472_GAIN_16X then gain_temp = 16 elseif gain_t == TCS3472_GAIN_60X then gain_temp = 60 end cpl = (atime_ms * gain_temp) / (TCS3472_GA * TCS3472_DF) lux = (TCS3472_R_Coef * r_comp + TCS3472_G_Coef * g_comp + TCS3472_B_Coef * b_comp) / cpl return lux end return tcs3472