Class: Artoo::Drivers::Button

Inherits:
Driver
  • Object
show all
Defined in:
lib/artoo/drivers/button.rb

Overview

Button driver behaviors for Firmata

Constant Summary collapse

COMMANDS =
[:is_pressed?].freeze
DOWN =
1
UP =
0

Instance Method Summary collapse

Instance Method Details

#eventsObject



42
43
44
# File 'lib/artoo/drivers/button.rb', line 42

def events
  connection.async_events
end

#find_event(name) ⇒ Object



38
39
40
# File 'lib/artoo/drivers/button.rb', line 38

def find_event(name)
  events.index {|e| e.name == name.to_sym}
end

#handle_eventsObject



31
32
33
34
35
36
# File 'lib/artoo/drivers/button.rb', line 31

def handle_events
  while i = find_event("digital_read_#{pin}") do
    event = events.slice!(i)
    update(event.data.first) if !event.nil?
  end
end

#is_pressed?Boolean

Returns True if pressed.

Returns:

  • (Boolean)

    True if pressed



13
14
15
# File 'lib/artoo/drivers/button.rb', line 13

def is_pressed?
  (@is_pressed ||= false) == true
end

#start_driverObject

Sets values to read and write from button and starts driver



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/artoo/drivers/button.rb', line 19

def start_driver
  connection.set_pin_mode(pin, Firmata::PinModes::INPUT)
  connection.toggle_pin_reporting(pin)

  every(interval) do
    connection.read_and_process
    handle_events
  end

  super
end

#update(value) ⇒ Object

Publishes events according to the button feedback



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/artoo/drivers/button.rb', line 47

def update(value)
  if value == DOWN
    @is_pressed = true
    publish(event_topic_name("update"), "push", value)
    publish(event_topic_name("push"), value)
  else
    @is_pressed = false
    publish(event_topic_name("update"), "release", value)
    publish(event_topic_name("release"), value)
  end
end