Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pentlander
Created July 14, 2016 00:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pentlander/f1974aeab2d26a2bde609a5c0fa24afe to your computer and use it in GitHub Desktop.
Save pentlander/f1974aeab2d26a2bde609a5c0fa24afe to your computer and use it in GitHub Desktop.
Vibrate to the contact's initials in morse code with Tasker
var dotLength = 110;
var dashLength = 3 * dotLength;
var pauseBetweenElements = 2 * dotLength;
var pauseBetweenWords = 3 * dotLength;
function nameToVibrations(fullName) {
var initials = getInitials(fullName);
var vibrations = initials.map(wordToMorseCode)
.map(morseCodeToVibrations)
.join(",".concat(pauseBetweenWords).concat(","));
return "0,".concat(vibrations);
}
function inspect(value) {
console.log(value);
return value;
}
function morseCodeToVibrations(morseCode) {
console.log("Morse", morseCode);
var vibrations = "";
for (var i = 0; i < morseCode.length; ++i) {
var code = morseCode[i];
if (code === ".") {
vibrations = vibrations.concat(dotLength);
} else if (code === "-") {
vibrations = vibrations.concat(dashLength);
}
if (i === morseCode.length - 1) break;
vibrations = vibrations.concat(",".concat(pauseBetweenElements).concat(","));
}
console.log(vibrations);
return vibrations;
}
function wordToMorseCode(word) {
var morseCode = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...",
"-", "..-", "...-", ".--", "-..-", "-.--", "--.."];
var encodedWord = "";
for (var i = 0; i < word.length; ++i) {
var morseCodeIndex = word.charCodeAt(i) - 97;
encodedWord = encodedWord.concat(morseCode[morseCodeIndex]);
}
return encodedWord;
}
function getInitials(fullName) {
return fullName.toLowerCase()
.replace(/[^a-z ]/g, "")
.split(' ')
.map(function(name) {
firstLetter = name[0];
return typeof firstLetter != undefined ? firstLetter : "";
});
}
var vibrations = nameToVibrations(String(global("SMSRN")));
vibratePattern(vibrations);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment