Skip to content

Improvements for the CXD5602PWBIMU and IMU add-on board #18

Description

@btpsj

Recent work landed (#14) providing support for the CXD5602PWBIMU and corresponding IMU add-on board. The example is functional but can be improved with various aspects, so I'm documenting some of potential changes here. I don't have a board so I'll defer changes to later, since some aspects my have larger API changes that need validation on the hardware itself.

  • refactor(imu): renaming structs to have capital I
    Some structs have a capital I like PwbImuParts but others have it lowercase Pwbimu. This should be changed to be consistent and just use capital I across all
  • refactor(imu): separate sensor from board
    The pwbimu.rs module is big, and holds two distinct components. One is for the sensor and the other is for the Spresense-specific implementation. To make it easier to manage, splitting these into two separate modules would be ideal. A large refactoring could also split the sensor code out to be its own crate. This would make it easier to reuse, testing, compile-times without necessarily being coupled to depending on the spresense-bsp.
  • feat(imu): improve ergonomics around generic type parameters
    Many of the structs and functions have several generic type parameters, and some functions require skipping a clippy lint to allow for complicated type signatures. This, I expect, can be difficult to work with by hand, and also cause really large type errors if things are wrong. Having to skip the clippy lint as well points to needing to shift to some other design. Some different strategies to mitigate this are: grouping the GPIO pins under a trait and Pins struct, making the state transitions be from state-specific structs instead of encoding in a type parameter.
  • feat(imu): builder for enabling sensor
    The current api has power_on, enable, and configure methods. Instead of needing to carry out this sequence, and forgetting some part, it would be easier and less error-prone to combine things together, and apply the configuration using a builder struct.
  • fix(imu): switch imu example to uart_alt
    The *_alt.rs modules are intended to replace the the current set of peripherals, which don't have the new API around clock configuration.
  • refactor(imu): switch example to let-else
    There are few cases in the example for the error paths where it seems like it would be more ergonomic to use a let-else block, especially since fail returns !, and cleanup some verbose match ... { Ok(s) => s, ... blocks.
  • feat(imu): flatten error types
    Similar to the type complexity mentioned above, but not necessarily an improvement. There are two ways to handle errors, which is adding a generic type parameter, which makes passing the full error information downstream easier, but the types get complicated, especially if you have an error type that has multiple errors like Error which handles errors across I2C, SPI, and GPIO. Another strategy is using a thiserror and #[from] to flatten the Error. There is some potential information loss, but worth looking into and making the error type a nicer to work with. There are also some functions like power_off that ignore the GPIO warnings using let _ = pin.set_high(); and it would be easier to support failing when the Error types don't clog up the type signature and need more clippy lint skips
  • refactor: make imu state tokens structs
    Not necessarily needed change, but generally, for typestate markers, struct Token is used, and not enum Token {}. While there are some small differences, going with the convention would be the ideal, or at the very least document why there is this difference.
  • refactor: i2c address as enum
    The I2C address for the IMU sensor is passed as just u8. The general convention leans more towards enums with options for switching.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions