Skip to content

Commit 70149b1

Browse files
committed
add LPS22 pressure sensor
0 parents  commit 70149b1

File tree

8 files changed

+237
-0
lines changed

8 files changed

+237
-0
lines changed

.gitignore

Whitespace-only changes.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019, microbit/micropython Chinese community
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

LPS22.py

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/**
2+
* ST LPS22HB/HD/HH pressure Sensor I2C extension for makecode.
3+
* From microbit/micropython Chinese community.
4+
* https://github.com/makecode-extensions
5+
*/
6+
7+
/**
8+
* ST LPS22HB/HD/HH pressure Sensor I2C extension
9+
*/
10+
//% weight=100 color=#60a0e0 icon="\uf2c8" block="LPS22"
11+
namespace LPS22 {
12+
export enum LPS22_I2C_ADDRESS {
13+
//% block="92"
14+
ADDR_92 = 92,
15+
//% block="93"
16+
ADDR_93 = 93
17+
}
18+
19+
export enum LPS22_P_UNIT {
20+
//% block="Pa"
21+
Pa = 0,
22+
//% block="hPa"
23+
hPa = 1
24+
}
25+
26+
export enum LPS22_T_UNIT {
27+
//% block="C"
28+
C = 0,
29+
//% block="F"
30+
F = 1
31+
}
32+
33+
const LPS22_CTRL_REG1 = (0x10)
34+
const LPS22_CTRL_REG2 = (0x11)
35+
const LPS22_STATUS = (0x27)
36+
const LPS22_TEMP_OUT_L = (0x2B)
37+
const LPS22_PRESS_OUT_XL = (0x28)
38+
const LPS22_PRESS_OUT_L = (0x29)
39+
40+
let LPS22_I2C_ADDR = LPS22.LPS22_I2C_ADDRESS.ADDR_93 // defaut address
41+
let _oneshot = false
42+
43+
init() // init sensor at startup
44+
45+
// set dat to reg
46+
function setreg(reg: number, dat: number): void {
47+
let tb = pins.createBuffer(2)
48+
tb[0] = reg
49+
tb[1] = dat
50+
pins.i2cWriteBuffer(LPS22_I2C_ADDR, tb)
51+
}
52+
53+
// read a Int8LE from reg
54+
function getInt8LE(reg: number): number {
55+
pins.i2cWriteNumber(LPS22_I2C_ADDR, reg, NumberFormat.UInt8BE);
56+
return pins.i2cReadNumber(LPS22_I2C_ADDR, NumberFormat.Int8LE);
57+
}
58+
59+
// read a UInt8LE from reg
60+
function getUInt8LE(reg: number): number {
61+
pins.i2cWriteNumber(LPS22_I2C_ADDR, reg, NumberFormat.UInt8BE);
62+
return pins.i2cReadNumber(LPS22_I2C_ADDR, NumberFormat.UInt8LE);
63+
}
64+
65+
// read a Int16LE from reg
66+
function getInt16LE(reg: number): number {
67+
pins.i2cWriteNumber(LPS22_I2C_ADDR, reg | 0x80, NumberFormat.UInt8BE);
68+
return pins.i2cReadNumber(LPS22_I2C_ADDR, NumberFormat.Int16LE);
69+
}
70+
71+
// read a UInt16LE from reg
72+
function getUInt16LE(reg: number): number {
73+
pins.i2cWriteNumber(LPS22_I2C_ADDR, reg | 0x80, NumberFormat.UInt8BE);
74+
return pins.i2cReadNumber(LPS22_I2C_ADDR, NumberFormat.UInt16LE);
75+
}
76+
77+
// set a mask dat to reg
78+
function setreg_mask(reg: number, dat: number, mask: number): void {
79+
setreg(reg, (getUInt8LE(reg) & mask) | dat)
80+
}
81+
82+
// limit decimal digits
83+
function bitround(x: number, b: number = 1): number {
84+
let n = 10 ** b
85+
return Math.round(x * n) / n
86+
}
87+
88+
/**
89+
* Init sensor
90+
*/
91+
//% block="Initialize, address %addr"
92+
//% addr.defl=LPS22.LPS22_I2C_ADDRESS.ADDR_93
93+
export function init(addr: LPS22.LPS22_I2C_ADDRESS = LPS22.LPS22_I2C_ADDRESS.ADDR_93) {
94+
LPS22_I2C_ADDR = (addr == LPS22.LPS22_I2C_ADDRESS.ADDR_93) ? LPS22.LPS22_I2C_ADDRESS.ADDR_93 : LPS22.LPS22_I2C_ADDRESS.ADDR_92
95+
96+
// ODR = 1 EN_LPFP= 1 BDU= 1
97+
setreg(LPS22_CTRL_REG1, 0x1A)
98+
oneshot_mode(false)
99+
}
100+
101+
// oneshot mode handle
102+
function ONE_SHOT(b: number): void {
103+
if (_oneshot) {
104+
setreg(LPS22_CTRL_REG2, getUInt8LE(LPS22_CTRL_REG2) | 0x01)
105+
getUInt8LE(0x28 + b * 2)
106+
while (true) {
107+
if (getUInt8LE(LPS22_STATUS) & b)
108+
return
109+
}
110+
}
111+
}
112+
113+
/**
114+
* set oneshot mode to reduce power consumption
115+
*/
116+
//% block="oneshot mode %oneshot"
117+
export function oneshot_mode(oneshot: boolean = false) {
118+
_oneshot = oneshot
119+
let t = (oneshot) ? 0 : 0x10
120+
setreg_mask(LPS22_CTRL_REG1, t, 0x0F)
121+
}
122+
123+
/**
124+
* get temperature
125+
*/
126+
//% block="temperature %u"
127+
export function temperature(u: LPS22.LPS22_T_UNIT = LPS22.LPS22_T_UNIT.C): number {
128+
ONE_SHOT(2)
129+
let T = getInt16LE(LPS22_TEMP_OUT_L) / 100
130+
if (u == LPS22.LPS22_T_UNIT.F) T = 32 + T * 9 / 5
131+
return bitround(T, 1)
132+
}
133+
134+
/**
135+
* get pressure from sensor
136+
*/
137+
//% block="pressure %u"
138+
//% u.defl=LPS22.LPS22_P_UNIT.hPa
139+
export function pressure(u: LPS22.LPS22_P_UNIT = LPS22.LPS22_P_UNIT.hPa): number {
140+
ONE_SHOT(1)
141+
let P = (getUInt16LE(LPS22_PRESS_OUT_L) * 256 + getUInt8LE(LPS22_PRESS_OUT_XL)) / 4096
142+
if (u == LPS22.LPS22_P_UNIT.Pa) P = P * 100
143+
return Math.round(P)
144+
}
145+
146+
/**
147+
* calaulate altitude use pressure and temperature
148+
*/
149+
//% block="altitude"
150+
export function altitude(): number {
151+
return (((1013.25 / pressure()) ** (1 / 5.257)) - 1.0) * (temperature() + 273.15) / 0.0065
152+
}
153+
}

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# LPS22HB/HD/HH package
2+
3+
ST LPS22 Pressure Sensor I2C extension for makecode.
4+
5+
Author: shaoziyang
6+
Date: 2019.Jul
7+
8+
## Basic usage
9+
```
10+
basic.forever(function () {
11+
serial.writeValue("T", LPS22.temperature(LPS22.LPS22_T_UNIT.C))
12+
serial.writeValue("P", LPS22.pressure(LPS22.LPS22_P_UNIT.hPa))
13+
serial.writeValue("A", LPS22.altitude())
14+
basic.pause(1000)
15+
})
16+
```
17+
18+
**Note**
19+
altitude() function is always zero because MakeCode power function error ([see issue #2192](https://github.com/microsoft/pxt-microbit/issues/2192))! It will be available after microsoft fixed it.
20+
21+
## License
22+
23+
MIT
24+
25+
Copyright (c) 2018, microbit/micropython Chinese community
26+
27+
## Supported targets
28+
29+
* for PXT/microbit
30+
31+
32+
[From microbit/micropython Chinese community](http://www.micropython.org.cn)

_locales/zh/LPS22-strings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"LPS22.init|block": "初始化, 地址 %addr",
3+
"LPS22.oneshot_mode|block": "oneshot 模式 %oneshot",
4+
"LPS22.temperature|block": "温度 %u",
5+
"LPS22.pressure|block": "气压 %u",
6+
"LPS22.altitude|block": "高度"
7+
}

icon.png

196 KB
Loading

pxt.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "LPS22",
3+
"version": "1.0.0",
4+
"description": "ST LPS22 Pressure Sensor I2C extension for makecode.",
5+
"license": "MIT",
6+
"dependencies": {
7+
"core": "*"
8+
},
9+
"files": [
10+
"README.md",
11+
"_locales/zh/LPS22-strings.json",
12+
"LPS22.ts"
13+
],
14+
"testFiles": [
15+
"test.ts"
16+
],
17+
"public": true
18+
}

test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
basic.forever(function () {
2+
basic.pause(1000)
3+
serial.writeValue("T", LPS22.temperature(LPS22.LPS22_T_UNIT.C))
4+
serial.writeValue("P", LPS22.pressure(LPS22.LPS22_P_UNIT.hPa))
5+
serial.writeValue("A", LPS22.altitude())
6+
})

0 commit comments

Comments
 (0)