Get ready for a major remodel, fellas. We're back in hardware mode.
This commit is contained in:
parent
ffc48d8dce
commit
031613e59f
7 changed files with 78 additions and 13 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
target
|
||||
Cargo.lock
|
36
src/lib.rs
36
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<UdpSocket> = OnceLock::new();
|
||||
static SOCKET: OnceLock<output::sockets> = OnceLock::new();
|
||||
|
||||
fn flatten<T, const N: usize>(v: Vec<Vec<[T; N]>>) -> Vec<T> {
|
||||
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<u8> = vec![2, 2];
|
||||
let mut leds: Vec<RGB> = Vec::new();
|
||||
// let mut header: Vec<u8> = vec![2, 2];
|
||||
let mut leds: Vec<rgb::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 {
|
||||
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<u8> = leds.into_iter().flatten().collect();
|
||||
header.append(&mut flattened);
|
||||
// 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);
|
||||
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")
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
0
src/output/d2xx.rs
Normal file
0
src/output/d2xx.rs
Normal file
15
src/output/mod.rs
Normal file
15
src/output/mod.rs
Normal file
|
@ -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<rgb::RGB>) -> bool {
|
||||
match self {
|
||||
sockets::udprealtime(s) => {
|
||||
s.send(leds)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
0
src/output/serial.rs
Normal file
0
src/output/serial.rs
Normal file
24
src/output/udprealtime.rs
Normal file
24
src/output/udprealtime.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use std::net::{SocketAddr, UdpSocket};
|
||||
use crate::rgb::RGB;
|
||||
|
||||
pub struct output {
|
||||
pub socket: Option<UdpSocket>
|
||||
}
|
||||
|
||||
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<RGB>) -> bool {
|
||||
let mut header: Vec<u8> = vec![2, 2];
|
||||
let mut flattened: Vec<u8> = *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
|
||||
}
|
||||
}
|
14
src/rgb.rs
Normal file
14
src/rgb.rs
Normal file
|
@ -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<u8, 3>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
IntoIterator::into_iter([self.red, self.green, self.blue])
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue