From 3a6bf42438f34dece49b687089c2c010ede76da4 Mon Sep 17 00:00:00 2001 From: Kyan Wanschers Date: Sat, 20 Apr 2024 20:17:51 +0200 Subject: [PATCH] Just how I screwed up this segway... to our sponsor. --- Cargo.lock | 7 ++++++ Cargo.toml | 15 +++++++++++ src/lib.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..2934d55 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "wacca-led" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0372518 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +cargo-features = ["per-package-target"] + +[package] +name = "wacca-led" +version = "0.1.0" +edition = "2021" +default-target="x86_64-pc-windows-gnu" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + + +[lib] +crate-type = ["cdylib"] + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..570eff8 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,74 @@ +#![allow(non_snake_case)] +use std::ptr; +use std::net::{SocketAddr, UdpSocket}; +use std::sync::OnceLock; +use std::convert::TryFrom; + +struct RGB { + red: u8, + green: u8, + 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]) + } +} +static SOCKET: OnceLock = OnceLock::new(); + +fn flatten(v: Vec>) -> Vec { + v.into_iter().flatten().flatten().collect() +} + +#[no_mangle] +pub extern fn USBIntLED_getVersion() -> i64 { + println!("Get Version"); + let path = std::env::current_dir().unwrap(); + println!("The current directory is {}", path.display()); + 257 +} + +#[no_mangle] +pub extern fn USBIntLED_Init() -> bool { + println!("Init"); + let _ = SOCKET.set(UdpSocket::bind("0.0.0.0:0").unwrap()); + return true; +} + +#[no_mangle] +pub extern fn USBIntLED_Terminate() -> bool { + println!("Terminate"); + return true +} + +#[no_mangle] +pub extern fn USBIntLED_set(_a1: i64, a2: usize) { + 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 { + 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); + match SOCKET.get() { + Some(socket) => { + let sock: UdpSocket = socket.try_clone().unwrap(); + let _ = sock.send_to(&header, addr); + }, + None => { + println!("Socket hasn't been initialized yet") + }, + } +}