Skip to content

Instantly share code, notes, and snippets.

@benortiz benortiz/plasticplayer.js Secret

Last active Mar 5, 2019
Embed
What would you like to do?
var WIFI_NAME = "wifiname";
var WIFI_OPTIONS = { password : "password" };
var PATH_TO_DATA = "https://benortiz.github.io/playlist/collection/index.json";
var host = "192.168.1.169";
var isPlaying = true;
var uris = [];
var nfc;
var g;
var spi;
var currentId = "";
var foundCard = false;
var timer;
var isReady = false;
var HTTP = require("http");
var NEOPIXEL = require("neopixel");
var WIFI = require("EspruinoWiFi");
pinMode(B1, 'input_pulldown');
pinMode(B14, 'input_pulldown');
function onCheckPlayState(data){
if (data.result == "playing") {
isPlaying = true;
} else {
isPlaying = false;
}
setLEDColor();
}
function onGetTitle(data){
if (data.result.name !== null) {
g.clear();
g.drawString(data.result.name,0,0);
var artist = JSON.stringify(data.result.artists[0]);
artist = JSON.parse(artist);
g.drawString(artist.name,0,10);
g.flip();
setLEDColor();
} else {
g.clear();
g.drawString("Plastic Player",0,0);
g.flip();
}
}
function onCommand(data) {
}
function start(){
g.clear();
g.drawString("Connecting to network",0,0);
g.flip();
}
function setup(){
NEOPIXEL.write(B15, [0,0,0]);
I2C2.setup({scl:B10,sda:B3});
nfc = require("PN532").connect(I2C2);
nfc.SAMConfig();
setInterval(function() {
nfc.findCards(function(card) {
card = JSON.stringify(card);
checkCard(card);
return;
});
}, 1000);
spi = new SPI();
spi.setup({mosi: B6 /* D1 */, sck:B5 /* D0 */});
g = require("SSD1306").connectSPI(spi, A0 /* DC */, B7, start,{ height : 32 });
}
function isCardInDatabase(id){
for (var i=0; i < uris.length; i++){
var record = uris[i];
g.clear();
g.drawString(record.tagId,0,0);
g.flip();
if (record.tagId == id ) {
return record;
}
}
return false;
}
function checkCard(id) {
if (uris.length < 1) {
getDatabase();
} else {
var record = isCardInDatabase(id);
if (record !== false && currentId != id) {
currentId = id;
changeTracklist(record.collectionId);
}
if (record === false) {
g.clear();
g.drawString(id,0,0);
g.flip();
}
}
}
function startWifi(){
NEOPIXEL.write(B15, [255,255,0]);
WIFI.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
if (err) {
digitalWrite(LED1,1);
g.clear();
g.drawString("Connection error: "+err,0,0);
g.flip();
return;
}
NEOPIXEL.write(B15, [255,0,0]);
g.clear();
g.drawString("Connected to "+WIFI_NAME,0,0);
g.flip();
digitalWrite(LED2,1);
getDatabase();
});
}
function initPlayer() {
sendData("core.playback.get_state", null, function(data) {
if (data.result == "playing") {
isPlaying = true;
} else {
isPlaying = false;
}
setLEDColor();
g.clear();
g.drawString("Plastic Player",0,0);
g.flip();
//sendData("core.playback.get_current_track",null,onGetTitle);
});
}
function mopidyCommand(method, params) {
var body = {
method: method,
id:1,
jsonrpc:"2.0"
};
if (params !== null){
body.params = {
uri: params
};
}
body = JSON.stringify(body);
return body;
}
function playRandomUri() {
var selection = uris[Math.floor(Math.random() * uris.length)];
changeTracklist(selection.uri);
}
function getDatabase() {
g.clear();
g.drawString("Requesting Database...",0,0);
g.flip();
var finalData ="";
var req = HTTP.get(PATH_TO_DATA, function(res) {
res.on('data', function(data) {
finalData += data;
});
res.on('close', function(data) {
uris = [];
var json = JSON.parse(finalData);
for (var i=0; i < json.data.items.length; i++) {
var record = json.data.items[i];
uris.push(record);
}
g.clear();
g.drawString(json.data.items.length+ " records read",0,0);
g.flip();
initPlayer();
});
});
req.on('error',function(err){
g.clear();
g.drawString("database: " + err.message,0,0);
g.flip();
});
}
function sendData(method, params, callback) {
var finalData ="";
var body = mopidyCommand(method,params);
var options = {
host: host,
port: 80,
path: '/mopidy/rpc',
method: 'POST',
headers: {
"Content-Type" : "application/json",
"Content-Length": body.length
}
};
var req = HTTP.request(options, function(res) {
res.on('data', function(data) {
finalData += data;
});
res.on('close', function(data) {
var obj = JSON.parse(finalData);
callback(obj);
});
});
req.on('error',function(err){
g.clear();
g.drawString("Mopidy: " +err.message,0,0);
g.flip();
});
req.end(body);
}
function changeTracklist(uri) {
if (uri !== "") {
g.clear();
g.drawString("Changing tracklist...",0,0);
g.flip();
NEOPIXEL.write(B15, [255,255,0]);
sendData("core.tracklist.clear", null, function(e) {
NEOPIXEL.write(B15, [0,0,255]);
sendData("core.tracklist.add", uri, function(d) {
NEOPIXEL.write(B15, [255,0,0]);
sendData("core.playback.play", null, function(f) {
sendData("core.playback.get_current_track", null, onGetTitle);
});
});
});
}
}
function setLEDColor(){
if (isPlaying) {
NEOPIXEL.write(B15, [100,100,100]);
} else {
NEOPIXEL.write(B15, [0,255,0]);
}
}
function togglePlay(){
if (isPlaying) {
isPlaying = false;
sendData("core.playback.pause", null, onCommand);
} else {
isPlaying = true;
sendData("core.playback.resume", null, function(d){
sendData("core.playback.get_current_track", null, onGetTitle);
});
}
setLEDColor();
}
setWatch(function(e) {
togglePlay();
}, B1, { repeat: true, edge: 'rising', debounce: 100 });
setWatch(function(e) {
sendData("core.playback.next", null, function(e) {
sendData("core.playback.get_current_track", null, onGetTitle);
});
}, B14, { repeat: true, edge: 'rising', debounce: 100 });
setup();
startWifi();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.