Class: Denko::Sensor::JSNSR04T

Inherits:
Object
  • Object
show all
Includes:
Behaviors::Component, Behaviors::Lifecycle, Behaviors::Poller
Defined in:
lib/denko/sensor/jsnsr04t.rb

Overview

For JSN-SR04T sensor in mode 2 ONLY.

Constant Summary collapse

UART_CLASSES =
[Denko::UART::Hardware, Denko::UART::BitBang]
TIMEOUT =
0.500

Constants included from Behaviors::Reader

Behaviors::Reader::READ_WAIT_TIME

Constants included from Behaviors::Lifecycle

Behaviors::Lifecycle::CALLBACK_METHODS

Instance Attribute Summary collapse

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::State

#state

Attributes included from Behaviors::Component

#board, #params

Instance Method Summary collapse

Methods included from Behaviors::Poller

#poll, #poll_using, #stop

Methods included from Behaviors::Threaded

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

Methods included from Behaviors::Reader

#read, #read_busy?, #read_nb, #read_raw, #read_using, #update

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #pre_callback_filter, #remove_callback, #update

Methods included from Behaviors::State

#update_state

Methods included from Behaviors::Lifecycle

included

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Attribute Details

#uartObject (readonly)

Returns the value of attribute uart.



14
15
16
# File 'lib/denko/sensor/jsnsr04t.rb', line 14

def uart
  @uart
end

Instance Method Details

#_readObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/denko/sensor/jsnsr04t.rb', line 26

def _read
  # Trigger read
  uart.write("U")

  # Get line from UART
  start = Time.now
  line  = nil
  until line || (Time.now - start > TIMEOUT)
    line = uart.gets
    sleep 0.010
  end

  # Extract mm as integer
  if line && line.strip.end_with?("mm")
    value = line.gsub("mm").strip.to_i
    self.update(value)
    return value
  end

  return nil
end