Method: OpenC3::BinaryAccessor.write_array

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

.write_array(values, bit_offset, bit_size, data_type, array_size, buffer, endianness, overflow) ⇒ Array

Writes an array of binary data of any data type to a buffer

Parameters:

  • values (Array)

    Values to write into the buffer

  • bit_offset (Integer)

    Bit offset to the start of the array. A negative number means to offset from the end of the buffer.

  • bit_size (Integer)

    Size of each item in the array in bits

  • data_type (Symbol)
  • array_size (Integer)

    Size in bits of the array as represented in the buffer. Size 0 means to fill the buffer with as many bit_size number of items that exist (negative means excluding the final X number of bits).

  • buffer (String)

    Binary string buffer to write to

  • endianness (Symbol)

Returns:

  • (Array)

    values passed in as a parameter

Raises:

  • (ArgumentError)


1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
# File 'lib/openc3/accessors/binary_accessor.rb', line 1075

def self.write_array(values, bit_offset, bit_size, data_type, array_size, buffer, endianness, overflow)
  # Save given values of bit offset, bit size, and array_size
  given_bit_offset = bit_offset
  given_bit_size = bit_size
  given_array_size = array_size

  # Verify an array was given
  raise ArgumentError, "values must be an Array type class is #{values.class}" unless values.kind_of? Array

  # Handle negative and zero bit sizes
  raise ArgumentError, "bit_size #{given_bit_size} must be positive for arrays" if bit_size <= 0

  # Handle negative bit offsets
  if bit_offset < 0
    bit_offset = ((buffer.length * 8) + bit_offset)
    raise_buffer_error(:write, buffer, data_type, given_bit_offset, given_bit_size) if bit_offset < 0
  end

  # Handle negative and zero array sizes
  if array_size <= 0
    if given_bit_offset < 0
      raise ArgumentError, "negative or zero array_size (#{given_array_size}) cannot be given with negative bit_offset (#{given_bit_offset})"
    else
      end_bytes = -(given_array_size / 8)
      lower_bound = bit_offset / 8
      upper_bound = (bit_offset + (bit_size * values.length) - 1) / 8
      old_upper_bound = buffer.length - 1 - end_bytes

      if upper_bound < old_upper_bound
        # Remove extra bytes from old buffer
        buffer[(upper_bound + 1)..old_upper_bound] = ''
      elsif upper_bound > old_upper_bound
        # Grow buffer and preserve bytes at end of buffer if necessary
        buffer_length = buffer.length
        diff = upper_bound - old_upper_bound
        buffer << (ZERO_STRING * diff)
        if end_bytes > 0
          buffer[(upper_bound + 1)..(buffer.length - 1)] = buffer[(old_upper_bound + 1)..(buffer_length - 1)]
        end
      end

      array_size = ((buffer.length * 8) - bit_offset + array_size)
    end
  end

  # Get data bounds for this array
  lower_bound = bit_offset / 8
  upper_bound = (bit_offset + array_size - 1) / 8
  num_bytes   = upper_bound - lower_bound + 1

  # Check for byte alignment
  byte_aligned = ((bit_offset % 8) == 0)

  # Calculate the number of writes
  num_writes = array_size / bit_size
  # Check for a negative array_size and adjust the number of writes
  # to simply be the number of values in the passed in array
  if given_array_size <= 0
    num_writes = values.length
  end

  # Ensure the buffer has enough room
  if bit_offset + (num_writes * bit_size) > buffer.length * 8
    raise_buffer_error(:write, buffer, data_type, given_bit_offset, given_bit_size)
  end

  # Ensure the given_array_size is an even multiple of bit_size
  raise ArgumentError, "array_size #{given_array_size} not a multiple of bit_size #{given_bit_size}" if array_size % bit_size != 0

  raise ArgumentError, "too many values #{values.length} for given array_size #{given_array_size} and bit_size #{given_bit_size}" if num_writes < values.length

  # Check overflow type
  raise "unknown overflow type #{overflow}" unless OVERFLOW_TYPES.include?(overflow)

  case data_type
  when :STRING, :BLOCK
    #######################################
    # Handle :STRING and :BLOCK data types
    #######################################

    if byte_aligned
      num_writes.times do |index|
        self.write(values[index], bit_offset, bit_size, data_type, buffer, endianness, overflow)
        bit_offset += bit_size
      end
    else
      raise ArgumentError, "bit_offset #{given_bit_offset} is not byte aligned for data_type #{data_type}"
    end

  when :INT, :UINT
    ###################################
    # Handle :INT and :UINT data types
    ###################################

    if byte_aligned and (bit_size == 8 or bit_size == 16 or bit_size == 32 or bit_size == 64)
      ###########################################################
      # Handle byte-aligned 8, 16, 32, and 64 bit :INT and :UINT
      ###########################################################

      case bit_size
      when 8
        if data_type == :INT
          values = self.check_overflow_array(values, MIN_INT8, MAX_INT8, MAX_UINT8, bit_size, data_type, overflow)
          packed = values.pack(PACK_8_BIT_INT_ARRAY)
        else # data_type == :UINT
          values = self.check_overflow_array(values, 0, MAX_UINT8, MAX_UINT8, bit_size, data_type, overflow)
          packed = values.pack(PACK_8_BIT_UINT_ARRAY)
        end

      when 16
        if data_type == :INT
          values = self.check_overflow_array(values, MIN_INT16, MAX_INT16, MAX_UINT16, bit_size, data_type, overflow)
          if endianness == HOST_ENDIANNESS
            packed = values.pack(PACK_NATIVE_16_BIT_INT_ARRAY)
          else # endianness != HOST_ENDIANNESS
            packed = values.pack(PACK_NATIVE_16_BIT_INT_ARRAY)
            self.byte_swap_buffer!(packed, 2)
          end
        else # data_type == :UINT
          values = self.check_overflow_array(values, 0, MAX_UINT16, MAX_UINT16, bit_size, data_type, overflow)
          if endianness == :BIG_ENDIAN
            packed = values.pack(PACK_BIG_ENDIAN_16_BIT_UINT_ARRAY)
          else # endianness == :LITTLE_ENDIAN
            packed = values.pack(PACK_LITTLE_ENDIAN_16_BIT_UINT_ARRAY)
          end
        end

      when 32
        if data_type == :INT
          values = self.check_overflow_array(values, MIN_INT32, MAX_INT32, MAX_UINT32, bit_size, data_type, overflow)
          if endianness == HOST_ENDIANNESS
            packed = values.pack(PACK_NATIVE_32_BIT_INT_ARRAY)
          else # endianness != HOST_ENDIANNESS
            packed = values.pack(PACK_NATIVE_32_BIT_INT_ARRAY)
            self.byte_swap_buffer!(packed, 4)
          end
        else # data_type == :UINT
          values = self.check_overflow_array(values, 0, MAX_UINT32, MAX_UINT32, bit_size, data_type, overflow)
          if endianness == :BIG_ENDIAN
            packed = values.pack(PACK_BIG_ENDIAN_32_BIT_UINT_ARRAY)
          else # endianness == :LITTLE_ENDIAN
            packed = values.pack(PACK_LITTLE_ENDIAN_32_BIT_UINT_ARRAY)
          end
        end

      when 64
        if data_type == :INT
          values = self.check_overflow_array(values, MIN_INT64, MAX_INT64, MAX_UINT64, bit_size, data_type, overflow)
          if endianness == HOST_ENDIANNESS
            packed = values.pack(PACK_NATIVE_64_BIT_INT_ARRAY)
          else # endianness != HOST_ENDIANNESS
            packed = values.pack(PACK_NATIVE_64_BIT_INT_ARRAY)
            self.byte_swap_buffer!(packed, 8)
          end
        else # data_type == :UINT
          values = self.check_overflow_array(values, 0, MAX_UINT64, MAX_UINT64, bit_size, data_type, overflow)
          if endianness == HOST_ENDIANNESS
            packed = values.pack(PACK_NATIVE_64_BIT_UINT_ARRAY)
          else # endianness != HOST_ENDIANNESS
            packed = values.pack(PACK_NATIVE_64_BIT_UINT_ARRAY)
            self.byte_swap_buffer!(packed, 8)
          end
        end
      end

      # Adjust packed size to hold number of items written
      buffer[lower_bound..upper_bound] = adjust_packed_size(num_bytes, packed) if num_bytes > 0

    else
      ##################################
      # Handle :INT and :UINT Bitfields
      ##################################

      raise ArgumentError, "write_array does not support little endian bit fields with bit_size greater than 1-bit" if endianness == :LITTLE_ENDIAN and bit_size > 1

      num_writes.times do |index|
        self.write(values[index], bit_offset, bit_size, data_type, buffer, endianness, overflow)
        bit_offset += bit_size
      end
    end

  when :FLOAT
    ##########################
    # Handle :FLOAT data type
    ##########################

    if byte_aligned
      case bit_size
      when 32
        if endianness == :BIG_ENDIAN
          packed = values.pack(PACK_BIG_ENDIAN_32_BIT_FLOAT_ARRAY)
        else # endianness == :LITTLE_ENDIAN
          packed = values.pack(PACK_LITTLE_ENDIAN_32_BIT_FLOAT_ARRAY)
        end

      when 64
        if endianness == :BIG_ENDIAN
          packed = values.pack(PACK_BIG_ENDIAN_64_BIT_FLOAT_ARRAY)
        else # endianness == :LITTLE_ENDIAN
          packed = values.pack(PACK_LITTLE_ENDIAN_64_BIT_FLOAT_ARRAY)
        end

      else
        raise ArgumentError, "bit_size is #{given_bit_size} but must be 32 or 64 for data_type #{data_type}"
      end

      # Adjust packed size to hold number of items written
      buffer[lower_bound..upper_bound] = adjust_packed_size(num_bytes, packed) if num_bytes > 0

    else
      raise ArgumentError, "bit_offset #{given_bit_offset} is not byte aligned for data_type #{data_type}"
    end

  else
    ############################
    # Handle Unknown data types
    ############################
    raise ArgumentError, "data_type #{data_type} is not recognized"
  end # case data_type

  values
end