Class: Cosmos::PacketItem

Inherits:
StructureItem
  • Object
show all
Defined in:
lib/cosmos/packets/packet_item.rb,
ext/cosmos/ext/packet/packet.c

Overview

Maintains knowledge of an item in a Packet

Direct Known Subclasses

TableItem

Constant Summary collapse

STATE_COLORS =

The allowable state colors

[:GREEN, :YELLOW, :RED]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, bit_offset, bit_size, data_type, endianness, array_size = nil, overflow = :ERROR) ⇒ Object

Create a StructureItem by setting all the attributes. It calls all the setter routines to do the attribute verification and then verifies the overall integrity.

It also initializes the attributes of the PacketItem.

Parameters:

  • name (String)

    The item name

  • bit_offset (Integer)

    Offset to the item starting at 0

  • bit_size (Integer)

    Size of the items in bits

  • data_type (Symbol)

    DATA_TYPES

  • endianness (Symbol)
  • array_size (Integer, nil) (defaults to: nil)

    Size of the array item in bits. For example, if the bit_size is 8, an array_size of 16 holds two values.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cosmos/packets/packet_item.rb', line 81

def initialize(name, bit_offset, bit_size, data_type, endianness, array_size = nil, overflow = :ERROR)
  super(name, bit_offset, bit_size, data_type, endianness, array_size, overflow)
  @format_string = nil
  @read_conversion = nil
  @write_conversion = nil
  @id_value = nil
  @states = nil
  @description = nil
  @units_full = nil
  @units = nil
  @default = nil
  @range = nil
  @required = false
  @hazardous = nil
  @state_colors = nil
  @limits = PacketItemLimits.new
  @persistence_setting = 1
  @persistence_count = 0
  @meta = nil
end

Instance Attribute Details

#defaultObject

The default value type depends on the data_type of the PacketItem

Returns:

  • Default value for this item



53
54
55
# File 'lib/cosmos/packets/packet_item.rb', line 53

def default
  @default
end

#descriptionString

Returns Description of the item.

Returns:

  • (String)

    Description of the item



39
40
41
# File 'lib/cosmos/packets/packet_item.rb', line 39

def description
  @description
end

#format_stringString

Returns Printf-style string used to format the item.

Returns:

  • (String)

    Printf-style string used to format the item



20
21
22
# File 'lib/cosmos/packets/packet_item.rb', line 20

def format_string
  @format_string
end

#hazardousHash

States that are hazardous for this item as well as their descriptions

Returns:

  • (Hash)

    Hazardous states given as STATE_NAME => DESCRIPTION. If no description was given the value will be nil.



67
68
69
# File 'lib/cosmos/packets/packet_item.rb', line 67

def hazardous
  @hazardous
end

#id_valueObject

The id_value type depends on the data_type of the PacketItem

Returns:

  • Value used to identify a packet



32
33
34
# File 'lib/cosmos/packets/packet_item.rb', line 32

def id_value
  @id_value
end

#limitsPacketItemLimits

Returns All information regarding limits for this PacketItem.

Returns:



77
78
79
# File 'lib/cosmos/packets/packet_item.rb', line 77

def limits
  @limits
end

#rangeRange

The valid range of values for this item. Returns nil for items with data_type of :STRING or :BLOCK items.

Returns:

  • (Range)

    Valid range of values or nil



58
59
60
# File 'lib/cosmos/packets/packet_item.rb', line 58

def range
  @range
end

#read_conversionConversion

Conversion instance used when reading the PacketItem

Returns:



24
25
26
# File 'lib/cosmos/packets/packet_item.rb', line 24

def read_conversion
  @read_conversion
end

#requiredBoolean

default value. true if it must be specified.

Returns:

  • (Boolean)

    Whether this item must be specified or can use its



62
63
64
# File 'lib/cosmos/packets/packet_item.rb', line 62

def required
  @required
end

#state_colorsHash

Colors associated with states

Returns:

  • (Hash)

    State colors given as STATE_NAME => COLOR



71
72
73
# File 'lib/cosmos/packets/packet_item.rb', line 71

def state_colors
  @state_colors
end

#statesHash

States are used to convert from a numeric value to a String.

Returns:

  • (Hash)

    Item states given as STATE_NAME => VALUE



36
37
38
# File 'lib/cosmos/packets/packet_item.rb', line 36

def states
  @states
end

#unitsString

Returns the abbreviated units of the item. For example, if the item represents a voltage, this would return “V”.

Returns:

  • (String)

    Abbreviated units of the item



49
50
51
# File 'lib/cosmos/packets/packet_item.rb', line 49

def units
  @units
end

#units_fullString

Returns the fully spelled out units of the item. For example, if the item represents a voltage, this would return “Voltage”.

Returns:

  • (String)

    Units of the item



44
45
46
# File 'lib/cosmos/packets/packet_item.rb', line 44

def units_full
  @units_full
end

#write_conversionConversion

Conversion instance used when writing the PacketItem

Returns:



28
29
30
# File 'lib/cosmos/packets/packet_item.rb', line 28

def write_conversion
  @write_conversion
end

Instance Method Details

#check_default_and_range_data_typesObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cosmos/packets/packet_item.rb', line 183

def check_default_and_range_data_types
  if @default and !@write_conversion
    if @array_size
      raise ArgumentError, "#{@name}: default must be an Array but is a #{default.class}" unless Array === @default
    else
      case data_type
      when :INT, :UINT
        raise ArgumentError, "#{@name}: default must be a Integer but is a #{@default.class}" unless Integer === @default
        if @range
          raise ArgumentError, "#{@name}: minimum must be a Integer but is a #{@range.first.class}" unless Integer === @range.first
          raise ArgumentError, "#{@name}: maximum must be a Integer but is a #{@range.last.class}" unless Integer === @range.last
        end
      when :FLOAT
        raise ArgumentError, "#{@name}: default must be a Float but is a #{@default.class}" unless Float === @default or Integer === @default
        @default = @default.to_f
        if @range
          raise ArgumentError, "#{@name}: minimum must be a Float but is a #{@range.first.class}" unless Float === @range.first or Integer === @range.first
          raise ArgumentError, "#{@name}: maximum must be a Float but is a #{@range.last.class}" unless Float === @range.last or Integer === @range.last
          @range = ((@range.first.to_f)..(@range.last.to_f))
        end
      when :BLOCK, :STRING
        raise ArgumentError, "#{@name}: default must be a String but is a #{@default.class}" unless String === @default
        @default = @default.clone.freeze
      end
    end
  end
end

#cloneObject Also known as: dup

Make a light weight clone of this item



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/cosmos/packets/packet_item.rb', line 252

def clone
  item = super()
  item.format_string = self.format_string.clone if self.format_string
  item.read_conversion = self.read_conversion.clone if self.read_conversion
  item.write_conversion = self.write_conversion.clone if self.write_conversion
  item.states = self.states.clone if self.states
  item.description = self.description.clone if self.description
  item.units_full = self.units_full.clone if self.units_full
  item.units = self.units.clone if self.units
  item.default = self.default.clone if self.default and String === self.default
  item.hazardous = self.hazardous.clone if self.hazardous
  item.state_colors = self.state_colors.clone if self.state_colors
  item.limits = self.limits.clone if self.limits
  item.meta = self.meta.clone if @meta
  item
end

#metaObject



247
248
249
# File 'lib/cosmos/packets/packet_item.rb', line 247

def meta
  @meta ||= {}
end

#to_hashObject



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/cosmos/packets/packet_item.rb', line 270

def to_hash
  hash = super()
  hash['format_string'] = self.format_string
  if self.read_conversion
    hash['read_conversion'] = self.read_conversion.to_s
  else
    hash['read_conversion'] = nil
  end
  if self.write_conversion
    hash['write_conversion'] = self.write_conversion.to_s
  else
    hash['write_conversion'] = nil
  end
  hash['id_value'] = self.id_value
  hash['states'] = self.states
  hash['description'] = self.description
  hash['units_full'] = self.units_full
  hash['units'] = self.units
  hash['default'] = self.default
  hash['range'] = self.range
  hash['required'] = self.required
  hash['hazardous'] = self.hazardous
  hash['state_colors'] = self.state_colors
  hash['limits'] = self.limits.to_hash
  hash['meta'] = nil
  hash['meta'] = @meta if @meta
  hash
end