Skip to content

Commit 199eb35

Browse files
author
Pascal Hertleif
authored
Merge pull request #6 from marcelbuesing/fix-range-check-on-bool
Fix range check on bool
2 parents 1f4d5d4 + f143c5e commit 199eb35

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

src/main.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -379,15 +379,18 @@ fn render_signal(mut w: impl Write, signal: &Signal, dbc: &DBC, msg: &Message) -
379379
)?;
380380
{
381381
let mut w = PadAdapter::wrap(&mut w);
382-
writeln!(w, r##"#[cfg(feature = "range_checked")]"##)?;
383-
writeln!(
384-
w,
385-
r##"if value < {min}_{typ} || {max}_{typ} < value {{ return Err(CanError::ParameterOutOfRange{{ message_id: {message_id} }}); }}"##,
386-
typ = signal_to_rust_type(&signal),
387-
message_id = msg.message_id().0,
388-
min = signal.min(),
389-
max = signal.max(),
390-
)?;
382+
383+
if signal.signal_size != 1 {
384+
writeln!(w, r##"#[cfg(feature = "range_checked")]"##)?;
385+
writeln!(
386+
w,
387+
r##"if value < {min}_{typ} || {max}_{typ} < value {{ return Err(CanError::ParameterOutOfRange{{ message_id: {message_id} }}); }}"##,
388+
typ = signal_to_rust_type(&signal),
389+
message_id = msg.message_id().0,
390+
min = signal.min(),
391+
max = signal.max(),
392+
)?;
393+
}
391394
signal_to_payload(&mut w, signal)?;
392395
}
393396
writeln!(&mut w, "}}")?;

testing/can-messages/src/messages.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,13 @@ impl Bar {
180180
pub const MESSAGE_ID: u32 = 512;
181181

182182
/// Construct new Bar from values
183-
pub fn new(one: u8, two: f32, three: u8, four: u8) -> Result<Self, CanError> {
183+
pub fn new(one: u8, two: f32, three: u8, four: u8, five: bool) -> Result<Self, CanError> {
184184
let mut res = Self { raw: [0u8; 8] };
185185
res.set_one(one)?;
186186
res.set_two(two)?;
187187
res.set_three(three)?;
188188
res.set_four(four)?;
189+
res.set_five(five)?;
189190
Ok(res)
190191
}
191192

@@ -367,6 +368,42 @@ impl Bar {
367368
value.pack_be_bits(&mut self.raw, start_bit, bits);
368369
Ok(())
369370
}
371+
372+
/// Five
373+
///
374+
/// - Min: 0
375+
/// - Max: 1
376+
/// - Unit: "boolean"
377+
/// - Receivers: Dolor
378+
#[inline(always)]
379+
pub fn five(&self) -> bool {
380+
self.five_raw()
381+
}
382+
383+
/// Get raw value of Five
384+
///
385+
/// - Start bit: 30
386+
/// - Signal size: 1 bits
387+
/// - Factor: 1
388+
/// - Offset: 0
389+
/// - Byte order: BigEndian
390+
/// - Value type: Unsigned
391+
#[inline(always)]
392+
pub fn five_raw(&self) -> bool {
393+
let signal = u8::unpack_be_bits(&self.raw, (30 - (1 - 1)), 1);
394+
395+
signal == 1
396+
}
397+
398+
/// Set value of Five
399+
#[inline(always)]
400+
pub fn set_five(&mut self, value: bool) -> Result<(), CanError> {
401+
let value = value as u8;
402+
let start_bit = 30;
403+
let bits = 1;
404+
value.pack_be_bits(&mut self.raw, start_bit, bits);
405+
Ok(())
406+
}
370407
}
371408

372409
impl core::convert::TryFrom<&[u8]> for Bar {

testing/dbc-examples/example.dbc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ BO_ 512 Bar: 8 Ipsum
1717
SG_ Two : 7|8@0+ (0.39,0) [0.00|100] "%" Dolor
1818
SG_ Three : 13|3@0+ (1,0) [0|7] "" Dolor
1919
SG_ Four : 10|2@0+ (1,0) [0|3] "" Dolor
20+
SG_ Five : 30|1@0+ (1,0) [0|1] "boolean" Dolor
2021

2122

2223

0 commit comments

Comments
 (0)