class MovingsignApi::Command
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}
Attributes
receiver[RW]
The receiving sign's address. See {SenderReceiverAddress}
sender[RW]
The sender's address. See {SenderReceiverAddress}
Public Instance Methods
command_code()
click to toggle source
Returns the command identifier string. See specification for list of valid command codes.
@return [String]
# File lib/movingsign_api/commands/command.rb, line 29 def command_code raise MovingsignApi::NotImplementedError, "Needs to be implemented in subclass." end
receiver=(val)
click to toggle source
# File lib/movingsign_api/commands/command.rb, line 22 def receiver=(val) @receiver = MovingsignApi::SenderReceiverAddress.parse(val) end
sender=(val)
click to toggle source
# File lib/movingsign_api/commands/command.rb, line 18 def sender=(val) @sender = MovingsignApi::SenderReceiverAddress.parse(val) end
to_bytes()
click to toggle source
Returns a byte array representing this command, appropriate for sending to the sign's serial port
@return [Array<Byte>]
# 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
Private Instance Methods
command_payload_bytes()
click to toggle source
Returns command specific byte array payload
@return [Array<Byte>]
# File lib/movingsign_api/commands/command.rb, line 62 def command_payload_bytes raise MovingsignApi::NotImplementedError, "Needs to be implemented in subclass." end
generate_checksum_bytes(payload)
click to toggle source
Returns a checksum string (4 characters) appropriate for sending to the serial port
ie: '12AF' (note capitalization)
# File lib/movingsign_api/commands/command.rb, line 69 def generate_checksum_bytes(payload) sum = payload.reduce :+ sum_hex = ('%04x' % sum).upcase string_to_ascii_bytes sum_hex end