Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract GPIO pin number constants to common enum #85

Merged
merged 2 commits into from
May 30, 2024
Merged

Conversation

taesungh
Copy link
Member

@taesungh taesungh commented May 28, 2024

Resolves #84.

Changes

  • Move all constants for GPIO pin numbers used by components to a single module to avoid having conflicting numbers
    • GpioPins enum will enforce discriminant
  • Use num_enum::IntoPrimitive to convert from enum value to u8 using .into()

@taesungh taesungh requested a review from a team May 28, 2024 02:44
pod-operation/src/utils/gpio.rs Outdated Show resolved Hide resolved
pod-operation/src/utils/gpio.rs Outdated Show resolved Hide resolved
pod-operation/src/utils/mod.rs Show resolved Hide resolved
@samderanova
Copy link
Contributor

Thanks for working on this! Maybe there should also be some sort of mechanism that enforces only one use of each pin at a time.

I suppose we can also ignore the error in the checks regarding the unused enum field.

Copy link
Member Author

@taesungh taesungh left a comment

Choose a reason for hiding this comment

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

A regular enum would enforce enum fields to be discriminant whereby having multiple of the same value would be an error, but using a regular enum would require casting each pin value with as u8 when passed into Gpio::get which seems inconvenient (and more generally risks lossy conversion), but perhaps this is preferred to enforce discriminants.

pod-operation/src/utils/gpio.rs Outdated Show resolved Hide resolved
pod-operation/src/utils/gpio.rs Outdated Show resolved Hide resolved
@ryescholin ryescholin self-requested a review May 29, 2024 07:43
Copy link
Member

@ryescholin ryescholin left a comment

Choose a reason for hiding this comment

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

Looks great!

@taesungh
Copy link
Member Author

Actually, I found a suitable compromise for having the enum, will update soon.

@taesungh taesungh marked this pull request as draft May 29, 2024 17:41
@taesungh taesungh marked this pull request as draft May 29, 2024 17:41
@taesungh taesungh marked this pull request as draft May 29, 2024 17:41
@taesungh taesungh marked this pull request as ready for review May 29, 2024 20:55
taesungh added 2 commits May 29, 2024 19:55
- Move all constants for GPIO pin numbers used by components to a single
  module to avoid having conflicting numbers
- Use a struct instead of enum to avoid needing to cast `as u8`
- Install `num_enum` in `pod-operation`
- Implements `From<Enum>` for underlying u8 repr so we can use `.into()`
@taesungh taesungh changed the title Extract GPIO pin number constants to common struct Extract GPIO pin number constants to common enum May 30, 2024
@taesungh taesungh merged commit d221109 into main May 30, 2024
2 checks passed
@taesungh taesungh deleted the refactor/gpio branch June 1, 2024 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GPIO pins for wheel encoder are needed for I2C
3 participants