Skip to content

Commit

Permalink
Extract GPIO pin number constants to common enum (#85)
Browse files Browse the repository at this point in the history
- Move all constants for GPIO pin numbers used by components to a single
  module to avoid having conflicting numbers
- Install `num_enum` in `pod-operation`
- Use `num_enum::IntoPrimitive` to derive numeric conversion traits
  - Implements `From<Enum>` for underlying `u8` repr to use `.into()`
  • Loading branch information
taesungh authored May 30, 2024
1 parent 797dd93 commit d221109
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 17 deletions.
79 changes: 79 additions & 0 deletions pod-operation/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pod-operation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
enum-map = "2.7.3"
once_cell = "1.19.0"
num_enum = "0.7.2"

rppal = { version = "0.17.1", features = ["hal"] }
ina219 = "0.1.0"
Expand Down
11 changes: 7 additions & 4 deletions pod-operation/src/components/brakes.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use rppal::gpio::{Gpio, OutputPin};
use tracing::debug;

use rppal::gpio::{Gpio, OutputPin};
use crate::utils::GpioPins;

pub struct Brakes {
pin: OutputPin,
}

const PIN_BRAKES: u8 = 26;

impl Brakes {
pub fn new() -> Self {
Brakes {
pin: Gpio::new().unwrap().get(PIN_BRAKES).unwrap().into_output(),
pin: Gpio::new()
.unwrap()
.get(GpioPins::PNEUMATICS_RELAY.into())
.unwrap()
.into_output(),
}
}

Expand Down
7 changes: 3 additions & 4 deletions pod-operation/src/components/high_voltage_system.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use rppal::gpio::{Gpio, OutputPin};
use tracing::debug;

use rppal::gpio::{Gpio, OutputPin};
use crate::utils::GpioPins;

pub struct HighVoltageSystem {
pin: OutputPin,
}

const PIN_CONTACTOR_RELAY: u8 = 20;

impl HighVoltageSystem {
pub fn new() -> Self {
HighVoltageSystem {
pin: Gpio::new()
.unwrap()
.get(PIN_CONTACTOR_RELAY)
.get(GpioPins::CONTACTOR_RELAY.into())
.unwrap()
.into_output(),
}
Expand Down
7 changes: 3 additions & 4 deletions pod-operation/src/components/signal_light.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use rppal::gpio::{Gpio, OutputPin};
use tracing::debug;

use rppal::gpio::{Gpio, OutputPin};
use crate::utils::GpioPins;

pub struct SignalLight {
pin: OutputPin,
}

const PIN_SIGNAL_LIGHT: u8 = 21;

impl SignalLight {
pub fn new() -> Self {
SignalLight {
pin: Gpio::new()
.unwrap()
.get(PIN_SIGNAL_LIGHT)
.get(GpioPins::SIGNAL_LIGHT_RELAY.into())
.unwrap()
.into_output(),
}
Expand Down
16 changes: 11 additions & 5 deletions pod-operation/src/components/wheel_encoder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rppal::gpio::{Gpio, InputPin, Level};
use std::time::Instant;

const PIN_ENCODER_A: u8 = 1;
const PIN_ENCODER_B: u8 = 2;
use rppal::gpio::{Gpio, InputPin, Level};

use crate::utils::GpioPins;

pub struct WheelEncoder {
counter: f32,
Expand All @@ -20,8 +20,14 @@ impl WheelEncoder {
let gpio = Gpio::new().unwrap();
WheelEncoder {
counter: 0.0,
pin_a: gpio.get(PIN_ENCODER_A).unwrap().into_input(),
pin_b: gpio.get(PIN_ENCODER_B).unwrap().into_input(),
pin_a: gpio
.get(GpioPins::WHEEL_ENCODER_A.into())
.unwrap()
.into_input(),
pin_b: gpio
.get(GpioPins::WHEEL_ENCODER_B.into())
.unwrap()
.into_input(),
a_last_read: Level::High,
b_last_read: Level::Low,
last_distance: 0.0,
Expand Down
1 change: 1 addition & 0 deletions pod-operation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use tracing_subscriber::FmtSubscriber;
mod components;
mod demo;
mod state_machine;
mod utils;

use crate::components::brakes::Brakes;
use crate::components::gyro::Gyroscope;
Expand Down
12 changes: 12 additions & 0 deletions pod-operation/src/utils/gpio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[derive(num_enum::IntoPrimitive)]
#[repr(u8)]
#[allow(non_camel_case_types)]
pub enum GpioPins {
_RESERVED_I2C_SDA = 2,
_RESERVED_I2C_SCL = 3,
WHEEL_ENCODER_A = 23,
WHEEL_ENCODER_B = 24,
CONTACTOR_RELAY = 20,
SIGNAL_LIGHT_RELAY = 21,
PNEUMATICS_RELAY = 26,
}
2 changes: 2 additions & 0 deletions pod-operation/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod gpio;
pub use gpio::GpioPins;

0 comments on commit d221109

Please sign in to comment.