Just how I screwed up this segway... to our sponsor.
This commit is contained in:
commit
3a6bf42438
3 changed files with 96 additions and 0 deletions
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
@ -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"
|
15
Cargo.toml
Normal file
15
Cargo.toml
Normal file
|
@ -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]
|
74
src/lib.rs
Normal file
74
src/lib.rs
Normal file
|
@ -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<u8, 3>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
IntoIterator::into_iter([self.red, self.green, self.blue])
|
||||
}
|
||||
}
|
||||
static SOCKET: OnceLock<UdpSocket> = OnceLock::new();
|
||||
|
||||
fn flatten<T, const N: usize>(v: Vec<Vec<[T; N]>>) -> Vec<T> {
|
||||
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<u8> = vec![2, 2];
|
||||
let mut leds: Vec<RGB> = 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<u8> = 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")
|
||||
},
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue