Method: OpenC3::BinaryAccessor.check_overflow

Defined in:
lib/openc3/accessors/binary_accessor.rb

.check_overflow(value, min_value, max_value, hex_max_value, bit_size, data_type, overflow) ⇒ Integer

Checks for overflow of an integer data type

Parameters:

  • value (Integer)

    Value to write into the buffer

  • min_value (Integer)

    Minimum allowed value

  • max_value (Integer)

    Maximum allowed value

  • hex_max_value (Integer)

    Maximum allowed value if specified in hex

  • bit_size (Integer)

    Size of the item in bits

  • data_type (Symbol)
  • overflow (Symbol)

Returns:

  • (Integer)

    Potentially modified value



1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
# File 'lib/openc3/accessors/binary_accessor.rb', line 1348

def self.check_overflow(value, min_value, max_value, hex_max_value, bit_size, data_type, overflow)
  if overflow == :TRUNCATE
    # Note this will always convert to unsigned equivalent for signed integers
    value = value % (hex_max_value + 1)
  else
    if value > max_value
      if overflow == :SATURATE
        value = max_value
      else
        if overflow == :ERROR or value > hex_max_value
          raise ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}"
        end
      end
    elsif value < min_value
      if overflow == :SATURATE
        value = min_value
      else
        raise ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}"
      end
    end
  end
  value
end