summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 2ef43e8..6193277 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -160,7 +160,7 @@ pub mod ioctl
}
}
-#[derive(Debug)]
+#[derive(Clone, Copy, Debug)]
pub struct KbLedState(u8);
impl KbLedState
@@ -180,6 +180,12 @@ impl KbLedState
pub fn get(con: &Console) -> io::Result<Self> { ioctl::kdgetled(con) }
#[inline]
+ pub fn set(&self, con: &Console) -> io::Result<()>
+ {
+ ioctl::kdsetled(con, Some(*self))
+ }
+
+ #[inline]
pub fn cap(&self) -> bool { (self.0 & 0x4) != 0 }
#[inline]
@@ -221,6 +227,26 @@ impl From<KbLedState> for u8
fn from(state: KbLedState) -> Self { state.0 }
}
+impl TryFrom<u8> for KbLedState
+{
+ type Error = io::Error;
+
+ fn try_from(val: u8) -> io::Result<Self>
+ {
+ if val <= 0b111 {
+ Ok(Self(val))
+ } else {
+ Err(io::Error::new(
+ io::ErrorKind::Other,
+ format!(
+ "invalid raw led value: {:#b}; must not exceed 3 b",
+ val
+ ),
+ ))
+ }
+ }
+}
+
#[cfg(test)]
mod kb_led_state
{