Class: ArduinoIrRemote::Device

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/arduino_ir_remote/ir_remote.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ Device

Returns a new instance of Device.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/arduino_ir_remote/ir_remote.rb', line 7

def initialize(port)
  @status = Status::CLOSE
  @state = nil
  @serial = SerialPort.new(port, 57600, 8, 1, SerialPort::NONE) # 57600bps, 8bit, stopbit1, parity-none
  @status = Status::OPEN
  Thread.new do
    while status == Status::OPEN do
      process_input @serial.gets.strip
    end
  end
  @temp_pin = 0
  @analogs = Array.new 6, 0
  sleep 3
end

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/arduino_ir_remote/ir_remote.rb', line 5

def status
  @status
end

#temp_pinObject

Returns the value of attribute temp_pin.



4
5
6
# File 'lib/arduino_ir_remote/ir_remote.rb', line 4

def temp_pin
  @temp_pin
end

Instance Method Details

#analog_read(pin) ⇒ Object



41
42
43
# File 'lib/arduino_ir_remote/ir_remote.rb', line 41

def analog_read(pin)
  @analogs[pin]
end

#closeObject



22
23
24
25
26
# File 'lib/arduino_ir_remote/ir_remote.rb', line 22

def close
  return if status == Status::CLOSE
  @status = Status::CLOSE
  @serial.close
end

#read(&block) ⇒ Object



36
37
38
39
# File 'lib/arduino_ir_remote/ir_remote.rb', line 36

def read(&block)
  once :__ir_read, &block if block_given?
  @serial.write "r"
end

#temp_sensorObject



45
46
47
# File 'lib/arduino_ir_remote/ir_remote.rb', line 45

def temp_sensor
  analog_read(@temp_pin).to_f*5*100/1024
end

#wait(&block) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/arduino_ir_remote/ir_remote.rb', line 49

def wait(&block)
  loop do
    if block_given?
      yield
    else
      sleep 1
    end
  end
end

#write(data) ⇒ Object



29
30
31
32
33
34
# File 'lib/arduino_ir_remote/ir_remote.rb', line 29

def write(data)
  "w#{data}W".split(//).each do |c|
    @serial.write c
    sleep 0.001
  end
end