代码拉取完成,页面将自动刷新
local i2cId = 1
local da267Addr = 0x26
local intPin = 39
local function ind()
log.info("int", gpio.get(intPin))
if gpio.get(intPin) == 1 then
log.info("da267", "interrupt")
i2c.send(i2cId, da267Addr, 0x02, 1)
local data = i2c.recv(i2cId, da267Addr, 6)
if data and #data == 6 then
local xl, xm, yl, ym, zl, zm = string.byte(data, 1, 1), string.byte(data, 2, 2), string.byte(data, 3, 3), string.byte(data, 4, 4), string.byte(data, 5, 5), string.byte(data, 6, 6)
x, y, z = (xm << 8 | xl ) >> 4, (ym << 8 | yl) >> 4, (zm << 8 | zl ) >> 4
log.info("da267", "x:", x, "y:", y, "z:", z)
sys.publish("GSENSOR", x, y, z)
else
sys.publish("RESTORE_GSENSOR")
end
i2c.send(i2cId, da267Addr, 0x0D, 1)
local data = i2c.recv(i2cId, da267Addr, 2)
if data and #data == 2 then
local xl, xm = string.byte(data, 1, 1), string.byte(data, 2, 2)
local step = ((xl << 8) + xm) // 2
log.info("da267", "step:", step)
sys.publish("STEP_COUNTER", step)
else
sys.publish("RESTORE_GSENSOR")
end
end
end
gpio.setup(intPin, ind)
local function init()
i2c.close(i2cId)
i2c.setup(i2cId, i2c.SLOW)
i2c.send(i2cId, da267Addr, {0x00, 0x24}, 1)
sys.wait(20)
i2c.send(i2cId, da267Addr, {0x0F, 0x00}, 1)
i2c.send(i2cId, da267Addr, {0x11, 0x34}, 1)
i2c.send(i2cId, da267Addr, {0x10, 0x07}, 1)
sys.wait(50)
-- int set1
i2c.send(i2cId, da267Addr, {0x16, 0x87}, 1)
-- init active interrupt
i2c.send(i2cId, da267Addr, {0x38, 0x03}, 1)
i2c.send(i2cId, da267Addr, {0x39, 0x05}, 1)
i2c.send(i2cId, da267Addr, {0x3A, 0x05}, 1)
i2c.send(i2cId, da267Addr, {0x3B, 0x05}, 1)
i2c.send(i2cId, da267Addr, {0x19, 0x04}, 1)
-- enable active
i2c.send(i2cId, da267Addr, {0x11, 0x30}, 1)
-- init step counter
i2c.send(i2cId, da267Addr, {0x33, 0x80}, 1)
end
sys.taskInit(function()
mcu.altfun(mcu.I2C, i2cId, 23, 2, 0)
mcu.altfun(mcu.I2C, i2cId, 24, 2, 0)
while true do
init()
while true do
local result = sys.waitUntil("RESTORE_GSENSOR", 60 * 1000)
if result then
break
end
i2c.send(i2cId, da267Addr, 0x01, 1)
local data = i2c.recv(i2cId, da267Addr, 1)
if not data or data == "" or string.byte(data) ~= 0x13 then
break
end
end
end
end)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。