Nareshkumar Rao
7 months ago
4 changed files with 50 additions and 30 deletions
@ -1,24 +1,35 @@ |
|||||
use std::{error::Error, thread, time::Duration}; |
|
||||
|
use std::{error::Error, time::Duration}; |
||||
|
|
||||
use dht11::Dht11Sensor; |
use dht11::Dht11Sensor; |
||||
|
use linux_realtime::ThreadAttributes; |
||||
|
|
||||
mod dht11; |
mod dht11; |
||||
|
|
||||
fn main() -> Result<(), Box<dyn Error>> { |
fn main() -> Result<(), Box<dyn Error>> { |
||||
unsafe { |
|
||||
libc::setpriority(libc::PRIO_PROCESS, 0, -20); |
|
||||
} |
|
||||
let mut sensor = Dht11Sensor::new(2)?; |
let mut sensor = Dht11Sensor::new(2)?; |
||||
loop { |
|
||||
match sensor.read() { |
|
||||
Ok(data) => println!( |
|
||||
"Humidity: {}; Temperature: {}", |
|
||||
data.humidity, data.temperature |
|
||||
), |
|
||||
Err(msg) => println!("Error: {}", msg), |
|
||||
} |
|
||||
thread::sleep(Duration::from_millis(2000)); |
|
||||
} |
|
||||
|
let t = linux_realtime::JoinHandle::spawn( |
||||
|
ThreadAttributes { |
||||
|
stack_size: libc::PTHREAD_STACK_MIN, |
||||
|
scheduler_policy: linux_realtime::SchedulerPolicy::Fifo, |
||||
|
thread_priority: 99, |
||||
|
}, |
||||
|
move || -> Result<(), Box<dyn Error>> { |
||||
|
loop { |
||||
|
let mut rtclock = linux_realtime::Clock::new()?; |
||||
|
match sensor.read() { |
||||
|
Ok(data) => println!( |
||||
|
"Humidity: {}; Temperature: {}", |
||||
|
data.humidity, data.temperature |
||||
|
), |
||||
|
Err(msg) => println!("Error: {}", msg), |
||||
|
} |
||||
|
for n in 1..10 { |
||||
|
rtclock.sleep(Duration::from_millis(500))?; |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
)?; |
||||
|
_ = t.join(); |
||||
|
|
||||
Ok(()) |
Ok(()) |
||||
} |
} |
||||
|
Loading…
Reference in new issue