Class: Ev3dev::Led

Inherits:
Device show all
Defined in:
lib/ev3dev/led.rb

Constant Summary collapse

PATH =
"/sys/class/leds"
GREEN =
[255,   0]
RED =
[  0, 255]
AMBER =
[255, 255]
MAX_BRIGHTNESS =
255

Instance Attribute Summary collapse

Attributes inherited from Device

#device_path

Instance Method Summary collapse

Constructor Details

#initializeLed

Returns a new instance of Led.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ev3dev/led.rb', line 13

def initialize()
  raise "couldn't find LED attributes" unless File.exist?(PATH)

  @left_green   = set_led_path("left" , "green")
  @left_red     = set_led_path("left" , "red"  )
  @right_green  = set_led_path("right", "green")
  @right_red    = set_led_path("right", "red"  )

  @default_paths  = [@left_green, @left_red, @right_green, @right_red]
  @left_paths     = [@left_green, @left_red]
  @right_paths    = [@right_green, @right_red]

  @paths = @default_paths
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



127
128
129
130
131
132
# File 'lib/ev3dev/led.rb', line 127

def method_missing(name, *args, &block)
  raise "couldn't specify the left/right green/red LED" if @paths.size != 1
  @device_path = @paths[0]

  super
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



3
4
5
# File 'lib/ev3dev/led.rb', line 3

def paths
  @paths
end

Instance Method Details



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ev3dev/led.rb', line 70

def blink(*args)
  args = args.flatten
  args = [MAX_BRIGHTNESS, MAX_BRIGHTNESS] if args.empty?
  set_each_trigger_brightness(@paths, "timer", args)

  if args.size == 3
    if @paths.size == 1
      @paths.each do |path|
        brightness = args[0]
        next if brightness == 0

        delay_on_time  = args[1]
        delay_off_time = args[2]

        write_value_with_check(path, "delay_on" , delay_on_time)
        write_value_with_check(path, "delay_off", delay_off_time)
      end
    else
      raise "ArgumentError: wrong number of arguments (3 for 4)"
    end
  end

  if args.size == 4
    @paths.each_with_index do |path, index|
      brightness = args[index % 2]
      next if brightness == 0

      delay_on_time  = args[2]
      delay_off_time = args[3]

      write_value_with_check(path, "delay_on" , delay_on_time)
      write_value_with_check(path, "delay_off", delay_off_time)
    end
  end

  @paths = @default_paths
end

#flashObject



119
120
121
122
123
124
125
# File 'lib/ev3dev/led.rb', line 119

def flash()
  raise "couldn't specify the left/right green/red LED" if @paths.size != 1
  path = @paths[0]
  write_value_to_file(path, "activate", 1)

  @paths = @default_paths
end

#leftObject



28
29
30
31
# File 'lib/ev3dev/led.rb', line 28

def left()
  @paths = @left_paths
  self
end

#left_greenObject



38
39
40
41
# File 'lib/ev3dev/led.rb', line 38

def left_green()
  @paths = [@left_green]
  self
end

#left_redObject



43
44
45
46
# File 'lib/ev3dev/led.rb', line 43

def left_red()
  @paths = [@left_red]
  self
end

#offObject



66
67
68
# File 'lib/ev3dev/led.rb', line 66

def off()
  self.on(0, 0)
end

#on(*args) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/ev3dev/led.rb', line 58

def on(*args)
  args = args.flatten
  args = [MAX_BRIGHTNESS, MAX_BRIGHTNESS] if args.empty?
  set_each_trigger_brightness(@paths, "none", args)

  @paths = @default_paths
end

#rightObject



33
34
35
36
# File 'lib/ev3dev/led.rb', line 33

def right()
  @paths = @right_paths
  self
end

#right_greenObject



48
49
50
51
# File 'lib/ev3dev/led.rb', line 48

def right_green()
  @paths = [@right_green]
  self
end

#right_redObject



53
54
55
56
# File 'lib/ev3dev/led.rb', line 53

def right_red()
  @paths = [@right_red]
  self
end

#set_flash(duration_time = 10) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/ev3dev/led.rb', line 108

def set_flash(duration_time = 10)
  raise "couldn't specify the left/right green/red LED" if @paths.size != 1
  path = @paths[0]
  write_value_to_file(path, "brightness", 0)
  write_value_to_file(path, "trigger"   , "transient")
  write_value_with_check(path, "duration" , duration_time)
  write_value_with_check(path, "state"    , 1)

  @paths = @default_paths
end