Class: Domotics::Arduino::BoardEmulator

Inherits:
Object
  • Object
show all
Defined in:
lib/domotics/arduino/board_emulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ BoardEmulator

Returns a new instance of BoardEmulator.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/domotics/arduino/board_emulator.rb', line 6

def initialize(args = {})
  success = ArduinoBase::SUCCESSREPRLY
  @master, @slave = PTY.open
  @type = args[:type] || :normal
  @state_list = Hash.new(ArduinoBase::LOW)
  Thread.new do
    loop do
      case @type
      when :dead
        terminate
      when :crasy
        @master.gets
        @master.puts rand(4).times.map{ rand(10) }.join(' ')
      when :disconnect
        @master.gets
        @slave.close
        @master.close
      else
        message = @master.gets
        command, pin, value = message.chomp.split(" ").map{ |m| m.to_i }
        case command
        when ArduinoBase::ECHOREPLY
          @master.puts "#{value} #{pin}"
        when ArduinoBase::SETDIGITAL
          @state_list[pin] = value
          @master.puts success
        else
          @master.puts success
        end
      end
    end
  end
end

Instance Attribute Details

#state_listObject

Returns the value of attribute state_list.



5
6
7
# File 'lib/domotics/arduino/board_emulator.rb', line 5

def state_list
  @state_list
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/domotics/arduino/board_emulator.rb', line 5

def type
  @type
end

Instance Method Details

#portObject



39
40
41
# File 'lib/domotics/arduino/board_emulator.rb', line 39

def port
  @slave.path
end

#set_internal_state(pin, state) ⇒ Object



42
43
44
# File 'lib/domotics/arduino/board_emulator.rb', line 42

def set_internal_state(pin, state)
  @state_list[pin] = state
end

#toggle_pin(pin) ⇒ Object



45
46
47
48
# File 'lib/domotics/arduino/board_emulator.rb', line 45

def toggle_pin(pin)
  @state_list[pin] = @state_list[pin] == ArduinoBase::LOW ? ArduinoBase::HIGH : ArduinoBase::LOW
  @master.puts "0 #{pin} #{@state_list[pin]}"
end