Files
kb-firmware/src/display.rs

85 lines
2.5 KiB
Rust

use embedded_graphics::{
mono_font::{MonoTextStyleBuilder, ascii::FONT_6X10},
pixelcolor::BinaryColor,
prelude::Point,
text::{Baseline, Text},
draw_target::DrawTarget,
Drawable,
};
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306, mode::BufferedGraphicsMode};
use embassy_rp::i2c::I2c;
use core::fmt::Write;
/// Display type encapsulating the I2C interface, its size and graphics mode
pub type Display = Ssd1306<
I2CInterface<I2c<'static, embassy_rp::peripherals::I2C0, embassy_rp::i2c::Async>>,
DisplaySize128x32,
BufferedGraphicsMode<DisplaySize128x32>
>;
/// Function that initializes the display with given settings
pub fn init_display(bus: I2c<'static, embassy_rp::peripherals::I2C0, embassy_rp::i2c::Async>) -> Option<Display> {
let interface = I2CDisplayInterface::new(bus);
let mut display = Ssd1306::new(
interface,
DisplaySize128x32,
DisplayRotation::Rotate180,
)
.into_buffered_graphics_mode();
match display.init() {
Ok(_) => Some(display),
Err(_) => None,
}
}
/// Temp function which updates the display with currently pressed keys
pub fn update_display(
display: &mut Option<Display>,
pressed_keys: &[(usize, usize, crate::keymap::KeyCode)],
) -> Option<()> {
if let Some(display) = display {
display.clear(BinaryColor::Off).ok()?;
let text_style = MonoTextStyleBuilder::new()
.font(&FONT_6X10)
.text_color(BinaryColor::On)
.build();
if pressed_keys.is_empty() {
Text::with_baseline("No keys", Point::zero(), text_style, Baseline::Top)
.draw(display)
.ok()?;
} else {
let mut y_pos = 0;
for (row_idx, col_idx, keycode) in pressed_keys.iter().take(3) {
let mut msg = heapless::String::<32>::new();
write!(&mut msg, "R{}C{}: 0x{:02X}", row_idx, col_idx, keycode.as_u8()).ok();
Text::with_baseline(&msg, Point::new(0, y_pos), text_style, Baseline::Top)
.draw(display)
.ok()?;
y_pos += 10;
}
}
display.flush().ok()?;
Some(())
} else {
None
}
}
/// Helper function to clear the display
pub fn clear_display(
display: &mut Option<Display>,
) -> Option<()> {
if let Some(display) = display {
display.clear(BinaryColor::Off).ok()?;
display.flush().ok()?;
Some(())
} else {
None
}
}