Skip to content

Instantly share code, notes, and snippets.

@hiroeorz
Created December 29, 2012 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroeorz/4407647 to your computer and use it in GitHub Desktop.
Save hiroeorz/4407647 to your computer and use it in GitHub Desktop.
Controlling LED on beagleboard-xm on Ubuntu Linux from Ruby.
# control led on beagleboard.
class BeagleBoardXmLed
LED_FILE_FORMAT =
"/sys/devices/platform/leds-gpio/leds/beagleboard::@name@/brightness"
# led: ledname (usr0 : usr1 : pmu_stat)
def on(led)
control(led, 1)
end
# led: ledname (usr0 : usr1 : pmu_stat)
def off(led)
control(led, 0)
end
# led: ledname (usr0 : usr1 : pmu_stat)
def status(led)
File.open(filepath(led)) do |f|
state = f.read.chomp
return (state == "1")
end
end
# led: ledname (usr0 : usr1 : pmu_stat)
# interval: brink interval
# count: blink count
def blink(led, opts = {:interval => 1, :count => 5})
interval = opts[:interval]
count = opts[:count].to_i * 2
count.times do
status(led) ? off(led) : on(led)
sleep interval
end
end
private
# led: ledname (usr0 : usr1 : pmu_stat)
# output: 1 or 0
def control(led, output)
check_output output
File.open(filepath(led), "w") do |f|
f.print output.to_s
end
end
# output: 1 or 0
def check_output(output)
unless [1,0].include? output
raise ArgumentError.new "invalid control output:#{output}"
end
end
# led: ledname (usr0 : usr1 : pmu_stat)
def filepath(led)
filepath = LED_FILE_FORMAT.sub(/@name@/, led.to_s)
raise "no such device:#{led}" unless File.exist?(filepath)
filepath
end
end
if __FILE__ == $0
led = BeagleBoardXmLed.new
led.blink :usr0, interval:0.1, count:10
led.blink :usr1, interval:0.1, count:10
led.blink :pmu_stat, interval:0.1, count:10
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment