Class: HumbleRPiPluginButton

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

Instance Method Summary collapse

Constructor Details

#initialize(settings: {ignore_keyup: true}, variables: {}) ⇒ HumbleRPiPluginButton

Returns a new instance of HumbleRPiPluginButton.



12
13
14
15
16
17
18
19
# File 'lib/humble_rpi-plugin-button.rb', line 12

def initialize(settings: {ignore_keyup: true}, variables: {})

  @ignore_keyup = settings[:ignore_keyup] || true
  @pins = settings[:pins].map {|x| RPiPinIn.new x, pull: :up}
  @notifier = variables[:notifier]
  @device_id = variables[:device_id] || 'pi'
  
end

Instance Method Details

#startObject Also known as: on_start



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
59
60
61
62
63
64
65
# File 'lib/humble_rpi-plugin-button.rb', line 21

def start()
  
  notifier = @notifier
  device_id = @device_id
  
  t0 = Time.now + 1
      
  puts 'ready to detect buttons'
  
  @pins.each.with_index do |button, i|
    
    puts 'button %s on GPIO %s enabled ' % [i+1, button]
          
    n = (i+1).to_s
          
    Thread.new do      
      
      button.watch do |value|
        
        # ignore any movements that happened 250 
        #               milliseconds ago since the last movement
        if t0 + 0.25 < Time.now then                   
            
          state = case value
          when 1
            @ignore_keyup ? :press : :down
          when 0
            :up unless @ignore_keyup
          end

          if state            
            notifier.notice "%s/button/%s: key%s" % [device_id, i, state]
          end
          
          t0 = Time.now
          
        end
      end
      
    end      
    
  end    

  
end