-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
119 lines (90 loc) · 3.17 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from flask import Flask
from flask import jsonify
from flask import render_template
import mmh3
import random # may not be needed long term
import time
import unicornhat
API_PATH_PREFIX = 'api'
NUM_HASH_FUNCTIONS = 3
COLOR_BIT_SET = (255, 0, 0)
COLOR_BIT_WRITING = (0, 255, 0)
COLOR_BIT_QUERYING = (0, 0, 255)
NUM_TRANSITIONS = 4
app = Flask(__name__)
unicornhat.set_layout(unicornhat.AUTO)
unicornhat.rotation(180)
unicornhat.brightness(0.19)
unicorn_width, unicorn_height = unicornhat.get_shape()
unicornhat.off()
NUM_LEDS = unicorn_width * unicorn_height
def get_led_position(led):
unicorn_width, unicorn_height = unicornhat.get_shape()
return (led % unicorn_height, led // unicorn_width)
def toggle_leds(leds, transition_color, new_color):
orig_colors = []
for led in leds:
orig_colors.append(unicornhat.get_pixel(led[0], led[1]))
for n in range(NUM_TRANSITIONS):
for l in range(len(leds)):
this_led = leds[l]
unicornhat.set_pixel(this_led[0], this_led[1], transition_color[0], transition_color[1], transition_color[2])
unicornhat.show()
time.sleep(0.3)
for l in range(len(leds)):
this_led = leds[l]
this_orig_color = orig_colors[l]
unicornhat.set_pixel(this_led[0], this_led[1], this_orig_color[0], this_orig_color[1], this_orig_color[2])
unicornhat.show()
time.sleep(0.3)
if n == (NUM_TRANSITIONS - 1):
for l in range(len(leds)):
this_led = leds[l]
unicornhat.set_pixel(this_led[0], this_led[1], new_color[0], new_color[1], new_color[2])
unicornhat.show()
def query_led_status(led):
pos = get_led_position(led)
r, g, b = unicornhat.get_pixel(pos[0], pos[1])
toggle_leds([pos], COLOR_BIT_QUERYING, (r, g, b))
return not (r == 0 and g == 0 and b == 0)
def set_led_status(leds):
led_positions = []
for led in leds:
led_positions.append(get_led_position(led))
toggle_leds(led_positions, COLOR_BIT_WRITING, COLOR_BIT_SET)
def add_to_filter(element):
leds = []
for n in range(NUM_HASH_FUNCTIONS):
led = mmh3.hash(element, n) % NUM_LEDS
print(str(led))
leds.append(led)
set_led_status(leds)
return True
def exists_in_filter(element):
for n in range(NUM_HASH_FUNCTIONS):
led = mmh3.hash(element, n) % NUM_LEDS
print(str(led))
if (query_led_status(led) == False):
return False
return True
def reset_filter():
for n in range(2):
unicornhat.set_all(0, 0, 255)
unicornhat.show()
time.sleep(0.3)
unicornhat.off()
time.sleep(0.3)
unicornhat.off()
return True
@app.route(f'/{API_PATH_PREFIX}/add/<element>', methods=['POST'])
def add(element):
return jsonify({ 'result': add_to_filter(element) }), 201
@app.route(f'/{API_PATH_PREFIX}/exists/<element>')
def exists(element):
return jsonify({ 'result': exists_in_filter(element) })
@app.route(f'/{API_PATH_PREFIX}/reset', methods=['POST'])
def reset():
return jsonify({ 'result': reset_filter() })
@app.route('/')
def homepage():
return render_template('homepage.html')