technocreatives / dbc-codegen

Generate Rust structs for messages from a dbc (CAN bus definition) file.
Apache License 2.0
48 stars 31 forks source link

Fix issue with decoding signed values of non-standard length #79

Closed Dillonmcewan closed 2 months ago

Dillonmcewan commented 5 months ago

I noticed certain signals were getting decoded incorrectly from one of my DBC files. I tracked this down to signed values that have less bits than the corresponding rust type. For instance if I have a signed signal foo with 4 bits (corresponding to a rust i8):

If I try to set a value of -3, it correctly gets encoded in fn set_foo to the bits [1, 0, 1, 1]. Then in fn foo_raw it was first loading the data as a u8, and since the 4 most significant bits get dropped, the value gets read in as 13 rather than 253. So when that value gets converted to an i8 the result is 13 instead of -3. It seems like bitvec::field::BitSlice::load_le<i8>, on the other hand, works correctly without the intermediate conversion to a u8

Dillonmcewan commented 3 months ago

Hi @killercup anything I can do to help get this fix merged in?