6 @author Dozingfiretruck
8 --注意:因使用了sys.wait()所有api需要在协程中使用
10 local sc7a20 = require "sc7a20"
13 sys.taskInit(function()
14 i2c.setup(i2cid,i2c_speed)
15 sc7a20.init(i2cid)--初始化,传入i2c_id
17 local sc7a20_data = sc7a20.get_data()
18 log.info("sc7a20_data", "sc7a20_data.x"..(sc7a20_data.x),"sc7a20_data.y"..(sc7a20_data.y),"sc7a20_data.z"..(sc7a20_data.z))
26 local sys
= require
"sys"
29 local SC7A20_ADDRESS_ADR
-- sc7a20设备地址
31 local SC7A20_ADDRESS_ADR_LOW
= 0x18
32 local SC7A20_ADDRESS_ADR_HIGH
= 0x19
33 local SC7A20_CHIP_ID_CHECK
= 0x0f -- 器件ID检测
34 local SC7A20_CHIP_ID
= 0x11 -- 器件ID
36 local SC7A20_REG_CTRL_REG1
= 0x20 -- 控制寄存器1
37 local SC7A20_REG_CTRL_REG2
= 0x21 -- 控制寄存器2
38 local SC7A20_REG_CTRL_REG3
= 0x22 -- 控制寄存器3
39 local SC7A20_REG_CTRL_REG4
= 0x23 -- 控制寄存器4
40 local SC7A20_REG_CTRL_REG5
= 0x24 -- 控制寄存器5
41 local SC7A20_REG_CTRL_REG6
= 0x25 -- 控制寄存器6
42 local SC7A20_REG_REFERENCE
= 0x26 -- 参考寄存器
43 local SC7A20_REG_STATUS_REG
= 0x27 -- 状态寄存器
44 local SC7A20_REG_OUT_X_L
= 0x28 -- X轴数据低字节
45 local SC7A20_REG_OUT_X_H
= 0x29 -- X轴数据高字节
46 local SC7A20_REG_OUT_Y_L
= 0x2A -- Y轴数据低字节
47 local SC7A20_REG_OUT_Y_H
= 0x2B -- Y轴数据高字节
48 local SC7A20_REG_OUT_Z_L
= 0x2C -- Z轴数据低字节
49 local SC7A20_REG_OUT_Z_H
= 0x2D -- Z轴数据高字节
50 local SC7A20_REG_FIFO_CTRL_REG
= 0x2E -- FIFO控制寄存器
51 local SC7A20_REG_FIFO_SRC_REG
= 0x2F -- FIFO源寄存器
52 local SC7A20_REG_INT1_CFG
= 0x30 -- 中断1配置寄存器
53 local SC7A20_REG_INT1_SRC
= 0x31 -- 中断1源寄存器
54 local SC7A20_REG_INT1_THS
= 0x32 -- 中断1阈值寄存器
55 local SC7A20_REG_INT1_DURATION
= 0x33 -- 中断1持续时间寄存器
56 local SC7A20_REG_INT2_CFG
= 0x34 -- 中断2配置寄存器
57 local SC7A20_REG_INT2_SRC
= 0x35 -- 中断2源寄存器
58 local SC7A20_REG_INT2_THS
= 0x36 -- 中断2阈值寄存器
59 local SC7A20_REG_INT2_DURATION
= 0x37 -- 中断2持续时间寄存器
60 local SC7A20_REG_CLICK_CFG
= 0x38 -- 单击配置寄存器
61 local SC7A20_REG_CLICK_SRC
= 0x39 -- 单击源寄存器
62 local SC7A20_REG_CLICK_THS
= 0x3A -- 单击阈值寄存器
63 local SC7A20_REG_TIME_LIMIT
= 0x3B -- 单击时间限制寄存器
64 local SC7A20_REG_TIME_LATENCY
= 0x3C -- 单击时间延迟寄存器
65 local SC7A20_REG_TIME_WINDOW
= 0x3D -- 单击时间窗口寄存器
66 local SC7A20_REG_ACT_THS
= 0x3E -- 活动阈值寄存器
67 local SC7A20_REG_ACT_DUR
= 0x3F -- 活动持续时间寄存器
71 local function chip_check()
72 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR_LOW
, SC7A20_CHIP_ID_CHECK
)--读器件地址
73 local revData
= i2c
.recv(i2cid
, SC7A20_ADDRESS_ADR_LOW
, 1)
74 if revData
:byte() ~= nil then
75 SC7A20_ADDRESS_ADR
= SC7A20_ADDRESS_ADR_LOW
77 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR_HIGH
, SC7A20_CHIP_ID_CHECK
)--读器件地址
79 local revData
= i2c
.recv(i2cid
, SC7A20_ADDRESS_ADR_HIGH
, 1)
80 if revData
:byte() ~= nil then
81 SC7A20_ADDRESS_ADR
= SC7A20_ADDRESS_ADR_HIGH
83 log.info("i2c", "Can't find sc7a20 device")
87 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR
, SC7A20_CHIP_ID_CHECK
)--读器件地址
89 local revData
= i2c
.recv(i2cid
, SC7A20_ADDRESS_ADR
, 1)
90 if revData
:byte() == SC7A20_CHIP_ID
then
91 log.info("Device i2c id is: SC7A20")
93 log.info("i2c", "Can't find sc7a20 device")
102 @api sc7a20.init(i2c_id)
104 @return bool 成功返回true
108 function sc7a20
.init(i2c_id
)
110 sys
.wait(20)--20 毫秒等待设备稳定
112 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR
, {SC7A20_REG_CTRL_REG1
,0X47})
113 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR
, {SC7A20_REG_CTRL_REG2
,0X00})
114 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR
, {SC7A20_REG_CTRL_REG3
,0X00})
115 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR
, {SC7A20_REG_CTRL_REG4
,0X88})
116 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR
, {SC7A20_REG_CTRL_REG6
,0X00})
117 log.info("sc7a20 init_ok")
126 @api sc7a20.get_data()
127 @return table sc7a20 数据
129 local sc7a20_data = sc7a20.get_data()
130 log.info("sc7a20_data", "sc7a20_data.x"..(sc7a20_data.x),"sc7a20_data.y"..(sc7a20_data.y),"sc7a20_data.z"..(sc7a20_data.z))
132 function sc7a20
.get_data()
133 local accel
={x
=nil,y
=nil,z
=nil}
134 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR
,SC7A20_REG_OUT_X_L
)
135 _
,accel
.x
= pack
.unpack(i2c
.recv(i2cid
, SC7A20_ADDRESS_ADR
, 2),">h")
136 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR
,SC7A20_REG_OUT_Y_L
)
137 _
,accel
.y
= pack
.unpack(i2c
.recv(i2cid
, SC7A20_ADDRESS_ADR
, 2),">h")
138 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR
,SC7A20_REG_OUT_Z_L
)
139 _
,accel
.z
= pack
.unpack(i2c
.recv(i2cid
, SC7A20_ADDRESS_ADR
, 2),">h")
146 @api sc7a20.set_thresh (i2cid, activity, time_inactivity)
151 sc7a20.set_thresh(0, string.char(0x05), string.char(0x05))
153 function sc7a20
.set_thresh(i2cid
, activity
, time_inactivity
)
154 i2c
.writeReg(i2cid
, SC7A20_ADDRESS_ADR
, SC7A20_REG_ACT_THS
, activity
)
155 i2c
.writeReg(i2cid
, SC7A20_ADDRESS_ADR
, SC7A20_REG_ACT_DUR
, time_inactivity
)
161 @api sc7a20.set_irqf(i2cid, int, irqf_ths, irqf_duration, irqf_cfg)
163 @number 中断脚 传入1及配置INT1脚,传入2及配置INT2脚
168 sc7a20.set_irqf(0, 1, string.char(0x05), string.char(0x05), string.char(0x00))
170 function sc7a20
.set_irqf(i2cid
, int
, irqf_ths
, irqf_duration
, irqf_cfg
)
172 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR
, {SC7A20_REG_CTRL_REG3
,0X40}) -- AOI1中断映射到INT1上
173 i2c
.writeReg(i2cid
, SC7A20_ADDRESS_ADR
, SC7A20_REG_INT1_THS
, irqf_ths
)
174 i2c
.writeReg(i2cid
, SC7A20_ADDRESS_ADR
, SC7A20_REG_INT1_DURATION
, irqf_duration
)
175 i2c
.writeReg(i2cid
, SC7A20_ADDRESS_ADR
, SC7A20_REG_INT1_CFG
, irqf_cfg
)
177 i2c
.send(i2cid
, SC7A20_ADDRESS_ADR
, {SC7A20_REG_CTRL_REG6
,0X42}) -- AOI2中断映射到INT2上 并且配置低电平触发中断
178 i2c
.writeReg(i2cid
, SC7A20_ADDRESS_ADR
, SC7A20_REG_INT2_THS
, irqf_ths
)
179 i2c
.writeReg(i2cid
, SC7A20_ADDRESS_ADR
, SC7A20_REG_INT2_DURATION
, irqf_duration
)
180 i2c
.writeReg(i2cid
, SC7A20_ADDRESS_ADR
, SC7A20_REG_INT2_CFG
, irqf_cfg
)
182 log.info("sc7a20", "int Parameter error")