Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Inspired by: http://thejackalofjavascript.com/raspberry-pi-node-js-led-emit-morse-code/
- */
- // Example: node morse.js sos
- // Uncomment the following to test script when event loop spins forever
- // const spinTestExecFunction = process.nextTick
- // Uncomment the following to test script when event loop is saturated
- // const spinTestExecFunction = setImmediate
- const onoff = require("onoff");
- const Q = require("q");
- const gapDurationMillis = 10; /* Smallest unit of time to wait */
- const postDeactivateGaps = 5; /* How long to wait after using led */
- const codes = { /* Morse codes */
- "." : {enableLed:true, gaps:10}, // dot
- "_" : {enableLed:true, gaps:40}, // dash
- "|" : {enableLed:false, gaps:20}, // letter end
- ":" : {enableLed:false, gaps:0} // word separator
- };
- const pattern = { /* Conversion from sourceLetter to morseCodes */
- "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 main = function (text) {
- var led = new onoff.Gpio(17, "out");
- emitTextAsMorseCodeAsPromise(led, text)
- .done(function (o) {
- console.info("DONE!");
- process.exit(0);
- });
- },
- emitTextAsMorseCodeAsPromise = function (led, text) {
- var promise = Q(),
- normalizedText = text.toLowerCase()
- .replace(/[^a-z0-9\.,\?/@]+/g, " ");
- console.info("Text to morse code: [" + normalizedText + "]");
- normalizedText.split("")
- .forEach(function (sourceLetter) {
- var morseCodes = pattern[sourceLetter];
- promise = promise.then(function () {
- console.log("morseCode: [" + sourceLetter + "] -> [" + morseCodes +"]");
- });
- morseCodes.split("")
- .forEach(function (morseCode) {
- promise = promise.then(function () {
- return emitMorseCodeAsPromise(led, morseCode);
- });
- });
- });
- return promise;
- },
- emitMorseCodeAsPromise = function (led, morseCode) {
- return Q({led:led, morseCode:morseCode})
- .then(function (bucket) {
- var code = codes[bucket.morseCode];
- if (!code) {
- throw new Error("Unknown code: ["+bucket.morseCode+"]");
- }
- bucket.code = code;
- console.log("emitting: [" + bucket.morseCode +"]");
- if (bucket.code.enableLed) { // activate
- bucket.led.writeSync(1);
- }
- return Q(bucket).delay(getGapDelay(bucket.code.gaps));
- })
- .then(function (bucket) {
- if (bucket.code.enableLed) { // deactivate
- bucket.led.writeSync(0);
- return Q(bucket).delay(getGapDelay(postDeactivateGaps));
- }
- return bucket;
- });
- },
- getGapDelay = function (gaps) {
- return gapDurationMillis * gaps;
- };
- // Add duration of script to console messages
- (function (con) {
- var functions = {}, key, startTime = process.hrtime(),levelLabel = { "log" : "DEBUG", "info" : "INFO", "warn" : "WARN", "error" : "ERROR", "trace" : "TRACE" },getTime = function () {var runningTime = process.hrtime(startTime);return "" + runningTime[0] + "." + "0".repeat(9 - Math.ceil(Math.log10(runningTime[1]))) + runningTime[1];/* return new Date().toISOString().toString(); */};
- for (key in con) {
- if (typeof(con[key]) != "function") { continue; }
- switch (key) { case "log": case "info": case "warn": case "error": case "trace":
- functions[key] = con[key]; // original function
- con[key] = (function(name) {return function () {Array.prototype.unshift.apply(arguments,["["+getTime()+"]","["+levelLabel[name]+"]"]);functions[name].apply(con, arguments);}})(key)
- break;
- default: break;
- }}
- })(console);
- // Start our app
- process.nextTick(function () {
- main((process.argv[2] ? process.argv[2] : ""));
- });
- (function spinForever () {
- if (typeof spinTestExecFunction == "undefined") { return; }
- process.stdout.write(".");
- spinTestExecFunction(spinForever);
- })();
Add Comment
Please, Sign In to add comment