-
Notifications
You must be signed in to change notification settings - Fork 215
Improve Logging #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Improve Logging #314
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b0c5b4e
Separate the main logger functionality from the framebuffer section o…
asensio-project c40c19f
Check formatting
asensio-project ab93f1b
Support Serial Being logger
asensio-project 34e50ca
Add configuration options
asensio-project 008a382
Apply suggestions
asensio-project 7475960
Fix typo
phil-opp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ fn main() { | |
(97, 9), | ||
(106, 9), | ||
(115, 1), | ||
(116, 1), | ||
(117, 1), | ||
]; | ||
|
||
let mut code = String::new(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
use bootloader_api::info::{FrameBufferInfo, PixelFormat}; | ||
use core::{fmt, ptr}; | ||
use font_constants::BACKUP_CHAR; | ||
use noto_sans_mono_bitmap::{ | ||
get_raster, get_raster_width, FontWeight, RasterHeight, RasterizedChar, | ||
}; | ||
|
||
/// Additional vertical space between lines | ||
const LINE_SPACING: usize = 2; | ||
/// Additional horizontal space between characters. | ||
const LETTER_SPACING: usize = 0; | ||
|
||
/// Padding from the border. Prevent that font is too close to border. | ||
const BORDER_PADDING: usize = 1; | ||
|
||
/// Constants for the usage of the [`noto_sans_mono_bitmap`] crate. | ||
mod font_constants { | ||
use super::*; | ||
|
||
/// Height of each char raster. The font size is ~0.84% of this. Thus, this is the line height that | ||
/// enables multiple characters to be side-by-side and appear optically in one line in a natural way. | ||
pub const CHAR_RASTER_HEIGHT: RasterHeight = RasterHeight::Size16; | ||
|
||
/// The width of each single symbol of the mono space font. | ||
pub const CHAR_RASTER_WIDTH: usize = get_raster_width(FontWeight::Regular, CHAR_RASTER_HEIGHT); | ||
|
||
/// Backup character if a desired symbol is not available by the font. | ||
/// The '�' character requires the feature "unicode-specials". | ||
pub const BACKUP_CHAR: char = '�'; | ||
|
||
pub const FONT_WEIGHT: FontWeight = FontWeight::Regular; | ||
} | ||
|
||
/// Returns the raster of the given char or the raster of [`font_constants::BACKUP_CHAR`]. | ||
fn get_char_raster(c: char) -> RasterizedChar { | ||
fn get(c: char) -> Option<RasterizedChar> { | ||
get_raster( | ||
c, | ||
font_constants::FONT_WEIGHT, | ||
font_constants::CHAR_RASTER_HEIGHT, | ||
) | ||
} | ||
get(c).unwrap_or_else(|| get(BACKUP_CHAR).expect("Should get raster of backup char.")) | ||
} | ||
|
||
/// Allows logging text to a pixel-based framebuffer. | ||
pub struct FrameBufferWriter { | ||
framebuffer: &'static mut [u8], | ||
info: FrameBufferInfo, | ||
x_pos: usize, | ||
y_pos: usize, | ||
} | ||
|
||
impl FrameBufferWriter { | ||
/// Creates a new logger that uses the given framebuffer. | ||
pub fn new(framebuffer: &'static mut [u8], info: FrameBufferInfo) -> Self { | ||
let mut logger = Self { | ||
framebuffer, | ||
info, | ||
x_pos: 0, | ||
y_pos: 0, | ||
}; | ||
logger.clear(); | ||
logger | ||
} | ||
|
||
fn newline(&mut self) { | ||
self.y_pos += font_constants::CHAR_RASTER_HEIGHT.val() + LINE_SPACING; | ||
self.carriage_return() | ||
} | ||
|
||
fn carriage_return(&mut self) { | ||
self.x_pos = BORDER_PADDING; | ||
} | ||
|
||
/// Erases all text on the screen. Resets `self.x_pos` and `self.y_pos`. | ||
pub fn clear(&mut self) { | ||
self.x_pos = BORDER_PADDING; | ||
self.y_pos = BORDER_PADDING; | ||
self.framebuffer.fill(0); | ||
} | ||
|
||
fn width(&self) -> usize { | ||
self.info.width | ||
} | ||
|
||
fn height(&self) -> usize { | ||
self.info.height | ||
} | ||
|
||
/// Writes a single char to the framebuffer. Takes care of special control characters, such as | ||
/// newlines and carriage returns. | ||
fn write_char(&mut self, c: char) { | ||
match c { | ||
'\n' => self.newline(), | ||
'\r' => self.carriage_return(), | ||
c => { | ||
let new_xpos = self.x_pos + font_constants::CHAR_RASTER_WIDTH; | ||
if new_xpos >= self.width() { | ||
self.newline(); | ||
} | ||
let new_ypos = | ||
self.y_pos + font_constants::CHAR_RASTER_HEIGHT.val() + BORDER_PADDING; | ||
if new_ypos >= self.height() { | ||
self.clear(); | ||
} | ||
self.write_rendered_char(get_char_raster(c)); | ||
} | ||
} | ||
} | ||
|
||
/// Prints a rendered char into the framebuffer. | ||
/// Updates `self.x_pos`. | ||
fn write_rendered_char(&mut self, rendered_char: RasterizedChar) { | ||
for (y, row) in rendered_char.raster().iter().enumerate() { | ||
for (x, byte) in row.iter().enumerate() { | ||
self.write_pixel(self.x_pos + x, self.y_pos + y, *byte); | ||
} | ||
} | ||
self.x_pos += rendered_char.width() + LETTER_SPACING; | ||
} | ||
|
||
fn write_pixel(&mut self, x: usize, y: usize, intensity: u8) { | ||
let pixel_offset = y * self.info.stride + x; | ||
let color = match self.info.pixel_format { | ||
PixelFormat::Rgb => [intensity, intensity, intensity / 2, 0], | ||
PixelFormat::Bgr => [intensity / 2, intensity, intensity, 0], | ||
PixelFormat::U8 => [if intensity > 200 { 0xf } else { 0 }, 0, 0, 0], | ||
other => { | ||
// set a supported (but invalid) pixel format before panicking to avoid a double | ||
// panic; it might not be readable though | ||
self.info.pixel_format = PixelFormat::Rgb; | ||
panic!("pixel format {:?} not supported in logger", other) | ||
} | ||
}; | ||
let bytes_per_pixel = self.info.bytes_per_pixel; | ||
let byte_offset = pixel_offset * bytes_per_pixel; | ||
self.framebuffer[byte_offset..(byte_offset + bytes_per_pixel)] | ||
.copy_from_slice(&color[..bytes_per_pixel]); | ||
let _ = unsafe { ptr::read_volatile(&self.framebuffer[byte_offset]) }; | ||
} | ||
} | ||
|
||
unsafe impl Send for FrameBufferWriter {} | ||
unsafe impl Sync for FrameBufferWriter {} | ||
|
||
impl fmt::Write for FrameBufferWriter { | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
for c in s.chars() { | ||
self.write_char(c); | ||
} | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.