Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
adding compass example
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Aug 30, 2023
1 parent eec42af commit b6e76e8
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Introduction
:target: https://micropython-lis3mdl.readthedocs.io/en/latest/
:alt: Documentation Status


.. image:: https://img.shields.io/badge/micropython-Ok-purple.svg
:target: https://micropython.org
:alt: micropython
Expand Down
4 changes: 0 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
sys.path.insert(0, os.path.abspath(".."))

try:
# Inject mock modules so that we can build the
# documentation without having the real stuff available
from mock import Mock

to_be_mocked = [
Expand Down Expand Up @@ -41,9 +39,7 @@
"MicroPython": ("https://docs.micropython.org/en/latest/", None),
}

# autodoc_mock_imports = ["digitalio", "busio"]
autoclass_content = "both"
# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
Expand Down
9 changes: 9 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ Example showing the Operation mode setting
.. literalinclude:: ../examples/lis3mdl_operation_mode.py
:caption: examples/lis3mdl_operation_mode.py
:lines: 5-

Compass Example
------------------------

Example showing how to use the LIS3MDL as a compass

.. literalinclude:: ../examples/lis3mdl_compass.py
:caption: examples/lis3mdl_compass.py
:lines: 5-
29 changes: 24 additions & 5 deletions examples.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
{
"urls": [
["micropython_lis3mdl/examples/lis3mdl_data_rate.py", "github:jposada202020/MicroPython_LIS3MDL/examples/lis3mdl_data_rate.py"],
["micropython_lis3mdl/examples/lis3mdl_scale_range.py", "github:jposada202020/MicroPython_LIS3MDL/examples/lis3mdl_scale_range.py"],
["micropython_lis3mdl/examples/lis3mdl_simpletest.py", "github:jposada202020/MicroPython_LIS3MDL/examples/lis3mdl_simpletest.py"],
["micropython_lis3mdl/examples/lis3mdl_low_power_mode.py", "github:jposada202020/MicroPython_LIS3MDL/examples/lis3mdl_low_power_mode.py"],
["micropython_lis3mdl/examples/lis3mdl_operation_mode.py", "github:jposada202020/MicroPython_LIS3MDL/examples/lis3mdl_operation_mode.py"]
[
"micropython_lis3mdl/examples/lis3mdl_data_rate.py",
"github:jposada202020/MicroPython_LIS3MDL/examples/lis3mdl_data_rate.py"
],
[
"micropython_lis3mdl/examples/lis3mdl_scale_range.py",
"github:jposada202020/MicroPython_LIS3MDL/examples/lis3mdl_scale_range.py"
],
[
"micropython_lis3mdl/examples/lis3mdl_simpletest.py",
"github:jposada202020/MicroPython_LIS3MDL/examples/lis3mdl_simpletest.py"
],
[
"micropython_lis3mdl/examples/lis3mdl_low_power_mode.py",
"github:jposada202020/MicroPython_LIS3MDL/examples/lis3mdl_low_power_mode.py"
],
[
"micropython_lis3mdl/examples/lis3mdl_operation_mode.py",
"github:jposada202020/MicroPython_LIS3MDL/examples/lis3mdl_operation_mode.py"
],
[
"micropython_lis3mdl/examples/lis3mdl_compass.py",
"github:jposada202020/MicroPython_LIS3MDL/examples/lis3mdl_compass.py"
]
],
"version": "1"
}
29 changes: 29 additions & 0 deletions examples/lis3mdl_compass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT

import time
from math import atan2, degrees
from machine import Pin, I2C
from micropython_lis3mdl import lis3mdl

i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis3mdl.LIS3MDL(i2c)


def vector_2_degrees(x, y):
angle = degrees(atan2(y, x))
if angle < 0:
angle += 360
return angle


def get_heading(_sensor):
magnet_x, magnet_y, _ = _sensor.magnetic
return vector_2_degrees(magnet_x, magnet_y)


while True:
print(f"heading: {get_heading(lis):.2f} degrees")
time.sleep(0.2)
4 changes: 2 additions & 2 deletions micropython_lis3mdl/lis3mdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
MicroPython Driver for the ST LIS3MDL magnetometer
* Author(s): Jose D. Montoya
* Author: Jose D. Montoya
"""
Expand Down Expand Up @@ -139,7 +139,7 @@ def __init__(self, i2c, address: int = 0x1C) -> None:
self._address = address

if self._device_id != 0x3D:
raise RuntimeError("Failed to find LIS3MDL")
raise RuntimeError("Failed to find the LIS3MDL sensor")

self._operation_mode = CONTINUOUS
self._scale_cached_factor = scale_range_factor[self._scale_range]
Expand Down

0 comments on commit b6e76e8

Please sign in to comment.