60 lines
1.9 KiB
Rust
60 lines
1.9 KiB
Rust
|
use std::{collections::HashMap, time::Duration};
|
||
|
|
||
|
use anyhow::Result;
|
||
|
use egui_extras::RetainedImage;
|
||
|
use scrap::{Capturer, Display};
|
||
|
|
||
|
use crate::{
|
||
|
capture,
|
||
|
image_processing::{self, Region},
|
||
|
ocr,
|
||
|
state::{ParsedFrame, SharedAppState},
|
||
|
};
|
||
|
|
||
|
async fn run_loop_once(state: &SharedAppState, regions: &[Region]) -> Result<()> {
|
||
|
let frame = capture::get_frame()?;
|
||
|
let ocr_results = ocr::ocr_all_regions(&frame, ®ions).await;
|
||
|
|
||
|
let mut saved_frames = HashMap::new();
|
||
|
if state.lock().unwrap().debug_frames {
|
||
|
for region in regions {
|
||
|
let mut extracted = image_processing::extract_region(&frame, region);
|
||
|
image_processing::filter_to_white(&mut extracted, 0.95, 0.05);
|
||
|
let retained = RetainedImage::from_image_bytes(
|
||
|
®ion.name,
|
||
|
&image_processing::to_png_bytes(&extracted),
|
||
|
)
|
||
|
.unwrap();
|
||
|
saved_frames.insert(region.name.clone(), retained);
|
||
|
}
|
||
|
}
|
||
|
{
|
||
|
let mut state = state.lock().unwrap();
|
||
|
let parsed = ParsedFrame::parse(&ocr_results);
|
||
|
if parsed.lap_time.is_some() {
|
||
|
state.last_frame = Some(parsed);
|
||
|
}
|
||
|
state.raw_data = ocr_results;
|
||
|
state.saved_frames = saved_frames;
|
||
|
}
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
pub async fn run_control_loop(state: SharedAppState) -> Result<()> {
|
||
|
let regions = vec![
|
||
|
Region::parse("health 91 1364 52 24").unwrap(),
|
||
|
Region::parse("gas 208 1364 52 24").unwrap(),
|
||
|
Region::parse("tyres 325 1364 52 24").unwrap(),
|
||
|
Region::parse("lap 2295 46 140 87").unwrap(),
|
||
|
Region::parse("best 2325 169 183 43").unwrap(),
|
||
|
Region::parse("lap_time 2325 222 183 43").unwrap(),
|
||
|
];
|
||
|
|
||
|
loop {
|
||
|
if let Err(e) = run_loop_once(&state, ®ions).await {
|
||
|
eprintln!("Error in control loop: {:?}", e)
|
||
|
}
|
||
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
||
|
}
|
||
|
}
|