Class: CanMessenger::Signal
- Inherits:
-
Object
- Object
- CanMessenger::Signal
- Defined in:
- lib/can_messenger/dbc.rb
Overview
Represents a signal within a CAN message.
A Signal defines how a piece of data is encoded within a CAN message, including its bit position, length, byte order, signedness, and scaling. Signals can represent physical values (like temperature, speed) that are encoded as integers in the CAN frame but scaled to engineering units.
Instance Attribute Summary collapse
-
#endianness ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#factor ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#length ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#name ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#offset ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#sign ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#start_bit ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
Instance Method Summary collapse
-
#decode(bytes) ⇒ Float
Decodes this signal’s value from the message byte array.
-
#encode(bytes, value) ⇒ void
Encodes a value into the message byte array at this signal’s bit position.
-
#initialize(name, start_bit:, length:, endianness:, sign:, factor:, offset:) ⇒ Signal
constructor
Initializes a new Signal instance.
Constructor Details
#initialize(name, start_bit:, length:, endianness:, sign:, factor:, offset:) ⇒ Signal
Initializes a new Signal instance.
290 291 292 293 294 295 296 297 298 |
# File 'lib/can_messenger/dbc.rb', line 290 def initialize(name, start_bit:, length:, endianness:, sign:, factor:, offset:) # rubocop:disable Metrics/ParameterLists @name = name @start_bit = start_bit @length = length @endianness = endianness @sign = sign @factor = factor @offset = offset end |
Instance Attribute Details
#endianness ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
279 280 281 |
# File 'lib/can_messenger/dbc.rb', line 279 def endianness @endianness end |
#factor ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
279 280 281 |
# File 'lib/can_messenger/dbc.rb', line 279 def factor @factor end |
#length ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
279 280 281 |
# File 'lib/can_messenger/dbc.rb', line 279 def length @length end |
#name ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
279 280 281 |
# File 'lib/can_messenger/dbc.rb', line 279 def name @name end |
#offset ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
279 280 281 |
# File 'lib/can_messenger/dbc.rb', line 279 def offset @offset end |
#sign ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
279 280 281 |
# File 'lib/can_messenger/dbc.rb', line 279 def sign @sign end |
#start_bit ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
279 280 281 |
# File 'lib/can_messenger/dbc.rb', line 279 def start_bit @start_bit end |
Instance Method Details
#decode(bytes) ⇒ Float
Decodes this signal’s value from the message byte array.
Extracts the raw integer value from the appropriate bit positions, then applies the signal’s scaling (factor and offset) to convert it to engineering units.
324 325 326 327 |
# File 'lib/can_messenger/dbc.rb', line 324 def decode(bytes) raw = extract_bits(bytes) (raw * factor) + offset end |
#encode(bytes, value) ⇒ void
This method returns an undefined value.
Encodes a value into the message byte array at this signal’s bit position.
Converts the engineering unit value to a raw integer using the signal’s factor and offset, then places the bits into the appropriate positions within the message bytes.
310 311 312 313 314 |
# File 'lib/can_messenger/dbc.rb', line 310 def encode(bytes, value) raw = ((value - offset) / factor).round validate_signal_bounds(bytes.size) insert_bits(bytes, raw) end |