added button function
[led-wall.git] / server.py
index 3f9bc55..5167904 100644 (file)
--- a/server.py
+++ b/server.py
@@ -2,6 +2,12 @@ import socket
 import threading
 import time
 from neopixel import *
+import RPi.GPIO as GPIO
+import time
+
+GPIO.setmode(GPIO.BCM)
+
+GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
 LED_COUNT      = 1024      # Number of LED pixels.
 LED_PIN        = 18      # GPIO pin connected to the pixels (18 uses PWM!).
@@ -12,6 +18,8 @@ LED_BRIGHTNESS = 128     # Set to 0 for darkest and 255 for brightest
 LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
 LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
 
+on = True
+
 bind_ip = '0.0.0.0'
 bind_port = 10000
 strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
@@ -26,19 +34,45 @@ pixels = []
 for i in range(1024):
   pixels.append(tuple((0,0,0)))
 
-strip.show()
+pixel_status = []
+for i in range(1024):
+  pixel_status.append(0)
 
+strip.show()
 print "Starting Server"
+
+def led_timeout(strip,pixel):
+  pixel_status[pixel] = time.time() + 30
+  for i in range(1024):
+    if (pixel_status[i] < time.time()) & (pixel_status[i] != 0):
+      strip.setPixelColor(i, Color(0,0,0))
+
 while True:
+  input_state = GPIO.input(17)
+  if input_state == False:
+    if on == True:
+      on = False
+      for i in range(1024):
+        strip.setPixelColor(i, Color(0,0,0))
+      strip.show()
+    else:
+      on = True
+      for i in range(1024):
+        strip.setPixelColor(i, pixels[i])
+    time.sleep(0.5)
+
   data, addr = server.recvfrom(1024)
   if data:
-    #print 'Received {}'.format(data)
     elements=format(data).split(",")
+    if len(elements)>2:
+      print "Kill the sniffer and start over"
+      exit(0)
     color=elements[0].split(":")
     pixel_color=pixels[int(color[1])]
     red=pixel_color[0]
     green=pixel_color[1]
     blue=pixel_color[2]
+    led_timeout(strip,int(color[1]))
     if (int(elements[1])==0):
       strip.setPixelColor(int(color[1]), Color(0,0,0))
     if color[0]=="R":
@@ -50,4 +84,6 @@ while True:
     if color[0]=="B":
       strip.setPixelColor(int(color[1]), Color(green, red, int(elements[1])))
       pixels[int(color[1])] =(red,green,int(elements[1]))
+
+  if on==True:  
     strip.show()