Skip to content

Commit

Permalink
Merge pull request #16 from TheCacophonyProject/no-ffc-inrecording
Browse files Browse the repository at this point in the history
No ffc inrecording
  • Loading branch information
gferraro authored Aug 28, 2024
2 parents 80ebc21 + b4a3dea commit 4e3e2f6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/attiny_rtc_i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ impl SharedI2C {
Err(x) => Err(x),
}
}

pub fn tc2_agent_is_recording(&mut self, delay: &mut Delay) -> Result<bool, Error> {
match self.try_attiny_read_command(REG_TC2_AGENT_STATE, delay, None) {
Ok(state) => Ok((state & 0x04) == 0x04),
Err(e) => Err(e),
}
}

pub fn pi_is_waking_or_awake(&mut self, delay: &mut Delay) -> Result<bool, Error> {
match self.try_attiny_read_command(REG_CAMERA_STATE, delay, None) {
Ok(state) => {
Expand Down
23 changes: 18 additions & 5 deletions src/core0_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ pub fn frame_acquisition_loop(
let crc_check = Crc::<u16>::new(&CRC_16_XMODEM);
let do_crc_check = true;
let mut crc_buffer = [0u8; 164];

let mut prev_segment_was_4 = false;
let mut scanline_buffer = [0u16; 82];

Expand All @@ -102,7 +101,8 @@ pub fn frame_acquisition_loop(
// Do FFC every 20 mins?
let mut needs_ffc = false;
let mut ffc_requested = false;

let mut can_do_ffc = true;
let mut high_power_mode = false;
let mut seen_telemetry_revision = [0u8, 0u8];
let mut times_telemetry_revision_stable = -1;
let mut frames_seen = 0;
Expand Down Expand Up @@ -135,7 +135,11 @@ pub fn frame_acquisition_loop(
}
if !transferring_prev_frame && prev_frame_needs_transfer {
// Initiate the transfer of the previous frame
sio_fifo.write(Core1Task::ReceiveFrame.into());
if needs_ffc {
sio_fifo.write(Core1Task::ReceiveFrameWithPendingFFC.into());
} else {
sio_fifo.write(Core1Task::ReceiveFrame.into());
}
sio_fifo.write(selected_frame_buffer);
if selected_frame_buffer == 0 {
selected_frame_buffer = 1;
Expand Down Expand Up @@ -175,7 +179,8 @@ pub fn frame_acquisition_loop(
if got_sync && valid_frame_current_segment_num == 1 {
// Check if we need an FFC
if unverified_frame_counter == prev_frame_counter + 2 {
if !is_recording
if !needs_ffc
&& !is_recording
&& telemetry.msec_on != 0
&& (telemetry.time_at_last_ffc != 0 || !has_done_initial_ffc)
{
Expand All @@ -192,7 +197,11 @@ pub fn frame_acquisition_loop(
{
needs_ffc = true;
ffc_requested = false;
if high_power_mode {
can_do_ffc = false;
}
}

// Sometimes the header is invalid, but the frame becomes valid and gets sync.
// Because the telemetry revision is static across frame headers we can detect this
// case and not send the frame, as it may cause false triggers.
Expand Down Expand Up @@ -325,6 +334,7 @@ pub fn frame_acquisition_loop(
} else {
frame_buffer_local_2
};

LittleEndian::write_u16_into(
&scanline_buffer[2..],
buffer
Expand All @@ -339,7 +349,7 @@ pub fn frame_acquisition_loop(
prev_segment_was_4 = is_last_segment;
if packet_id == last_packet_id_for_segment {
if is_last_segment {
if needs_ffc && !ffc_requested {
if can_do_ffc && needs_ffc && !ffc_requested {
ffc_requested = true;
info!("Requesting needed FFC");
let success = lepton.do_ffc();
Expand Down Expand Up @@ -468,6 +478,7 @@ pub fn frame_acquisition_loop(
}
} else if message == Core1Task::EndRecording.into() {
recording_ended = true;
can_do_ffc = true;
if let Some(message) = sio_fifo.read() {
if message == Core1Task::FrameProcessingComplete.into() {
transferring_prev_frame = false;
Expand All @@ -493,6 +504,8 @@ pub fn frame_acquisition_loop(
// Wait until the watchdog timer kills us.
nop();
}
} else if message == Core1Task::HighPowerMode.into() {
high_power_mode = true;
}
}
// if !transferring_prev_frame || recording_started {
Expand Down
63 changes: 55 additions & 8 deletions src/core1_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub enum Core1Task {
EndFileOffload = 0xfb,
ReadyToSleep = 0xef,
RequestReset = 0xea,
HighPowerMode = 0xeb,
ReceiveFrameWithPendingFFC = 0xac,
}

impl Into<u32> for Core1Task {
Expand Down Expand Up @@ -363,6 +365,7 @@ pub fn core_1_task(
huffman_table.copy_from_slice(&HUFFMAN_TABLE[..]);

sio.fifo.write_blocking(Core1Task::Ready.into());

let radiometry_enabled = sio.fifo.read_blocking();
info!("Core 1 got radiometry enabled: {}", radiometry_enabled == 2);
let lepton_version = if radiometry_enabled == 2 { 35 } else { 3 };
Expand Down Expand Up @@ -521,7 +524,6 @@ pub fn core_1_task(
None,
);
}

// NOTE: We'll only wake the pi if we have files to offload, and it is *outside* the recording
// window, or the previous offload happened more than 24 hours ago, or the flash is nearly full.
// Otherwise, if the rp2040 happens to restart, we'll pretty much
Expand Down Expand Up @@ -610,7 +612,9 @@ pub fn core_1_task(

warn!("Core 1 is ready to receive frames");
sio.fifo.write_blocking(Core1Task::Ready.into());

if !device_config.config().use_low_power_mode {
sio.fifo.write_blocking(Core1Task::HighPowerMode.into());
}
let mut cptv_stream: Option<CptvStream> = None;
let mut prev_frame: [u16; (FRAME_WIDTH * FRAME_HEIGHT) + 1] =
[0u16; (FRAME_WIDTH * FRAME_HEIGHT) + 1];
Expand Down Expand Up @@ -644,6 +648,8 @@ pub fn core_1_task(

let mut made_shutdown_status_recording = false;
let mut making_status_recording = false;
let mut high_power_recording = false;
let mut last_rec_check = 0;
// Enable raw frame transfers to pi – if not already enabled.
pi_spi.enable_pio_spi();
info!(
Expand All @@ -652,12 +658,18 @@ pub fn core_1_task(
);
loop {
let input = sio.fifo.read_blocking();
crate::assert_eq!(
input,
Core1Task::ReceiveFrame.into(),
"Got unknown fifo input to core1 task loop {}",
input
);
let needs_ffc: bool;
if input == Core1Task::ReceiveFrameWithPendingFFC.into() {
needs_ffc = true;
} else {
crate::assert_eq!(
input,
Core1Task::ReceiveFrame.into(),
"Got unknown fifo input to core1 task loop {}",
input
);
needs_ffc = false;
}

let start = timer.get_counter();
// Get the currently selected buffer to transfer/write to disk.
Expand Down Expand Up @@ -686,6 +698,41 @@ pub fn core_1_task(
(frame_telemetry, frame_header_is_valid)
};

//if in high power mode need to check thermal-recorder hasn't made a recording
if needs_ffc && !device_config.use_low_power_mode() {
//only check status every 20 seconds
if frame_telemetry.frame_num - last_rec_check > 9 * 20 {
if let Ok(is_recording) = shared_i2c.tc2_agent_is_recording(&mut delay) {
high_power_recording = is_recording;
last_rec_check = frame_telemetry.frame_num;
info!(
"Checking if recording {} am recording ?? {}",
is_recording, high_power_recording
);
if high_power_recording {
sio.fifo.write(Core1Task::StartRecording.into());
high_power_recording = true;
} else {
sio.fifo.write(Core1Task::EndRecording.into());
high_power_recording = false;
thread_local_frame_buffer
.as_mut()
.unwrap()
.ffc_imminent(true);
}
}
} else if !high_power_recording {
//depending on timing might get 2 frames with needs_ffc event after said ok
thread_local_frame_buffer
.as_mut()
.unwrap()
.ffc_imminent(true);
}
} else if high_power_recording {
high_power_recording = false;
last_rec_check = 0;
}

let frame_transfer_start = timer.get_counter();
// Transfer RAW frame to pi if it is available.
let transfer = if frame_header_is_valid {
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ impl FrameBuffer {
&mut self.0
}

pub fn ffc_imminent(&mut self, is_imminent: bool) {
self.0[TRANSFER_HEADER_LENGTH + 636] = if is_imminent { 1 } else { 0 };
self.0[TRANSFER_HEADER_LENGTH + 637] = if is_imminent { 1 } else { 0 };
}

pub fn frame_data_as_u8_slice_mut(&mut self) -> &mut [u8] {
&mut self.0[TRANSFER_HEADER_LENGTH..TRANSFER_HEADER_LENGTH + 39040]
}
Expand Down

0 comments on commit 4e3e2f6

Please sign in to comment.