Skip to content

Commit

Permalink
Add pressure transducer abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
samderanova committed Apr 28, 2024
1 parent 02c6c1f commit 0f4ae82
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
19 changes: 3 additions & 16 deletions pod-operation/src/components/ina219.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,9 @@ use ina219::INA219;
use rppal::i2c::I2c;
use tracing::debug;

pub async fn read_current() {
let device = I2c::new().unwrap();

// Use the default INA219 address, located at 0x40. This changes depending
// on whether we've set A0 and A1 to high.
let mut ina = INA219::new(device, 0x40);
debug!("Initialized I2C and INA219");

// In the Adafruit INA219 Python Driver, the calibration value is set to
// 4096 (0x1000) and Current_LSB is set to 0.1 by default.
ina.calibrate(0x1000).unwrap();
pub fn read_current(ina: &mut INA219<I2c>) -> f32 {
ina.calibrate(0xffff).unwrap();
debug!("Calibrating INA219");

loop {
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
let current = ina.current().unwrap();
println!("current: {:?}", current);
}
return ina.current().unwrap() as f32 / 160.0;
}
1 change: 1 addition & 0 deletions pod-operation/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod ina219;
pub mod pressure_transducer;
pub mod signal_light;
21 changes: 21 additions & 0 deletions pod-operation/src/components/pressure_transducer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::components::ina219::read_current;
use ina219::INA219;
use rppal::i2c::I2c;
use tracing::debug;

pub struct PressureTransducer {
ina: INA219<I2c>,
}

impl PressureTransducer {
pub fn new(ina219_addr: u8) -> Self {
let device = I2c::new().unwrap();
let ina219 = INA219::new(device, ina219_addr);
debug!("Initialized I2C and INA219");
PressureTransducer { ina: ina219 }
}

pub fn read(&mut self) -> f32 {
read_current(&mut self.ina)
}
}
5 changes: 5 additions & 0 deletions pod-operation/src/demo.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use tracing::info;

use crate::components::pressure_transducer::PressureTransducer;
use crate::components::signal_light::SignalLight;

pub async fn blink(mut signal_light: SignalLight) {
let mut i = 0;

let mut pt = PressureTransducer::new(0x40);

info!("Starting blink demo.");
loop {
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
Expand All @@ -14,6 +17,8 @@ pub async fn blink(mut signal_light: SignalLight) {
signal_light.disable();
}

println!("Current reading: {:?}", pt.read());

i += 1;
}
}
3 changes: 0 additions & 3 deletions pod-operation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod components;
mod demo;
mod handlers;

use crate::components::ina219::read_current;
use crate::components::signal_light::SignalLight;

#[tokio::main]
Expand All @@ -24,8 +23,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let signal_light = SignalLight::new();
tokio::spawn(demo::blink(signal_light));

tokio::spawn(read_current());

let app = axum::Router::new().layer(layer);

info!("Starting server on port 5000");
Expand Down

0 comments on commit 0f4ae82

Please sign in to comment.