diff options
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | examples/width_height.rs | 12 | ||||
-rw-r--r-- | src/lib.rs | 27 | ||||
-rw-r--r-- | src/of_node.rs | 24 |
4 files changed, 64 insertions, 0 deletions
@@ -7,3 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +byteorder = "1.*" diff --git a/examples/width_height.rs b/examples/width_height.rs new file mode 100644 index 0000000..77bb5da --- /dev/null +++ b/examples/width_height.rs @@ -0,0 +1,12 @@ +use charlcd::Screen; +use std::io::Write; + +fn main() -> std::io::Result<()> { + let mut screen = Screen::default()?; + + screen.clear()?; + screen.write(format!("width: {}\n", screen.width()?).as_bytes())?; + screen.write(format!("height: {}\n", screen.height()?).as_bytes())?; + + Ok(()) +} @@ -1,4 +1,5 @@ mod codes; +mod of_node; use std::fs::{File, OpenOptions}; use std::path::Path; @@ -198,4 +199,30 @@ impl FileScreen { pub fn default() -> std::io::Result<FileScreen> { Screen::from_dev_path(&Path::new(DEFAULT_SCREEN_DEV_PATH)) } + + /// Get the width of the screen, in number of characters it can display. + /// + /// **Important note:** The implementation behind this function is + /// currently a hack that will go find the value in the `auxdisplay` + /// platform device tree node in + /// `/sys/devices/platform/auxdisplay/of_node/*`. This is because the + /// `charlcd` driver does not export the screen width nor height to + /// userspace (could be using `ioctl` or `read` syscalls). + /// + pub fn width(&self) -> std::io::Result<u32> { + of_node::display_width_chars() + } + + /// Get the height of the screen, in number of characters it can display. + /// + /// **Important note:** The implementation behind this function is + /// currently a hack that will go find the value in the `auxdisplay` + /// platform device tree node in + /// `/sys/devices/platform/auxdisplay/of_node/*`. This is because the + /// `charlcd` driver does not export the screen width nor height to + /// userspace (could be using `ioctl` or `read` syscalls). + /// + pub fn height(&self) -> std::io::Result<u32> { + of_node::display_height_chars() + } } diff --git a/src/of_node.rs b/src/of_node.rs new file mode 100644 index 0000000..5dff2a0 --- /dev/null +++ b/src/of_node.rs @@ -0,0 +1,24 @@ +// This module is a hack to read additional information in the DT nodes in the +// kernel to retrieve data that is currently not exposed in the charlcd.c +// driver. + +use std::fs::File; +use std::io::Result; + +use byteorder::{BigEndian, ReadBytesExt}; + +fn read_u32_node(node_name: &str) -> Result<u32> { + let mut f = File::open(format!( + "/sys/devices/platform/auxdisplay/of_node/{}", + node_name + ))?; + Ok(f.read_u32::<BigEndian>()?) +} + +pub fn display_height_chars() -> Result<u32> { + read_u32_node("display-height-chars") +} + +pub fn display_width_chars() -> Result<u32> { + read_u32_node("display-width-chars") +} |