remove: 移除多余的mind目录
[LuatOS.git] / script / libs / sc7a20.lua
blobbd11bd059fe0175131ab682fe9de2333759cd366
1 --[[
2 @module sc7a20
3 @summary sc7a20
4 @version 1.0
5 @date 2022.04.11
6 @author Dozingfiretruck
7 @usage
8 --注意:因使用了sys.wait()所有api需要在协程中使用
9 -- 用法实例
10 local sc7a20 = require "sc7a20"
11 i2cid = 0
12 i2c_speed = i2c.FAST
13 sys.taskInit(function()
14 i2c.setup(i2cid,i2c_speed)
15 sc7a20.init(i2cid)--初始化,传入i2c_id
16 while 1 do
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))
19 sys.wait(1000)
20 end
21 end)
25 local sc7a20 = {}
26 local sys = require "sys"
27 local i2cid
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 -- 活动持续时间寄存器
70 --器件ID检测
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
76 else
77 i2c.send(i2cid, SC7A20_ADDRESS_ADR_HIGH, SC7A20_CHIP_ID_CHECK)--读器件地址
78 sys.wait(50)
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
82 else
83 log.info("i2c", "Can't find sc7a20 device")
84 return false
85 end
86 end
87 i2c.send(i2cid, SC7A20_ADDRESS_ADR, SC7A20_CHIP_ID_CHECK)--读器件地址
88 sys.wait(50)
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")
92 else
93 log.info("i2c", "Can't find sc7a20 device")
94 return false
95 end
96 return true
97 end
100 --[[
101 sc7a20 初始化
102 @api sc7a20.init(i2c_id)
103 @number i2c id
104 @return bool 成功返回true
105 @usage
106 sc7a20.init(0)
108 function sc7a20.init(i2c_id)
109 i2cid = i2c_id
110 sys.wait(20)--20 毫秒等待设备稳定
111 if chip_check() then
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")
118 sys.wait(20)
119 return true
121 return false
124 --[[
125 获取 sc7a20 数据
126 @api sc7a20.get_data()
127 @return table sc7a20 数据
128 @usage
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")
140 return accel
144 --[[
145 设置 sc7a20 活动阀值
146 @api sc7a20.set_thresh (i2cid, activity, time_inactivity)
147 @number i2c id
148 @number 活动阀值
149 @number 活动持续时间
150 @usage
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)
159 --[[
160 设置 sc7a20 中断设置
161 @api sc7a20.set_irqf(i2cid, int, irqf_ths, irqf_duration, irqf_cfg)
162 @number i2c id
163 @number 中断脚 传入1及配置INT1脚,传入2及配置INT2脚
164 @number 中断阀值
165 @number 中断持续时间
166 @number 中断配置
167 @usage
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)
171 if int == 1 then
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)
176 elseif int == 2 then
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)
181 else
182 log.info("sc7a20", "int Parameter error")
187 return sc7a20