PROJECT = "sensor" VERSION = "1.0.0" require "log" require "sys" require "misc" -- i2c ID i2cid = 2 -- i2c 速率 speed = 100000 -- 初始化 function init(address) if i2c.setup(i2cid, speed, -1, 1) ~= speed then log.error("i2c", "setup fail", addr) return end addr = address end -- 读取数据 function send(...) sys.wait(10) if not addr then log.info("i2c", "addr err") return end local t = {...} if i2c.send(i2cid, addr, t) ~= #t then log.error("i2c", "send fail", #t) return end return true end -- 发送数据 function read(n) sys.wait(10) if not addr then log.info("i2c", "addr err") return "\x00" end val = i2c.recv(i2cid, addr, n) if val and #val>0 then return val end return "\x00" end -- 颜色识别传感器 function TCS34725() init(0x29) send(0x83, 0xff) send(0x81, 0x00) send(0x8f, 0x00) send(0x80, 0x03) sys.wait(800) while true do sys.wait(1000) send(0x94) _, c, red, green, blue = pack.unpack(read(8), "