Skip to content

Commit

Permalink
Merge pull request #126 from FoamyGuy/config_mode_fix
Browse files Browse the repository at this point in the history
ensure config mode before setting range or bandwidth settings
  • Loading branch information
FoamyGuy authored Nov 20, 2024
2 parents 00b16c6 + 0a31521 commit 27a28af
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions adafruit_bno055.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,17 @@ def accel_range(self) -> int:

@accel_range.setter
def accel_range(self, rng: int = ACCEL_4G) -> None:
old_mode = None
if self.mode != CONFIG_MODE:
old_mode = self.mode
self.mode = CONFIG_MODE
self._write_register(_PAGE_REGISTER, 0x01)
value = self._read_register(_ACCEL_CONFIG_REGISTER)
masked_value = 0b11111100 & value
self._write_register(_ACCEL_CONFIG_REGISTER, masked_value | rng)
self._write_register(_PAGE_REGISTER, 0x00)
if old_mode is not None:
self.mode = old_mode

@property
def accel_bandwidth(self) -> int:
Expand All @@ -534,11 +540,17 @@ def accel_bandwidth(self) -> int:
def accel_bandwidth(self, bandwidth: int = ACCEL_62_5HZ) -> None:
if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]:
raise RuntimeError("Mode must not be a fusion mode")
old_mode = None
if self.mode != CONFIG_MODE:
old_mode = self.mode
self.mode = CONFIG_MODE
self._write_register(_PAGE_REGISTER, 0x01)
value = self._read_register(_ACCEL_CONFIG_REGISTER)
masked_value = 0b11100011 & value
self._write_register(_ACCEL_CONFIG_REGISTER, masked_value | bandwidth)
self._write_register(_PAGE_REGISTER, 0x00)
if old_mode is not None:
self.mode = old_mode

@property
def accel_mode(self) -> int:
Expand Down Expand Up @@ -574,11 +586,17 @@ def gyro_range(self) -> int:
def gyro_range(self, rng: int = GYRO_2000_DPS) -> None:
if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]:
raise RuntimeError("Mode must not be a fusion mode")
old_mode = None
if self.mode != CONFIG_MODE:
old_mode = self.mode
self.mode = CONFIG_MODE
self._write_register(_PAGE_REGISTER, 0x01)
value = self._read_register(_GYRO_CONFIG_0_REGISTER)
masked_value = 0b00111000 & value
self._write_register(_GYRO_CONFIG_0_REGISTER, masked_value | rng)
self._write_register(_PAGE_REGISTER, 0x00)
if old_mode is not None:
self.mode = old_mode

@property
def gyro_bandwidth(self) -> int:
Expand All @@ -594,11 +612,17 @@ def gyro_bandwidth(self) -> int:
def gyro_bandwidth(self, bandwidth: int = GYRO_32HZ) -> None:
if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]:
raise RuntimeError("Mode must not be a fusion mode")
old_mode = None
if self.mode != CONFIG_MODE:
old_mode = self.mode
self.mode = CONFIG_MODE
self._write_register(_PAGE_REGISTER, 0x01)
value = self._read_register(_GYRO_CONFIG_0_REGISTER)
masked_value = 0b00000111 & value
self._write_register(_GYRO_CONFIG_0_REGISTER, masked_value | bandwidth)
self._write_register(_PAGE_REGISTER, 0x00)
if old_mode is not None:
self.mode = old_mode

@property
def gyro_mode(self) -> int:
Expand Down

0 comments on commit 27a28af

Please sign in to comment.