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 properties: Vec<String>,
pub playback_state: PlaybackState,
pub capacity: u16,
pub capacity: usize,
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::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<Utc> = 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<Point>,
_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)
}

View File

@ -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,
}
}