Skip to content

Instantly share code, notes, and snippets.

@NoahRoseLedesma
Created March 3, 2016 19:58
Show Gist options
  • Save NoahRoseLedesma/952079b252e2b09fecee to your computer and use it in GitHub Desktop.
Save NoahRoseLedesma/952079b252e2b09fecee to your computer and use it in GitHub Desktop.
Script to play music files generated through midiConvert.js
if(process.argv[2] == undefined)
{
console.log("No song specified");
}
var songName = process.argv[2];
var fs = require('fs');
// Load song
var mySong = [];
try {
mySong = JSON.parse(fs.readFileSync(songName));
} catch(ex) {
console.log(ex);
process.exit(1);
}
var irobot = require('./index');
var robot = new irobot.Robot('/dev/ttyUSB0');
robot.on('ready', function() {
console.log('READY');
});
// robot.on('sensordata', function (data) {
// console.log('SENSOR DATA', data);
// });
var keypress = require('keypress');
velocity = { left: 0, right: 0 };
keypress(process.stdin);
// listen for the "keypress" event
process.stdin.on('keypress', function(ch, key) {
console.log('got "keypress"', key);
if (!key) return;
if (key.name == 'w') {
console.log("moved up");
//up(10);
velocity.left += 10;
velocity.right += 10;
robot.drive(velocity);
//robot.rtsTrue();
} else if (key.name == 's') {
velocity.left -= 10;
velocity.right -= 10;
robot.drive(velocity);
//robot.rtsFalse();
} else if (key.name == 'd') {
velocity.left -= 5;
velocity.right += 5;
robot.drive(velocity);
console.log("moved right");
//turnRightDegrees(10);
} else if (key.name == 'a') {
velocity.left += 5;
velocity.right -= 5;
robot.drive(velocity);
console.log("moved left");
//turnLeftDegrees(10);
} else if (key.name == 'space') {
console.log("stop me");
velocity.left = 0;
velocity.right = 0;
robot.drive(velocity);
} else if (key.name == 'f') {
console.log("fullMode");
robot.fullMode();
} else if (key.name == 'p') {
console.log("passiveMode");
robot.passiveMode();
} else if (key.name == 'g') {
console.log("go -- safeMode");
robot.safeMode();
} else if(key.name == 'p') {
console.log("stoping song");
robot.sing([[0, 100]]);
} else if (key.name == 'q') {
console.log("play song");
// Change tempo
for(i in mysong){
mysong[i][1] = mysong[i][1]
}
robot.sing(mysong)
}
if (key && key.ctrl && key.name == 'c') {
console.log('shift control.c');
process.exit();
//process.stdin.pause();
}
});
process.stdin.setRawMode(true);
process.stdin.resume();
batteryVolts = 0;
robot.on('sensordata', function(data) {
if (batteryVolts !== data.battery.voltage.volts) {
batteryVolts = data.battery.voltage.volts;
console.log('voltage', batteryVolts);
}
});
robot.on('bump', function(e) {
console.log('BUMP', e);
});
robot.on('button', function(e) {
console.log('BUTTON', e);
});
robot.on('cliff', function(e) {
console.log('CLIFF', e);
});
robot.on('ir', function(e) {
console.log('IR', e);
});
robot.on('mode', function(e) {
console.log('MODE', e);
});
robot.on('overcurrent', function(e) {
console.log('OVERCURRENT', e);
});
robot.on('virtualwall', function(e) {
console.log('VIRTUALWALL', e);
});
robot.on('wall', function(e) {
console.log('WALL', e);
});
robot.on('wheeldrop', function(e) {
console.log('WHEELDROP', e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment