#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ClockRatio {
Div10,
Div40,
}
#[non_exhaustive]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ScramblingThreshold {
#[default]
Above340,
AtOrAbove340,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SinkTmdsFacts {
pub scdc_present: bool,
pub scrambling_supported: bool,
pub lte_340mcsc_scramble: bool,
}
impl SinkTmdsFacts {
pub const NONE: Self = Self::new(false, false, false);
#[must_use]
pub const fn new(
scdc_present: bool,
scrambling_supported: bool,
lte_340mcsc_scramble: bool,
) -> Self {
Self {
scdc_present,
scrambling_supported,
lte_340mcsc_scramble,
}
}
}
#[cfg(feature = "display-types")]
impl From<&display_types::cea861::HdmiForumSinkCap> for SinkTmdsFacts {
fn from(cap: &display_types::cea861::HdmiForumSinkCap) -> Self {
Self::new(
cap.scdc_present,
cap.scdc_present && cap.max_tmds_rate_mhz != 0,
cap.lte_340mcsc_scramble,
)
}
}
#[cfg(feature = "display-types")]
impl From<Option<&display_types::cea861::HdmiForumSinkCap>> for SinkTmdsFacts {
fn from(cap: Option<&display_types::cea861::HdmiForumSinkCap>) -> Self {
cap.map_or(Self::NONE, Self::from)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SourceTmdsFacts {
pub low_rate_scrambling: bool,
}
impl SourceTmdsFacts {
#[must_use]
pub const fn new(low_rate_scrambling: bool) -> Self {
Self {
low_rate_scrambling,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TmdsPolicy {
pub low_rate_scrambling: bool,
pub threshold: ScramblingThreshold,
}
impl TmdsPolicy {
pub const DEFAULT: Self = Self {
low_rate_scrambling: true,
threshold: ScramblingThreshold::Above340,
};
#[must_use]
pub const fn with_low_rate_scrambling(self, low_rate_scrambling: bool) -> Self {
Self {
low_rate_scrambling,
..self
}
}
#[must_use]
pub const fn with_threshold(self, threshold: ScramblingThreshold) -> Self {
Self { threshold, ..self }
}
}
impl Default for TmdsPolicy {
fn default() -> Self {
Self::DEFAULT
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SinkTmdsConfig {
pub scrambling_enable: bool,
pub high_tmds_clock_ratio: bool,
}
impl SinkTmdsConfig {
#[must_use]
pub const fn new(scrambling_enable: bool, high_tmds_clock_ratio: bool) -> Self {
Self {
scrambling_enable,
high_tmds_clock_ratio,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sink_facts_none_is_all_clear() {
assert_eq!(SinkTmdsFacts::NONE, SinkTmdsFacts::new(false, false, false));
}
#[test]
fn sink_facts_new_keeps_field_order() {
let f = SinkTmdsFacts::new(true, false, true);
assert!(f.scdc_present);
assert!(!f.scrambling_supported);
assert!(f.lte_340mcsc_scramble);
}
#[test]
fn policy_default_matches_const() {
assert_eq!(TmdsPolicy::default(), TmdsPolicy::DEFAULT);
const { assert!(TmdsPolicy::DEFAULT.low_rate_scrambling) };
assert_eq!(TmdsPolicy::DEFAULT.threshold, ScramblingThreshold::Above340);
assert_eq!(
ScramblingThreshold::default(),
ScramblingThreshold::Above340
);
}
#[test]
fn policy_builders_change_one_field() {
let p = TmdsPolicy::DEFAULT.with_low_rate_scrambling(false);
assert!(!p.low_rate_scrambling);
assert_eq!(p.threshold, ScramblingThreshold::Above340);
let p = TmdsPolicy::DEFAULT.with_threshold(ScramblingThreshold::AtOrAbove340);
assert!(p.low_rate_scrambling);
assert_eq!(p.threshold, ScramblingThreshold::AtOrAbove340);
}
#[test]
fn source_facts_and_sink_config_new() {
assert!(SourceTmdsFacts::new(true).low_rate_scrambling);
let c = SinkTmdsConfig::new(true, false);
assert!(c.scrambling_enable);
assert!(!c.high_tmds_clock_ratio);
}
#[test]
fn constructors_are_const() {
const SINK: SinkTmdsFacts = SinkTmdsFacts::new(true, true, true);
const SOURCE: SourceTmdsFacts = SourceTmdsFacts::new(false);
const POLICY: TmdsPolicy = TmdsPolicy::DEFAULT
.with_low_rate_scrambling(false)
.with_threshold(ScramblingThreshold::AtOrAbove340);
const CONFIG: SinkTmdsConfig = SinkTmdsConfig::new(false, true);
const { assert!(SINK.scdc_present && !SOURCE.low_rate_scrambling) };
const { assert!(!POLICY.low_rate_scrambling && CONFIG.high_tmds_clock_ratio) };
}
#[cfg(feature = "display-types")]
mod from_display_types {
use super::*;
use display_types::cea861::{HdmiForumFrl, HdmiForumSinkCap};
fn cap(scdc: bool, max_mhz: u16, lte: bool) -> HdmiForumSinkCap {
HdmiForumSinkCap::new(
1,
max_mhz,
scdc,
false,
false,
false,
lte,
false,
false,
false,
HdmiForumFrl::NotSupported,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
None,
None,
None,
)
}
#[test]
fn every_combination() {
let table = [
((false, 0, false), (false, false, false)),
((false, 0, true), (false, false, true)),
((false, 600, false), (false, false, false)),
((false, 600, true), (false, false, true)),
((true, 0, false), (true, false, false)),
((true, 0, true), (true, false, true)),
((true, 600, false), (true, true, false)),
((true, 600, true), (true, true, true)),
];
for ((scdc, max_mhz, lte), (f_scdc, f_supported, f_lte)) in table {
assert_eq!(
SinkTmdsFacts::from(&cap(scdc, max_mhz, lte)),
SinkTmdsFacts::new(f_scdc, f_supported, f_lte),
"{scdc} {max_mhz} {lte}"
);
}
}
#[test]
fn absent_block_is_none() {
assert_eq!(SinkTmdsFacts::from(None), SinkTmdsFacts::NONE);
let c = cap(true, 600, true);
assert_eq!(
SinkTmdsFacts::from(Some(&c)),
SinkTmdsFacts::new(true, true, true)
);
}
}
}