Skip to content

Instantly share code, notes, and snippets.

@lxe
Last active December 18, 2015 10:39
Show Gist options
  • Save lxe/5770450 to your computer and use it in GitHub Desktop.
Save lxe/5770450 to your computer and use it in GitHub Desktop.
function morse(s, speed) {
s = s.toLowerCase().replace(/[^\w ]/g, '');
var codes = {
'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' : '----.'
}
var speed = speed || 200
, toneDelay = dot = speed
, letterDelay = dash = speed * 3
, $handle = $('.handle');
function tone(len) {
return function(next) {
$handle.mousedown();
setTimeout(function() {
$handle.mouseup();
setTimeout(next, toneDelay)
}, len);
}
}
function tones(c) {
return function(next) {
console.log(c);
var fn = function() {
setTimeout(next, letterDelay);
};
if (c === ' ') return fn();
var code = codes[c];
code.split('').reverse().forEach(function(l) {
var t = tone(l === '-' ? dash : dot);
fn = t.bind({ }, fn);
});
return fn();
}
}
var fn = function() {
console.log('done!');
};
s.split('').reverse().forEach(function(c) {
var t = tones(c);
fn = t.bind({ }, fn);
});
return fn();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment