Skip to content

Commit 33b5132

Browse files
committed
micropython/drivers: Move "lps22h" sensor driver from main repo.
Signed-off-by: Jim Mussared <[email protected]>
1 parent b3e443c commit 33b5132

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2016-2019 shaoziyang <[email protected]>
5+
Copyright (c) 2022 Ibrahim Abdelkader <[email protected]>
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
LPS22HB/HH pressure sensor driver for MicroPython.
26+
27+
Example usage:
28+
29+
import time
30+
from lps22h import LPS22H
31+
from machine import Pin, I2C
32+
33+
bus = I2C(1, scl=Pin(15), sda=Pin(14))
34+
lps = LPS22H(bus, oneshot=False)
35+
36+
while (True):
37+
print("Pressure: %.2f hPa Temperature: %.2f C"%(lps.pressure(), lps.temperature()))
38+
time.sleep_ms(10)
39+
"""
40+
import machine
41+
42+
_LPS22_CTRL_REG1 = const(0x10)
43+
_LPS22_CTRL_REG2 = const(0x11)
44+
_LPS22_STATUS = const(0x27)
45+
_LPS22_TEMP_OUT_L = const(0x2B)
46+
_LPS22_PRESS_OUT_XL = const(0x28)
47+
_LPS22_PRESS_OUT_L = const(0x29)
48+
49+
50+
class LPS22H:
51+
def __init__(self, i2c, address=0x5C, oneshot=False):
52+
self.i2c = i2c
53+
self.addr = address
54+
self.oneshot = oneshot
55+
self.buf = bytearray(1)
56+
# ODR=1 EN_LPFP=1 BDU=1
57+
self._write_reg(_LPS22_CTRL_REG1, 0x1A)
58+
self.set_oneshot_mode(self.oneshot)
59+
60+
def _int16(self, d):
61+
return d if d < 0x8000 else d - 0x10000
62+
63+
def _write_reg(self, reg, dat):
64+
self.buf[0] = dat
65+
self.i2c.writeto_mem(self.addr, reg, self.buf)
66+
67+
def _read_reg(self, reg, width=8):
68+
self.i2c.readfrom_mem_into(self.addr, reg, self.buf)
69+
val = self.buf[0]
70+
if width == 16:
71+
val |= self._read_reg(reg + 1) << 8
72+
return val
73+
74+
def _tigger_oneshot(self, b):
75+
if self.oneshot:
76+
self._write_reg(_LPS22_CTRL_REG2, self._read_reg(_LPS22_CTRL_REG2) | 0x01)
77+
self._read_reg(0x28 + b * 2)
78+
while True:
79+
if self._read_reg(_LPS22_STATUS) & b:
80+
return
81+
machine.idle()
82+
83+
def set_oneshot_mode(self, oneshot):
84+
self._read_reg(_LPS22_CTRL_REG1)
85+
self.oneshot = oneshot
86+
if oneshot:
87+
self.buf[0] &= 0x0F
88+
else:
89+
self.buf[0] |= 0x10
90+
self._write_reg(_LPS22_CTRL_REG1, self.buf[0])
91+
92+
def pressure(self):
93+
if self.oneshot:
94+
self._tigger_oneshot(1)
95+
return (
96+
self._read_reg(_LPS22_PRESS_OUT_XL) + self._read_reg(_LPS22_PRESS_OUT_L, 16) * 256
97+
) / 4096
98+
99+
def temperature(self):
100+
if self.oneshot:
101+
self._tigger_oneshot(2)
102+
return self._int16(self._read_reg(_LPS22_TEMP_OUT_L, 16)) / 100
103+
104+
def altitude(self):
105+
return (
106+
(((1013.25 / self.pressure()) ** (1 / 5.257)) - 1.0)
107+
* (self.temperature() + 273.15)
108+
/ 0.0065
109+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module("lps22h.py", opt=3)

0 commit comments

Comments
 (0)