Class: Denko::Sensor::HCSR04

Inherits:
Object
  • Object
show all
Includes:
Behaviors::MultiPin, Behaviors::Poller
Defined in:
lib/denko/sensor/hcsr04.rb

Constant Summary collapse

SPEED_OF_SOUND =

Speed of sound in meters per second.

343.0

Instance Attribute Summary

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::MultiPin

#pin, #pins, #proxies

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from Behaviors::Poller

#poll, #poll_using, #stop

Methods included from Behaviors::Threaded

#enable_interrupts, included, #stop, #stop_thread, #threaded, #threaded_loop

Methods included from Behaviors::Reader

#read, #read_using, #wait_for_read

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #initialize, #remove_callback, #update

Methods included from Behaviors::State

#initialize, #state

Methods included from Behaviors::MultiPin

#before_initialize, #convert_pins, #proxy_pin, #proxy_states, #require_pin, #require_pins

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Method Details

#_readObject



22
23
24
# File 'lib/denko/sensor/hcsr04.rb', line 22

def _read
  board.hcsr04_read(echo.pin, trigger.pin)
end

#after_initialize(options = {}) ⇒ Object



15
16
17
18
19
20
# File 'lib/denko/sensor/hcsr04.rb', line 15

def after_initialize(options={})
  super(options)

  # Receive values from echo pin.
  echo.add_callback { |data| self.update(data) }
end

#initialize_pins(options = {}) ⇒ Object



10
11
12
13
# File 'lib/denko/sensor/hcsr04.rb', line 10

def initialize_pins(options={})
  proxy_pin :trigger, DigitalIO::Output
  proxy_pin :echo,    DigitalIO::Input
end

#pre_callback_filter(us) ⇒ Object



26
27
28
29
30
# File 'lib/denko/sensor/hcsr04.rb', line 26

def pre_callback_filter(us)
  # Data is microseconds roundtrip time. Convert to mm.
  um = (us/2) * SPEED_OF_SOUND
  mm = um / 1000.0
end