lj_rust/src/device/etherdream.rs

109 lines
2.9 KiB
Rust

use ether_dream::{dac};
use crate::conf::EtherDreamConf;
use crate::device::{Device, Status, PlaybackState};
use crate::errors::{LJError, LJResult};
use crate::point::Point;
use chrono::Utc;
pub struct EtherdreamDevice {
pub conf: EtherDreamConf,
dac: dac,
sent_points: u16,
state: PlaybackState,
lack: String,
last_traced_at : String
}
impl EtherdreamDevice {
pub fn new(conf: &EtherDreamConf) -> LJResult<Self> {
let id = conf.id;
// // Todo : use the config
// let (dac_broadcast, source_addr) = ether_dream::recv_dac_broadcasts()
// .expect("failed to bind to UDP socket")
// .filter_map(Result::ok)
// .next()
// .unwrap();
// let mac_address = dac::MacAddress(dac_broadcast.mac_address);
//
// println!(
// "Discovered DAC \"{}\" at \"{}\"! Connecting...",
// mac_address, source_addr
// );
// Establish the TCP connection.
let mut stream = dac::stream::connect(&dac_broadcast, source_addr.ip().clone()).unwrap();
// If we want to create an animation (in our case a moving sine wave) we need a frame rate.
let frames_per_second = 60.0;
// Lets use the DAC at an eighth the maximum scan rate.
let points_per_second = stream.dac().max_point_rate / 32;
// Determine the number of points per frame given our target frame and point rates.
let points_per_frame = (points_per_second as f32 / frames_per_second) as u16;
// Prepare the DAC's playback engine and await the repsonse.
stream
.queue_commands()
.prepare_stream()
.submit()
.err()
.map(|err| {
eprintln!(
"err occurred when submitting PREPARE_STREAM \
command and listening for response: {}",
err
);
});
println!("Ready for playback!");
Ok(Self {
conf: (*conf).clone(),
dac,
sent_points: 0,
state: PlaybackState::PREPARE,
lack: "".to_string(),
last_traced_at: "1985-04-12T23:20:50.52Z".to_string()
})
}
}
impl Device for EtherdreamDevice {
fn status(&self) -> Status {
let lack = self.lack.clone();
Status {
last_traced_at: self.last_traced_at.clone(),
properties: vec!["foo".to_string()],
playback_state: self.state,
capacity: self.sent_points,
lack,
}
}
fn draw(&mut self,
line: Vec<Point>,
speed: u32,
) -> LJResult<()> {
self.state = PlaybackState::IDLE;
while let Ok(DeviceStatus::NotReady) = self.dac.status() {}
self.state = PlaybackState::PLAYING;
let points: Vec<helios_dac::Point> = line.into_iter().map(|p| p.into()).collect();
let frame = Frame::new(speed, points.clone());
self.dac.write_frame(frame.clone())?;
self.sent_points = points.len() as u16;
self.last_traced_at = Utc::now().to_rfc3339();
Ok(())
}
fn stop(&mut self) -> LJResult<()> {
self.dac.stop()?;
Ok(())
}
}