diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..bb0d570 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,26 @@ +name: Cargo Build Release + +on: + push: + pull_request: + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + runs-on: windows-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Update rust to nightly + run: rustup update nightly && rustup default nightly + - name: Check for errors + run: cargo check + - name: Build with release profile + run: cargo build --release + - name: Archive output from build + uses: actions/upload-artifact@v4 + with: + name: build + path: target/x86_64-pc-windows-gnu/release/*.dll \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index e1d2576..0e4dca8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,6 @@ use std::ptr; use std::net::{SocketAddr, UdpSocket}; use std::sync::OnceLock; -use std::convert::TryFrom; mod output; mod rgb; @@ -39,12 +38,18 @@ pub extern fn USBIntLED_getVersion() -> i64 { pub extern fn USBIntLED_Init() -> bool { println!("Init"); // let _ = SOCKET.set(UdpSocket::bind("0.0.0.0:0").unwrap()); - let mut e = output::udprealtime::output { - socket: Some(UdpSocket::bind("0.0.0.0:0").unwrap()) + // let mut e = output::udprealtime::output { + // socket: Some(UdpSocket::bind("0.0.0.0:0").unwrap()) + // }; + let ok = match output::udprealtime::Output::create(SocketAddr::from(([127, 0, 0, 1], 21324))) { + Ok(e) => { match SOCKET.set(output::sockets::udprealtime(e)) { + Ok(_) => true, + Err(_) => false, + }}, + Err(e) => { println!("{:?}",e); false }, }; // e.init(); - let _ = SOCKET.set(output::sockets::udprealtime(e)); - return true + return ok } #[no_mangle] diff --git a/src/output/mod.rs b/src/output/mod.rs index cdd8a5b..442686b 100644 --- a/src/output/mod.rs +++ b/src/output/mod.rs @@ -1,7 +1,7 @@ pub mod udprealtime; use crate::rgb; pub enum sockets { - udprealtime(udprealtime::output) + udprealtime(udprealtime::Output) } impl sockets { diff --git a/src/output/udprealtime.rs b/src/output/udprealtime.rs index 4e4dc5b..3747cd0 100644 --- a/src/output/udprealtime.rs +++ b/src/output/udprealtime.rs @@ -1,25 +1,31 @@ use std::net::{SocketAddr, UdpSocket}; -use crate::rgb::RGB; +use crate::rgb; -pub struct output { - pub socket: Option +pub struct Output { + pub socket: UdpSocket, + pub target: SocketAddr } -impl output { +impl Output { // pub fn init(&mut self) -> bool { // self.socket = // return true; // } - pub fn send(&self, leds: &Vec) -> bool { + pub fn create(addr: SocketAddr) -> Result { + let socket = match UdpSocket::bind("0.0.0.0:0") { + Ok(socket) => socket, + Err(_) => todo!(), + }; + return Ok(Output { + socket: socket, + target: addr, + }) + } + pub fn send(&self, leds: &Vec) -> bool { let mut header: Vec = vec![2, 2]; - let leds2 = Vec::clone(leds); - let mut flattened: Vec = leds2.into_iter().flatten().collect(); - let addr = SocketAddr::from(([100, 64, 0, 79], 21324)); + let mut flattened: Vec = rgb::serialize(&leds); header.append(&mut flattened); - match &self.socket { - Some(s) => { let _ = s.send_to(&header, addr); }, - None => () - } + let _ = &self.socket.send_to(&header, &self.target); return true } } \ No newline at end of file diff --git a/src/rgb.rs b/src/rgb.rs index 575b47b..1cf0a12 100644 --- a/src/rgb.rs +++ b/src/rgb.rs @@ -13,3 +13,8 @@ impl IntoIterator for RGB { IntoIterator::into_iter([self.red, self.green, self.blue]) } } + +pub fn serialize(s: &Vec) -> Vec { + let p = s.clone(); + p.into_iter().flatten().collect() +}