Class: Cosmos::PacketItem
- 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
Constant Summary collapse
- STATE_COLORS =
The allowable state colors
[:GREEN, :YELLOW, :RED]
Instance Attribute Summary collapse
-
#default ⇒ Object
The default value type depends on the data_type of the PacketItem.
-
#description ⇒ String
Description of the item.
-
#format_string ⇒ String
Printf-style string used to format the item.
-
#hazardous ⇒ Hash
States that are hazardous for this item as well as their descriptions.
-
#id_value ⇒ Object
The id_value type depends on the data_type of the PacketItem.
-
#limits ⇒ PacketItemLimits
All information regarding limits for this PacketItem.
-
#range ⇒ Range
The valid range of values for this item.
-
#read_conversion ⇒ Conversion
Conversion instance used when reading the PacketItem.
-
#required ⇒ Boolean
default value.
-
#state_colors ⇒ Hash
Colors associated with states.
-
#states ⇒ Hash
States are used to convert from a numeric value to a String.
-
#units ⇒ String
Returns the abbreviated units of the item.
-
#units_full ⇒ String
Returns the fully spelled out units of the item.
-
#write_conversion ⇒ Conversion
Conversion instance used when writing the PacketItem.
Instance Method Summary collapse
- #check_default_and_range_data_types ⇒ Object
-
#clone ⇒ Object
(also: #dup)
Make a light weight clone of this item.
-
#initialize(name, bit_offset, bit_size, data_type, endianness, array_size = nil, overflow = :ERROR) ⇒ Object
constructor
Create a StructureItem by setting all the attributes.
- #meta ⇒ Object
- #to_hash ⇒ Object
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.
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
#default ⇒ Object
The default value type depends on the data_type of the PacketItem
53 54 55 |
# File 'lib/cosmos/packets/packet_item.rb', line 53 def default @default end |
#description ⇒ String
Returns Description of the item.
39 40 41 |
# File 'lib/cosmos/packets/packet_item.rb', line 39 def description @description end |
#format_string ⇒ String
Returns 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 |
#hazardous ⇒ Hash
States that are hazardous for this item as well as their descriptions
67 68 69 |
# File 'lib/cosmos/packets/packet_item.rb', line 67 def hazardous @hazardous end |
#id_value ⇒ Object
The id_value type depends on the data_type of the PacketItem
32 33 34 |
# File 'lib/cosmos/packets/packet_item.rb', line 32 def id_value @id_value end |
#limits ⇒ PacketItemLimits
Returns All information regarding limits for this PacketItem.
77 78 79 |
# File 'lib/cosmos/packets/packet_item.rb', line 77 def limits @limits end |
#range ⇒ Range
The valid range of values for this item. Returns nil for items with data_type of :STRING or :BLOCK items.
58 59 60 |
# File 'lib/cosmos/packets/packet_item.rb', line 58 def range @range end |
#read_conversion ⇒ Conversion
Conversion instance used when reading the PacketItem
24 25 26 |
# File 'lib/cosmos/packets/packet_item.rb', line 24 def read_conversion @read_conversion end |
#required ⇒ Boolean
default value. true if it must be specified.
62 63 64 |
# File 'lib/cosmos/packets/packet_item.rb', line 62 def required @required end |
#state_colors ⇒ Hash
Colors associated with states
71 72 73 |
# File 'lib/cosmos/packets/packet_item.rb', line 71 def state_colors @state_colors end |
#states ⇒ Hash
States are used to convert from a numeric value to a String.
36 37 38 |
# File 'lib/cosmos/packets/packet_item.rb', line 36 def states @states end |
#units ⇒ String
Returns the abbreviated units of the item. For example, if the item represents a voltage, this would return “V”.
49 50 51 |
# File 'lib/cosmos/packets/packet_item.rb', line 49 def units @units end |
#units_full ⇒ String
Returns the fully spelled out units of the item. For example, if the item represents a voltage, this would return “Voltage”.
44 45 46 |
# File 'lib/cosmos/packets/packet_item.rb', line 44 def units_full @units_full end |
#write_conversion ⇒ Conversion
Conversion instance used when writing the PacketItem
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_types ⇒ Object
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 |
#clone ⇒ Object 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. = self..clone if @meta item end |
#meta ⇒ Object
247 248 249 |
# File 'lib/cosmos/packets/packet_item.rb', line 247 def @meta ||= {} end |
#to_hash ⇒ Object
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 |