Skip to content

Instantly share code, notes, and snippets.

@LevitatingBusinessMan
Last active September 20, 2022 09:59
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 LevitatingBusinessMan/abe0c8a79a3572c3f2568412baed0e30 to your computer and use it in GitHub Desktop.
Save LevitatingBusinessMan/abe0c8a79a3572c3f2568412baed0e30 to your computer and use it in GitHub Desktop.
Morse code on your numkey
#!/usr/bin/ruby
require './morse_dict.rb'
require 'readline'
LED = '/sys/class/leds/input3::numlock/brightness'
OLD = File.read LED
File.write LED, 0
DIT_LENGTH = 0.1
def light(duration)
File.write LED, 1
sleep duration * DIT_LENGTH
File.write LED, 0
sleep 3 * DIT_LENGTH
end
def flash(morse)
for c in morse.chars
print c
case c
when '.'
light 1
when '-'
light 3
when ' '
sleep 5 * DIT_LENGTH
else
STDERR.puts "Wrong character '#{c}' in flash"
end
end
puts
end
while input = Readline.readline("morse> ", true)
morse = ""
for c in input.chars
if !MORSE_DICT.keys.include?(c)
STDERR.puts "Invalid character '#{c}'"
break
end
morse << MORSE_DICT[c]
end
#puts morse
flash morse
end
File.write LED, OLD
MORSE_DICT = {
"a" => ".-",
"b" => "-...",
"c" => "-.-.",
"d" => "-..",
"e" => ".",
"f" => "..-.",
"g" => "--.",
"h" => "....",
"i" => "..",
"j" => ".---",
"k" => "-.-",
"l" => ".-..",
"m" => "--",
"n" => "-.",
"o" => "---",
"p" => ".--.",
"q" => "--.-",
"r" => ".-.",
"s" => "...",
"t" => "-",
"u" => "..-",
"v" => "...-",
"w" => ".--",
"x" => "-..-",
"y" => "-.--",
"z" => "--..",
" " => " ",
"1" => ".----",
"2" => "..---",
"3" => "...--",
"4" => "....-",
"5" => ".....",
"6" => "-....",
"7" => "--...",
"8" => "---..",
"9" => "----.",
"0" => "-----"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment