class MyFirmataPluginButton

a plugin for the myfirmata gem

Public Class Methods

new(arduino, settings: {}, variables: {}) click to toggle source
# File lib/myfirmata-plugin-button.rb, line 11
def initialize(arduino, settings: {}, variables: {})
  
  @arduino = arduino
  @settings, @variables = settings, variables
  
end

Public Instance Methods

on_start()
Alias for: start
start() click to toggle source
# 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
Also aliased as: on_start