Advertisement
nntndfrk

led_flask

Aug 10th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. import time
  2. import threading, Queue
  3. from flask import Flask, jsonify, abort, request
  4. from neopixel import *
  5. from ledconf import *
  6.  
  7.  
  8. app = Flask(__name__)
  9.  
  10.  
  11. #======== NeoPixel RGB-LED  ========
  12.  
  13. strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
  14. strip.begin()
  15.  
  16. strip_q = Queue.Queue()
  17.  
  18.  
  19. def wheel(pos):
  20.     """Generate rainbow colors across 0-255 positions."""
  21.     if pos < 85:
  22.         return Color(pos * 3, 255 - pos * 3, 0)
  23.     elif pos < 170:
  24.         pos -= 85
  25.         return Color(255 - pos * 3, 0, pos * 3)
  26.     else:
  27.         pos -= 170
  28.         return Color(0, pos * 3, 255 - pos * 3)
  29.  
  30. def colorWipe(strip, color, wait_ms=50):
  31.     """Wipe color across display a pixel at a time."""
  32.     qwe = strip.get_nowait()
  33.     for i in range(qwe.numPixels()):
  34.         qwe.setPixelColor(i, color)
  35.         qwe.show()
  36.         time.sleep(wait_ms/1000.0)
  37.    
  38.     strip.task_done()
  39.  
  40. def rainbow(strip, wait_ms=20, iterations=1):
  41.     """Draw rainbow that fades across all pixels at once."""
  42.     qwe = strip.get_nowait()
  43.     for j in range(256*iterations):
  44.         for i in range(qwe.numPixels()):
  45.             qwe.setPixelColor(i, wheel((i+j) & 255))
  46.         qwe.show()
  47.         time.sleep(wait_ms/1000.0)
  48.  
  49.     strip.task_done()
  50.  
  51.  
  52.  
  53. #======== end NeoPixel  ========
  54.  
  55.  
  56.  
  57.  
  58. @app.route('/api/v1.0/led/colorWipe', methods=['PUT'])
  59. def apicolorWipe():
  60.     if not request.json:
  61.         abort(400)
  62.     if not 'color' in request.json:
  63.         abort(400)
  64.     if not 'wait_ms' in request.json:
  65.         wait_ms = 50
  66.     else:
  67.         wait_ms = int(request.json.get('wait_ms'))
  68.     lcolor = request.json.get('color').split()
  69.     color = Color(int(lcolor[0]), int(lcolor[1]), int(lcolor[2]))
  70.  
  71.     try:
  72.         strip_q.put(strip)
  73.         t = threading.Thread(target=colorWipe, args=(strip_q, color, wait_ms,))
  74.         t.daemon = True
  75.         t.start()
  76.         # t.join()
  77.         return jsonify({'colorWipe': 'OK'})
  78.     except:
  79.         abort(501)
  80.  
  81.  
  82. @app.route('/api/v1.0/led/rainbow', methods=['PUT'])
  83. def apirainbow():
  84.     if not request.json:
  85.         abort(400)
  86.     if not 'wait_ms' in request.json:
  87.         wait_ms = 20
  88.     else:
  89.         wait_ms = int(request.json.get('wait_ms'))
  90.     if not 'iterations' in request.json:
  91.         iterations = 1
  92.     else:
  93.         iterations = int(request.json.get('iterations'))
  94.  
  95.     try:
  96.         strip_q.put(strip)
  97.         u = threading.Thread(target=rainbow, args=(strip_q, wait_ms, iterations,))
  98.         u.daemon = True
  99.         u.start()
  100.         # u.join()
  101.         return jsonify({'rainbow': 'OK'})
  102.     except:
  103.         abort(501)
  104.  
  105.  
  106. if __name__ == "__main__":
  107.     print("starting...")
  108.     app.run(host='0.0.0.0', port=8001, debug=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement