Skip to content

Commit

Permalink
adjust config
Browse files Browse the repository at this point in the history
  • Loading branch information
gferraro committed May 13, 2024
1 parent 6a00ecc commit d7d8bfc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/device_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,13 @@ impl DeviceConfig {
std::process::exit(1);
}
info!("Got config {:?}", device_config);

let inside_recording_window =
device_config.time_is_in_recording_window(&Utc::now().naive_utc());
info!("Inside recording window: {}", inside_recording_window);
if !inside_recording_window {
device_config.print_next_recording_window(&Utc::now().naive_utc());
if device_config.is_audio_device().unwrap_or_default(){
let inside_recording_window =
device_config.time_is_in_recording_window(&Utc::now().naive_utc());
info!("Inside recording window: {}", inside_recording_window);
if !inside_recording_window {
device_config.print_next_recording_window(&Utc::now().naive_utc());
}
}
device_config.load_last_offload();
Ok(device_config)
Expand Down Expand Up @@ -826,7 +827,8 @@ impl DeviceConfig {
let mut buf = Cursor::new(output);
let device_id = self.device_id();
buf.write_u32::<LittleEndian>(device_id).unwrap();

buf.write_u8(if self.is_audio_device().unwrap_or_default() { 1 } else { 0 })
.unwrap();
let (latitude, longitude) = self.lat_lng();
buf.write_f32::<LittleEndian>(latitude).unwrap();
buf.write_f32::<LittleEndian>(longitude).unwrap();
Expand Down Expand Up @@ -869,8 +871,7 @@ impl DeviceConfig {
buf.write(&device_name[0..device_name_length]).unwrap();


buf.write_u8(if self.is_audio_device().unwrap_or_default() { 1 } else { 0 })
.unwrap();

buf.write_i64::<LittleEndian>(self.last_offload.unwrap_or_default()).unwrap();

}
Expand Down
44 changes: 42 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn wav_header(audio_bytes: &Vec<u8>) -> [u8;44]{
cursor+=1;
}

let file_size = audio_bytes.len() + 44;
let file_size = audio_bytes.len() + 36;
for b in file_size.to_le_bytes(){
header[cursor] =b;
cursor+=1;
Expand Down Expand Up @@ -199,6 +199,7 @@ fn wav_header(audio_bytes: &Vec<u8>) -> [u8;44]{
header[cursor] =b;
cursor+=1;
}

let sr = sr*2;
for b in sr.to_le_bytes(){
header[cursor] =b;
Expand All @@ -221,7 +222,6 @@ fn wav_header(audio_bytes: &Vec<u8>) -> [u8;44]{
header[cursor] =b;
cursor+=1;
}

return header
}
fn save_audio_file_to_disk(audio_bytes: Vec<u8>, output_dir: &str) {
Expand Down Expand Up @@ -344,6 +344,19 @@ fn read_attiny_firmware_version(attiny_i2c: &mut I2c) -> Result<u8, &'static str
read_attiny_reg(attiny_i2c, 0x01)
}

fn set_attiny_tc2_agent_test_audio_rec(attiny_i2c: &mut I2c) -> Result<(), &'static str> {
let state = read_tc2_agent_state(attiny_i2c);
if let Ok(state) = state {
if (state & 0x04 ==0x04){
Err("Already recording so not doing test rec")
}else{
write_attiny_command(attiny_i2c, 0x07, state | 0x08)
}
} else {
Err("Failed reading ready state from attiny")
}
}

fn set_attiny_tc2_agent_ready(attiny_i2c: &mut I2c) -> Result<(), &'static str> {
let state = read_tc2_agent_state(attiny_i2c);
if let Ok(state) = state {
Expand Down Expand Up @@ -385,6 +398,21 @@ fn main() {
std::process::exit(1);
});

//set up dbus service
// let _ = thread::Builder::new().name("dbus-service".to_string()).spawn_with_priority(ThreadPriority::Max, move |result| {


// let agentService = AgentService { count: 0 };
// let _conn = connection::Builder::session()?
// .name("org.zbus.TC2Agent")?
// .serve_at("/org/zbus/TC2Agent", agentService)?
// .build()
// .await?;

// // Do other things or go to wait forever
// pending::<()>().await;
// }

let mut current_config = device_config.unwrap();
let initial_config = current_config.clone();
let (config_tx, config_rx) = channel();
Expand Down Expand Up @@ -431,6 +459,7 @@ fn main() {

// We want real-time priority for all the work we do.
let frame_acquire = false;
let mut take_test_audio = false;
let _ = thread::Builder::new().name("frame-acquire".to_string()).spawn_with_priority(ThreadPriority::Max, move |result| {
assert!(result.is_ok(), "Thread must have permissions to run with realtime priority, run as root user");

Expand Down Expand Up @@ -683,6 +712,17 @@ fn main() {
}
}
}
if take_test_audio && device_config.is_audio_device().unwrap_or_default(){
if let Some(ref mut attiny_i2c_interface) = attiny_i2c_interface {
if set_attiny_tc2_agent_test_audio_rec( attiny_i2c_interface).is_ok() {
info!("Set test rec");
if !device_config.use_low_power_mode() || safe_to_restart_rp2040(attiny_i2c_interface) {
// NOTE: Always reset rp2040 on startup if it's safe to do so.
restart_tx.send(true);
}
}
}
}

// // We might have to abandon using pin interrupt to trigger SPI, and just constantly poll for a pattern to align to.
// // Align our SPI reads to the start of the sequence 1, 2, 3, 4, 1, 2, 3, 4
Expand Down

0 comments on commit d7d8bfc

Please sign in to comment.