Class: MyFirmataPluginButton

Inherits:
Object
  • Object
show all
Defined in:
lib/myfirmata-plugin-button.rb

Instance Method Summary collapse

Constructor Details

#initialize(arduino, settings: {}, variables: {}) ⇒ MyFirmataPluginButton

Returns a new instance of MyFirmataPluginButton.



11
12
13
14
15
16
# File 'lib/myfirmata-plugin-button.rb', line 11

def initialize(arduino, settings: {}, variables: {})
  
  @arduino = arduino
  @settings, @variables = settings, variables
  
end

Instance Method Details

#startObject Also known as: on_start



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/myfirmata-plugin-button.rb', line 18

def start()
      
  puts 'ready to detect button'
  
  t = Time.now
  
  notifier = @variables[:notifier]
  topic = @variables[:device_id] + '/button'
  msg = @settings[:msg] || 'pressed'
  pinx = @settings[:pin] || 2
  triggers = @settings[:event_triggers] || @settings[:trigger_events] || 1

  interval = if triggers == 1 then
    @settings[:interval] || 1
  else
    @settings[:interval] || 0.1
  end
  
  old_state, state = 'up', ''

  @arduino.on :digital_read do |pin, pressed|
    
    if pin == pinx and t + interval < Time.now then
      
      if triggers == 1 then
        notifier.notice "%s: %s" % [topic, msg] if pressed
      else
        
        state = pressed ? 'down' : 'up'          
        next if state == old_state

        notifier.notice "%s: %s" % [topic, state]
        old_state = state
      end
      
      t = Time.now
      
    end
  end

end