Class: TermuxRubyApi::SubSystems::Sensor

Inherits:
Base
  • Object
show all
Defined in:
lib/termux_ruby_api/sub_systems/sensor.rb

Instance Attribute Summary

Attributes inherited from Base

#owner

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from TermuxRubyApi::SubSystems::Base

Instance Method Details

#capture(delay: 1000, limit: nil, sensors:, &block) ⇒ Object

Note:

If limit==nil this method never returns. In that case is normal to invoke it from inside a thread or a child process.

Produces continues updates of the selected sensors If a block is passed, the results are yielded to the block as soon as they’re ready in the form of an array of hashes, where each element of the array corresponds to one of the captured sensors. If no block is passed, the IO object for the output string and the wait Thread are returned. See Base#json_streamed_api_command for details.

Parameters:

  • delay (Fixnum) (defaults to: 1000)

    time in milliseconds to wait between updates

  • limit (Fixnum) (defaults to: nil)

    number of updates. If nil, it sends updates forever

  • sensors (Array <String>)

    names of sensors to capture (as returned from #list)



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/termux_ruby_api/sub_systems/sensor.rb', line 16

def capture(delay: 1000, limit: nil, sensors:, &block)
  args = []
  args += ['-n', limit.to_s] unless limit.nil?
  args += ['-d', delay.to_s] unless delay.nil?
  args += ['-s', sensors.join(',')]
  if block_given?
    owner.json_streamed_api_command('sensor', nil, *args) do |json_result|
      yield json_result
    end
  else
    return owner.json_streamed_api_command('sensor', nil, *args)
  end
end

#capture_once(*sensors, &block) ⇒ Object

Gets a single update of the selected sensors If a block is passed, the results are yielded to the block as soon as they’re ready in the form of an array of hashes, where each element of the array corresponds to one of the captured sensors. If no block is passed, the array of hashes is returned.

Parameters:

  • sensors (String)

    names of sensors to capture (as returned from #list)



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/termux_ruby_api/sub_systems/sensor.rb', line 36

def capture_once(*sensors, &block)
  data = nil
  capture(limit: 1, sensors: sensors) do |json_result|
    data = json_result
  end
  if block_given?
    yield data
  else
    return data
  end
end

#listString

Returns the list of available sensors

Returns:

  • (String)


50
51
52
# File 'lib/termux_ruby_api/sub_systems/sensor.rb', line 50

def list
  owner.json_api_command('sensor', nil, '-l')&.fetch(:sensors)
end