From 951b033c19a9f07956f9defd7447f934e4a98f21 Mon Sep 17 00:00:00 2001 From: gferraro Date: Wed, 16 Oct 2024 16:25:25 +0200 Subject: [PATCH 1/4] always take shutdown status --- src/core1_task.rs | 90 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 26 deletions(-) diff --git a/src/core1_task.rs b/src/core1_task.rs index b6b4549..dca84fd 100644 --- a/src/core1_task.rs +++ b/src/core1_task.rs @@ -58,6 +58,11 @@ pub enum Core1Task { ReceiveFrameWithPendingFFC = 0xac, } +enum StatusRecording { + StartupStatus = 0, + ShutdownStatus = 1, +} + impl Into for Core1Task { fn into(self) -> u32 { self as u32 @@ -645,11 +650,22 @@ pub fn core_1_task( let mut made_startup_status_recording = (is_cptv && has_files_to_offload) || (is_cptv && did_offload_files); - let mut made_shutdown_status_recording = false; + //there is a small posibility that we miss shutdown status recordings if the flash was full and while + //we are offloading it goes outside of the recording window + //we could check last recording is a status to counter this + let mut made_shutdown_status_recording = !device_config.time_is_in_recording_window( + &synced_date_time.date_time_utc, + &Some(current_recording_window), + ); let mut making_status_recording = false; + let mut status_recording_pending = if !made_startup_status_recording { + Some(StatusRecording::StartupStatus) + } else { + None + }; + let mut high_power_recording = false; let mut last_rec_check = 0; - let mut lost_frames = 0; // Enable raw frame transfers to pi – if not already enabled. pi_spi.enable_pio_spi(); @@ -826,34 +842,34 @@ pub fn core_1_task( this_frame_motion_detection.got_new_trigger(); should_start_new_recording = !flash_storage.is_too_full_to_start_new_recordings() - && (motion_detection_triggered_this_frame || !made_startup_status_recording) + && motion_detection_triggered_this_frame && cptv_stream.is_none(); // wait until lepton stabilises before recording - if made_startup_status_recording - && !made_shutdown_status_recording - && !motion_detection_triggered_this_frame - && cptv_stream.is_none() - { + if made_startup_status_recording && !made_shutdown_status_recording { if dev_mode { if synced_date_time.date_time_utc + Duration::minutes(1) > startup_date_time_utc + Duration::minutes(4) { warn!("Make shutdown status recording"); - should_start_new_recording = true; - made_shutdown_status_recording = true; - making_status_recording = true; + status_recording_pending = Some(StatusRecording::ShutdownStatus); } } else { let (_, window_end) = ¤t_recording_window; if &(synced_date_time.date_time_utc + Duration::minutes(1)) > window_end { warn!("Make shutdown status recording"); - should_start_new_recording = true; - made_shutdown_status_recording = true; - making_status_recording = true; + status_recording_pending = Some(StatusRecording::ShutdownStatus); } } } + if status_recording_pending.is_some() + && !should_start_new_recording + && cptv_stream.is_none() + { + should_start_new_recording = true; + making_status_recording = true; + } + // TODO: Do we want to have a max recording length timeout, or just pause recording if a subject stays in the frame // but doesn't move for a while? Maybe if a subject is stationary for 1 minute, we pause, and only resume // recording if there is new movement, or it moves again? If the night ends in this way, we end the recording then. @@ -888,16 +904,21 @@ pub fn core_1_task( should_start_new_recording && is_inside_recording_window; if is_inside_recording_window { // Should we make a 2-second status recording at the beginning or end of the window? - if !made_startup_status_recording && !motion_detection_triggered_this_frame - { - warn!("Make startup status recording"); - made_startup_status_recording = true; - making_status_recording = true; - } else { - // We're making a shutdown recording. - } - } else if made_startup_status_recording { + // if !made_startup_status_recording && !motion_detection_triggered_this_frame + // { + // warn!("Make startup status recording"); + // made_startup_status_recording = true; + // making_status_recording = true; + // } else { + // // We're making a shutdown recording. + // } + } else if !making_status_recording { info!("Would start recording, but outside recording window"); + } else if made_startup_status_recording && !made_shutdown_status_recording { + should_start_new_recording = true; + //force shutdown status recording even outside of window + } else { + making_status_recording = false; } } else if !should_end_current_recording { if let Some(cptv_stream) = &mut cptv_stream { @@ -975,6 +996,17 @@ pub fn core_1_task( if making_status_recording { making_status_recording = false; + match status_recording_pending.unwrap() { + StatusRecording::StartupStatus => { + made_startup_status_recording = true; + //only make a shutdown if we made a startup + made_shutdown_status_recording = false; + } + StatusRecording::ShutdownStatus => { + made_shutdown_status_recording = true; + } + } + status_recording_pending = None; } } cptv_stream = None; @@ -1115,7 +1147,7 @@ pub fn core_1_task( info!("Got frame #{}", frame_num); } - let one_min_check_start = timer.get_counter(); + // let one_min_check_start = timer.get_counter(); // let expected_rtc_sync_time_us = 3500; let expected_rtc_sync_time_us = 4200; //using slower clock speed @@ -1192,7 +1224,10 @@ pub fn core_1_task( ); logged_flash_storage_nearly_full = true; } - if is_outside_recording_window || flash_storage_nearly_full { + if ((!device_config.use_low_power_mode() || made_shutdown_status_recording) + && is_outside_recording_window) + || flash_storage_nearly_full + { if flash_storage_nearly_full || (is_outside_recording_window && flash_storage.has_files_to_offload()) { @@ -1364,6 +1399,9 @@ pub fn core_1_task( &mut flash_storage, ); } + } else if is_outside_recording_window && !made_shutdown_status_recording { + making_status_recording = true; + //force shutdown recording outside of window } // Make sure timing is as close as possible to the non-sync case @@ -1404,7 +1442,7 @@ pub fn core_1_task( // about the same delay.delay_us(expected_rtc_sync_time_us as u32); } - let one_min_check_end = timer.get_counter(); + // let one_min_check_end = timer.get_counter(); // info!( // "Loop took {}µs, 1min check {}µs, frame transfer {}µs", From e6ed1d59cedc50bfaac6c1106f25d508c11157f9 Mon Sep 17 00:00:00 2001 From: gferraro Date: Wed, 16 Oct 2024 19:11:30 +0200 Subject: [PATCH 2/4] fix logic --- src/core1_task.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/core1_task.rs b/src/core1_task.rs index dca84fd..8214415 100644 --- a/src/core1_task.rs +++ b/src/core1_task.rs @@ -57,6 +57,7 @@ pub enum Core1Task { HighPowerMode = 0xeb, ReceiveFrameWithPendingFFC = 0xac, } +#[derive(Format)] enum StatusRecording { StartupStatus = 0, @@ -845,7 +846,10 @@ pub fn core_1_task( && motion_detection_triggered_this_frame && cptv_stream.is_none(); // wait until lepton stabilises before recording - if made_startup_status_recording && !made_shutdown_status_recording { + if made_startup_status_recording + && !made_shutdown_status_recording + && status_recording_pending.is_none() + { if dev_mode { if synced_date_time.date_time_utc + Duration::minutes(1) > startup_date_time_utc + Duration::minutes(4) @@ -920,6 +924,9 @@ pub fn core_1_task( } else { making_status_recording = false; } + if making_status_recording { + info!("Making status recording {}", status_recording_pending); + } } else if !should_end_current_recording { if let Some(cptv_stream) = &mut cptv_stream { if let Some(prev_telemetry) = &prev_frame_telemetry { From a2e7805e03b858782b67eb5d227839f0b154483f Mon Sep 17 00:00:00 2001 From: gferraro Date: Wed, 16 Oct 2024 19:32:28 +0200 Subject: [PATCH 3/4] get window before offloading --- src/core1_task.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/core1_task.rs b/src/core1_task.rs index 8214415..6ea7e70 100644 --- a/src/core1_task.rs +++ b/src/core1_task.rs @@ -580,6 +580,16 @@ pub fn core_1_task( }; let is_cptv = flash_storage.has_cptv_files(false); + let mut made_startup_status_recording = is_cptv && has_files_to_offload; + + let current_recording_window = + device_config.next_or_current_recording_window(&synced_date_time.date_time_utc); + + let mut made_shutdown_status_recording = !device_config.time_is_in_recording_window( + &synced_date_time.date_time_utc, + &Some(current_recording_window), + ); + info!( "Has cptv files? {} has files? {}", is_cptv, @@ -640,24 +650,13 @@ pub fn core_1_task( ); let mut motion_detection: Option = None; - let current_recording_window = - device_config.next_or_current_recording_window(&synced_date_time.date_time_utc); let mut logged_frame_transfer = false; let mut logged_told_rpi_to_sleep = false; let mut logged_pi_powered_down = false; let mut logged_flash_storage_nearly_full = false; // NOTE: If there are already recordings on the flash memory, // assume we've already made the startup status recording during this recording window. - let mut made_startup_status_recording = - (is_cptv && has_files_to_offload) || (is_cptv && did_offload_files); - //there is a small posibility that we miss shutdown status recordings if the flash was full and while - //we are offloading it goes outside of the recording window - //we could check last recording is a status to counter this - let mut made_shutdown_status_recording = !device_config.time_is_in_recording_window( - &synced_date_time.date_time_utc, - &Some(current_recording_window), - ); let mut making_status_recording = false; let mut status_recording_pending = if !made_startup_status_recording { Some(StatusRecording::StartupStatus) From 364f7ec0cf01db883463598069dbcff2943c755e Mon Sep 17 00:00:00 2001 From: gferraro Date: Sun, 10 Nov 2024 21:25:32 +0100 Subject: [PATCH 4/4] remove unneeded logic --- src/core1_task.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core1_task.rs b/src/core1_task.rs index 6ea7e70..1a3f81f 100644 --- a/src/core1_task.rs +++ b/src/core1_task.rs @@ -580,7 +580,7 @@ pub fn core_1_task( }; let is_cptv = flash_storage.has_cptv_files(false); - let mut made_startup_status_recording = is_cptv && has_files_to_offload; + let mut made_startup_status_recording = is_cptv; let current_recording_window = device_config.next_or_current_recording_window(&synced_date_time.date_time_utc);