fix: etherdream should work with device trait adjustments

This commit is contained in:
alban 2023-07-24 17:35:15 +02:00
parent 6f04925ea5
commit 514f4cc7c9
3 changed files with 70 additions and 50 deletions

View File

@ -42,7 +42,7 @@ pub struct Status {
pub last_traced_at: String, pub last_traced_at: String,
pub properties: Vec<String>, pub properties: Vec<String>,
pub playback_state: PlaybackState, pub playback_state: PlaybackState,
pub capacity: u16, pub capacity: usize,
pub lack: String, pub lack: String,
} }

View File

@ -1,16 +1,20 @@
use std::time; #[warn(unused_imports)]
use log::{ debug, info, warn};
use std::net::SocketAddr; use std::net::SocketAddr;
use std::thread::sleep;
use ether_dream::dac::stream::{CommunicationError, connect}; use ether_dream::dac::stream::{CommunicationError, connect};
use ether_dream::dac::{Playback, Stream}; use ether_dream::dac::{Playback, Stream};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use std::time::SystemTime; use std::time;
use std::time::{Duration, SystemTime};
use crate::conf::EtherDreamConf; use crate::conf::EtherDreamConf;
use crate::device::{Device, Status, PlaybackState}; use crate::device::{Device, Status, PlaybackState};
use crate::errors::{LJError, LJResult}; use crate::errors::{LJError, LJResult};
use crate::point::{Color, Point}; use crate::point::{Color, Point};
use ether_dream::protocol::{DacBroadcast, DacResponse}; use ether_dream::protocol::{DacBroadcast, DacResponse};
use log::{debug, info, warn};
#[warn(dead_code)] #[warn(dead_code)]
pub struct EtherdreamDevice { pub struct EtherdreamDevice {
@ -114,14 +118,20 @@ impl EtherdreamDevice {
Ok(stream) Ok(stream)
} }
fn points_capacity(&self) -> u16 { fn points_capacity(&self) -> usize {
/*** /***
Determine the number of points needed to fill the DAC. Determine the number of points needed to fill the DAC.
***/ ***/
// Fixme thread 'main' panicked at 'attempt to subtract with overflow', src/device/etherdream.rs:144:24 // Fixme thread 'main' panicked at 'attempt to subtract with overflow', src/device/etherdream.rs:144:24
let n_points = self.dac.buffer_capacity as u16 - self.stream.dac().dac.status.buffer_fullness as u16 - 1; let n_points = self.dac.buffer_capacity as usize - self.stream.dac().dac.status.buffer_fullness as usize - 1;
n_points n_points
} }
fn ping(&mut self) -> LJResult<()> {
Ok(self.stream.queue_commands().ping().submit()?)
}
} }
impl Device for EtherdreamDevice { impl Device for EtherdreamDevice {
@ -135,62 +145,72 @@ impl Device for EtherdreamDevice {
let now: DateTime<Utc> = now.into(); let now: DateTime<Utc> = now.into();
let now = now.to_rfc3339(); let now = now.to_rfc3339();
let status = Status { Status {
last_traced_at: now, last_traced_at: now,
properties: vec!["foo".to_string()], properties: vec!["foo".to_string()],
playback_state, playback_state,
capacity: self.points_capacity(), capacity: self.points_capacity(),
lack: self.dac_response.to_string(), lack: self.dac_response.to_string(),
}; }
// debug!("Dac Status: {:?} ", status ); // debug!("Dac Status: {:?} ", status );
// debug!("Etherdream Dac {:?} ", self.dac ); // debug!("Etherdream Dac {:?} ", self.dac );
debug!("Stream dac{:?}", self.stream.dac()); // debug!("Stream dac{:?}", self.stream.dac());
status // status
} }
fn draw(&mut self, fn draw(&mut self,
line: Vec<Point>, line: Vec<Point>,
_speed: u32, _speed: u32,
) -> LJResult<()> { ) -> LJResult<()> {
let n_points = self.points_capacity(); let chunk_size = 64;
// let n_points = &line.len(); let points_iter = line.into_iter();
debug!("Etherdream::device draw Generating {:?} points", n_points); for chunk in points_iter.as_slice().chunks(chunk_size){
debug!("New chunk length: {:?}", chunk.len());
loop {
match self.stream let capacity = self.points_capacity();
.queue_commands() if chunk.len() > capacity as usize {
.data( debug!("Sleep, capacity : {:?}", capacity);
line.into_iter() // Sleep for 1/100th of a sec
.map(|point| point.into()) sleep(Duration::new( 0, 10000000));
.take(n_points as usize) self.ping();
) } else {
// .data(sine_wave.by_ref().take(n_points as usize)) break;
}
.submit() {
Err(err) => {
// We should account for
// 'Broken pipe (os error 32)'
// Connection reset by peer (os error 104)
self.dac_response = match err {
CommunicationError::Io(err) => {
warn!("IO ERROR while drawing: '{}'",err);
DacResponse::ACK
}
CommunicationError::Protocol(err) => {
warn!("Protocol ERROR while drawing: '{}'",err);
DacResponse::ACK
}
CommunicationError::Response(err) => {
warn!("Response ERROR while drawing: '{}'",err);
err.response.response
}
};
} }
Ok(_) => { debug!("drawing");
self.dac_response = DacResponse::ACK; match self.stream
debug!("Draw is ok"); .queue_commands()
} .data(
}; chunk.into_iter()
.map(|point| (*point).into())
.take(chunk_size as usize)
)
.submit() {
Err(err) => {
// We should account for
// 'Broken pipe (os error 32)'
// Connection reset by peer (os error 104)
self.dac_response = match err {
CommunicationError::Io(err) => {
warn!("IO ERROR while drawing: '{}'",err);
DacResponse::ACK
}
CommunicationError::Protocol(err) => {
warn!("Protocol ERROR while drawing: '{}'",err);
DacResponse::ACK
}
CommunicationError::Response(err) => {
warn!("Response ERROR while drawing: '{}'",err);
err.response.response
}
};
}
Ok(_) => {
self.dac_response = DacResponse::ACK;
// debug!("Draw is ok");
}
};
}
Ok(()) Ok(())
} }
@ -274,7 +294,7 @@ impl Iterator for SineWave {
u1, u1,
u2, u2,
}; };
debug!("{:?}",p); // debug!("{:?}",p);
self.point += 1; self.point += 1;
Some(p) Some(p)
} }

View File

@ -57,7 +57,7 @@ impl Device for HeliosDevice {
last_traced_at: self.last_traced_at.clone(), last_traced_at: self.last_traced_at.clone(),
properties: vec!["foo".to_string()], properties: vec!["foo".to_string()],
playback_state: self.state, playback_state: self.state,
capacity: self.sent_points, capacity: self.sent_points as usize,
lack, lack,
} }
} }