Class: MovingsignApi::Sign

Inherits:
Object
  • Object
show all
Defined in:
lib/movingsign_api/sign.rb

Overview

Manipulates a Movingsign attached to a serial port

Examples:

Construct a sign

require 'movingsign_api'

sign = MovingsignApi::Sign.new('/dev/ttyUSB0')

Show Text

sign.show_text('Hello World!')

Turn Sound Off

sign.set_sound false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_path) ⇒ Sign



22
23
24
# File 'lib/movingsign_api/sign.rb', line 22

def initialize(device_path)
  @device_path = device_path
end

Instance Attribute Details

#device_pathObject

Serial port device path (ie: /dev/ttyUSB0)



19
20
21
# File 'lib/movingsign_api/sign.rb', line 19

def device_path
  @device_path
end

Instance Method Details

#send_command(command) ⇒ self

Sends the specified Movingsign command to this sign’s serial port



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/movingsign_api/sign.rb', line 70

def send_command(command)
  SerialPort.open(self.device_path, 9600, 8, 1) do |port|
    # flush anything existing on the port
    port.flush
    flush_read_buffer(port)

    byte_string = command.to_bytes.pack('C*')

    begin
      while byte_string && byte_string.length != 0
        count = port.write_nonblock(byte_string)
        byte_string = byte_string[count,-1]

        port.flush
      end
    rescue IO::WaitWritable
      if IO.select([], [port], [], 5)
        retry
      else
        raise IOError, "Timeout writing command to #{self.device_path}"
      end
    end

    # wait for expected confirmation signals
    got_eot = false
    got_soh = false
    loop do
      begin
        c = port.read_nonblock(1)

        case c
          when "\x04"
            if ! got_eot
              got_eot = true
            else
              raise IOError, "Got EOT reply twice from #{self.device_path}"
            end
          when "\x01"
            if got_eot
              if ! got_soh
                got_soh = true

                break
              else
                raise IOError, "Got SOH twice from #{self.device_path}"
              end
            else
              raise IOError, "Got SOH before EOT from #{self.device_path}"
            end
        end
      rescue IO::WaitReadable
        if IO.select([port], [], [], 3)
          retry
        else
          raise IOError, "Timeout waiting for command reply from #{self.device_path}.  EOT:#{got_eot} SOH:#{got_soh}"
        end
      end
    end
  end

  self
end

#set_sound(on) ⇒ self

Turns on/off sound when the sign receives a command

This is a shorthand for MovingsignApi::SetSoundCommand



59
60
61
62
63
# File 'lib/movingsign_api/sign.rb', line 59

def set_sound(on)
  cmd = SetSoundCommand.new on

  send_command cmd
end

#show_identityObject

Show the #device_path value on the display (useful for diagnostics)



27
28
29
30
31
32
# File 'lib/movingsign_api/sign.rb', line 27

def show_identity
  cmd = WriteTextCommand.new
  cmd.text = self.device_path

  send_command cmd
end

#show_text(text, options = {}) ⇒ self Also known as: write_text

Displays the given text on the board.

This is short-hand for the WriteTextCommand

Options Hash (options):



43
44
45
46
47
48
49
# File 'lib/movingsign_api/sign.rb', line 43

def show_text(text, options = {})
  cmd = WriteTextCommand.new
  cmd.display_pause = options[:display_pause] if options[:display_pause]
  cmd.text = text

  send_command cmd
end