Skip to content

Instantly share code, notes, and snippets.

@samelhusseini
Last active March 29, 2022 01:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samelhusseini/71bf0f721e0f26a96c320b69f83ec9c6 to your computer and use it in GitHub Desktop.
Save samelhusseini/71bf0f721e0f26a96c320b69f83ec9c6 to your computer and use it in GitHub Desktop.
A node script for listening to micro:bit serial messages and controlling spotify on a Mac
var SerialPort = require("serialport");
const Readline = SerialPort.parsers.Readline;
var spotify = require('spotify-node-applescript');
var port = new SerialPort('/dev/tty.usbmodem1422', {
baudRate: 115200,
autoOpen: false
})
const parser = new Readline();
port.pipe(parser);
port.open(() => {
console.log("Port open");
parser.on('data', (data) => {
console.log('Received Data: ' + data.toString());
processData(data);
});
})
function processData(data) {
if (data.indexOf('NEXT') == 0) {
// Handle NEXT received
spotify.setShuffling(false);
spotify.next(function () {
// Playing the next song
});
} else if (data.indexOf('PREVIOUS') == 0) {
// Handle PREVIOUS received
spotify.setShuffling(false);
spotify.previous(function () {
// Playing the previous song
});
} else if (data.indexOf('SHUFFLE') == 0) {
// Handle SHUFFLE received
spotify.setShuffling(true);
spotify.next(function () {
// Playing the next song
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment