class Ev3dev::Led

Constants

AMBER
GREEN
MAX_BRIGHTNESS
PATH
RED

Attributes

paths[R]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

flash() click to toggle source
# 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
left() click to toggle source
# File lib/ev3dev/led.rb, line 28
def left
  @paths = @left_paths
  self
end
left_green() click to toggle source
# File lib/ev3dev/led.rb, line 38
def left_green
  @paths = [@left_green]
  self
end
left_red() click to toggle source
# File lib/ev3dev/led.rb, line 43
def left_red
  @paths = [@left_red]
  self
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method Ev3dev::Device#method_missing
# 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
off() click to toggle source
# File lib/ev3dev/led.rb, line 66
def off
  self.on(0, 0)
end
on(*args) click to toggle source
# 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
right() click to toggle source
# File lib/ev3dev/led.rb, line 33
def right
  @paths = @right_paths
  self
end
right_green() click to toggle source
# File lib/ev3dev/led.rb, line 48
def right_green
  @paths = [@right_green]
  self
end
right_red() click to toggle source
# File lib/ev3dev/led.rb, line 53
def right_red
  @paths = [@right_red]
  self
end
set_flash(duration_time = 10) click to toggle source
# 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

Private Instance Methods

set_each_trigger_brightness(led_paths, trigger_mode, *led_brightness) click to toggle source
# File lib/ev3dev/led.rb, line 146
def set_each_trigger_brightness(led_paths, trigger_mode, *led_brightness)
  led_brightness = led_brightness.flatten
  raise "ArgumentError: no led_brightness" if led_brightness.empty?

  led_paths.each_with_index do |path, index|
    brightness = led_brightness[index % 2]
    brightness = MAX_BRIGHTNESS if brightness > MAX_BRIGHTNESS

    write_value_to_file(path, "trigger"   , trigger_mode)
    write_value_to_file(path, "brightness", brightness)
  end
end
set_led_path(side, color) click to toggle source
# File lib/ev3dev/led.rb, line 136
def set_led_path(side, color)
  path_array = Dir.glob(["#{PATH}/*#{side}*#{color}*", "#{PATH}/*#{color}*#{side}*"],
                         File::FNM_CASEFOLD)
  if path_array.size == 1
    path_array.first
  else
    raise "no such LED attribute: #{side} #{color}"
  end
end
write_value_to_file(path, attribute, value) click to toggle source
# File lib/ev3dev/led.rb, line 159
def write_value_to_file(path, attribute, value)
  file_path = File.join path, attribute
  raise "no such LED attribute: #{file_path}" unless File.exist? file_path
  IO.write file_path, value
end
write_value_with_check(path, attribute, value) click to toggle source

sysfs group permissions created too late - Issue #225 - ev3dev/ev3dev github.com/ev3dev/ev3dev/issues/225

# File lib/ev3dev/led.rb, line 167
def write_value_with_check(path, attribute, value)
  file_path = File.join path, attribute
  raise "no such LED attribute: #{file_path}" unless File.exist? file_path

  wait_counter = 0
  until File.writable? file_path
    sleep 0.05
    wait_counter += 1
    raise "writable timeout: #{file_path}" if wait_counter > 20
  end
  IO.write file_path, value
end