From 514f4cc7c9183ba61e3d1602c1c304ded6039220 Mon Sep 17 00:00:00 2001 From: alban Date: Mon, 24 Jul 2023 17:35:15 +0200 Subject: [PATCH] fix: etherdream should work with device trait adjustments --- src/device.rs | 2 +- src/device/etherdream.rs | 116 +++++++++++++++++++++++---------------- src/device/helios.rs | 2 +- 3 files changed, 70 insertions(+), 50 deletions(-) diff --git a/src/device.rs b/src/device.rs index 6cbf0d5..cf83dba 100644 --- a/src/device.rs +++ b/src/device.rs @@ -42,7 +42,7 @@ pub struct Status { pub last_traced_at: String, pub properties: Vec, pub playback_state: PlaybackState, - pub capacity: u16, + pub capacity: usize, pub lack: String, } diff --git a/src/device/etherdream.rs b/src/device/etherdream.rs index e4ea6a6..b9e3cab 100644 --- a/src/device/etherdream.rs +++ b/src/device/etherdream.rs @@ -1,16 +1,20 @@ -use std::time; +#[warn(unused_imports)] +use log::{ debug, info, warn}; + use std::net::SocketAddr; +use std::thread::sleep; use ether_dream::dac::stream::{CommunicationError, connect}; use ether_dream::dac::{Playback, Stream}; use chrono::{DateTime, Utc}; -use std::time::SystemTime; +use std::time; +use std::time::{Duration, SystemTime}; use crate::conf::EtherDreamConf; use crate::device::{Device, Status, PlaybackState}; use crate::errors::{LJError, LJResult}; use crate::point::{Color, Point}; use ether_dream::protocol::{DacBroadcast, DacResponse}; -use log::{debug, info, warn}; + #[warn(dead_code)] pub struct EtherdreamDevice { @@ -114,14 +118,20 @@ impl EtherdreamDevice { Ok(stream) } - fn points_capacity(&self) -> u16 { + fn points_capacity(&self) -> usize { /*** 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 - 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 } + + fn ping(&mut self) -> LJResult<()> { + + Ok(self.stream.queue_commands().ping().submit()?) + + } } impl Device for EtherdreamDevice { @@ -135,62 +145,72 @@ impl Device for EtherdreamDevice { let now: DateTime = now.into(); let now = now.to_rfc3339(); - let status = Status { + Status { last_traced_at: now, properties: vec!["foo".to_string()], playback_state, capacity: self.points_capacity(), lack: self.dac_response.to_string(), - }; + } // debug!("Dac Status: {:?} ", status ); // debug!("Etherdream Dac {:?} ", self.dac ); - debug!("Stream dac{:?}", self.stream.dac()); - status + // debug!("Stream dac{:?}", self.stream.dac()); + // status } fn draw(&mut self, line: Vec, _speed: u32, ) -> LJResult<()> { - let n_points = self.points_capacity(); - // let n_points = &line.len(); - debug!("Etherdream::device draw Generating {:?} points", n_points); - - - match self.stream - .queue_commands() - .data( - line.into_iter() - .map(|point| point.into()) - .take(n_points as usize) - ) - // .data(sine_wave.by_ref().take(n_points 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 - } - }; + let chunk_size = 64; + let points_iter = line.into_iter(); + for chunk in points_iter.as_slice().chunks(chunk_size){ + debug!("New chunk length: {:?}", chunk.len()); + loop { + let capacity = self.points_capacity(); + if chunk.len() > capacity as usize { + debug!("Sleep, capacity : {:?}", capacity); + // Sleep for 1/100th of a sec + sleep(Duration::new( 0, 10000000)); + self.ping(); + } else { + break; + } } - Ok(_) => { - self.dac_response = DacResponse::ACK; - debug!("Draw is ok"); - } - }; + debug!("drawing"); + match self.stream + .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(()) } @@ -274,7 +294,7 @@ impl Iterator for SineWave { u1, u2, }; - debug!("{:?}",p); + // debug!("{:?}",p); self.point += 1; Some(p) } diff --git a/src/device/helios.rs b/src/device/helios.rs index 0971aeb..9106243 100644 --- a/src/device/helios.rs +++ b/src/device/helios.rs @@ -57,7 +57,7 @@ impl Device for HeliosDevice { last_traced_at: self.last_traced_at.clone(), properties: vec!["foo".to_string()], playback_state: self.state, - capacity: self.sent_points, + capacity: self.sent_points as usize, lack, } }