|
|
@@ -1,6 +1,7 @@
|
|
1
|
1
|
use std::convert::TryInto;
|
|
2
|
2
|
use std::thread;
|
|
3
|
3
|
use std::time::{Duration, Instant};
|
|
|
4
|
+use std::sync::mpsc;
|
|
4
|
5
|
|
|
5
|
6
|
use embedded_nrf24l01::{Configuration, CrcMode, DataRate, NRF24L01};
|
|
6
|
7
|
use linux_embedded_hal::spidev::{SpiModeFlags, Spidev, SpidevOptions};
|
|
|
@@ -10,13 +11,14 @@ use log::{error, info};
|
|
10
|
11
|
|
|
11
|
12
|
use crate::spi::EmbeddedHalSpidev;
|
|
12
|
13
|
use crate::Error;
|
|
|
14
|
+use crate::{SensorUpdate, SensorData};
|
|
13
|
15
|
|
|
14
|
|
-pub fn start() {
|
|
|
16
|
+pub fn start(updates: mpsc::Sender<SensorUpdate>) {
|
|
15
|
17
|
// TODO: Channels for sensor readings.
|
|
16
|
18
|
thread::spawn(move || {
|
|
17
|
19
|
// Whenever a error occurs, reinitialize the radio module shortly later.
|
|
18
|
20
|
loop {
|
|
19
|
|
- match radio_thread() {
|
|
|
21
|
+ match radio_thread(&updates) {
|
|
20
|
22
|
Ok(()) => {}
|
|
21
|
23
|
Err(e) => {
|
|
22
|
24
|
error!("Radio error: {:?}", e);
|
|
|
@@ -27,7 +29,7 @@ pub fn start() {
|
|
27
|
29
|
});
|
|
28
|
30
|
}
|
|
29
|
31
|
|
|
30
|
|
-fn radio_thread() -> Result<(), Error> {
|
|
|
32
|
+fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
|
|
31
|
33
|
info!("Initializing radio...");
|
|
32
|
34
|
|
|
33
|
35
|
// The NRF module is connected as follows:
|
|
|
@@ -90,13 +92,20 @@ fn radio_thread() -> Result<(), Error> {
|
|
90
|
92
|
loop {
|
|
91
|
93
|
if let Some(pipe) = nrf24.can_read().unwrap() {
|
|
92
|
94
|
let payload = nrf24.read().unwrap();
|
|
93
|
|
- info!("packet received on pipe {}: {:?}", pipe, payload.as_ref());
|
|
|
95
|
+ info!("packet received on pipe {}: {:?}, {}", pipe, payload.as_ref(), payload.len());
|
|
94
|
96
|
let pressure = u32::from_le_bytes(payload.as_ref()[0..4].try_into().unwrap());
|
|
95
|
97
|
let temperature = u32::from_le_bytes(payload.as_ref()[4..8].try_into().unwrap());
|
|
96
|
98
|
let humidity = u32::from_le_bytes(payload.as_ref()[8..12].try_into().unwrap());
|
|
97
|
|
- info!("pressure: {}", pressure as f32 / 100.0);
|
|
98
|
|
- info!("temperature: {}", temperature as f32 / 100.0);
|
|
99
|
|
- info!("humidity: {}", humidity as f32 / 1024.0);
|
|
|
99
|
+ let pressure = pressure as f32 / 100.0;
|
|
|
100
|
+ let temperature = temperature as f32 / 100.0;
|
|
|
101
|
+ let humidity = humidity as f32 / 1024.0;
|
|
|
102
|
+ info!("pressure: {}", pressure);
|
|
|
103
|
+ info!("temperature: {}", temperature);
|
|
|
104
|
+ info!("humidity: {}", humidity);
|
|
|
105
|
+ updates.send(SensorUpdate{
|
|
|
106
|
+ location: 0,
|
|
|
107
|
+ data: vec![SensorData::Temperature(temperature), SensorData::Pressure(pressure), SensorData::Humidity(humidity)],
|
|
|
108
|
+ }).unwrap();
|
|
100
|
109
|
let end = Instant::now();
|
|
101
|
110
|
let elapsed = end.duration_since(start);
|
|
102
|
111
|
info!("Debug: {:?}", elapsed);
|