Skip to content

Commit

Permalink
Merge branch 'font' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jackra1n committed Nov 21, 2023
2 parents f645704 + 7ad41b6 commit 65d9905
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 30 deletions.
Binary file added data/pcsenior.raw
Binary file not shown.
Binary file modified docs/IMG_3890.webp
Binary file not shown.
44 changes: 14 additions & 30 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
use crate::display_types::{Display, PCSENIOR8_STYLE, FONT_6X12, FONT_5X8, PROFONT12};
use linux_embedded_hal::I2cdev;
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306, mode::BufferedGraphicsMode};
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
use display_interface::DisplayError;
use embedded_graphics::{
mono_font::{ascii, MonoTextStyleBuilder, MonoTextStyle},
pixelcolor::BinaryColor,
prelude::*,
text::Text
};
use profont::PROFONT_12_POINT;

const PROFONT12: MonoTextStyle<'_, BinaryColor> = MonoTextStyleBuilder::new()
.font(&PROFONT_12_POINT)
.text_color(BinaryColor::On)
.build();

const FONT_6X12: MonoTextStyle<'_, BinaryColor> = MonoTextStyleBuilder::new()
.font(&ascii::FONT_6X12)
.text_color(BinaryColor::On)
.build();

const FONT_5X8: MonoTextStyle<'_, BinaryColor> = MonoTextStyleBuilder::new()
.font(&ascii::FONT_5X8)
.text_color(BinaryColor::On)
.build();

type Display = Ssd1306<I2CInterface<I2cdev>, DisplaySize128x32, BufferedGraphicsMode<DisplaySize128x32>>;

pub struct PoeDisplay {
display: Display
Expand All @@ -47,44 +30,45 @@ impl PoeDisplay {
) -> Result<(), DisplayError> {
let disp = &mut self.display;

let y_offset = 8;
let y_offset = 7;
let display_width = 128;
let char_width: i32 = 8;
let x_margin = Point::new(2, 0).x_axis();
let y_margin = Point::new(0, 1).y_axis();

disp.clear(BinaryColor::Off)?;

// top center: ip address
let ip_width = ip_address.len() as i32 * char_width;
let ip_x_position = (display_width - ip_width) / 2;
Text::new(ip_address, Point::new(ip_x_position, y_offset), PROFONT12).draw(disp)?;
Text::new(ip_address, Point::new(ip_x_position, y_offset), PCSENIOR8_STYLE).draw(disp)?;

// middle left: cpu usage
let cpu_width = cpu_usage.len() as i32 * char_width;
let cpu_point = Point::new(34 - cpu_width, 12 + y_offset);
let next = Text::new(&cpu_usage, cpu_point, PROFONT12).draw(disp)?;
let next = Text::new("%", next, FONT_6X12).draw(disp)?;
let next = Text::new(&cpu_usage, cpu_point, PCSENIOR8_STYLE).draw(disp)?;
let next = Text::new("%", next + y_margin, FONT_6X12).draw(disp)?;
Text::new("CPU", next + x_margin, FONT_5X8).draw(disp)?;

// bottom left: ram usage
let ram_width = ram_usage.len() as i32 * char_width;
let ram_point = Point::new(34 - ram_width, 23 + y_offset);
let next = Text::new(&ram_usage, ram_point, PROFONT12).draw(disp)?;
let next = Text::new("%", next, FONT_6X12).draw(disp)?;
let next = Text::new(&ram_usage, ram_point, PCSENIOR8_STYLE).draw(disp)?;
let next = Text::new("%", next + y_margin, FONT_6X12).draw(disp)?;
Text::new("RAM", next + x_margin, FONT_5X8).draw(disp)?;

// middle right: temp
let temp_width = temp.len() as i32 * char_width;
let temp_point = Point::new(99 - temp_width, 12 + y_offset);
let next = Text::new(&temp, temp_point, PROFONT12).draw(disp)?;
let next = Text::new("°", next, PROFONT12).draw(disp)?;
Text::new("C", next, PROFONT12).draw(disp)?;
let next = Text::new(&temp, temp_point, PCSENIOR8_STYLE).draw(disp)?;
let next = Text::new("°", next + Point::new(0, 3), PROFONT12).draw(disp)?;
Text::new("C", next - Point::new(0, 2), PCSENIOR8_STYLE).draw(disp)?;

// bottom right: disk usage
let disk_width = disk_usage.len() as i32 * char_width;
let disk_point = Point::new(99 - disk_width, 23 + y_offset);
let next = Text::new(disk_usage, disk_point, PROFONT12).draw(disp)?;
let next = Text::new("%", next, FONT_6X12).draw(disp)?;
let next = Text::new(disk_usage, disk_point, PCSENIOR8_STYLE).draw(disp)?;
let next = Text::new("%", next + y_margin, FONT_6X12).draw(disp)?;
Text::new("DISK", next + x_margin, FONT_5X8).draw(disp)?;

disp.flush()?;
Expand Down
48 changes: 48 additions & 0 deletions src/display_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use linux_embedded_hal::I2cdev;
use ssd1306::{prelude::*, Ssd1306, mode::BufferedGraphicsMode};
use embedded_graphics::{
image::ImageRaw,
mono_font::{ascii, MonoTextStyleBuilder, MonoFont, MonoTextStyle, DecorationDimensions, mapping::StrGlyphMapping},
pixelcolor::BinaryColor,
prelude::*
};
use profont::PROFONT_12_POINT;


pub type Display = Ssd1306<I2CInterface<I2cdev>, DisplaySize128x32, BufferedGraphicsMode<DisplaySize128x32>>;


pub const PROFONT12: MonoTextStyle<'_, BinaryColor> = MonoTextStyleBuilder::new()
.font(&PROFONT_12_POINT)
.text_color(BinaryColor::On)
.build();

pub const FONT_6X12: MonoTextStyle<'_, BinaryColor> = MonoTextStyleBuilder::new()
.font(&ascii::FONT_6X12)
.text_color(BinaryColor::On)
.build();

pub const FONT_5X8: MonoTextStyle<'_, BinaryColor> = MonoTextStyleBuilder::new()
.font(&ascii::FONT_5X8)
.text_color(BinaryColor::On)
.build();

pub const GLYPH_MAPPING: StrGlyphMapping = StrGlyphMapping::new(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~°", '?' as usize - ' ' as usize);

pub const PCSENIOR8: MonoFont = MonoFont {
image: ImageRaw::new(
include_bytes!("../data/pcsenior.raw"),
128,
),
character_size: Size::new(8, 10),
character_spacing: 0,
baseline: 7,
underline: DecorationDimensions::new(9, 1),
strikethrough: DecorationDimensions::new(10 / 2, 1),
glyph_mapping: &GLYPH_MAPPING,
};

pub const PCSENIOR8_STYLE: MonoTextStyle<'_, BinaryColor> = MonoTextStyleBuilder::new()
.font(&PCSENIOR8)
.text_color(BinaryColor::On)
.build();
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use fan_controller::FanController;
mod display;
use display::PoeDisplay;

mod display_types;

fn main() -> Result<(), Box<dyn Error>> {
let mut poe_disp = PoeDisplay::new()?;
Expand Down

0 comments on commit 65d9905

Please sign in to comment.