Skip to content

Commit

Permalink
first attempt at looping
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Bestoso authored and Peter Bestoso committed Mar 21, 2017
1 parent 07f49ee commit e019f58
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import multiprocessing
import os
import random
import time

from flask import Flask, jsonify, request, abort

LIGHT_IDS = [ '23', '24', '22', '25', '17']
# set to true to print light values instead of setting
DEV_MODE = False

DEFAULT_INCR = 0.01
LIGHT_BRIGHTS = [0.0, 0.0, 0.0, 0.0, 0.0]
LIGHT_IDS = [ '23', '24', '22', '25', '17']
MAX_BRIGHTNESS = 0.8
MIN_BRIGHTNESS = 0.0
DEFAULT_INCR = 0.01
NUM_LIGHTS = 5

app = Flask(__name__)
command_queue = multiprocessing.Queue()

def set_light(light_id, brightness):
LIGHT_BRIGHTS[light_id] = brightness
os.system("echo " + LIGHT_IDS[light_id] + "=" + str(brightness)
+ " > /dev/pi-blaster")
if DEV_MODE:
print LIGHT_IDS[light_id] + ": " + str(brightness)
else:
os.system("echo " + LIGHT_IDS[light_id] + "=" + str(brightness)
+ " > /dev/pi-blaster")

def all_lights_up(incr):
while True:
Expand Down Expand Up @@ -95,25 +104,27 @@ def randomize():
set_light(i, LIGHT_BRIGHTS[i] - incr)
if done:
return



@app.route('/wave')
def wave_endpoint():
wave_lights()
return '{}'

@app.route('/up')
def up_endpoint():
all_lights_up(DEFAULT_INCR)
command_queue.put("UP")
return '{}'

@app.route('/down')
def down_endpoint():
all_lights_down(DEFAULT_INCR)
command_queue.put("DOWN")
return '{}'

@app.route('/blink')
def blink_endpoint():
command_queue.put("BLINK")
return '{}'

light_id_arg = request.args.get('light')
if not light_id_arg:
all_lights_down(DEFAULT_INCR)
Expand Down Expand Up @@ -177,7 +188,24 @@ def sparkle_endpoint():
randomize()
all_lights_up(DEFAULT_INCR)
return '{}'


def test_loop():
command = "STEADY"
while True:
if not command_queue.empty():
command = command_queue.get()
if command == "BLINK":
blink_light(0)
elif command == "UP":
all_lights_up(DEFAULT_INCR)
command = "STEADY"
elif command == "DOWN":
all_lights_down(DEFAULT_INCR)
command = "STEADY"

if __name__ == '__main__':
all_lights_up(DEFAULT_INCR / 5)
p = multiprocessing.Process(target=test_loop)
p.start()
app.run(debug=True)
p.join()

0 comments on commit e019f58

Please sign in to comment.