Class: Lignite::MessageSender

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

Overview

FIXME: Possibly merge with Connection (UsbConnection)

Instance Method Summary collapse

Methods included from Logger

default_logger, #logger

Methods included from Bytes

#f32, #hexdump, #u16, #u32, #u8, #unpack_f32, #unpack_u16, #unpack_u32, #unpack_u8

Constructor Details

#initialize(connection) ⇒ MessageSender

Returns a new instance of MessageSender.



10
11
12
13
# File 'lib/lignite/message_sender.rb', line 10

def initialize(connection)
  @c = connection
  @buf = ""
end

Instance Method Details

#direct_command(instr_bytes, global_size: 0, local_size: 0) ⇒ Object



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

def direct_command(instr_bytes, global_size: 0, local_size: 0)
  body = u16(var_alloc(global_size: global_size, local_size: local_size)) +
         instr_bytes
  cmd = Message.direct_command_no_reply(body)
  send(cmd.bytes)
end

#direct_command_with_reply(instr_bytes, global_size: 0, local_size: 0) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lignite/message_sender.rb', line 22

def direct_command_with_reply(instr_bytes, global_size: 0, local_size: 0)
  body = u16(var_alloc(global_size: global_size, local_size: local_size)) +
         instr_bytes
  cmd = Message.direct_command_with_reply(body)
  send(cmd.bytes)

  reply = Message.reply_from_bytes(receive)
  assert_match(reply.msgid, cmd.msgid, "Reply id")
  if reply.error?
    raise VMError # no details?
  end

  reply.globals
end

#system_command_with_reply(instr_bytes) ⇒ Object



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

def system_command_with_reply(instr_bytes)
  cmd = Message.system_command_with_reply(instr_bytes)
  send(cmd.bytes)

  reply = Message.reply_from_bytes(receive)
  assert_match(reply.msgid, cmd.msgid, "Reply id")
  assert_match(reply.command, unpack_u8(instr_bytes[0]), "Command num")
  if reply.error?
    raise VMError, "Error: %u" % reply.status
  end

  reply.data
end