Class: Lignite::DirectCommands

Inherits:
Object
  • Object
show all
Includes:
Bytes, VariableDeclarer
Defined in:
lib/lignite/direct_commands.rb

Overview

This enables sending commands without wrapping them in a .rbf program

Class Method Summary collapse

Instance Method Summary collapse

Methods included from VariableDeclarer

#array8, #data16, #data32, #data8, #dataf, #datas

Methods included from Bytes

#bin_to_hex, #f32, #hex_to_bin, #u16, #u32, #u8, #unpack_f32, #unpack_u16, #unpack_u32, #unpack_u8

Constructor Details

#initialize(conn = Connection.create) ⇒ DirectCommands

Returns a new instance of DirectCommands.

Parameters:

  • conn (Connection) (defaults to: Connection.create)


15
16
17
18
19
# File 'lib/lignite/direct_commands.rb', line 15

def initialize(conn = Connection.create)
  @op_compiler = OpCompiler.new
  @conn = conn
  @globals = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/lignite/direct_commands.rb', line 52

def method_missing(name, *args)
  if @op_compiler.respond_to?(name)
    insb = @op_compiler.send(name, *args)
    direct_command(insb)
  else
    super
  end
end

Class Method Details

.run(conn = Connection.create, &block) ⇒ Object



8
9
10
11
12
# File 'lib/lignite/direct_commands.rb', line 8

def self.run(conn = Connection.create, &block)
  dc = new(conn)
  dc.instance_exec(&block)
  dc.close
end

Instance Method Details

#block(&body) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lignite/direct_commands.rb', line 38

def block(&body)
  locals = Variables.new
  bodyc = BodyCompiler.new(@globals, locals, RbfDeclarer::Dummy.new)
  bodyc.instance_exec(&body)

  bs = bodyc.bytes
  lsize = locals.bytesize
  if @globals
    direct_command_with_reply(bs, global_size: @globals.bytesize, local_size: lsize)
  else
    direct_command(bs, global_size: 0, local_size: lsize)
  end
end

#closeObject



21
22
23
# File 'lib/lignite/direct_commands.rb', line 21

def close
  @conn.close
end

#respond_to_missing?(name, _include_private) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/lignite/direct_commands.rb', line 61

def respond_to_missing?(name, _include_private)
  @op_compiler.respond_to?(name) || super
end

#variablesObject



25
26
27
# File 'lib/lignite/direct_commands.rb', line 25

def variables
  @globals
end

#with_reply(&body) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/lignite/direct_commands.rb', line 30

def with_reply(&body)
  @globals = Variables.new
  ret_bytes = instance_exec(&body)
  ret = @globals.unpack(ret_bytes)
  @globals = nil
  ret # TODO: decode according to type
end