Advertisement
justthatoneotherdude

Raspberry Pi Spotify Box

Jan 2nd, 2017
11,721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.26 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. #Code to run Dotstar LEDs on your Raspberry Pi running Volumio with physical buttons and a webserver
  4. #Lots of code chunks taken from http://blog.shinium.eu/2015/06/dotstar-leds-with-raspberry-pi-python.html
  5. #Many thanks to him for posting his code for others to use.
  6.  
  7. import RPi.GPIO as GPIO
  8. import time
  9. import subprocess
  10. import sys, os
  11. import telnetlib
  12. import sys,os
  13. import time
  14. import subprocess
  15.  
  16. from dotstar import Adafruit_DotStar   
  17. from colour import Color
  18.  
  19. from random import randint,choice,uniform
  20.  
  21. from rotary_class import RotaryEncoder
  22.  
  23. from threading import Thread
  24.  
  25. #switch and single LED
  26. #switch = 25
  27. #light = 18
  28.  
  29. powerbutton = 4
  30.  
  31. #rotary encoder declarations
  32.  
  33. PIN_A = 12
  34. PIN_B = 21
  35. BUTTON = 20
  36.  
  37. #LED Strip declarations
  38. datapin = 22
  39. clockpin = 23
  40. numpixels = 32
  41.  
  42. strip = Adafruit_DotStar(numpixels, datapin, clockpin)
  43.  
  44. strip.begin()
  45.  
  46. #volume indicators
  47. global volume
  48. volume = 3.0
  49. disp = 1.0
  50.  
  51. #color = 0xFF0000
  52.  
  53. #GPIO setup
  54. prev_input = 0
  55. switch1 = 16
  56. switch2 = 5
  57. switch3 = 26
  58. switch4 = 13
  59.  
  60. GPIO.setmode(GPIO.BCM)
  61. GPIO.setwarnings(False)
  62. #GPIO.setup(light,GPIO.OUT)
  63. GPIO.setup(switch1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  64. GPIO.setup(switch2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  65. GPIO.setup(switch3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  66. GPIO.setup(switch4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  67. GPIO.setup(powerbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  68.  
  69. #misc variable definitions
  70. bState = {switch1:0, switch2:0, switch3:0, switch4:0, powerbutton:0}
  71. thingHappening = "nothing"
  72.  
  73. switchstates = [99,99,99,99]
  74. switchcolours = {0:0xFFFF00,1:0xFFFF00,2:0xFFFF00,3:0xFFFF00}
  75.  
  76. volstates = [99,99,99,99,99,99,99,99,99,99]
  77. volcolor = 0xFFFFFF
  78.  
  79. phasing = 1
  80. prevphasing = 1
  81.  
  82. grouplen = 5
  83.  
  84.  
  85.  
  86. #define pulse
  87.  
  88. def phase(c1,c2,interval,px=None):
  89.  
  90.     c1 = Color(c1)
  91.     c2 = Color(c2)
  92.     grad = list(c1.range_to(c2,128))
  93.  
  94.     for c in grad:
  95.         rbg = c.rgb
  96.         red = int(round(255* rbg[0]))
  97.         blue = int(round(255 * rbg[1]))
  98.         green = int(round(255 * rbg[2]))
  99.         if px != None:
  100.             strip.setPixelColor(px,red,blue,green)
  101.         else:
  102.             for n in range(numpixels):
  103.                 if n in volstates:
  104.                     for i,v in enumerate(volstates):
  105.                         if v == n:
  106.                             strip.setPixelColor(n,volcolor)
  107.                 elif n in switchstates:
  108.                     for i,v in enumerate(switchstates):
  109.                         if v == n:
  110.                             strip.setPixelColor(n,switchcolours[i])
  111.                 else:
  112.                     strip.setPixelColor(n,red,blue,green)
  113.         strip.show()
  114.         time.sleep(interval)
  115.  
  116.  
  117. def brighten(b1,b2,interval):
  118.     grad = range(min(b1,b2),max(b1,b2))
  119.     step = float(interval) / abs(b1-b2)
  120.     if b2 > b1:
  121.         for c in grad:
  122.             strip.setBrightness(c)
  123.             time.sleep(step)
  124.     else:
  125.         grad.reverse()
  126.         for c in grad:
  127.             strip.setBrightness(c)
  128.             time.sleep(step)
  129.        
  130.  
  131. def phaseStrip(b):
  132.     strip.setBrightness(b)
  133.     phase("#FF0000","#00FF00",0.02)
  134.     phase("#00FF00","#0000FF",0.02)
  135.     phase("#0000FF","#FF0000",0.02)
  136.     time.sleep(0.0001)
  137.  
  138. def hsv_to_rgb(h,s,v):
  139.     if s == 0.0: v*=255; return (v,v,v)
  140.     i = int(h*6.)
  141.     f = (h*6.)-i; p,q,t = int(255*(v*(1.-s))),int(255*(v*(1.-s*f))), int(255*(v*(1.-s*(1.-f)))); v*=255; i%=6
  142.     if i == 0: return [v,t,p]
  143.     if i == 1: return [q,v,p]
  144.     if i == 2: return [p,v,t]
  145.     if i == 3: return [p,q,v]
  146.     if i == 4: return [t,p,v]
  147.     if i == 5: return [v,p,q]
  148.  
  149.  
  150. def randColour():
  151.     h = uniform(0,100)/100
  152.     s = uniform(70,100)/100
  153.     v = uniform(70,100)/100
  154.     rb = hsv_to_rgb(h,s,v)
  155.     r = int(round(rb[0]))
  156.     g = int(round(rb[1]))
  157.     b = int(round(rb[2]))
  158.     col = (r,g,b)
  159.    
  160.     return col
  161.  
  162.  
  163. def flashStrip(numflash,int1, int2):
  164.     global bigrainbow
  165.     for r in range(0,numflash):
  166.         rcol = randColour()
  167.         for n in range (numpixels):
  168.             strip.setPixelColor(n,rcol[0],rcol[1],rcol[2])
  169.             print rcol
  170.         strip.show()
  171.         time.sleep(int1)
  172.         for n in range(numpixels):
  173.             strip.setPixelColor(n,0)
  174.         strip.show()
  175.         time.sleep(int2)
  176.     time.sleep(0.05)
  177.  
  178. def colourWave(num):
  179.     for r in range(num):
  180.         head = 0
  181.         tail = 0 - grouplen
  182.         for n in range(numpixels + grouplen):
  183.             c = randColour()
  184.             strip.setPixelColor(n,c[0],c[1],c[2])
  185.             strip.show()
  186.             time.sleep(0.005)
  187.             strip.setPixelColor(tail,0)
  188.             time.sleep(0.005)
  189.             head += 1
  190.             tail += 1
  191.        
  192.         head = numpixels
  193.         tail = numpixels + grouplen
  194.        
  195.         for n in range(numpixels + grouplen + 2):
  196.             c = randColour()
  197.             strip.setPixelColor(head,c[0],c[1],c[2])
  198.             strip.show()
  199.             time.sleep(0.005)
  200.             strip.setPixelColor(tail,0)
  201.             time.sleep(0.005)
  202.             head -= 1
  203.             tail -= 1
  204.  
  205.  
  206.  
  207. #define turnoff for break
  208.  
  209. def turnOff(intv):
  210.     c1 = "#" + hex(strip.getPixelColor(0)).ljust(8,"0").replace("0x","")
  211.     phase(c1,"#000000",intv)
  212.  
  213.  
  214. #define rotary encoder for volume  \_(0_0)_/
  215. voladjust = 0
  216.  
  217. def volumeDisp(vol):
  218.     global volstates
  219.     global voladjust
  220.     disp = vol / 10
  221.     disp = round(disp)
  222.     disp = int(disp)
  223. #   print disp
  224.     for i in range(0,disp):
  225.         volstates[i] = 19-i
  226.     for i in range(disp,10):
  227.         volstates[i] = 99
  228.     voladjust = 1
  229.     return
  230.    
  231.  
  232. def Timer():
  233.     global voladjust
  234.     global volstates
  235.     current = 0
  236.     while True:
  237.         while voladjust == 1:
  238.             current += 1
  239.             if current < 250:
  240.                 time.sleep(.01)
  241.             else:
  242.                 voladjust = 0
  243.                 current = 0    
  244.                 volstates = [99,99,99,99,99,99,99,99,99,99]
  245.  
  246.  
  247. def switch_event(event):
  248.     global phasing
  249.     global volume
  250.     phasing = 0
  251.     if event == RotaryEncoder.CLOCKWISE:
  252.         subprocess.call(['mpc', 'volume', '+2'])
  253.         if volume < 100:
  254.             volume = volume + 2
  255.         volumeDisp(volume)
  256.         time.sleep(.1)
  257.     elif event == RotaryEncoder.ANTICLOCKWISE:
  258.         subprocess.call(['mpc', 'volume', '-2'])
  259.         if volume >0:
  260.             volume = volume - 2
  261.         volumeDisp(volume)
  262.         time.sleep(.1)
  263. #   elif event == RotaryEncoder.BUTTONDOWN:
  264. #       tn.write("toggle\n")
  265. #       print "Toggle"
  266. #       strip.setBrightness(100)
  267. #       brighten(100,50,1)
  268. #       colourWave()
  269.     phasing = 1
  270.     return
  271.  
  272. #define the switch
  273.  
  274. rswitch = RotaryEncoder(PIN_A,PIN_B,BUTTON,switch_event)
  275.  
  276.  
  277. def buttonHandler(pin):
  278.     time.sleep(.05)
  279.    
  280.     if GPIO.input(pin) == 1:
  281.         if bState[pin] == 1:
  282.             bState[pin] = 0
  283.             switchOff(pin)
  284.     else:
  285.         if bState[pin] == 0:
  286.             bState[pin] = 1
  287.             switchOn(pin)
  288.  
  289. def switchOn(pin):
  290.     global thingHappening
  291.     global switchstates
  292.     global prevThing
  293.  
  294.     if pin == switch1:
  295.         switchstates[0] = 22
  296.         tn.write("add 2\n")
  297.         tn.write("play\n")
  298.         print "Added Playlist 1"
  299. #       strip.setPixelColor(4,0,255,0)
  300. #       strip.show()
  301.  
  302.     if pin == switch2:
  303.         switchstates[1] =19
  304.         tn.write("add 3\n")
  305.         tn.write("play\n")
  306.         print "Added Playlist 2"
  307. #       strip.setPixelColor(8,0,255,0)
  308. #       strip.show()
  309.    
  310.     if pin == switch3:
  311.         switchstates[2] = 12
  312.         tn.write("add 4\n")
  313.         tn.write("play\n")
  314.         print "Added Playlist 3"
  315.  
  316.     if pin == switch4:
  317.         switchstates[3] = 9
  318.         tn.write("add 6\n")
  319.         tn.write("play\n")
  320.         print "Added Playlist 4"
  321.        
  322.  
  323. def switchOff(pin):
  324.     global switchstates
  325.  
  326.     if pin == switch1:
  327.         switchstates[0] = 99
  328.     if pin == switch2:
  329.         switchstates[1] = 99
  330.     if pin == switch3:
  331.         switchstates[2] = 99
  332.     if pin == switch4:
  333.         switchstates[3] = 99
  334.     tn.write("qclear\n")
  335.     print "Cleared Queue"
  336.  
  337. def powerOff():
  338.     print "Begin Shutdown"
  339.     switchstates = [99,99,99,99]
  340.     turnOff(.01)
  341.     strip.setBrightness(0)
  342.     strip.close()
  343.     GPIO.cleanup()
  344.     tn.close()
  345.     print "Shutting Down"
  346.     os.system('sudo shutdown -h now')
  347.  
  348.  
  349. #Spotify Monitor
  350. spotifystate = "not"
  351. results = (-1, "None", "text")
  352.  
  353. def spotifyMonitor():
  354.     global spotifystate
  355.     while True:
  356.         results = (-1, "None", "text")
  357.         time.sleep(.1)
  358.         tn.write("status\n")
  359.         results = tn.expect(["playing"],.1)
  360. #       print results[0]
  361.    
  362.         if results[0] > -1:
  363.             spotifystate = "playing"
  364.         else:
  365.             spotifystate = "not"
  366.  
  367. #telnet setup
  368. HOST = "localhost"
  369.  
  370. tn = telnetlib.Telnet()
  371.  
  372. #in_state = GPIO.input(25)
  373.  
  374. #print "%s" % in_state
  375.  
  376. tn.open(HOST,6602,5)
  377.  
  378. tn.read_until("spop 0.0.1")
  379. time.sleep(.1)
  380. ##                                        NEED TO FIX THE SHUFFLE ON STARTUP - THIS IS SLOPPY AND ONLY WORKS WHEN YOU FIRST BOOT IT UP. ALTHOUGH, IN THE REAL WORLD, THAT MIGHT BE FINE
  381. tn.write("shuffle\n")
  382. time.sleep(.1)
  383.  
  384.  
  385.  
  386. def main():
  387.     global phasing
  388.     global switchstates
  389.     GPIO.add_event_detect(switch1,GPIO.BOTH, callback=buttonHandler, bouncetime=500)
  390.     GPIO.add_event_detect(switch2,GPIO.BOTH, callback=buttonHandler, bouncetime=500)
  391.     GPIO.add_event_detect(switch3,GPIO.BOTH, callback=buttonHandler, bouncetime=500)
  392.     GPIO.add_event_detect(switch4,GPIO.BOTH, callback=buttonHandler, bouncetime=500)
  393.     GPIO.add_event_detect(powerbutton, GPIO.FALLING, bouncetime=500)
  394.    
  395.     colourWave(3)
  396.  
  397.     while True:
  398.         try:
  399.             time.sleep(.2)
  400.             if spotifystate == "playing":
  401.                 phaseStrip(30)
  402. #               time.sleep(.01)
  403.             if spotifystate == "not":
  404.                 c1 = "#" + hex(strip.getPixelColor(1)).ljust(8,"0").replace("0x","")
  405.                 if c1 != "#000000":
  406.                     turnOff(.005)
  407.                     time.sleep(.01)
  408.                 strip.setPixelColor(30,0,0,15)
  409.                 strip.show()
  410.             if GPIO.event_detected(powerbutton):
  411.                 print "Shutdown?"
  412.                 colourWave(5)
  413. #               time.sleep(3)
  414.                 if GPIO.input(powerbutton):
  415.                     print "Shutdown Aborted"
  416.                 else:
  417.                     powerOff()
  418.                     break
  419.  
  420.  
  421.         except KeyboardInterrupt:
  422.             switchstates = [99,99,99,99]
  423.             turnOff(.005)
  424.             strip.setBrightness(0)
  425.             strip.close()
  426.             GPIO.cleanup()
  427.             tn.close()
  428.             print "Done"
  429.             break
  430.    
  431.     GPIO.cleanup()
  432.  
  433.  
  434. if True:
  435.  
  436.     smon = Thread(target = spotifyMonitor)
  437.     smon.daemon = True
  438.     smon.start()
  439.    
  440.     tmr = Thread(target = Timer)
  441.     tmr.daemon = True
  442.     tmr.start()
  443.  
  444.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement