add: libfota需要兼容老的hmeta库, 没有hwver函数
[LuatOS.git] / script / libs / l3g4200d.lua
bloba5db7d120289b55a07c35960fdf6b81318ac53d9
1 --[[
2 @module l3g4200d
3 @summary l3g4200d 三轴数字陀螺仪传感器
4 @version 1.0
5 @date 2022.04.12
6 @author Dozingfiretruck
7 @usage
8 --注意:因使用了sys.wait()所有api需要在协程中使用
9 -- 用法实例
10 local l3g4200d = require "l3g4200d"
11 i2cid = 0
12 i2c_speed = i2c.FAST
13 sys.taskInit(function()
14 i2c.setup(i2cid,i2c_speed)
15 l3g4200d.init(i2cid)--初始化,传入i2c_id
16 while 1 do
17 local l3g4200d_data = l3g4200d.get_data()
18 log.info("l3g4200d_data", l3g4200d_data.x,l3g4200d_data.y,l3g4200d_data.z)
19 sys.wait(1000)
20 end
21 end)
25 local l3g4200d = {}
26 local sys = require "sys"
27 local i2cid
29 local L3G4200D_ADDRESS_ADR
31 local L3G4200D_ADDRESS_ADR_LOW = 0x68
32 local L3G4200D_ADDRESS_ADR_HIGH = 0x69
34 local L3G4200D_CHIP_ID_CHECK = 0x0F
35 local L3G4200D_CHIP_ID = 0xD3
37 ---器件所用地址
39 local L3G4200D_CTRL_REG1 = 0x20
40 local L3G4200D_CTRL_REG2 = 0x21
41 local L3G4200D_CTRL_REG3 = 0x22
42 local L3G4200D_CTRL_REG4 = 0x23
43 local L3G4200D_CTRL_REG5 = 0x24
44 local L3G4200D_REFERENCE = 0x25
45 local L3G4200D_OUT_TEMP = 0x26
46 local L3G4200D_STATUS_REG = 0x27
47 local L3G4200D_OUT_X_L = 0x28
48 local L3G4200D_OUT_X_H = 0x29
49 local L3G4200D_OUT_Y_L = 0x2A
50 local L3G4200D_OUT_Y_H = 0x2B
51 local L3G4200D_OUT_Z_L = 0x2C
52 local L3G4200D_OUT_Z_H = 0x2D
53 local L3G4200D_FIFO_CTRL_REG = 0x2E
54 local L3G4200D_FIFO_SRC_REG = 0x2F
55 local L3G4200D_INT1_CFG = 0x30
56 local L3G4200D_INT1_SRC = 0x31
57 local L3G4200D_INT1_TSH_XH = 0x32
58 local L3G4200D_INT1_TSH_XL = 0x33
59 local L3G4200D_INT1_TSH_YH = 0x34
60 local L3G4200D_INT1_TSH_YL = 0x35
61 local L3G4200D_INT1_TSH_ZH = 0x36
62 local L3G4200D_INT1_TSH_ZL = 0x37
63 local L3G4200D_INT1_DURATION = 0x38
65 --器件ID检测
66 local function chip_check()
67 i2c.send(i2cid, L3G4200D_ADDRESS_ADR_HIGH, L3G4200D_CHIP_ID_CHECK)--读器件地址
68 local revData = i2c.recv(i2cid, L3G4200D_ADDRESS_ADR_HIGH, 1)
69 if revData:byte() ~= nil then
70 L3G4200D_ADDRESS_ADR = L3G4200D_ADDRESS_ADR_HIGH
71 else
72 i2c.send(i2cid, L3G4200D_ADDRESS_ADR_LOW, L3G4200D_CHIP_ID_CHECK)--读器件地址
73 sys.wait(50)
74 local revData = i2c.recv(i2cid, L3G4200D_ADDRESS_ADR_LOW, 1)
75 if revData:byte() ~= nil then
76 L3G4200D_ADDRESS_ADR = L3G4200D_ADDRESS_ADR_LOW
77 else
78 log.info("Can't find L3G4200D device")
79 return false
80 end
81 end
83 i2c.send(i2cid, L3G4200D_ADDRESS_ADR, L3G4200D_CHIP_ID_CHECK)--读器件地址
84 local revData = i2c.recv(i2cid, L3G4200D_ADDRESS_ADR, 1)
85 if revData:byte() == L3G4200D_CHIP_ID then
86 log.info("Device i2c id is: L3G4200D")
87 return true
88 else
89 log.info("Can't find L3G4200D device")
90 return false
91 end
92 end
94 --[[
95 l3g4200d 初始化
96 @api l3g4200d.init(i2c_id)
97 @number 所在的i2c总线id
98 @return bool 成功返回true
99 @usage
100 l3g4200d.init(0)
102 function l3g4200d.init(i2c_id)
103 i2cid = i2c_id
104 if chip_check() then
105 i2c.send(i2cid, L3G4200D_ADDRESS_ADR, {L3G4200D_CTRL_REG1,0x0F})
106 i2c.send(i2cid, L3G4200D_ADDRESS_ADR, {L3G4200D_CTRL_REG2,0x00})
107 i2c.send(i2cid, L3G4200D_ADDRESS_ADR, {L3G4200D_CTRL_REG3,0x08})
108 i2c.send(i2cid, L3G4200D_ADDRESS_ADR, {L3G4200D_CTRL_REG4,0x30})
109 i2c.send(i2cid, L3G4200D_ADDRESS_ADR, {L3G4200D_CTRL_REG5,0x00})
111 return true
114 --[[
115 获取 l3g4200d 数据
116 @api l3g4200d.get_data()
117 @return table l3g4200d 数据
118 @usage
119 local l3g4200d_data = l3g4200d.get_data()
120 log.info("l3g4200d_data", l3g4200d_data.x,l3g4200d_data.y,l3g4200d_data.z)
122 function l3g4200d.get_data()
123 local l3g4200d_data = {}
124 i2c.send(i2cid, L3G4200D_ADDRESS_ADR,L3G4200D_OUT_X_L)
125 local data = i2c.recv(i2cid, L3G4200D_ADDRESS_ADR, 6)
126 _, l3g4200d_data.x, l3g4200d_data.y, l3g4200d_data.z = pack.unpack(data, "<h3")
127 return l3g4200d_data
130 return l3g4200d