use display_interface::{AsyncWriteOnlyDataCommand, DataFormat, DisplayError};
#[derive(Debug)]
#[allow(dead_code)]
pub enum Command {
AddressMode(bool),
Contrast(u8),
AllOn(bool),
Invert(bool),
DisplayResolution(u8),
DisplayOn(bool),
ColumnAddressLow(u8),
ColumnAddressHigh(u8),
PageAddress(u8),
LargePageAddress(u8),
StartLine(u8),
SegmentRemap(bool),
Multiplex(u8),
ReverseComDir(bool),
DisplayOffset(u8),
ComPinConfig(bool),
DisplayClockDiv(u8, u8),
PreChargePeriod(u8, u8),
VcomhDeselect(VcomhLevel),
Noop,
ChargePump(bool),
}
impl Command {
pub async fn send<DI>(self, iface: &mut DI) -> Result<(), DisplayError>
where
DI: AsyncWriteOnlyDataCommand,
{
let (data, len) = match self {
Command::AddressMode(mode) => ([0x20 | (mode as u8), 0, 0, 0, 0, 0, 0], 1),
Command::Contrast(val) => ([0x81, val, 0, 0, 0, 0, 0], 2),
Command::AllOn(on) => ([0xA4 | (on as u8), 0, 0, 0, 0, 0, 0], 1),
Command::Invert(inv) => ([0xA6 | (inv as u8), 0, 0, 0, 0, 0, 0], 1),
Command::DisplayResolution(resolution) => ([0xA9, resolution, 0, 0, 0, 0, 0], 2),
Command::DisplayOn(on) => ([0xAE | (on as u8), 0, 0, 0, 0, 0, 0], 1),
Command::ColumnAddressLow(addr) => ([0xF & addr, 0, 0, 0, 0, 0, 0], 1),
Command::ColumnAddressHigh(addr) => ([0x10 | (0xF & addr), 0, 0, 0, 0, 0, 0], 1),
Command::PageAddress(page) => ([0xB0 | (page), 0, 0, 0, 0, 0, 0], 1),
Command::LargePageAddress(page) => ([0xB0, page, 0, 0, 0, 0, 0], 2),
Command::StartLine(line) => ([0x40 | (0x3F & line), 0, 0, 0, 0, 0, 0], 1),
Command::SegmentRemap(remap) => ([0xA0 | (remap as u8), 0, 0, 0, 0, 0, 0], 1),
Command::Multiplex(ratio) => ([0xA8, ratio, 0, 0, 0, 0, 0], 2),
Command::ReverseComDir(rev) => ([0xC0 | ((rev as u8) << 3), 0, 0, 0, 0, 0, 0], 1),
Command::DisplayOffset(offset) => ([0xD3, offset, 0, 0, 0, 0, 0], 2),
Command::ComPinConfig(alt) => ([0xDA, 0x02 | ((alt as u8) << 4), 0, 0, 0, 0, 0], 2),
Command::DisplayClockDiv(fosc, div) => {
([0xD5, ((0xF & fosc) << 4) | (0xF & div), 0, 0, 0, 0, 0], 2)
}
Command::PreChargePeriod(phase1, phase2) => (
[0xD9, ((0xF & phase2) << 4) | (0xF & phase1), 0, 0, 0, 0, 0],
2,
),
Command::VcomhDeselect(level) => ([0xDB, (level as u8) << 4, 0, 0, 0, 0, 0], 2),
Command::Noop => ([0xE3, 0, 0, 0, 0, 0, 0], 1),
Command::ChargePump(en) => ([0xAD, 0x8A | (en as u8), 0, 0, 0, 0, 0], 2),
};
iface.send_commands(DataFormat::U8(&data[0..len])).await
}
}
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub enum NFrames {
F2 = 0b111,
F3 = 0b100,
F4 = 0b101,
F5 = 0b000,
F25 = 0b110,
F64 = 0b001,
F128 = 0b010,
F256 = 0b011,
}
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub enum VcomhLevel {
V065 = 0b001,
V077 = 0b010,
V083 = 0b011,
Auto = 0b100,
}