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
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 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 |
# File 'lib/openc3/accessors/binary_accessor.rb', line 913 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 necesssary 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 |