Class: MovingsignApi::Command

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/movingsign_api/commands/command.rb

Overview

Command class, subclassed by each MovingsignApi class

When subclassing, be sure to implement #command_code and #command_payload_bytes

Also see some useful subclasses: WriteTextCommand, WriteControlCommand

Direct Known Subclasses

WriteControlCommand, WriteTextCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#receiverObject

The receiving sign’s address. See SenderReceiverAddress



16
17
18
# File 'lib/movingsign_api/commands/command.rb', line 16

def receiver
  @receiver
end

#senderObject

The sender’s address. See SenderReceiverAddress



14
15
16
# File 'lib/movingsign_api/commands/command.rb', line 14

def sender
  @sender
end

Instance Method Details

#command_codeString

Returns the command identifier string. See specification for list of valid command codes.

Returns:

  • (String)

Raises:



29
30
31
# File 'lib/movingsign_api/commands/command.rb', line 29

def command_code
  raise MovingsignApi::NotImplementedError, "Needs to be implemented in subclass."
end

#to_bytesArray<Byte>

Returns a byte array representing this command, appropriate for sending to the sign’s serial port

Returns:

  • (Array<Byte>)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/movingsign_api/commands/command.rb', line 36

def to_bytes
  # set defaults
  self.sender ||= :pc
  self.receiver ||= 1

  bytes = []

  bytes.concat [0x00] * 5                               # start of command
  bytes.concat [0x01]                                   # <SOH>
  bytes.concat self.sender.to_bytes                     # Sender Address
  bytes.concat self.receiver.to_bytes                   # Reciver Address
  bytes.concat [0x02]                                   # <STX>
  bytes.concat string_to_ascii_bytes(command_code)      # Command Code
  bytes.concat command_payload_bytes                    # command specific payload
  bytes.concat [0x03]                                   # <ETX>
  bytes.concat generate_checksum_bytes(bytes[10..-1])   # Checksum bytes (4)
  bytes.concat [0x04]                                   # <EOT>

  bytes
end