Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
aerobs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andreas Gattringer
aerobs
Commits
6ee2144f
Commit
6ee2144f
authored
1 year ago
by
Andreas Gattringer
Browse files
Options
Downloads
Patches
Plain Diff
added SHT45 support
parent
e8598bfd
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
umnp/microcontroller/sensors/sht45/__init__.py
+94
-0
94 additions, 0 deletions
umnp/microcontroller/sensors/sht45/__init__.py
with
94 additions
and
0 deletions
umnp/microcontroller/sensors/sht45/__init__.py
0 → 100644
+
94
−
0
View file @
6ee2144f
try
:
from
machine
import
I2C
except
ImportError
:
from
umnp.microcontroller.umock.machine
import
I2C
import
asyncio
import
time
SHT45_HIRES
=
0xFD
NO_HOLD_WAIT_TIME
=
18.5
/
1000
/
1000
SHT45_WAIT_TIME_MS
=
9
SHT45_RESET_TIME_MS
=
1
SHT45_SOFT_RESET
=
0x94
SHT45_DEFAULT_ADDRESS
=
0x44
class
SHT45
:
def
__init__
(
self
,
i2c
:
I2C
,
i2c_address
=
SHT45_DEFAULT_ADDRESS
):
self
.
_i2c
=
i2c
self
.
_i2c_address
=
i2c_address
self
.
_initialised
=
False
self
.
_nan
=
float
(
"
NAN
"
)
devices
=
self
.
_i2c
.
scan
()
if
self
.
_i2c_address
not
in
devices
:
raise
RuntimeError
(
"
SHT45: Device not found
"
)
if
self
.
reset
():
self
.
_initialised
=
True
else
:
print
(
"
Could not initialise device
"
)
@property
def
initialised
(
self
)
->
bool
:
return
self
.
_initialised
def
_command
(
self
,
command
:
int
)
->
int
:
pass
def
_calulate_crc8
(
self
,
data
,
polynom
=
0x31
,
init
=
0xFF
):
# CRC-8-Dallas/Maxim for I2C with 0x31 polynomial
crc
=
init
for
byte
in
data
:
crc
^=
byte
for
_
in
range
(
8
):
if
crc
&
0x80
:
crc
=
(
crc
<<
1
)
^
polynom
else
:
crc
=
crc
<<
1
crc
&=
0xFF
return
crc
def
reset
(
self
)
->
bool
:
cmd
=
bytearray
(
1
)
cmd
[
0
]
=
SHT45_SOFT_RESET
n_ack
=
self
.
_i2c
.
writeto
(
self
.
_i2c_address
,
cmd
)
time
.
sleep
(
SHT45_RESET_TIME_MS
/
1000
)
if
n_ack
==
1
:
return
True
return
False
def
_decode_and_verify_value
(
self
,
buffer
):
if
self
.
_calulate_crc8
(
buffer
[:
2
])
!=
buffer
[
2
]:
return
self
.
_nan
return
buffer
[
1
]
+
(
buffer
[
0
]
<<
8
)
def
_translate_temperature
(
self
,
buffer
)
->
float
:
t_raw
=
self
.
_decode_and_verify_value
(
buffer
)
return
-
45
+
175
*
t_raw
/
(
2
**
16
-
1
)
def
_translate_rh
(
self
,
buffer
):
rh_raw
=
self
.
_decode_and_verify_value
(
buffer
)
return
-
6
+
125
*
rh_raw
/
(
2
**
16
-
1
)
async
def
measure
(
self
):
temperature
=
self
.
_nan
rh
=
self
.
_nan
if
not
self
.
initialised
:
return
temperature
,
rh
cmd
=
bytearray
(
1
)
cmd
[
0
]
=
SHT45_HIRES
n_ack
=
self
.
_i2c
.
writeto
(
self
.
_i2c_address
,
cmd
)
if
n_ack
!=
1
:
return
temperature
,
rh
await
asyncio
.
sleep
(
SHT45_WAIT_TIME_MS
/
1000
)
data
=
self
.
_i2c
.
readfrom
(
self
.
_i2c_address
,
6
)
temperature
=
self
.
_translate_temperature
(
data
[
0
:
3
])
rh
=
self
.
_translate_rh
(
data
[
3
:
6
])
return
temperature
,
rh
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment