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_u16, #unpack_u32, #unpack_u8

Constructor Details

#initialize(connection) ⇒ MessageSender

Returns a new instance of MessageSender.



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

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

Instance Method Details

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



13
14
15
16
17
18
# File 'lib/lignite/message_sender.rb', line 13

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



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

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.data
end

#system_command_with_reply(instr_bytes) ⇒ Object



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

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, %u" % reply.status
  end

  reply.data
end