Skip to content

Instantly share code, notes, and snippets.

@atchoo78
Last active December 16, 2017 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atchoo78/697747ce21495fa0d67ba15c54635a01 to your computer and use it in GitHub Desktop.
Save atchoo78/697747ce21495fa0d67ba15c54635a01 to your computer and use it in GitHub Desktop.
Display current iTunes Track on LED display (max7219 or compatible Dot Matrix LED) connected to Raspberry PI/Arduino.
# -*- coding: utf-8 -*-
#!/usr/bin/env python
# server side script for Raspberry PI
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import re
import argparse
import time
# Luma.LED_Matrix drivers & library is © 2017 Richard Hull
# https://luma-led-matrix.readthedocs.io/en/latest/index.html
# https://github.com/rm-hull/luma.led_matrix
from luma.core.interface.serial import spi, noop
from luma.core.render import canvas
from luma.led_matrix.device import max7219
from luma.core.virtual import viewport
from luma.core.legacy import text, show_message
from luma.core.legacy.font import proportional, LCD_FONT
serial = spi(port=0, device=0, gpio=noop())
# Details regarding the block_orientation and cascaded options:
# https://luma-led-matrix.readthedocs.io/en/latest/python-usage.html#daisy-chaining
device = max7219(serial, cascaded=4, block_orientation=-90, rotate=0)
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
self._set_headers()
show_message(device, post_data, fill="white", font=proportional(LCD_FONT))
def run(server_class=HTTPServer, handler_class=S, port=8181):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Starting LED Alert...'
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
# -*- coding: utf-8 -*-
#!/usr/bin/env python
# run this on your Mac
import Foundation
from AppKit import *
from PyObjCTools import AppHelper
import requests
LED_URL = "http://raspberrypi.local:8181"
class GetSongs(NSObject):
def getMySongs_(self, song):
song_details = {}
ui = song.userInfo()
song_details = dict(zip(ui.keys(), ui.values()))
playerState = (song_details['Player State'])
if not('Stopped' in playerState):
nowPlaying = song_details['Artist'] + ' : ' + song_details['Name']
else:
return
if('Playing' in playerState):
r = requests.post(url = LED_URL, data = nowPlaying.encode('utf-8'))
nc = Foundation.NSDistributedNotificationCenter.defaultCenter()
GetSongs = GetSongs.new()
nc.addObserver_selector_name_object_(GetSongs, 'getMySongs:', 'com.apple.iTunes.playerInfo',None)
nc.addObserver_selector_name_object_(GetSongs, 'getMySongs:', 'com.spotify.client.PlaybackStateChanged',None)
AppHelper.runConsoleEventLoop()
@atchoo78
Copy link
Author

The result:

30623677-52562466-9db9-11e7-91d6-3be2baf9877b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment