Class: HumbleRPiPluginKnockSensor

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

Instance Method Summary collapse

Constructor Details

#initialize(settings: {}, variables: {}) ⇒ HumbleRPiPluginKnockSensor

Returns a new instance of HumbleRPiPluginKnockSensor.



10
11
12
13
14
15
16
17
# File 'lib/humble_rpi-plugin-knocksensor.rb', line 10

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

  @threshold = variables[:threshold] || 10
  @notifier = variables[:notifier]
  @device_id = variables[:device_id] || 'pi'
  super pin: settings[:pin], clock: 18, dout: 23, din: 24, cs: 25
  
end

Instance Method Details

#startObject Also known as: on_start



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
# File 'lib/humble_rpi-plugin-knocksensor.rb', line 19

def start()
  
  
  t0 = Time.now + 1
  
  puts 'ready to detect knocks'
  
  loop do
    
    value = self.read()
    
    next if value <= @threshold

    # ignore any movements that happened less than 100
    #               milliseconds ago since the last movement
    if t0 + 0.1 < Time.now then          
                
      @notifier.notice "%s/knock: detected" % [@device_id]        
      t0 = Time.now

    end
          
  end
  
  
end