Class: LegoEv3::InfraredSensor

Inherits:
LegoSensor show all
Defined in:
lib/sensors/infrared.rb

Overview

More info: www.ev3dev.org/docs/sensors/lego-ev3-infrared-sensor/ TODO: make sense of proximity

Instance Attribute Summary

Attributes inherited from LegoSensor

#value

Instance Method Summary collapse

Constructor Details

#initialize(connection, id, port) ⇒ InfraredSensor

Returns a new instance of InfraredSensor.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sensors/infrared.rb', line 5

def initialize(connection, id, port)
  super(connection, id, port, 'lego-ev3-ir')

  @supported_modes = {
    proximity: 'IR-PROX',
    seek: 'IR-SEEK',
    control: 'IR-REMOTE',
    control_alt: 'IR-REM-A'
  }

  @modes_value_parts = {
    proximity: 1,
    seek: 8,
    control: 4,
    control_alt: 1
  }

  @control_pressed_buttons = [
    [],
    [:red_up],
    [:red_down],
    [:blue_up],
    [:blue_down],
    [:red_up, :blue_up],
    [:red_up, :blue_down],
    [:red_down, :blue_up],
    [:red_down, :blue_down],
    [:beacon_on],
    [:red_up, :red_down],
    [:blue_up, :blue_down]
  ]

  self.mode = :proximity

  @channel = 1

  init_values
end

Instance Method Details

#channelObject



44
45
46
# File 'lib/sensors/infrared.rb', line 44

def channel
  @channel
end

#channel=(new_value) ⇒ Object



48
49
50
51
52
# File 'lib/sensors/infrared.rb', line 48

def channel=(new_value)
  @channel = [1, new_value.to_i, 4].sort[1]

  init_values
end

#infoObject



81
82
83
84
85
86
87
88
# File 'lib/sensors/infrared.rb', line 81

def info
  super.merge({
    sub_type: :infrared,
    mode: @mode,
    value: @value,
    channel: @channel
  })
end

#modeObject



54
55
56
# File 'lib/sensors/infrared.rb', line 54

def mode
  @mode = parse_mode(LegoEv3::Commands::LegoSensor.get_mode!(@connection, @id))
end

#mode=(new_value) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sensors/infrared.rb', line 58

def mode=(new_value)
  to_set = @supported_modes[new_value.to_sym]
  throw new InvalidModeException(:sensor, :infrared, new_value, @supported_modes.keys) unless to_set

  LegoEv3::Commands::LegoSensor.set_mode(@connection, @id, to_set)
  LegoEv3::Commands::LegoSensor.get_mode(@connection, @id) do |m|
    @mode = parse_mode(m)
  end

  @connection.flush

  init_values

  @mode
end

#pollObject



74
75
76
77
78
79
# File 'lib/sensors/infrared.rb', line 74

def poll
  throw new InvalidChannelException(@channel) if @mode == :control_alt && @channel != 1
  @last_value_raw = @value_raw
  @value_raw = poll_value(@modes_value_parts[@mode])
  @value = parse_value(@value_raw)
end