Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Persson committed May 2, 2015
0 parents commit 3c4e713
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
twitter_keys.json
node_modules/
80 changes: 80 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var five = require('johnny-five'),
Twitter = require('node-twitter'),
MorseCode = require('morsecode'),
morseConverter = new MorseCode(),
keys = require('./twitter_keys.json'),
board = new five.Board(),
isBroadcasting = false;

var BPM = 400;

board.on('ready', function() {
// Creates a piezo object and defines the pin to be used for the signal
var piezo = new five.Piezo(3);

// Injects the piezo into the repl
board.repl.inject({
piezo: piezo
});

registerTweet = function (morseStr, textStr) {
if (isBroadcasting) return;
console.log('SENDING:', textStr);

var message = [],
duration = 0;

for (char in morseStr) {
if (morseStr[char] === '.') {
message.push(['C4', 1/8]);
duration += 1/8;
} else if (morseStr[char] === '_') {
message.push(['C4', 1]);
duration += 1;
}

if (morseStr[char] !== ' ') {
message.push(null, 1/8);
duration += 1/8;
}
}

broadcastTweet(message, duration);
}

broadcastTweet = function (message, duration) {
isBroadcasting = true;

piezo.play({
song: message,
tempo: BPM
});

setTimeout( function() {
// Ready for next tweet
isBroadcasting = false;
}, (4*duration*1000)/(BPM/60) + 2000);
}



var twitterStreamClient = new Twitter.StreamClient(
keys.CONSUMER_KEY,
keys.CONSUMER_SECRET,
keys.TOKEN_KEY,
keys.TOKEN_SECRET
);

twitterStreamClient.on('error', function(error) {
console.log('Error: ' + (error.code ? error.code + ' ' + error.message : error.message));
});

twitterStreamClient.on('tweet', function(tweet) {
var textStr = tweet.text;
var morseStr = morseConverter.translate(tweet.text);
registerTweet(morseStr, textStr);
});

twitterStreamClient.start(['teknodrom']);

});
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "johnny-five",
"version": "0.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Oscar Persson",
"dependencies": {
"johnny-five": "latest",
"node-twitter": "~0.5.2",
"morsecode": "~1.0.0"
}
}

0 comments on commit 3c4e713

Please sign in to comment.