Class: Msp430Bsl::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/msp430_bsl/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd_name, addr: nil, data: nil) ⇒ Command

Returns a new instance of Command.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/msp430_bsl/command.rb', line 18

def initialize(cmd_name, addr: nil, data: nil)
  raise Exceptions::Command::NameNotSupported, cmd_name unless self.class.supports?(cmd_name)

  @name = cmd_name
  @configs = self.class[@name]
  @code = configs[:code]
  @addr = addr
  @data = data

  validate
end

Instance Attribute Details

#addrObject

Returns the value of attribute addr.



6
7
8
# File 'lib/msp430_bsl/command.rb', line 6

def addr
  @addr
end

#codeObject

Returns the value of attribute code.



6
7
8
# File 'lib/msp430_bsl/command.rb', line 6

def code
  @code
end

#configsObject

Returns the value of attribute configs.



6
7
8
# File 'lib/msp430_bsl/command.rb', line 6

def configs
  @configs
end

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/msp430_bsl/command.rb', line 6

def data
  @data
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/msp430_bsl/command.rb', line 6

def name
  @name
end

Class Method Details

.[](cmd_name) ⇒ Object



12
13
14
15
16
# File 'lib/msp430_bsl/command.rb', line 12

def self.[](cmd_name)
  raise Exceptions::Command::NameNotSupported, cmd_name unless supports?(cmd_name)

  Configs::CMDS[cmd_name.to_sym]
end

.supports?(cmd_name) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/msp430_bsl/command.rb', line 8

def self.supports?(cmd_name)
  Configs::CMDS.keys.include? cmd_name.to_sym
end

Instance Method Details

#lengthObject



36
37
38
# File 'lib/msp430_bsl/command.rb', line 36

def length
  code.to_bytes_ary.length
end

#packetObject



30
31
32
33
34
# File 'lib/msp430_bsl/command.rb', line 30

def packet
  return @packet if @packet

  @packet = [code, splitted_addr, data].flatten.compact
end

#splitted_addrObject

Split address to [low, middle, high] bytes



41
42
43
44
45
# File 'lib/msp430_bsl/command.rb', line 41

def splitted_addr
  if addr
    [ (addr & 0xFF), ((addr >> 8) & 0xFF), ((addr >> 16) & 0xFF) ]
  end
end