Created
March 7, 2018 18:43
-
-
Save wose/b351175580153604d8ec0d6ea7fad4ef to your computer and use it in GitHub Desktop.
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
#![allow(non_camel_case_types, non_snake_case, dead_code)] | |
macro_rules! bit_range { | |
(pub struct $struct_name:tt : $struct_size:ty { | |
$($name:tt $offset:expr, $size:expr),+ | |
}) => { | |
#[derive(Debug)] | |
pub struct $struct_name { | |
bits: $struct_size | |
} | |
impl $struct_name { | |
pub fn new(bits: $struct_size) -> $struct_name { | |
$struct_name { bits } | |
} | |
$( | |
pub fn $name(&self) -> $struct_size { | |
let mut MASK = 0; | |
for _ in 0..$size { | |
MASK = (MASK << 1) | 1; | |
} | |
MASK <<= $offset; | |
(self.bits & MASK) >> $offset | |
} | |
)+ | |
} | |
} | |
} | |
bit_range! { | |
pub struct uMCP9808_REG__TEMP_AMB: u16 { | |
Decimal 0, 4, | |
Integer 4, 4, | |
Sign 8, 1, | |
VsTLow 9, 1, | |
VsTHigh 10, 1, | |
VsTCrit 11, 1 | |
} | |
} | |
fn main(){ | |
let v = uMCP9808_REG__TEMP_AMB::new(1234); | |
println!("{:?}", v); | |
println!("{:?}", v.Decimal()); | |
println!("{:?}", v.Integer()); | |
println!("{:?}", v.Sign()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment