Making Your Keyboard Lights Blink

Problem

You want to control the three standard keyboard LEDs (num lock, caps lock, and scroll lock) from a Ruby script.

Solution

Use the Blinkenlights library, available as the blinkenlights gem. It works on Windows or Linux (but not on Mac OS X), and it lets you toggle the lights individually or in patterns:

	require 
ubygems
	require linkenlights

	# Turn individual 
lights on or off.
	BlinkenLights.open do |lights|
	 lights.left = true
	 lights.middle = true
	 lights.right = true

	 lights.scr = false
	 lights.cap = false
	 lights.num = false
	end

	# Display a light show.
	BlinkenLights.open do |lights|
	 lights.left_to_right
	 10.times { lights.random }
	 lights.right_to_left
	end

Discussion

The keyboard lights are an often-overlooked user interface. They were originally designed to reflect information about the state of the keyboard itself, but they can be manipulated from the computer to display more interesting things. Each light can continually display one bit of information (such as whether you have new email), or can flash over time to indicate a rate (such as your computers use of incoming or outgoing bandwidth).

BlinkenLights works by writing special command codes to the Unix keyboard device (/dev/tty8 is the default, but /dev/console should also work). Usually, you can only write to these devices when running as root.

On Windows, BlinkenLights works by sending key events that make Windows think you actually hit the corresponding key. This means that if you tell BlinkenLights on Windows to turn on your caps lock light, caps lock itself is also enabled. The state of the light can be disconnected from the state of the keyboard.

When you pass a code block into Blinkenlights.open, BlinkenLights runs the block and then restores the original state of the lights. This avoids confusing those users who use their lights to keep track of the state of their keyboards. If you want your setting of the lights to persist until they e changed again, then use the return value of Blinkenlights.open instead of passing in a code block.

This code will turn on the first two lights to represent the number six in binary. Until they e changed again, whether through the keyboard or through code, theyll stay on. Even the end of your program won restore the original state of the lights.

	# Display the binary number 6 (that is, 110):
	BlinkenLights.new.set(6)

Heres a program that converts an alphanumeric message to Morse code and displays it on the keyboard lights:

	#!/usr/bin/ruby -w
	# blink_morse.rb
	require 
ubygems
	require linkenlights

	class String

	 # Morse code representations for 0-9 and A-Z.
	 MORSE_TABLE = %w{01111 00111 00011 00001 00000 10000 11000 11100 11110 11111
	 01 1000 1010 100 0 0010 110 0000 00 0111 101 0100 11
	 10 111 0110 1101 010 000 1 001 0001 011 1001 1011 1100}

	 def to_morse(dit_time = 0.3)
	 a = "A"[0]
	 zero = "0"[0]
	 words = upcase.gsub(/[^A-Z0-9s]/, "").split
	 BlinkenLights.open do |lights|
	 words.each do |word|
	 word.each_byte do |letter|
	 code = MORSE_TABLE[letter - (letter < a ? zero : a-10)]
	 code.each_byte do |signal|
	 lights.flash(dit_time * (signal == zero ? 1 : 3))
	 sleep(dit_time) # Space between parts of a letter.
	 end
	 sleep(dit_time * 3) # Space between letters.
	 end
	 sleep(dit_time * 5) # Space between words.
	 end
	 end
	 end
	end

	ARGV.shift.to_s.to_morse if $0 == __FILE_ _

See Also

  • The BlinkenLights homepage at http://blinkenlights.rubyforge.org/; see especially the generated RDoc at http://blinkenlights.rubyforge.org/doc/index.html, which lists the many light patterns defined by the library
  • The examples subdirectory of the installed gem contains sample programs that control the keyboard lights based on your system load or network activity
  • The name "Blinkenlights" is explained at http://www.catb.org/jargon/html/B/blinkenlights.html
  • An explanation of Morse code (http://en.wikipedia.org/wiki/Morse_code)
  • The idea for the blink_morse.rb program comes from Neal Stephensons novel Cryptonomicon


Strings

Numbers

Date and Time

Arrays

Hashes

Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming

XML and HTML

Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration



Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net