From 5f83b73011004267918d50e4058c6fedb742beb2 Mon Sep 17 00:00:00 2001 From: linuswck Date: Mon, 19 Feb 2024 15:08:00 +0800 Subject: [PATCH] cmd: Add active report mode cmd --- src/laser_diode/laser_diode.rs | 26 +++++++++++++++ src/laser_diode/ld_ctrl.rs | 3 +- src/main.rs | 22 +++++++++++- src/net/cmd_handler.rs | 61 ++++++++++++++++++++++++++-------- src/thermostat/thermostat.rs | 2 +- 5 files changed, 98 insertions(+), 16 deletions(-) diff --git a/src/laser_diode/laser_diode.rs b/src/laser_diode/laser_diode.rs index fadda12..1758ce4 100644 --- a/src/laser_diode/laser_diode.rs +++ b/src/laser_diode/laser_diode.rs @@ -6,6 +6,7 @@ use crate::laser_diode::ld_pwr_exc_protector::{LdPwrExcProtector, self}; use crate::laser_diode::pd_responsitivity; use core::marker::PhantomData; use crate::device::sys_timer::sleep; +use serde::{Deserialize, Serialize}; use uom::si::{ electric_current::milliampere, @@ -67,6 +68,16 @@ impl Default for Settings { } } +#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)] +pub struct StatusReport { + pwr_engaged: bool, + pwr_excursion: bool, + ld_i_set: ElectricCurrent, + pd_i: ElectricCurrent, + pd_pwr: Power, + term_status: Impedance, +} + pub struct LdDrive{ ctrl: LdCtrl, settings: Settings, @@ -120,6 +131,10 @@ impl LdDrive{ LdPwrExcProtector::get_status().v * Settings::PD_MON_TRANSCONDUCTANCE } + pub fn get_pd_pwr(&mut self) -> Power { + self.settings.pd_responsitivity.get_ld_pwr_from_ld_i(LdPwrExcProtector::get_status().v * Settings::PD_MON_TRANSCONDUCTANCE) + } + // Ramping up or down laser diode current according to preset current step size and time step. pub fn ld_set_i(&mut self, i: ElectricCurrent) -> ElectricCurrent { let mut prev_i_set = self.settings.ld_drive_current; @@ -178,4 +193,15 @@ impl LdDrive{ pub fn get_term_status(&mut self) -> Impedance { self.ctrl.get_lf_mod_in_impedance() } + + pub fn get_status_report(&mut self) -> StatusReport { + StatusReport { + pwr_engaged: LdPwrExcProtector::get_status().pwr_engaged, + pwr_excursion: LdPwrExcProtector::get_status().pwr_excursion, + ld_i_set: self.settings.ld_drive_current, + pd_i: self.get_pd_i(), + pd_pwr: self.get_pd_pwr(), + term_status: self.get_term_status(), + } + } } diff --git a/src/laser_diode/ld_ctrl.rs b/src/laser_diode/ld_ctrl.rs index 951e316..10ab455 100644 --- a/src/laser_diode/ld_ctrl.rs +++ b/src/laser_diode/ld_ctrl.rs @@ -1,3 +1,4 @@ +use serde::{Deserialize, Serialize}; use stm32f4xx_hal::{ gpio::{gpioa::*, gpiob::*, gpiod::*, Alternate, Input, Output, PushPull}, hal::{blocking::spi::Transfer, digital::{v2::OutputPin, v2::InputPin}}, @@ -13,7 +14,7 @@ use uom::si::{ use crate::laser_diode::max5719::{self, Dac}; use crate::laser_diode::laser_diode::TransimpedanceUnit; -#[derive(Debug)] +#[derive(Deserialize, Serialize, Debug, Clone, Copy)] pub enum Impedance { Is50Ohm, Not50Ohm, diff --git a/src/main.rs b/src/main.rs index 0b84742..30f9440 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,10 @@ use panic_halt as _; static mut ETH_DATA_BUFFER: [u8; 1024] = [0; 1024]; +pub struct DeviceSettings{ + report_readings: bool, +} + #[cfg(not(test))] #[entry] fn main() -> ! { @@ -80,6 +84,10 @@ fn main() -> ! { let mut should_reset = false; + let mut device_settings = DeviceSettings { + report_readings: false, + }; + loop { wd.feed(); @@ -98,6 +106,15 @@ fn main() -> ! { info!("power_excursion: {:?}", laser.pd_mon_status().pwr_excursion); info!("Termination Status: {:?}", laser.get_term_status()); + + if net::net::eth_is_socket_active() { + if device_settings.report_readings { + unsafe { + net::cmd_handler::send_ld_readings(&mut ETH_DATA_BUFFER, &mut laser); + net::cmd_handler::send_tec_readings(&mut ETH_DATA_BUFFER, &mut thermostat); + } + } + } } if net::net::eth_is_socket_active() { @@ -114,10 +131,13 @@ fn main() -> ! { }); let bytes = net::net::eth_recv(&mut ETH_DATA_BUFFER); debug!("Number of bytes recv: {:?}", bytes); - (laser, thermostat, should_reset) = net::cmd_handler::execute_cmd(&mut ETH_DATA_BUFFER, bytes, laser, thermostat); + (laser, thermostat, should_reset, device_settings) = net::cmd_handler::execute_cmd(&mut ETH_DATA_BUFFER, bytes, laser, thermostat, device_settings); } } } + else { + device_settings.report_readings = false; + } } else { // Should reset, close all TCP sockets. let mut any_socket_alive = false; diff --git a/src/net/cmd_handler.rs b/src/net/cmd_handler.rs index 54f20f6..7ea19d9 100644 --- a/src/net/cmd_handler.rs +++ b/src/net/cmd_handler.rs @@ -7,17 +7,24 @@ use uom::si::{ electrical_resistance::{ElectricalResistance, ohm}, f64::ThermodynamicTemperature, thermodynamic_temperature::degree_celsius }; -use crate::{laser_diode::laser_diode::LdDrive, net::net, thermostat::thermostat::StatusReport}; +use crate::{laser_diode::laser_diode::{ + LdDrive, StatusReport as LdStatusReport + }, + net::net, + thermostat::thermostat::StatusReport as TecStatusReport +}; use crate::thermostat::thermostat::Thermostat; use crate::thermostat::pid_state::PidSettings::*; use crate::device::dfu; use log::info; +use crate::DeviceSettings; #[derive(Deserialize, Serialize, Copy, Clone, Default, Debug)] enum DeviceCmd { #[default] Reserved, - Dfu + ReportStatus, + Dfu, } #[derive(Deserialize, Serialize, Copy, Clone, Default, Debug)] @@ -79,6 +86,7 @@ pub struct CmdJsonObj{ laser_diode_cmd: Option, thermostat_cmd: Option, device_cmd: Option, + data_bool: Option, data_f32: Option, data_f64: Option, } @@ -87,12 +95,33 @@ pub struct Cmd { json: CmdJsonObj } -#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)] -pub struct StatusReportStruct { - json: StatusReport +#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)] +pub struct TecStatusReportStruct { + json: TecStatusReport } -pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mut tec: Thermostat)->(LdDrive, Thermostat, bool){ +#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)] +pub struct LaserStatusReportStruct { + json: LdStatusReport +} + +pub fn send_ld_readings(buffer: &mut [u8], laser: &mut LdDrive){ + let status_report = LaserStatusReportStruct { + json: laser.get_status_report() + }; + let num_bytes = status_report.get_json("/json", buffer).unwrap(); + net::eth_send(buffer, num_bytes); +} + +pub fn send_tec_readings(buffer: &mut [u8], tec: &mut Thermostat){ + let status_report = TecStatusReportStruct { + json: tec.get_status_report() + }; + let num_bytes = status_report.get_json("/json", buffer).unwrap(); + net::eth_send(buffer, num_bytes); +} + +pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mut tec: Thermostat, mut device_settings: DeviceSettings)->(LdDrive, Thermostat, bool, DeviceSettings){ let mut should_reset = false; let mut cmd = Cmd { @@ -111,6 +140,16 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } should_reset = true; } + Some(DeviceCmd::ReportStatus) => { + match cmd.json.data_bool{ + Some(val) => { + device_settings.report_readings = val; + } + None => { + info!("Wrong Data type is received") + } + } + } None => { /* Do Nothing */} _ => { info!("Unimplemented Command") @@ -176,7 +215,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu info!("Not supported Yet") } Some(LdCmdEnum::GetLdStatus) => { - info!("Not supported Yet") + send_ld_readings(buffer, &mut laser); } Some(LdCmdEnum::GetAlramStatus) => { info!("Not supported Yet") @@ -334,11 +373,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::GetTecStatus) => { - let status_report = StatusReportStruct { - json: tec.get_status_report() - }; - let num_bytes = status_report.get_json("/json", buffer).unwrap(); - net::eth_send(buffer, num_bytes); + send_tec_readings(buffer, &mut tec); } Some(ThermostatCmdEnum::GetPidStatus) => { info!("Not supported Yet") @@ -356,5 +391,5 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu info!("Invalid Command: {:?}", err); } } - (laser, tec, should_reset) + (laser, tec, should_reset, device_settings) } diff --git a/src/thermostat/thermostat.rs b/src/thermostat/thermostat.rs index e856857..9b8b76b 100644 --- a/src/thermostat/thermostat.rs +++ b/src/thermostat/thermostat.rs @@ -338,7 +338,7 @@ impl Thermostat{ } } -#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)] +#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)] pub struct StatusReport { ts: u32, pid_engaged: bool,