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 Attribute Summary

Attributes inherited from Driver

#parent

Instance Method Summary collapse

Methods inherited from Driver

#command, #commands, #connection, #event_topic_name, #initialize, #interval, #known_command?, #method_missing, #pin

Constructor Details

This class inherits a constructor from Artoo::Drivers::Driver

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Artoo::Drivers::Driver

Instance Method Details

#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
30
# File 'lib/artoo/drivers/button.rb', line 19

def start_driver
  listener = ->(value) { update(value) }
  connection.on("digital-read-#{pin}", listener)
  connection.set_pin_mode(pin, Firmata::Board::INPUT)
  connection.toggle_pin_reporting(pin)

  every(interval) do
    connection.read_and_process
  end

  super
end

#update(value) ⇒ Object

Publishes events according to the button feedback



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/artoo/drivers/button.rb', line 33

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