class BirdbrainFinchInput

Constants

DEFAULT_FACTOR
DEFAULT_MAX_RESPONSE
DEFAULT_MIN_RESPONSE
DEFAULT_TYPE_METHOD
DEFAULT_UNLIMITED_MAX_RESPONSE
DEFAULT_UNLIMITED_MIN_RESPONSE
ORIENTATIONS
ORIENTATION_IN_BETWEEN
ORIENTATION_RESULTS

Public Class Methods

accelerometer(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 49
def self.accelerometer(device)
  xyz_response(device, 'finchAccel')
end
compass(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 53
def self.compass(device)
  (response = response_body('hummingbird', 'in', 'finchCompass', 'static', device)).nil? ? response : response.to_i
end
distance(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 27
def self.distance(device)
  distance_options = {}
  distance_options[:factor] = 0.0919
  distance_options[:min_response] = DEFAULT_UNLIMITED_MIN_RESPONSE
  distance_options[:max_response] = DEFAULT_UNLIMITED_MAX_RESPONSE

  sensor(device, 'Distance', 'static', distance_options)
end
encoder(device, direction) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 40
def self.encoder(device, direction)
  encoder_options = {}
  encoder_options[:min_response] = DEFAULT_UNLIMITED_MIN_RESPONSE
  encoder_options[:max_response] = DEFAULT_UNLIMITED_MAX_RESPONSE
  encoder_options[:type_method] = 'to_f'

  sensor(device, 'Encoder', calculate_left_or_right(direction), encoder_options)
end
finch?(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 15
def self.finch?(device)
  request_status(response_body('hummingbird', 'in', 'isFinch', 'static', device))
end
light(device, direction) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 23
def self.light(device, direction)
  sensor(device, 'Light', calculate_left_or_right(direction))
end
line(device, direction) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 36
def self.line(device, direction)
  sensor(device, 'Line', calculate_left_or_right(direction))
end
magnetometer(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 57
def self.magnetometer(device)
  xyz_response(device, 'finchMag')
end
moving?(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 19
def self.moving?(device)
  request_status(response_body('hummingbird', 'in', 'finchIsMoving', 'static', device))
end
orientation(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 61
def self.orientation(device)
  ORIENTATIONS.each_with_index do |orientation, index|
    return nil if (response = response_body('hummingbird', 'in', 'finchOrientation', orientation, device)).nil?

    return ORIENTATION_RESULTS[index] if request_status(response)
  end

  ORIENTATION_IN_BETWEEN
end
orientation_beak_down?(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 75
def self.orientation_beak_down?(device)
  orientation_check(device, 1)
end
orientation_beak_up?(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 71
def self.orientation_beak_up?(device)
  orientation_check(device, 0)
end
orientation_level?(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 87
def self.orientation_level?(device)
  orientation_check(device, 4)
end
orientation_tilt_left?(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 79
def self.orientation_tilt_left?(device)
  orientation_check(device, 2)
end
orientation_tilt_right?(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 83
def self.orientation_tilt_right?(device)
  orientation_check(device, 3)
end
orientation_upside_down?(device) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 91
def self.orientation_upside_down?(device)
  orientation_check(device, 5)
end

Private Class Methods

orientation_check(device, index) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 95
                     def self.orientation_check(device, index)
  request_status(response_body('hummingbird', 'in', 'finchOrientation', ORIENTATIONS[index], device))
end
sensor(device, sensor, other = nil, options = {}) click to toggle source
# File lib/birdbrain/birdbrain_finch_input.rb, line 99
                     def self.sensor(device, sensor, other = nil, options = {})
  return false if other == false # for invalid directions

  factor = options.key?(:factor) ? options[:factor] : DEFAULT_FACTOR
  min_response = options.key?(:min_response) ? options[:min_response] : DEFAULT_MIN_RESPONSE
  max_response = options.key?(:max_response) ? options[:max_response] : DEFAULT_MAX_RESPONSE
  type_method = options.key?(:type_method) ? options[:type_method] : DEFAULT_TYPE_METHOD

  request = ['hummingbird', 'in', sensor]
  request << other unless other.nil?
  request << device

  response = response_body(request)

  (response.nil? ? nil : bounds((response.to_f * factor).send(type_method), min_response, max_response))
end