Skip to content

Commit

Permalink
update to embedded-* v1.0.0, add bmp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sycrosity committed Jan 15, 2024
1 parent fd0ce6a commit d86f5f6
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 551 deletions.
521 changes: 65 additions & 456 deletions Cargo.lock

Large diffs are not rendered by default.

25 changes: 15 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,43 @@ esp-println = { version = "0.8.0", features = ["esp32", "log", "uart"] }

esp-alloc = { version = "0.3.0", optional = true }

esp-wifi = { version = "0.2.0", features = ["esp32", "wifi", "embassy-net", "wifi-logs"], optional = true }
# esp-wifi = { version = "0.2.0", features = ["esp32", "wifi", "embassy-net", "wifi-logs"], optional = true }
# smoltcp = { version = "0.11.0", default-features = false, features = ["proto-igmp", "proto-ipv4", "socket-tcp", "socket-icmp", "socket-udp", "medium-ethernet", "proto-dhcpv4", "socket-raw", "socket-dhcpv4"] }

log = "0.4.20"
embedded-error-chain = "1.0.0"
static_cell = "2"

shared-bus = { version = "0.3.1", features = ["xtensa"] }
shared-bus = { version = "0.4.0", features = ["xtensa"] }
embedded-graphics = { version = "0.8.1", features = ["nalgebra_support"] }
nalgebra = { version = "0.32.3", default-features = false }

embedded-svc = { version = "0.26.4", default-features = false, features = ["log"] }
embedded-io = "0.6.1"
embedded-hal = "1.0.0-rc.2"
embedded-hal = "^1.0"
# embedded-hal-async = { version = "1.0.0" }

heapless = { version = "0.8.0", default-features = false }



embassy-executor = { version = "0.4.0", features = ["nightly", "log"] }
embassy-time = { version = "0.2.0", features = ["log"] }
embassy-executor = { version = "0.5.0", features = ["nightly", "log"] }
embassy-time = { version = "0.3.0", features = ["log"] }
embassy-sync = { version = "0.5.0", features = ["log"] }
embassy-net = { version = "0.3.0", features = ["log", "proto-ipv4", "dns", "tcp", "medium-ethernet"], optional = true }
embassy-net = { version = "0.4.0", features = ["log", "proto-ipv4", "dns", "tcp", "medium-ethernet"], optional = true }

mpu6050 = { version = "0.1.6", git = "https://github.com/Sycrosity/mpu6050" }
ssd1306 = "0.8.4"
ssd1306 = { version = "0.8.4", git = "https://github.com/Sycrosity/ssd1306" }
#for the bmp280 senosr
bme280 = { version = "0.4.4", features = ["async"], git = "https://github.com/Sycrosity/bme280-rs" }
bme280 = { version = "0.4.4", features = [], git = "https://github.com/Sycrosity/bme280-rs" }

[features]
alloc = ["dep:esp-alloc"]
net = [
"dep:esp-wifi",
# "dep:esp-wifi",
"dep:embassy-net"
]
# log = ["dep:log",""]

[patch.crates-io]
esp32-hal = { git = "https://github.com/Sycrosity/esp-hal" }
shared-bus = { version = "0.4.0", git = "https://github.com/Sycrosity/shared-bus" }
131 changes: 131 additions & 0 deletions src/bme280.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use bme280::Measurements;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;

use crate::prelude::*;

pub type BmeSignal = Signal<CriticalSectionRawMutex, BmeData>;
pub static BME_SIGNAL: BmeSignal = Signal::new();
// pub type BmeData = bme280::Measurements<hal::i2c::Error>;

#[derive(Clone, Copy, ErrorCategory)]
#[error_category(links(BMEError))]
#[repr(u8)]
pub enum BMEError {
DataErr,
InterfaceError,
InitialisationError,
DelayError,
TimeoutError,
Ack,
}

impl From<bme280::Error<hal::i2c::Error>> for BMEError {
fn from(value: bme280::Error<hal::i2c::Error>) -> Self {
match value {
bme280::Error::CompensationFailed => Self::DataErr,
bme280::Error::Bus(e) => match e {
hal::i2c::Error::ExceedingFifo => todo!(),
hal::i2c::Error::AckCheckFailed => Self::Ack,
hal::i2c::Error::TimeOut => Self::TimeoutError,
hal::i2c::Error::ArbitrationLost => todo!(),
hal::i2c::Error::ExecIncomplete => todo!(),
hal::i2c::Error::CommandNrExceeded => todo!(),
},
// Self::InterfaceError,
bme280::Error::InvalidData => Self::DataErr,
bme280::Error::NoCalibrationData => Self::InitialisationError,
bme280::Error::UnsupportedChip => Self::InitialisationError,
bme280::Error::Delay => Self::DelayError,
}
}
}

type Result<T> = core::result::Result<T, BMEError>;

pub struct BME280 {
pub bme: bme280::i2c::BME280<SharedI2C>,
}

impl BME280 {
pub fn new(i2c: SharedI2C) -> Result<Self> {
println!("a");

let mut bme280 = Self {
bme: bme280::i2c::BME280::new_primary(i2c),
};

info!("g");

bme280.init()?;

info!("e");

// bme280.try_init().await?;

Ok(bme280)
}

pub async fn try_init(&mut self) -> Result<()> {
Backoff::new(|| self.init()).retry().await?;
Ok(())
}

pub fn init(&mut self) -> Result<()> {
let mut delay: Delay = Delay;

self.bme.init(&mut delay)?;
Ok(())
}

// pub fn init(&mut self) -> Result<()> {
// let mut delay: Delay = Delay;

// self.bme.init(&mut delay)?;
// Ok(())
// }
}

#[derive(Copy, Clone, Debug, Default)]
pub struct BmeData {
/// temperature in degrees celsius
pub temperature: f32,
/// pressure in pascals
pub pressure: f32,
/// percent relative humidity (`0` with BMP280)
pub humidity: f32,
}

impl<E: embedded_hal::i2c::Error> From<Measurements<E>> for BmeData {
fn from(value: Measurements<E>) -> Self {
Self {
temperature: value.temperature,
pressure: value.pressure,
humidity: value.humidity,
}
}
}

#[task]
pub async fn bme280_stream(
mut bme: BME280,
// control: &'static MpuSignal
) {
let mut delay: Delay = Delay;

bme.init().unwrap();

loop {
let bme_data: BmeData = bme
.bme
.measure(&mut delay)
.map(Into::<BmeData>::into)
.map_err(|e| error!("{e:?}"))
.unwrap_or_default();

info!("{bme_data:?}");

BME_SIGNAL.signal(bme_data);

Timer::after_millis(1000).await;
}
}
13 changes: 0 additions & 13 deletions src/bmp280.rs

This file was deleted.

36 changes: 7 additions & 29 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
mpu6050::{MpuSignal, MPU_SIGNAL},
prelude::*,
};
use crate::{mpu6050::MPU_SIGNAL, prelude::*};

use core::fmt::{self, Write};
use esp32_hal::{i2c::I2C, peripherals::I2C0};

use ssd1306::{
mode::{TerminalDisplaySize, TerminalMode, TerminalModeError},
prelude::*,
Expand Down Expand Up @@ -66,8 +63,6 @@ impl<SIZE: DisplaySize + TerminalDisplaySize> Display<SIZE> {

let mut display = Self { display };

// display.display.init()?;

display.init()?;

display.clear().print_error();
Expand All @@ -80,14 +75,6 @@ impl<SIZE: DisplaySize + TerminalDisplaySize> Display<SIZE> {
Ok(())
}

// #[deprecated]
// /// can't be retried!
// async fn try_init(&mut self) -> Result<()> {
// try_repeat(|| self.init(), DEFAULT_INTERVAL, DEFAULT_MAX_ELAPSED_TIME).await?;
// debug!("Initialised Display");
// Ok(())
// }

pub fn clear(&mut self) -> Result<()> {
self.display
.clear()
Expand All @@ -96,20 +83,11 @@ impl<SIZE: DisplaySize + TerminalDisplaySize> Display<SIZE> {
Ok(())
}

pub fn quick_clear(&mut self) -> Result<()> {

self.set_position(0, 0).map_err(|e| {
self.clear().unwrap();
e
})?;

self.write_str("").map_err(|e| {
self.clear().unwrap();
e
})?;

pub fn reset_pos(&mut self) -> Result<()> {
self.set_position(0, 0)?;
Ok(())
}

pub fn write_str(&mut self, s: &str) -> Result<()> {
self.display.write_str(s).map_err(DisplayError::from)?;
Ok(())
Expand All @@ -135,7 +113,7 @@ pub async fn screen_counter(mut display: Display<DisplaySize128x64>) {
loop {
Timer::after_millis(1).await;

display.quick_clear().print_warn();
display.reset_pos().print_warn();
display.write_fmt(format_args!("{}", counter)).print_warn();

counter = match counter.checked_add(1) {
Expand Down Expand Up @@ -182,6 +160,6 @@ pub async fn display_numerical_data(
.unwrap();
};

display.quick_clear().print_warn();
display.reset_pos().print_warn();
}
}
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![allow(clippy::unused_unit)]

pub mod blink;
pub mod bmp280;
pub mod bme280;
pub mod display;
pub mod errors;
pub mod mpu6050;
Expand Down Expand Up @@ -55,13 +55,15 @@ pub mod prelude {
pub use embassy_executor::task;
pub use esp_println::println;

pub use embassy_sync::signal::Signal;

pub use hal::{
embassy,
gpio::{AnyPin, Output, PushPull},
prelude::*,
};

pub use embassy_time::{Duration, Instant, Ticker, Timer};
pub use embassy_time::{Delay, Duration, Instant, Ticker, Timer};

pub use log::{debug, error, info, log, trace, warn};
}
11 changes: 6 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use hal::{

use embassy_executor::Spawner;
use esp_println::println;

// use esp_wifi::{initialize, EspWifiInitFor};
use mpu6050::Mpu6050;

#[main]
async fn main(spawner: Spawner) -> ! {
Expand All @@ -45,7 +44,8 @@ async fn main(spawner: Spawner) -> ! {
info!("Logger is setup");
println!("Hello world!");

// let _wifi_init = initialize(
// #[cfg(feature = "net")]
// let _wifi_init = esp32_wifi::initialize(
// EspWifiInitFor::Wifi,
// timer,
// Rng::new(peripherals.RNG),
Expand All @@ -68,15 +68,16 @@ async fn main(spawner: Spawner) -> ! {
.await
.unwrap();

let mpu = mpu6050::Mpu6050::new(shared_i2c.acquire_i2c());
let mpu = Mpu6050::new(shared_i2c.acquire_i2c());

// let mpu_data_signal = SIGNAL;
// let bme = BME280::new(shared_i2c.acquire_i2c()).map_err(|e| { error!("{e:?}") }).unwrap();

spawner.spawn(blink(led.degrade())).unwrap();
// spawner.spawn(screen_counter(display)).unwrap();
spawner.spawn(display_numerical_data(display)).unwrap();

spawner.spawn(mpu6050_stream(mpu)).unwrap();
// spawner.spawn(bme280_stream(bme)).unwrap();

let mut ticker = Ticker::every(Duration::from_secs(10));

Expand Down
21 changes: 6 additions & 15 deletions src/mpu6050.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::prelude::*;

use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal};
use embassy_time::Delay;
use hal::{i2c::I2C, peripherals::I2C0};

use mpu6050::*;
use nalgebra::{Vector2, Vector3};

Expand Down Expand Up @@ -42,7 +42,7 @@ pub async fn get_sensor_data(mut mpu: Mpu6050<SharedI2C>) {
info!("temp: {:?}c", temp);

// get gyro data, scaled with sensitivity
let gyro: Vector3<f32> = mpu.get_gyro_deg().unwrap();
let gyro: Vector3<f32> = mpu.get_gyro().unwrap();
info!("gyro: {:?}", gyro);

// get accelerometer data, scaled with sensitivity
Expand All @@ -62,21 +62,12 @@ pub async fn mpu6050_stream(

mpu.init(&mut delay).unwrap();

// mpu.

loop {
// let mpu_data = MpuData {
// roll_pitch: mpu.get_acc_angles(),
// temp: mpu.get_temp(),
// gyro: mpu.get_gyro_deg(),
// acc: mpu.get_acc().chain_err(CansatError::I2C)
// };

let mpu_data = MpuData {
roll_pitch: mpu.get_acc_angles().unwrap(),
temp: mpu.get_temp().unwrap(),
gyro: mpu.get_gyro_deg().unwrap(),
acc: mpu.get_acc().unwrap(),
roll_pitch: mpu.get_acc_angles().print_warn(),
temp: mpu.get_temp().print_warn(),
gyro: mpu.get_gyro().print_warn().map(|x| x.to_degrees()),
acc: mpu.get_acc().print_warn(),
};

trace!("{mpu_data:?}");
Expand Down
Loading

0 comments on commit d86f5f6

Please sign in to comment.