Skip to content

Refactor decode_devid to be callable by python code - #33882

Open
amilcarlucas wants to merge 1 commit into
ArduPilot:masterfrom
amilcarlucas:decode_ids_lib
Open

Refactor decode_devid to be callable by python code#33882
amilcarlucas wants to merge 1 commit into
ArduPilot:masterfrom
amilcarlucas:decode_ids_lib

Conversation

@amilcarlucas

Copy link
Copy Markdown
Contributor

Summary

Refactor the decode_devid script to be callable by other python scripts

Also fix the lining issues and provide tests and examples

Classification & Testing (check all that apply and add your own)

  • Checked by a human programmer
  • Non-functional change
  • No-binary change
  • Infrastructure change (e.g. unit tests, helper scripts)
  • Automated test(s) verify changes (e.g. unit test, autotest)
  • Tested manually, description below (e.g. SITL)
  • Tested on hardware
  • Logs attached
  • Logs available on request

Description

Here is the output of the python .\example_usage_decode_devid.py

======================================================================
EXAMPLE 1: Decoding a specific device ID
======================================================================

Input device ID (hex): 0x00051001
Input device ID (dec): 331777

Decoded components:
  Bus type: I2C (1)
  Bus: 0
  Address: 0x10 (16)
  Device type: 0x05 (5)
  Device name: DEVTYPE_BMM150 
  Is DRONECAN: False

Formatted output:
  bus_type:I2C(1)  bus:0 address:16(0x10) devtype:5(0x5) DEVTYPE_BMM150 

======================================================================
EXAMPLE 2: Processing multiple device IDs
======================================================================

IMU device with I2C bus
  Device ID: 0x00031801
  Result: bus_type:I2C(1)  bus:0 address:24(0x18) devtype:3(0x3) UNKNOWN

Compass with SPI bus
  Device ID: 0x0006b001
  Result: bus_type:I2C(1)  bus:0 address:176(0xb0) devtype:6(0x6) DEVTYPE_LSM9DS1

Barometer with I2C bus
  Device ID: 0x00000c02
  Result: bus_type:SPI(2)  bus:0 address:12(0xc) devtype:0(0x0) UNKNOWN

======================================================================
EXAMPLE 3: Browsing available sensor types
======================================================================

Available Compass Types:
  0x01: DEVTYPE_HMC5883_OLD
  0x02: DEVTYPE_LSM303D
  0x04: DEVTYPE_AK8963 
  0x05: DEVTYPE_BMM150 
  0x06: DEVTYPE_LSM9DS1
  ... and 19 more

Available IMU Types: 35 types
Available Barometer Types: 24 types
Available Airspeed Types: 11 types

======================================================================
EXAMPLE 4: Using with string input (hex or decimal)
======================================================================

Parsing from hex string '0x00031801': 202753
Parsing from decimal string '3227137': 3227137
Are they equal? False

Here is the output of the python .\test_decode_devid.py test:

================================================================================
Testing individual library functions
================================================================================

Testing parse_device_id():
  parse_device_id('0x00051001') = 331777
  parse_device_id('331777') = 331777
  parse_device_id('0x0006b001') = 438273

Testing decode_device_id():
  Input: 0x00051001
  Bus type: I2C (1)
  Bus: 0
  Address: 0x10
  Device type: 5
  Is DRONECAN: False

Testing get_device_type_name():
  Compass 0x05: DEVTYPE_BMM150 
  IMU 0x09: DEVTYPE_BMI160

Testing format_device_info():
  bus_type:I2C(1)  bus:0 address:16(0x10) devtype:5(0x5) DEVTYPE_BMM150 
================================================================================
Testing decode_devid.py vs decode_devid_lib.py
================================================================================

✓ Compass - I2C bus, BMM150
  Device ID: 0x00051001, Flag: -C

✓ Compass - I2C bus, HMC5883
  Device ID: 0x00071E05, Flag: -C

✓ Compass - I2C bus, AK8963
  Device ID: 0x0001E004, Flag: -C

✓ IMU - I2C bus, ACC_MPU6000
  Device ID: 0x00091301, Flag: -I

✓ IMU - SPI bus, BMI160
  Device ID: 0x00000902, Flag: -I

✓ Barometer - SPI bus, BMP280
  Device ID: 0x00000302, Flag: -B

✓ Barometer - SPI bus, MS5611
  Device ID: 0x00040B02, Flag: -B

✓ Airspeed - SPI bus, MS4525
  Device ID: 0x00000102, Flag: -A

✓ Airspeed - SPI bus, MS5525
  Device ID: 0x00020302, Flag: -A

✓ DRONECAN compass
  Device ID: 0x00010083, Flag: -C

✓ DRONECAN IMU
  Device ID: 0x00010183, Flag: -I

✓ Minimal device ID
  Device ID: 0x00000001, Flag: -C

✓ Maximum device ID
  Device ID: 0xFFFFFFFF, Flag: -I

================================================================================
Test Results: 13 passed, 0 failed
================================================================================

✓ All tests passed! Both scripts produce identical output.

I did break the _lib script on purpose and the test code found the issues.

Should it be merged like this or should I just change the original script?

Also fix the lining issues and provide tests and examples

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Refactors the decode_devid functionality into an importable Python module and adds scripts to demonstrate usage and validate parity with the original CLI output.

Changes:

  • Added decode_devid_lib.py providing reusable functions (parse_device_id, decode_device_id, get_device_type_name, format_device_info) plus a CLI entrypoint.
  • Added an example script demonstrating library usage (example_usage_decode_devid.py).
  • Added a test script that compares outputs of decode_devid.py vs decode_devid_lib.py (test_decode_devid.py).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
Tools/scripts/decode_devid_lib.py Introduces importable decoding library + CLI wrapper to preserve script usability.
Tools/scripts/example_usage_decode_devid.py Demonstrates how to call the new library functions from Python.
Tools/scripts/test_decode_devid.py Adds parity tests comparing original script output to library output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +106 to +124
if original_normalized == library_normalized:
self.tests_passed += 1
status = "✓ PASS"
return True
else:
self.tests_failed += 1
status = "✗ FAIL"
self.failures.append({
'description': description,
'device_id': device_id_hex,
'flag': device_type_flag,
'original': original_output,
'library': library_output,
})
return False

print(f"{status} | {description}")
print(f" Device ID: {device_id_hex}, Flag: {device_type_flag}")
return original_normalized == library_normalized


def decode_device_id(device_id: int) -> Dict[str, any]:
"""


def format_device_info(decode_info: Dict, device_type_name: str = "") -> str:
"""
Comment on lines +225 to +227
"""Command-line interface."""
parser = optparse.OptionParser("decode_devid.py")
parser.add_option("-C", "--compass", action='store_true', help='decode compass IDs')
Comment on lines +2 to +4
"""
Example script demonstrating how to use decode_devid.py as an importable module.
"""
import sys
from pathlib import Path

# Add the current directory to path so we can import decode_devid

# Example: Decode a compass device ID
# Let's use a compass device with I2C bus
device_id = 0x00051001 # I2C bus, compass type 0x01 (HMC5883_OLD)

@peterbarker peterbarker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right - it's a sideways copy of the script.

If you want to make a library out of the thing then do it in place.

@amilcarlucas

amilcarlucas commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

@peterbarker I just did a copy so that I can prove to you that it is working. That is what the test script does: compares the script with the lib.
I will be happy to do it in place once you are happy that is working and is has been correctly regression tested.

@amilcarlucas

Copy link
Copy Markdown
Contributor Author

I will fix the pre-commit hooks in the next few days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants