diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2f9e58 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 570eff8..032008c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,9 @@ use std::net::{SocketAddr, UdpSocket}; use std::sync::OnceLock; use std::convert::TryFrom; +mod output; +mod rgb; + struct RGB { red: u8, green: u8, @@ -18,7 +21,7 @@ impl IntoIterator for RGB { IntoIterator::into_iter([self.red, self.green, self.blue]) } } -static SOCKET: OnceLock = OnceLock::new(); +static SOCKET: OnceLock = OnceLock::new(); fn flatten(v: Vec>) -> Vec { v.into_iter().flatten().flatten().collect() @@ -35,8 +38,13 @@ pub extern fn USBIntLED_getVersion() -> i64 { #[no_mangle] pub extern fn USBIntLED_Init() -> bool { println!("Init"); - let _ = SOCKET.set(UdpSocket::bind("0.0.0.0:0").unwrap()); - return true; + // let _ = SOCKET.set(UdpSocket::bind("0.0.0.0:0").unwrap()); + let e = output::udprealtime::output { + socket: None + }; + e.init(); + let _ = SOCKET.set(output::Sockets(e)); + return true } #[no_mangle] @@ -47,28 +55,30 @@ pub extern fn USBIntLED_Terminate() -> bool { #[no_mangle] pub extern fn USBIntLED_set(_a1: i64, a2: usize) { - let mut header: Vec = vec![2, 2]; - let mut leds: Vec = Vec::new(); + // let mut header: Vec = vec![2, 2]; + let mut leds: Vec = Vec::new(); for i in (3..1920).step_by(4) { let n: u32 = unsafe { ptr::read((a2+i) as *const u32) }; let a: [u8; 4] =n.to_le_bytes(); - leds.push(RGB { + leds.push(rgb::RGB { red: a[1], green: a[2], blue: a[3], }); } - let addr = SocketAddr::from(([100, 64, 0, 79], 21324)); - let mut flattened: Vec = leds.into_iter().flatten().collect(); - header.append(&mut flattened); + // let addr = SocketAddr::from(([100, 64, 0, 79], 21324)); + // let mut flattened: Vec = leds.into_iter().flatten().collect(); + // header.append(&mut flattened); match SOCKET.get() { - Some(socket) => { - let sock: UdpSocket = socket.try_clone().unwrap(); - let _ = sock.send_to(&header, addr); + Some(&ref socket) => { + // let sock: UdpSocket = socket.try_clone().unwrap(); + // let _ = sock.send_to(&header, addr); + socket.send(&mut leds); + // output::sockets::udprealtime(*socket).send(leds) }, None => { println!("Socket hasn't been initialized yet") }, } -} +} \ No newline at end of file diff --git a/src/output/d2xx.rs b/src/output/d2xx.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/output/mod.rs b/src/output/mod.rs new file mode 100644 index 0000000..d8ceef5 --- /dev/null +++ b/src/output/mod.rs @@ -0,0 +1,15 @@ +pub mod udprealtime; +use crate::rgb; +pub enum sockets { + udprealtime(udprealtime::output) +} + +impl sockets { + pub fn send(&mut self, leds: &mut Vec) -> bool { + match self { + sockets::udprealtime(s) => { + s.send(leds) + } + } + } +} \ No newline at end of file diff --git a/src/output/serial.rs b/src/output/serial.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/output/udprealtime.rs b/src/output/udprealtime.rs new file mode 100644 index 0000000..d8b35fb --- /dev/null +++ b/src/output/udprealtime.rs @@ -0,0 +1,24 @@ +use std::net::{SocketAddr, UdpSocket}; +use crate::rgb::RGB; + +pub struct output { + pub socket: Option +} + +impl output { + pub fn init(&mut self) -> bool { + self.socket = Some(UdpSocket::bind("0.0.0.0:0").unwrap()); + return true; + } + pub fn send(&mut self, leds: &mut Vec) -> bool { + let mut header: Vec = vec![2, 2]; + let mut flattened: Vec = *leds.into_iter().flatten().collect(); + let addr = SocketAddr::from(([100, 64, 0, 79], 21324)); + header.append(&mut flattened); + match self.socket { + Some(s) => { let _ = s.send_to(&header, addr); }, + None => () + } + return true + } +} \ No newline at end of file diff --git a/src/rgb.rs b/src/rgb.rs new file mode 100644 index 0000000..2441a43 --- /dev/null +++ b/src/rgb.rs @@ -0,0 +1,14 @@ +pub struct RGB { + pub red: u8, + pub green: u8, + pub blue: u8 +} + +impl IntoIterator for RGB { + type Item = u8; + type IntoIter = std::array::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + IntoIterator::into_iter([self.red, self.green, self.blue]) + } +}