1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//! Example of using the MS4525DO sensor with the async API and Embassy.
//!
//! This example demonstrates how to read pressure and temperature data
//! from the MS4525DO sensor using the async API with the Embassy executor.
//!
//! ## Hardware Requirements
//!
//! - MS4525DO sensor connected via I2C
//! - I2C bus configured at appropriate speed (typically 100-400 kHz)
//! - Sensor connected to default address 0x28
//!
//! ## Usage
//!
//! This is a no_std example designed for Embassy async runtime.
//! You'll need to provide:
//! - An I2C peripheral that implements `embedded_hal_async::i2c::I2c`
//! - Embassy runtime configured for your platform
//! Note: This is a template example that needs to be adapted for your specific platform.
//! The code below is commented out as it requires platform-specific implementations.
//! Uncomment and modify according to your hardware setup.
// Uncomment for no_std embedded platforms:
// #![no_std]
// #![no_main]
// Note: You'll need to adjust these imports based on your platform
// This example shows the general structure for ESP32 with Embassy
// use ms4525do::async_api::Ms4525do;
// use ms4525do::calculate_airspeed;
// use embassy_executor::Spawner;
// use embassy_time::{Duration, Timer};
// use embassy_sync::blocking_mutex::raw::NoopRawMutex;
// use embassy_sync::channel::Channel;
// Platform-specific imports (example for ESP32)
// use esp_hal::{
// gpio,
// i2c::master::I2c,
// prelude::*,
// timer::timg::TimerGroup,
// };
// use esp_backtrace as _;
/// Static channel for sharing sensor data between tasks
// static SENSOR_DATA_CHANNEL: Channel<NoopRawMutex, (f32, f32, f32), 8> = Channel::new();
/// Embassy task to periodically read MS4525DO sensor data.
///
/// Reads pressure and temperature from the sensor at ~50 Hz,
/// calculates airspeed, and sends the results over a channel.
// #[embassy_executor::task]
// async fn read_airspeed_task(
// mut sensor: Ms4525do<I2c<'static, esp_hal::peripherals::I2C0>>,
// ) {
// loop {
// match sensor.read_data().await {
// Ok((pressure_pa, temp_c)) => {
// // Calculate airspeed from pressure and temperature
// let airspeed_ms = calculate_airspeed(pressure_pa, temp_c);
//
// // Log data using defmt (if enabled)
// #[cfg(feature = "defmt")]
// defmt::info!(
// "Airspeed: {:.2} m/s, Pressure: {:.2} Pa, Temp: {:.2} °C",
// airspeed_ms, pressure_pa, temp_c
// );
//
// // Send data to channel for other tasks to consume
// SENSOR_DATA_CHANNEL.send((pressure_pa, temp_c, airspeed_ms)).await;
// }
// Err(e) => {
// #[cfg(feature = "defmt")]
// defmt::warn!("Sensor read error: {:?}", e);
//
// // Wait before retrying on error
// Timer::after(Duration::from_millis(100)).await;
// }
// }
//
// // Read at ~50 Hz (adjust based on your requirements)
// Timer::after(Duration::from_millis(20)).await;
// }
// }
/// Embassy task to consume sensor data.
///
/// This is an example of how another task can receive and process
/// the sensor data from the channel.
///
// #[embassy_executor::task]
// async fn process_data_task() {
// loop {
// let (pressure, temp, airspeed) = SENSOR_DATA_CHANNEL.receive().await;
//
// // Process the data (example: could send over UART, store, etc.)
// #[cfg(feature = "defmt")]
// defmt::debug!("Processing: {:.2} m/s", airspeed);
//
// // Your processing logic here...
// }
// }
// Main entry point (adjust for your platform)
// #[esp_hal::main]
// async fn main(spawner: Spawner) {
// // Initialize platform peripherals
// let peripherals = esp_hal::init(esp_hal::Config::default());
//
// // Initialize Embassy timer
// let timg0 = TimerGroup::new(peripherals.TIMG0);
// esp_hal_embassy::init(timg0.timer0);
//
// // Configure I2C
// let i2c = I2c::new(
// peripherals.I2C0,
// io.pins.gpio21, // SDA
// io.pins.gpio22, // SCL
// 100.kHz(),
// );
//
// // Create sensor instance
// let sensor = Ms4525do::new(i2c);
//
// // Spawn async tasks
// spawner.spawn(read_airspeed_task(sensor)).ok();
// spawner.spawn(process_data_task()).ok();
// }
// Dummy main for example compilation