Add mpsc to send data to waterfall

This commit is contained in:
Lucas Schumacher 2024-05-07 17:56:14 -04:00
parent e59f6ce896
commit e5dc145ada
2 changed files with 49 additions and 34 deletions

View File

@ -1,11 +1,25 @@
use eframe::{egui_glow, glow}; use eframe::{egui_glow, glow};
use egui::mutex::Mutex; use egui::mutex::Mutex;
use std::sync::mpsc;
use std::sync::Arc; use std::sync::Arc;
mod waterfall; mod waterfall;
use waterfall::Waterfall; use waterfall::Waterfall;
pub mod turbo_colormap; pub mod turbo_colormap;
mod deadbeef_rand {
static mut RNG_SEED: u32 = 0x3d2faba7;
static mut RNG_BEEF: u32 = 0xdeadbeef;
pub fn rand() -> u8 {
unsafe {
RNG_SEED = (RNG_SEED << 7) ^ ((RNG_SEED >> 25).wrapping_add(RNG_BEEF));
RNG_BEEF = (RNG_BEEF << 7) ^ ((RNG_BEEF >> 25).wrapping_add(0xdeadbeef));
(RNG_SEED & 0xff) as u8
}
}
}
use deadbeef_rand::rand;
/// We derive Deserialize/Serialize so we can persist app state on shutdown. /// We derive Deserialize/Serialize so we can persist app state on shutdown.
pub struct TemplateApp { pub struct TemplateApp {
// Example stuff: // Example stuff:
@ -13,6 +27,7 @@ pub struct TemplateApp {
value: f32, value: f32,
/// Behind an `Arc<Mutex<…>>` so we can pass it to [`egui::PaintCallback`] and paint later. /// Behind an `Arc<Mutex<…>>` so we can pass it to [`egui::PaintCallback`] and paint later.
waterfall: Arc<Mutex<Waterfall>>, waterfall: Arc<Mutex<Waterfall>>,
fft_sender: mpsc::Sender<Vec<u8>>,
} }
impl TemplateApp { impl TemplateApp {
@ -24,6 +39,7 @@ impl TemplateApp {
// Load previous app state (if any). // Load previous app state (if any).
// Note that you must enable the `persistence` feature for this to work. // Note that you must enable the `persistence` feature for this to work.
let (tx, rx) = mpsc::channel();
let gl = cc let gl = cc
.gl .gl
.as_ref() .as_ref()
@ -33,7 +49,8 @@ impl TemplateApp {
// Example stuff: // Example stuff:
label: "Hello World!".to_owned(), label: "Hello World!".to_owned(),
value: 2.7, value: 2.7,
waterfall: Arc::new(Mutex::new(Waterfall::new(gl, 300, 300))), waterfall: Arc::new(Mutex::new(Waterfall::new(gl, 300, 300, rx))),
fft_sender: tx,
} }
} }
} }
@ -104,6 +121,12 @@ impl eframe::App for TemplateApp {
let _angle = response.drag_motion().x * 0.01; let _angle = response.drag_motion().x * 0.01;
let mut new_data = vec![0_u8; 300];
for data in new_data.iter_mut() {
*data = rand();
}
self.fft_sender.send(new_data).unwrap();
// Clone locals so we can move them into the paint callback: // Clone locals so we can move them into the paint callback:
let waterfall = self.waterfall.clone(); let waterfall = self.waterfall.clone();

View File

@ -3,6 +3,7 @@ use glow::HasContext as _;
use glow::{NEAREST, TEXTURE_2D, TEXTURE_MAG_FILTER, TEXTURE_MIN_FILTER}; use glow::{NEAREST, TEXTURE_2D, TEXTURE_MAG_FILTER, TEXTURE_MIN_FILTER};
use log; use log;
use std::mem::{size_of, transmute}; use std::mem::{size_of, transmute};
use std::sync::mpsc::Receiver;
const SIZE_OF_F32: i32 = size_of::<f32>() as i32; const SIZE_OF_F32: i32 = size_of::<f32>() as i32;
@ -15,19 +16,6 @@ unsafe fn check_for_gl_errors(gl: &glow::Context, msg: &str) {
} }
} }
mod deadbeef_rand {
static mut RNG_SEED: u32 = 0x3d2faba7;
static mut RNG_BEEF: u32 = 0xdeadbeef;
pub fn rand() -> u8 {
unsafe {
RNG_SEED = (RNG_SEED << 7) ^ ((RNG_SEED >> 25).wrapping_add(RNG_BEEF));
RNG_BEEF = (RNG_BEEF << 7) ^ ((RNG_BEEF >> 25).wrapping_add(0xdeadbeef));
(RNG_SEED & 0xff) as u8
}
}
}
use deadbeef_rand::rand;
use crate::app::turbo_colormap; use crate::app::turbo_colormap;
pub struct Waterfall { pub struct Waterfall {
@ -38,6 +26,8 @@ pub struct Waterfall {
vbo: glow::Buffer, vbo: glow::Buffer,
ebo: glow::Buffer, ebo: glow::Buffer,
offset: usize, offset: usize,
width: usize,
fft_in: Receiver<Vec<u8>>,
} }
impl Waterfall { impl Waterfall {
@ -54,11 +44,6 @@ impl Waterfall {
pub fn paint(&mut self, gl: &glow::Context, _angle: f32) { pub fn paint(&mut self, gl: &glow::Context, _angle: f32) {
use glow::HasContext as _; use glow::HasContext as _;
let mut new_data: [u8; 300] = [0; 300];
for data in new_data.iter_mut() {
*data = rand();
}
unsafe { unsafe {
// Bind our texturs // Bind our texturs
gl.active_texture(TEXTURE1); gl.active_texture(TEXTURE1);
@ -80,22 +65,27 @@ impl Waterfall {
check_for_gl_errors(&gl, "bind vao"); check_for_gl_errors(&gl, "bind vao");
// Update texture // Update texture
gl.tex_sub_image_2d( while let Ok(fft) = self.fft_in.try_recv() {
glow::TEXTURE_2D, if fft.len() != self.width {
0, todo!();
0, }
self.offset as i32, gl.tex_sub_image_2d(
300, glow::TEXTURE_2D,
1, 0,
glow::RED, 0,
glow::UNSIGNED_BYTE, self.offset as i32,
PixelUnpackData::Slice(&new_data), self.width as i32,
); 1,
check_for_gl_errors(&gl, "update texture"); glow::RED,
self.offset = (self.offset + 1) % 300; glow::UNSIGNED_BYTE,
PixelUnpackData::Slice(&fft),
);
check_for_gl_errors(&gl, "update texture");
self.offset = (self.offset + 1) % self.width;
}
if let Some(uniform) = gl.get_uniform_location(self.program, "offset") { if let Some(uniform) = gl.get_uniform_location(self.program, "offset") {
gl.uniform_1_f32(Some(&uniform), self.offset as f32 / 300.0); gl.uniform_1_f32(Some(&uniform), self.offset as f32 / self.width as f32);
} }
check_for_gl_errors(&gl, "update uniform"); check_for_gl_errors(&gl, "update uniform");
@ -106,7 +96,7 @@ impl Waterfall {
check_for_gl_errors(&gl, "APP PAINT"); check_for_gl_errors(&gl, "APP PAINT");
} }
} }
pub fn new(gl: &glow::Context, width: usize, height: usize) -> Self { pub fn new(gl: &glow::Context, width: usize, height: usize, fft_in: Receiver<Vec<u8>>) -> Self {
let vertices: [f32; 32] = [ let vertices: [f32; 32] = [
// positions // colors // texture coords // positions // colors // texture coords
1.0, 1.0, 0.0, /**/ 1.0, 0.0, 0.0, /**/ 1.0, 1.0, // top right 1.0, 1.0, 0.0, /**/ 1.0, 0.0, 0.0, /**/ 1.0, 1.0, // top right
@ -320,6 +310,8 @@ impl Waterfall {
vbo, vbo,
ebo, ebo,
offset: 0_usize, offset: 0_usize,
width,
fft_in,
} }
} }
} }