-
Hello, and thanks for taking the time to help a novice with a simple issue. I have a file that is a sequence of 16-bit messages. Each message has a certain format, for message 1 it is as follows: #[bitfield]
#[derive(BinRead, Debug)]
#[br(map = Self::from_bytes)]
pub struct message_bundle_1_message_1
{
status: B16,
setting1: bool, // 1 = Enabled
setting2: bool, // Spare, set to 0
setting3: bool, // 1 = Enabled
setting4: bool, // 1 = Enabled
setting5: bool, // Spare, set to 0
setting6: bool, // 1 = Enabled
setting7: bool, // 1 = Enabled
setting8: bool, // 1 = Enabled
setting9: bool, // 1 = Enabled
setting10: bool, // 1 = Enabled
setting11: bool, // 1 = Enabled
setting12: bool, // 1 = Enabled
setting13: bool, // Spare
setting14: bool, // 1 = Enabled
setting15: bool, // 1 = Enabled
setting16: bool, // 1 = Enabled
} This works fine for messages that are only single bit enables. However, a majority of messages are either a mix of numbers and enable-bits or n-bit numbers. I thought an implementation for this might be to simply use the smallest number possible, which in the case of a 3-bit number would be a U8, and provide a count for the variable but that failed due to: cannot find attribute `br` in this scope for the code: #[bitfield]
#[derive(BinRead, Debug)]
#[br(map = Self::from_bytes)]
pub struct message_bundle_1_message_3
{
// bits
status: B16,
#[br(count = 4)]
number_4_bit: u8,
enable_1: bool,
enable_2: bool,
enable_3: bool,
spare_1: bool, // Spare, set to 0
spare_2: bool, // Spare, set to 0
spare_3: bool, // Spare, set to 0
#[br(count = 3)]
number_3_bitB: u8,
#[br(count = 3)]
number_3_bitC: u8,
} I know the scoping issue is separate from the unpacking bits issue however, maybe this is a completely wrong way to do this. I am happy to change my process completely for a more robust and standard method (or even just one that works with a small hack). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Solution: use modular_bitfield::{bitfield, specifiers::*, BitfieldSpecifier};
#[bitfield]
#[derive(BinRead, Debug)]
pub struct Sam1Word3Bitfield
{
// bits
status: B16,
number_4_bits: B4,
enable_1: bool,
enable_2: bool,
enable_3: bool,
spare_group: B3, // Spare, set to 0
number_3_bit: B3,
number_3_bit: B3,
} |
Beta Was this translation helpful? Give feedback.
Solution: