web interface

This commit is contained in:
devdesk
2024-02-23 01:38:32 +02:00
parent ca742aa204
commit 373b17d8a5
4 changed files with 666 additions and 54 deletions

View File

@@ -1,7 +1,12 @@
use crate::offline::{Header, HDR_SIZE};
use bracket_color::prelude::*;
use clap::Parser;
use dotenv::dotenv;
use std::{io::Write, thread::spawn};
use std::{
io::Write,
sync::{Arc, Mutex},
thread::spawn,
};
use v4l::video::Output;
#[derive(Parser, Debug)]
@@ -38,9 +43,28 @@ fn once_upon_a_time_hsv2rgb(h: u8, s: u8, v: u8) -> (u8, u8, u8) {
(0, 0, 0)
}
fn main() -> anyhow::Result<()> {
fn rgb_to_u8s(rgb: &RGB) -> (u8, u8, u8) {
(
(rgb.r * 256.) as u8,
(rgb.g * 256.) as u8,
(rgb.b * 256.) as u8,
)
}
pub(crate) struct Streamer {
pub(crate) cutoff: Option<f64>,
}
pub(crate) fn initialize() -> Arc<Mutex<Streamer>> {
let args = Args::parse();
Arc::new(Mutex::new(Streamer {
cutoff: args.red_cutoff,
}))
}
fn main(streamer: Arc<Mutex<Streamer>>) -> anyhow::Result<()> {
dotenv().ok();
let args = Args::parse();
let device = match std::env::var("THERMALCAM_IFACE=enp1s0f0") {
Ok(d) => {
let device = pcap::Device::list()
@@ -62,7 +86,9 @@ fn main() -> anyhow::Result<()> {
const WIDTH: usize = 288;
const HEIGHT: usize = 384;
let greyscale = !args.temperature || args.red_cutoff.is_none();
println!("reading cutoff");
let cutoff = streamer.lock().unwrap().cutoff;
let greyscale = !args.temperature || cutoff.is_none();
let fourcc_repr = if greyscale {
[
b'Y', // | 0b10000000
@@ -74,6 +100,7 @@ fn main() -> anyhow::Result<()> {
// RGB32 is 4 bytes R, G, B, A
[b'R', b'G', b'B', b'4']
};
println!("using four cc {:?}", fourcc_repr);
let bytes_per_pixel = if greyscale { 2 } else { 4 };
let fourcc = v4l::format::FourCC { repr: fourcc_repr };
let mut out = v4l::Device::with_path(output)?;
@@ -115,6 +142,8 @@ fn main() -> anyhow::Result<()> {
|| (data.len() + len > FRAME_LEN)
{
if len == FRAME_LEN {
// read once per frame, can make it lower if need be
let cutoff = streamer.lock().unwrap().cutoff.unwrap_or(0.0);
// swap the bytes, we are using LE, not BE, 16 bit grayscale
// possibly limitation of current v4l2loopback or v4l rust wrapper or libv4l2
for i in 0..FRAME_LEN / 2 {
@@ -130,16 +159,15 @@ fn main() -> anyhow::Result<()> {
swapped[out_i..out_i + 2].copy_from_slice(&pixel_swapped);
} else {
pixel = pixel_to_celcius(pixel);
let cutoff = args.red_cutoff.unwrap();
let (r, g, b) = if pixel > (256.0 * cutoff) as u16 {
let p = pixel - (256.0 * cutoff) as u16;
let (r, g, b) = rgb_hsv::hsv_to_rgb((0.0, p as f32, 0.0));
(r as u8, g as u8, b as u8)
let rgb = HSV::from_f32(0.0, (p as f32) / 256.0, 0.0).to_rgb();
rgb_to_u8s(&rgb)
} else {
let h = frame[i * 2] as f32;
let v = frame[i * 2 + 1] as f32;
let (r, g, b) = rgb_hsv::hsv_to_rgb((h, 0.0, v));
(r as u8, g as u8, b as u8)
let rgb =
HSV::from_f32(pixel as f32 / 65536.0, 0.0, pixel as f32 / 65536.0)
.to_rgb();
rgb_to_u8s(&rgb)
};
let out_i = ((HEIGHT - 1 - y) + (WIDTH - 1 - x) * HEIGHT) * 4;
swapped[out_i..out_i + 4].copy_from_slice(&[0, r, g, b]);
@@ -155,9 +183,9 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
pub(crate) fn start_stream_thread() {
pub(crate) fn start_stream_thread(streamer: Arc<Mutex<Streamer>>) {
spawn(move || {
if let Err(e) = main() {
if let Err(e) = main(streamer) {
println!("oops: {:?}", e);
}
});