Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const five = require("johnny-five"),
- board = new five.Board(),
- Mopidy = require("mopidy"),
- mopidy = new Mopidy({
- webSocketUrl: "ws://salon.local:6680/mopidy/ws/"
- }),
- Iconv = require('iconv').Iconv,
- iconv = new Iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE'),
- SHUTDOWN_TIMEOUT= 3600000,
- DISPLAY_TIMEOUT = 5000,
- OFF_TIMEOUT = 10000,
- DISPLAY_CONTROLLER = "PCF8574T",
- PODCASTS_FILE = 'podcast+file:///etc/mopidy/podcast/Podcasts.opml'
- ;
- board.on("ready", function() {
- const lcd = new five.LCD({
- controller: DISPLAY_CONTROLLER
- }),
- onLed = new five.Led(13),
- onButton = new five.Button({pin: 12, isPullup: true}),
- playLed = new five.Led(11),
- playButton = new five.Button({pin: 10, isPullup: true}),
- shuffleLed = new five.Led(9),
- shuffleButton = new five.Button({pin: 8, isPullup: true}),
- previousTrackButton = new five.Button({pin: 7, isPullup: true}),
- nextTrackButton = new five.Button({pin: 6, isPullup: true}),
- previousListButton = new five.Button({pin: 5, isPullup: true}),
- nextListButton = new five.Button({pin: 4, isPullup: true});
- var currentPlaylist = 0,
- currentPlaylistName = "",
- isOn = false,
- displayTimeout,
- offTimeout,
- shutdownTimeout;
- function getTextLine(text) {
- return iconv.convert(text).toString("ASCII", 0, 19);
- }
- function setShutdownTimeout() {
- if (shutdownTimeout) {
- cancelTimeout(shutdownTimeout)
- }
- cancelTimeout = setTimeout(shutdown, SHUTDOWN_TIMEOUT);
- }
- function writeToLCD(line1, line2, line3, line4) {
- lcd.clear();
- lcd.cursor(0,0);
- lcd.print(getTextLine(line1));
- lcd.cursor(1,0);
- lcd.print(getTextLine(line2));
- lcd.cursor(2,0);
- lcd.print(getTextLine(line3));
- lcd.cursor(3,0);
- lcd.print(getTextLine(line4));
- showLcd()
- }
- function showLcd() {
- if (!isOn) {
- return;
- }
- lcd.backlight();
- lcd.on();
- if (displayTimeout) {
- clearTimeout(displayTimeout);
- }
- displayTimeout = setTimeout(hideLcd, DISPLAY_TIMEOUT);
- }
- function hideLcd() {
- lcd.noBacklight();
- lcd.off();
- }
- board.repl.inject({
- onLed,
- playLed,
- shuffleLed
- });
- function loadPlaylistContents(index, playlistRef) {
- function runPlaylist() {
- currentPlaylist = index;
- currentPlaylistName = playlistRef.name;
- if (isOn) {
- mopidy.playback.play();
- }
- }
- function addPlaylistTracks(playlist) {
- mopidy.tracklist.add({tracks: playlist.tracks}).then(runPlaylist);
- }
- switch (playlistRef.uri.substring(0,3)) {
- case "m3u" :
- mopidy.playlists.lookup([playlistRef.uri]).then(addPlaylistTracks)
- break;
- default:
- mopidy.tracklist.add({uris: [playlistRef.uri]}).then(runPlaylist);
- }
- }
- function loadPlaylist(index) {
- writeToLCD("Loading...","","","")
- mopidy.playlists.asList().then(function(playlists) {
- mopidy.library.browse([PODCASTS_FILE]).then(function(podcasts) {
- for (var i=0; i < podcasts.length; i++) {
- playlists.push(podcasts[i]);
- }
- if (index >= playlists.length) {
- index = 0;
- } else if (index < 0) {
- index = playlists.length - 1;
- }
- writeToLCD(playlists[index].name, "Loading...","","")
- mopidy.tracklist.clear().then(function() {
- loadPlaylistContents(index, playlists[index]);
- })
- })
- })
- }
- function setShuffleLed() {
- mopidy.tracklist.getRandom().then(function(isRandom) {
- if (isRandom) {
- shuffleLed.on();
- } else {
- shuffleLed.off();
- }
- })
- }
- function shutdown() {
- mopidy.playback.stop();
- onLed.off();
- shuffleLed.off();
- hideLcd();
- isOn = false;
- }
- function start() {
- onLed.on();
- setShuffleLed();
- setShutdownTimeout();
- isOn = true;
- }
- onButton.on("down", function() {
- if (isOn) {
- shutdown();
- } else {
- start();
- loadPlaylist(currentPlaylist);
- };
- });
- playButton.on("down", function() {
- if (!isOn) {
- return;
- }
- mopidy.playback.getState().then(function(state) {
- switch (state) {
- case "playing":
- mopidy.playback.pause();
- break;
- case "paused":
- mopidy.playback.play();
- }
- showLcd();
- setShutdownTimeout();
- });
- })
- shuffleButton.on("down", function() {
- if (!isOn) {
- return;
- }
- mopidy.tracklist.getRandom().then(function(isRandom) {
- mopidy.tracklist.setRandom([!isRandom])
- setShutdownTimeout();
- })
- })
- nextTrackButton.on("down", function() {
- if (!isOn) {
- return;
- }
- mopidy.playback.next();
- setShutdownTimeout();
- })
- previousTrackButton.on("down", function() {
- if (!isOn) {
- return;
- }
- mopidy.playback.previous();
- setShutdownTimeout();
- })
- nextListButton.on("down", function() {
- if (!isOn) {
- return;
- }
- loadPlaylist(currentPlaylist + 1);
- setShutdownTimeout();
- })
- previousListButton.on("down", function() {
- if (!isOn) {
- return;
- }
- loadPlaylist(currentPlaylist - 1);
- setShutdownTimeout();
- })
- mopidy.on("event:playbackStateChanged", function(event) {
- if (offTimeout) {
- clearTimeout(offTimeout);
- }
- switch(event.new_state) {
- case "playing":
- if (!isOn) {
- start();
- }
- playLed.on();
- break;
- case "stopped":
- offTimeout = setTimeout(shutdown, OFF_TIMEOUT);
- playLed.off();
- break;
- default:
- playLed.off();
- }
- });
- mopidy.on("event:trackPlaybackStarted", function(event) {
- var track = event.tl_track.track;
- writeToLCD(
- currentPlaylistName,
- "artists" in track && track.artists.length > 0 ? track.artists[0].name : "",
- "name" in track ? track.name : "",
- "album" in track ? track.album.name : "",
- )
- });
- mopidy.on("event:optionsChanged",setShuffleLed);
- mopidy.tracklist.setRepeat([true]);
- mopidy.on("event", console.log);
- mopidy.on("state", console.log);
- hideLcd();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement