From e3caaf5421908bd83cadf289d2d383e3255b95f0 Mon Sep 17 00:00:00 2001
From: Stephen D <webmaster@scd31.com>
Date: Fri, 2 Jun 2023 17:37:31 -0300
Subject: [PATCH] radio temperature

---
 src/control.rs | 26 +++++++++++++++++++++++---
 src/radio.rs   | 19 +++++++++++++++++--
 2 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/src/control.rs b/src/control.rs
index ae899f3..906dd8e 100644
--- a/src/control.rs
+++ b/src/control.rs
@@ -1,7 +1,7 @@
 use std::{
     sync::mpsc::{self, Receiver, Sender, SyncSender},
     thread,
-    time::Duration,
+    time::{Duration, Instant},
 };
 
 use anyhow::Context;
@@ -15,7 +15,8 @@ use crate::{
     ssdv::ssdv_encode,
 };
 
-const IMAGE_PACKET_QUEUE_LENGTH: usize = 1024;
+const IMAGE_PACKET_QUEUE_LENGTH: usize = 8192;
+const TEMP_REFRESH_INTERVAL: Duration = Duration::from_secs(5);
 
 pub struct Controller {
     config: Config,
@@ -95,6 +96,7 @@ impl Controller {
         };
 
         let mut text_msg_id = 0;
+        let mut last_got_temp = Instant::now();
         loop {
             while let Ok(tm) = telem_rx.try_recv() {
                 let tm = tm.into_raw(&mut text_msg_id).into();
@@ -109,7 +111,25 @@ impl Controller {
                     eprintln!("Could not send packet: {}", e);
                 }
             } else {
-                thread::sleep(Duration::from_millis(5));
+                thread::sleep(Duration::from_millis(50));
+            }
+
+            if Instant::now() - last_got_temp > TEMP_REFRESH_INTERVAL {
+                last_got_temp = Instant::now();
+
+                let temp = match radio.get_temp() {
+                    Ok(x) => x,
+                    Err(e) => {
+                        eprintln!("Could not get radio temp: {}", e);
+                        continue;
+                    }
+                };
+
+                let packet = Packet::new_text_message(&format!("Temp: {}", temp));
+
+                if let Err(e) = radio.send_packet(&packet.into_raw(&mut text_msg_id).into()) {
+                    eprintln!("Could not send packet: {}", e);
+                }
             }
         }
     }
diff --git a/src/radio.rs b/src/radio.rs
index 20edb24..af28180 100644
--- a/src/radio.rs
+++ b/src/radio.rs
@@ -7,6 +7,10 @@ use std::{
 
 use crate::packet::FecPacket;
 
+const BUFFER_STATUS_CMD: u8 = 0x00;
+const SEND_PACKET_CMD: u8 = 0x01;
+const GET_TEMP_CMD: u8 = 0x02;
+
 // Used for talking to an RF4463 via a microcontroller
 // The microcontroller takes in packet data over UART and sends it to the RF4463
 pub struct UartRadio {
@@ -22,7 +26,7 @@ impl UartRadio {
     }
 
     pub fn send_packet(&mut self, packet: &FecPacket) -> anyhow::Result<()> {
-        self.uart.write_all(&[0x01])?;
+        self.uart.write_all(&[SEND_PACKET_CMD])?;
         self.uart.write_all(&packet.0)?;
 
         let mut buf = [0; 1];
@@ -33,7 +37,7 @@ impl UartRadio {
             sleep(Duration::from_millis(10));
 
             // poll to see if the buffer has emptied yet
-            self.uart.write_all(&[0x00])?;
+            self.uart.write_all(&[BUFFER_STATUS_CMD])?;
             self.uart.flush()?;
 
             self.uart.read_exact(&mut buf)?;
@@ -41,4 +45,15 @@ impl UartRadio {
 
         Ok(())
     }
+
+    pub fn get_temp(&mut self) -> anyhow::Result<f32> {
+        self.uart.write_all(&[GET_TEMP_CMD])?;
+
+        let mut buf = [0; 4];
+        self.uart.read_exact(&mut buf)?;
+
+        let temp = f32::from_le_bytes(buf);
+
+        Ok(temp)
+    }
 }
-- 
GitLab