Skip to content

Commit

Permalink
updating magnetic property
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Apr 20, 2023
1 parent a817778 commit 1cf1091
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 56 deletions.
18 changes: 9 additions & 9 deletions examples/qmc5883l_advanced_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
# SPDX-License-Identifier: MIT
# pylint: disable=protected-access
import board
import circuitpython_qmc5883l as qmc5883
import qmc5883l


i2c = board.I2C()
qmc = qmc5883.QMC5883L(i2c)
qmc = qmc5883l.QMC5883L(i2c)

print("Initial Configuration register", bin(qmc._conf_reg))
print("Setting Oversample to 64(0b11)")
qmc.oversample = qmc5883.OVERSAMPLE_64
qmc.oversample = qmc5883l.OVERSAMPLE_64
print("Configuration register", bin(qmc._conf_reg))
print("Setting Oversample to 128(0b10)")
qmc.oversample = qmc5883.OVERSAMPLE_128
qmc.oversample = qmc5883l.OVERSAMPLE_128
print("Configuration register", bin(qmc._conf_reg))
print("Setting Oversample to 2G (0b00)")
qmc.field_range = qmc5883.FIELDRANGE_2G
qmc.field_range = qmc5883l.FIELDRANGE_2G
print("Configuration register", bin(qmc._conf_reg))
print("Setting Range to 8G (0b01)")
qmc.field_range = qmc5883.FIELDRANGE_8G
qmc.field_range = qmc5883l.FIELDRANGE_8G
print("Configuration register", bin(qmc._conf_reg))
print("setting ouput Data Rate 100HZ (0b10)")
qmc.output_data_rate = qmc5883.OUTPUT_DATA_RATE_100
qmc.output_data_rate = qmc5883l.OUTPUT_DATA_RATE_100
print("Configuration register", bin(qmc._conf_reg))
print("setting ouput Data Rate 200HZ (0b11)")
qmc.output_data_rate = qmc5883.OUTPUT_DATA_RATE_200
qmc.output_data_rate = qmc5883l.OUTPUT_DATA_RATE_200
print("Configuration register", bin(qmc._conf_reg))
print("setting Mode to Continuous (0b01)")
qmc.mode_control = qmc5883.MODE_CONTINUOUS
qmc.mode_control = qmc5883l.MODE_CONTINUOUS
print("Configuration register", bin(qmc._conf_reg))
12 changes: 6 additions & 6 deletions examples/qmc5883l_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
# SPDX-License-Identifier: MIT
import time
import board
import circuitpython_qmc5883l as qmc5883
import qmc5883l

i2c = board.I2C()
qmc = qmc5883.QMC5883L(i2c)
qmc = qmc5883l.QMC5883L(i2c)

qmc.oversample = qmc5883.OVERSAMPLE_128
qmc.field_range = qmc5883.FIELDRANGE_2G
qmc.output_data_rate = qmc5883.OUTPUT_DATA_RATE_200
qmc.mode_control = qmc5883.MODE_CONTINUOUS
qmc.oversample = qmc5883l.OVERSAMPLE_128
qmc.field_range = qmc5883l.FIELDRANGE_2G
qmc.output_data_rate = qmc5883l.OUTPUT_DATA_RATE_200
qmc.mode_control = qmc5883l.MODE_CONTINUOUS

for i in range(50):
mag_x, mag_y, mag_z = qmc.magnetic
Expand Down
49 changes: 8 additions & 41 deletions qmc5883l.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya for Trinity
#
# SPDX-License-Identifier: MIT
Expand All @@ -20,9 +19,10 @@
"""

import time
from micropython import const
from adafruit_bus_device import i2c_device
from adafruit_register.i2c_struct import ROUnaryStruct, UnaryStruct
from adafruit_register.i2c_struct import ROUnaryStruct, UnaryStruct, Struct
from adafruit_register.i2c_bits import RWBits, ROBits

try:
Expand Down Expand Up @@ -74,7 +74,7 @@ class QMC5883L:
.. code-block:: python
import board
import circuitpython_qmc5883l.qmc5883l as qmc5883l
import qmc5883l
Once this is done you can define your `board.I2C` object and define your sensor object
Expand All @@ -100,12 +100,7 @@ class QMC5883L:
_output_data_rate = RWBits(2, _REG_OPERATION_MODE, 2)
_mode_control = RWBits(2, _REG_OPERATION_MODE, 0)
_data_ready_register = ROBits(1, _REG_STATUS, 2)
_x_LSB = ROUnaryStruct(0x00, "H")
_x_MSB = ROUnaryStruct(0x01, "H")
_y_LSB = ROUnaryStruct(0x02, "H")
_y_MSB = ROUnaryStruct(0x03, "H")
_z_LSB = ROUnaryStruct(0x04, "H")
_z_MSB = ROUnaryStruct(0x05, "H")
_measures = Struct(0x00, "<hhhBh")

def __init__(self, i2c_bus: I2C, address: int = _I2C_ADDR) -> None:
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
Expand Down Expand Up @@ -291,36 +286,8 @@ def mode_control(self, mode: int) -> None:
@property
def magnetic(self):
"""Magnetic property"""
if self._data_ready_register == 1:

values = (
self._x_LSB,
self._x_MSB,
self._y_LSB,
self._y_MSB,
self._z_LSB,
self._z_MSB,
)

return (
values[0] / self.resolution,
values[2] / self.resolution,
values[4] / self.resolution,
)
return None

@staticmethod
def twos_complement(val: int, bits: int) -> int:
"""
Args:
val: Value to be converted
bits: nummber of bits
Returns: Converted Value
"""
if val & (1 << (bits - 1)):
val -= 1 << bits
while self._data_ready_register != 1:
time.sleep(0.001)
x, y, z, _, _ = self._measures

return val
return x / self.resolution, y / self.resolution, z / self.resolution

0 comments on commit 1cf1091

Please sign in to comment.