use crate::command::{Command, DisplayResolution};
use crate::display::{self, Dimensions, Rotation};
pub struct Builder {
power_setting: Command,
booster_soft_start: Command,
panel_setting: Command,
pll: Command,
dimensions: Option<Dimensions>,
rotation: Rotation,
}
#[derive(Debug)]
pub struct BuilderError {}
pub struct Config {
pub(crate) power_setting: Command,
pub(crate) booster_soft_start: Command,
pub(crate) panel_setting: Command,
pub(crate) pll: Command,
pub(crate) dimensions: Dimensions,
pub(crate) rotation: Rotation,
}
impl Default for Builder {
fn default() -> Self {
Builder {
power_setting: Command::PowerSetting(0x2b, 0x2b, 0x9),
booster_soft_start: Command::BoosterSoftStart(0x17, 0x17, 0x17),
panel_setting: Command::PanelSetting(DisplayResolution::R160x296), pll: Command::PLLControl(0x29), dimensions: None,
rotation: Rotation::default(),
}
}
}
impl Builder {
pub fn new() -> Self {
Self::default()
}
pub fn panel_setting(self, res: DisplayResolution) -> Self {
Self {
panel_setting: Command::PanelSetting(res),
..self
}
}
pub fn power_setting(self, vdh: u8, vdl: u8, vdhr: u8) -> Self {
Self {
power_setting: Command::PowerSetting(vdh, vdl, vdhr),
..self
}
}
pub fn booster_soft_start(self, vhh: u8, vhl: u8, vhgl: u8) -> Self {
Self {
booster_soft_start: Command::BoosterSoftStart(vhh, vhl, vhgl),
..self
}
}
pub fn pll(self, value: u8) -> Self {
Self {
pll: Command::PLLControl(value),
..self
}
}
pub fn dimensions(self, dimensions: Dimensions) -> Self {
assert!(
dimensions.cols % 4 == 0,
"cols must be evenly divisible by 4"
);
assert!(
dimensions.rows <= display::MAX_GATE_OUTPUTS, "rows must be less than MAX_GATE_OUTPUTS"
);
assert!(
dimensions.cols <= display::MAX_SOURCE_OUTPUTS, "columns must be less than MAX_SOURCE_OUTPUTS"
);
Self {
dimensions: Some(dimensions),
..self
}
}
pub fn rotation(self, rotation: Rotation) -> Self {
Self { rotation, ..self }
}
pub fn build(self) -> Result<Config, BuilderError> {
Ok(Config {
power_setting: self.power_setting,
booster_soft_start: self.booster_soft_start,
panel_setting: self.panel_setting,
pll: self.pll,
dimensions: self.dimensions.ok_or(BuilderError {})?,
rotation: self.rotation,
})
}
}