morseout.rb

Path: morseout.rb
Last Update: Thu Aug 24 15:35:02 PDT 2006

A sample app highlighting one of the more amusing features of the evdev library - the ability to control keyboard LEDs. The utility takes data on STDIN and ouputs it to the scoll lock LED of the given event device - in morse code. I think you could use this same functionality to do something a lot more useful (say, flash when you’ve got mail).

Usage

morseout.rb [—find-devices | -f]

morseout.rb —device /dev/input/eventX

morseout.rb —help | -h

device
One of the evdev devices in /dev/input/event*. It has to have an LED feature_type and the SCROLL feature. This basically means a keyboard device. To figure out which devices support this, use the —find-devices option
find-devices
lists the path and name of all devices that seem to support the required interface in /dev/input/event*. This is the default behavior if no other options are specified.

Examples

echo ‘SOS’ | sudo ./morseout.rb —device /dev/input/event3

sudo ./morseout.rb —find-devices

Required files

evdev   optparse   rdoc/usage  

Methods

Public Instance methods

[Source]

     # File morseout.rb, line 111
111: def display_code(c)
112:   if(c == :dot)
113:     flash_led($sound_quanta)
114:     sleep($sound_quanta)
115:   elsif(c == :dash)
116:     flash_led($sound_quanta*3)
117:     sleep($sound_quanta)
118:   elsif(c == :letter_pause)
119:     sleep($sound_quanta*3)
120:   elsif(c == :word_pause)
121:     sleep($sound_quanta*7)
122:   else
123:     raise("unknown code #{c} passed to display_code")
124:   end
125: end

[Source]

     # File morseout.rb, line 127
127: def find_devices()
128:   filenames = Dir.glob('/dev/input/event*')
129:   filenames.each { |file|
130:     Evdev::EventDevice.open(file) { |device|
131:       begin
132:         device.feature_type_named('LED').feature_named('SCROLLL')
133:         puts "#{file} (#{device.device_name})"
134:       rescue StandardError => e
135:         # if a exception was raised, the device does not support this feature
136:       end
137:     }
138:   }
139: end

[Source]

     # File morseout.rb, line 105
105: def flash_led(seconds)
106:   $led.write_value(1)
107:   sleep(seconds)
108:   $led.write_value(0)
109: end

[Source]

     # File morseout.rb, line 100
100: def morse_for_character(c)
101:   result = $mapping[c]
102:   return result + [:letter_pause]
103: end

[Validate]