Class: HumbleRPiPluginButton
- Inherits:
-
Object
- Object
- HumbleRPiPluginButton
- Defined in:
- lib/humble_rpi-plugin-button.rb
Instance Method Summary collapse
-
#initialize(settings: {ignore_keyup: true}, variables: {}) ⇒ HumbleRPiPluginButton
constructor
A new instance of HumbleRPiPluginButton.
- #start ⇒ Object (also: #on_start)
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
#start ⇒ Object 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 |, i| puts 'button %s on GPIO %s enabled ' % [i+1, ] n = (i+1).to_s Thread.new do .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 |