Class: CeladonRemote::Receiver

Inherits:
Object
  • Object
show all
Defined in:
lib/celadonremote/receiver.rb

Instance Method Summary collapse

Constructor Details

#initialize(port, code = (5..6), eol: "\r\n", eot: "N!") ⇒ Receiver

Returns a new instance of Receiver.



3
4
5
6
7
8
9
# File 'lib/celadonremote/receiver.rb', line 3

def initialize(port, code=(5..6), eol:"\r\n", eot:"N!")
  @ser = SerialPort.new(port, 9600, 8, 1, SerialPort::NONE)
  @eol = eol
  @code = code
  @eot = eot
  @codes = {}
end

Instance Method Details

#oncode(code, &b) ⇒ Object



11
12
13
# File 'lib/celadonremote/receiver.rb', line 11

def oncode(code, &b)
  @codes[code] = b
end

#start_listener(allow_hold: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/celadonremote/receiver.rb', line 15

def start_listener(allow_hold:false)
  prev_line = ""

  loop do
    l=@ser.readline(@eol)

    if allow_hold && l.start_with?(@eot)
      code = prev_line[@code]
    elsif l.start_with?(@eot)
      next
    else
      code = l[@code]
      prev_line = l
    end

    if @codes[code]
      @codes[code].call
    end
  end
end