use core::time::Duration;
use spiritlevel::procedure::{self, TmdsIo};
use crate::{
BringUpConfig, BringUpError, BringUpOutcome, Cancel, Clock, Delay, NoCancel, ReconnectCheck,
RestoreError, Restored, ScramblingStatus, SinkPresence, SinkTmdsConfig, SinkTmdsFacts,
TearDownConfig, TearDownError, Timestamp, TmdsConfigReading, TmdsEvent, TmdsLinkParams,
TmdsPhy, TmdsPhyConfig, TmdsScdc, TmdsVideo, TornDown, UpdateError,
};
type BringUpResult<S, P> = Result<
BringUpOutcome<<S as TmdsScdc>::Error, <P as TmdsPhy>::Error>,
BringUpError<<S as TmdsScdc>::Error, <P as TmdsPhy>::Error>,
>;
type RestoreResult<S, P> = Result<
Restored<<S as TmdsScdc>::Error>,
RestoreError<<S as TmdsScdc>::Error, <P as TmdsPhy>::Error>,
>;
type TearDownResult<S, P> = Result<
TornDown<<S as TmdsScdc>::Error>,
TearDownError<<S as TmdsScdc>::Error, <P as TmdsPhy>::Error>,
>;
#[cfg(test)]
mod tests;
#[derive(Debug)]
pub struct TmdsLink<S, P, K, D, X = NoCancel> {
scdc: S,
phy: P,
clock: K,
delay: D,
cancel: X,
}
impl<S: TmdsScdc, P: TmdsPhy, K: Clock, D: Delay> TmdsLink<S, P, K, D, NoCancel> {
pub fn new(scdc: S, phy: P, clock: K, delay: D) -> Self {
Self {
scdc,
phy,
clock,
delay,
cancel: NoCancel,
}
}
}
impl<S: TmdsScdc, P: TmdsPhy, K: Clock, D: Delay, X: Cancel> TmdsLink<S, P, K, D, X> {
pub fn with_cancel<X2: Cancel>(self, cancel: X2) -> TmdsLink<S, P, K, D, X2> {
TmdsLink {
scdc: self.scdc,
phy: self.phy,
clock: self.clock,
delay: self.delay,
cancel,
}
}
pub fn into_parts(self) -> (S, P, K, D, X) {
(self.scdc, self.phy, self.clock, self.delay, self.cancel)
}
pub fn scdc(&self) -> &S {
&self.scdc
}
pub fn scdc_mut(&mut self) -> &mut S {
&mut self.scdc
}
pub fn phy(&self) -> &P {
&self.phy
}
pub fn phy_mut(&mut self) -> &mut P {
&mut self.phy
}
pub fn clock(&self) -> &K {
&self.clock
}
pub async fn tear_down(
&mut self,
link: &TmdsLinkParams,
config: &TearDownConfig,
presence: SinkPresence,
) -> TearDownResult<S, P> {
self.tear_down_with_events(link, config, presence, &mut |_| {})
.await
}
pub async fn tear_down_with_events(
&mut self,
link: &TmdsLinkParams,
config: &TearDownConfig,
presence: SinkPresence,
record: &mut impl FnMut(TmdsEvent),
) -> TearDownResult<S, P> {
procedure::tear_down(&mut AsyncIo(self), link, config, presence, record).await
}
pub async fn bring_up(
&mut self,
link: &TmdsLinkParams,
video: TmdsVideo,
config: &BringUpConfig,
) -> BringUpResult<S, P> {
self.bring_up_with_events(link, video, config, &mut |_| {})
.await
}
pub async fn bring_up_with_events(
&mut self,
link: &TmdsLinkParams,
video: TmdsVideo,
config: &BringUpConfig,
record: &mut impl FnMut(TmdsEvent),
) -> BringUpResult<S, P> {
procedure::bring_up(&mut AsyncIo(self), link, video, config, record).await
}
pub async fn check_reconnect(
&mut self,
link: &TmdsLinkParams,
current_sink: SinkTmdsFacts,
) -> ReconnectCheck<S::Error> {
self.check_reconnect_with_events(link, current_sink, &mut |_| {})
.await
}
pub async fn check_reconnect_with_events(
&mut self,
link: &TmdsLinkParams,
current_sink: SinkTmdsFacts,
record: &mut impl FnMut(TmdsEvent),
) -> ReconnectCheck<S::Error> {
procedure::check_reconnect(&mut AsyncIo(self), link, current_sink, record).await
}
pub async fn quick_restore(
&mut self,
link: &TmdsLinkParams,
config: &BringUpConfig,
) -> RestoreResult<S, P> {
self.quick_restore_with_events(link, config, &mut |_| {})
.await
}
pub async fn quick_restore_with_events(
&mut self,
link: &TmdsLinkParams,
config: &BringUpConfig,
record: &mut impl FnMut(TmdsEvent),
) -> RestoreResult<S, P> {
procedure::quick_restore(&mut AsyncIo(self), link, config, record).await
}
}
struct AsyncIo<'a, S, P, K, D, X>(&'a mut TmdsLink<S, P, K, D, X>);
impl<S: TmdsScdc, P: TmdsPhy, K: Clock, D: Delay, X: Cancel> TmdsIo for AsyncIo<'_, S, P, K, D, X> {
type ScdcError = S::Error;
type PhyError = P::Error;
async fn write_tmds_config(&mut self, config: SinkTmdsConfig) -> Result<(), S::Error> {
self.0.scdc.write_tmds_config(config).await
}
async fn update_tmds_config(
&mut self,
config: SinkTmdsConfig,
) -> Result<(), UpdateError<S::Error>> {
self.0.scdc.update_tmds_config(config).await
}
async fn read_tmds_config(&mut self) -> Result<TmdsConfigReading, S::Error> {
self.0.scdc.read_tmds_config().await
}
async fn read_scrambling_status(&mut self) -> Result<ScramblingStatus, S::Error> {
self.0.scdc.read_scrambling_status().await
}
async fn read_sink_version(&mut self) -> Result<u8, S::Error> {
self.0.scdc.read_sink_version().await
}
async fn write_source_version(&mut self, version: u8) -> Result<(), S::Error> {
self.0.scdc.write_source_version(version).await
}
async fn clear_frl_rate(&mut self) -> Result<(), S::Error> {
self.0.scdc.clear_frl_rate().await
}
async fn clear_flt_update(&mut self) -> Result<bool, UpdateError<S::Error>> {
self.0.scdc.clear_flt_update().await
}
async fn configure(&mut self, config: TmdsPhyConfig) -> Result<(), P::Error> {
self.0.phy.configure(config).await
}
async fn set_scrambling(&mut self, enabled: bool) -> Result<(), P::Error> {
self.0.phy.set_scrambling(enabled).await
}
async fn start_output(&mut self) -> Result<(), P::Error> {
self.0.phy.start_output().await
}
async fn stop_output(&mut self) -> Result<(), P::Error> {
self.0.phy.stop_output().await
}
fn now(&self) -> Timestamp {
self.0.clock.now()
}
async fn delay(&mut self, duration: Duration) {
self.0.delay.delay(duration).await;
}
fn cancelled(&self) -> bool {
self.0.cancel.is_cancelled()
}
}