-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
bit order in reader #371
Comments
I have a MR that should add features that you want here regarding to Also, if you have an example I can make sure the MR does what you need. |
This is how I built my reader/writer, let me know if that serves as an example for reader/writer:
Theoretically what I'd really want is not to use a reader/writer at all but rather use only deku features. Then it would look like:
Or preferably |
If it's helpful, I'm also running into a place where having the ability to configure the bit order would be nice. I'm parsing the data described here, which — though it doesn't currently mention it — is in LSb 0. It's eight 13-bit values packed into 13 bytes. I've got (messy, probably bad rust) code to parse it here. It looks like with the MR I'll be able to do something like this? #[test]
fn test_probe_status_bit_order() {
#[derive(DekuRead, PartialEq, Debug)]
#[deku(bit_order = "ctx_lsb", ctx = "ctx_lsb: Order")]
pub struct Temperature {
#[deku(bits = "13")]
raw_value: u16,
}
impl Temperature {
pub fn new(raw_value: u16) -> Self {
Temperature { raw_value }
}
}
#[derive(DekuRead, PartialEq, Debug)]
#[deku(bit_order = "lsb")]
pub struct ProbeStatus {
temperatures: [Temperature; 8],
}
let data = [
0x4a, 0x63, 0x69, 0x2c, 0x8d, 0xa5, 0x31, 0x35, 0xaa, 0x46, 0xd5, 0xc0, 0x1a,
];
assert_eq!(
ProbeStatus {
temperatures: [
Temperature::new(842),
Temperature::new(843),
Temperature::new(843),
Temperature::new(843),
Temperature::new(851),
Temperature::new(853),
Temperature::new(853),
Temperature::new(856),
]
},
ProbeStatus::try_from(data.as_slice()).unwrap()
);
} The rest of the data in that doc is described in LSb 0 but it doesn't present a parsing issue unless the data crosses bytes. |
@simonft Did you already try the branch? I don't have time to make sure your asserts are correct, but I was getting the following error: (this could totally be from just my branch having a bug)
|
I hadn't, but I just did. I'm able to reproduce the test case failure, and I've updated the original snippet to be an actual working test case. Trying to figure out what's going wrong is breaking my brain a bit. Edit: ignore the below, I think I made a mistake.
I'm not 100% sure whether 512 or 64 is the correct answer here, but my intuition is they shouldn't be different. #[test]
fn test_bit_order_13() {
#[derive(DekuRead, PartialEq, Debug)]
#[deku(bit_order = "lsb")]
pub struct BitTest {
#[deku(bits = "13")]
raw_value1: u16,
#[deku(bits = "13")]
raw_value2: u16,
#[deku(bits = "6")]
raw_value3: u16,
}
let data = vec![0b00000000, 0b00000010, 0b01000000, 0b00000000];
let string_data = data
.iter()
.map(|f| (format!("{:08b}", f).chars().rev().collect()))
.collect::<Vec<String>>()
.join("");
println!("string_data: {}", string_data);
assert_eq!(string_data[0..13], string_data[13..26]);
assert_eq!(string_data.chars().nth(9).unwrap(), '1');
assert_eq!(
BitTest {
raw_value1: 2_u16.pow(9),
raw_value2: 2_u16.pow(9),
raw_value3: 0
},
BitTest::try_from(data.as_slice()).unwrap()
);
} |
Yes. doing bit_order is a brain bender.
Ah man, hopefully I didn't cause you much headache, I forgot to apply 62c1ddb when I rebased this branch once! I added your test as an example of it working! 32d4588. |
Should be closed with #483 |
I saw several issues on bit order, but non are related to
reader
attribute, so didn't want to add to those more complexity.I'm writing a
reader
that needs to read number of bits that's not aligned to byte boundary.My issue is that I need to read
Lsb0
and notMsb0
and seems likeDekuRead::read
is onlyMsb0
.Is there a way to read in in
reader
usingLsb0
(not by reading a byte and reversing, because my reads are not aligned on byte bounday)?The text was updated successfully, but these errors were encountered: