Skip to content

Instantly share code, notes, and snippets.

@OhMeadhbh
Last active August 29, 2015 14:00
Show Gist options
  • Save OhMeadhbh/4491d327cbbcbeb557a4 to your computer and use it in GitHub Desktop.
Save OhMeadhbh/4491d327cbbcbeb557a4 to your computer and use it in GitHub Desktop.
send a morse code signal by way of the OK LED on a raspberry pi
// Copyright (c) 2014, Meadhbh S. Hamrick
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Smithee, Spelvin, Agnew & Plinge nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
var fs = require( 'fs' );
var buffers = [
( new Buffer( "0\n" ) ),
( new Buffer( "1\n" ) )
];
var current = 0;
var element_len = 240;
var elements = {
' ': 2,
'.': 6
};
var letters = {
'a': [ 'dot', 'dash' ],
'b': [ 'dash', 'dot', 'dot', 'dot' ],
'c': [ 'dash', 'dot', 'dash', 'dot' ],
'd': [ 'dash', 'dot', 'dot' ],
'e': [ 'dot' ],
'f': [ 'dot', 'dot', 'dash', 'dot' ],
'g': [ 'dash', 'dash', 'dot' ],
'h': [ 'dot', 'dot', 'dot', 'dot' ],
'i': [ 'dot', 'dot' ],
'j': [ 'dot', 'dash', 'dash', 'dash' ],
'k': [ 'dash', 'dot', 'dash' ],
'l': [ 'dot', 'dash', 'dot', 'dot'],
'm': [ 'dash', 'dash' ],
'n': [ 'dash', 'dot' ],
'o': [ 'dash', 'dash', 'dash' ],
'p': [ 'dot', 'dash', 'dash', 'dot' ],
'q': [ 'dash', 'dash', 'dot', 'dash' ],
'r': [ 'dot', 'dash', 'dot' ],
's': [ 'dot', 'dot', 'dot' ],
't': [ 'dash' ],
'u': [ 'dot', 'dot', 'dash' ],
'v': [ 'dot', 'dot', 'dot', 'dash' ],
'w': [ 'dot', 'dash', 'dash' ],
'x': [ 'dash', 'dot', 'dot', 'dash' ],
'y': [ 'dash', 'dot', 'dash', 'dash' ],
'z': [ 'dash', 'dash', 'dot', 'dot' ],
'1': [ 'dot', 'dash', 'dash', 'dash' ],
'2': [ 'dot', 'dot', 'dash', 'dash', 'dash' ],
'3': [ 'dot', 'dot', 'dot', 'dash', 'dash' ],
'4': [ 'dot', 'dot', 'dot', 'dot', 'dash' ],
'5': [ 'dot', 'dot', 'dot', 'dot', 'dot' ],
'6': [ 'dash', 'dot', 'dot', 'dot', 'dot' ],
'7': [ 'dash', 'dash', 'dot', 'dot', 'dot' ],
'8': [ 'dash', 'dash', 'dash', 'dot', 'dot' ],
'9': [ 'dash', 'dash', 'dash', 'dash', 'dot', ],
'0': [ 'dash', 'dash', 'dash', 'dash', 'dash' ],
};
var message = "out of cheese error. rebooting universe.";
//var message = "watson come here. i need you";
//var message = "sos";
var events = [];
for( var i = 0; i < message.length; i++ ) {
var c = message.substr( i, 1 ).toLowerCase();
if( letters[ c ] ) {
for( var j = 0; j < letters[ c ].length; j++ ) {
if( 'dot' == letters[c][j] ) {
events.push( 1 );
events.push( 0 );
} else {
events.push( 1 );
events.push( 1 );
events.push( 1 );
events.push( 0 );
}
}
} else if( elements[ c ] ) {
for( var j = 0; j < elements[ c ]; j++ ) {
events.push( 0 );
}
}
}
console.log( events );
var fd = fs.openSync( '/sys/class/leds/led0/brightness', 'w' );
var interval = setInterval( function () {
if( current < events.length ) {
if( 'undefined' != typeof events[ current ] ) {
fs.writeSync( fd, buffers[ events[ current ] ], 0, buffers[ events[ current ] ].length );
}
current++;
} else {
clearInterval( interval );
fs.closeSync( fd );
}
}, 120 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment