Last active
March 29, 2022 01:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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