|
7 | 7 | import socketpool
|
8 | 8 | import wifi
|
9 | 9 |
|
10 |
| -from adafruit_httpserver import Server, Request, Response |
| 10 | +from adafruit_httpserver import Server, Request, Response, GET, POST |
11 | 11 |
|
12 | 12 |
|
13 | 13 | pool = socketpool.SocketPool(wifi.radio)
|
|
16 | 16 | pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
|
17 | 17 |
|
18 | 18 |
|
19 |
| -@server.route("/change-neopixel-color") |
| 19 | +@server.route("/change-neopixel-color", GET) |
20 | 20 | def change_neopixel_color_handler_query_params(request: Request):
|
21 |
| - """ |
22 |
| - Changes the color of the built-in NeoPixel using query/GET params. |
23 |
| - """ |
24 |
| - r = request.query_params.get("r") |
25 |
| - g = request.query_params.get("g") |
26 |
| - b = request.query_params.get("b") |
| 21 | + """Changes the color of the built-in NeoPixel using query/GET params.""" |
27 | 22 |
|
28 |
| - pixel.fill((int(r or 0), int(g or 0), int(b or 0))) |
| 23 | + # e.g. /change-neopixel-color?r=255&g=0&b=0 |
| 24 | + |
| 25 | + r = request.query_params.get("r") or 0 |
| 26 | + g = request.query_params.get("g") or 0 |
| 27 | + b = request.query_params.get("b") or 0 |
| 28 | + |
| 29 | + pixel.fill((int(r), int(g), int(b))) |
| 30 | + |
| 31 | + with Response(request, content_type="text/plain") as response: |
| 32 | + response.send(f"Changed NeoPixel to color ({r}, {g}, {b})") |
| 33 | + |
| 34 | + |
| 35 | +@server.route("/change-neopixel-color", POST) |
| 36 | +def change_neopixel_color_handler_post_body(request: Request): |
| 37 | + """Changes the color of the built-in NeoPixel using POST body.""" |
| 38 | + |
| 39 | + data = request.body # e.g b"255,0,0" |
| 40 | + r, g, b = data.decode().split(",") # ["255", "0", "0"] |
| 41 | + |
| 42 | + pixel.fill((int(r), int(g), int(b))) |
29 | 43 |
|
30 | 44 | with Response(request, content_type="text/plain") as response:
|
31 | 45 | response.send(f"Changed NeoPixel to color ({r}, {g}, {b})")
|
32 | 46 |
|
33 | 47 |
|
34 |
| -@server.route("/change-neopixel-color/<r>/<g>/<b>") |
35 |
| -def change_neopixel_color_handler_url_params(request: Request, r: str, g: str, b: str): |
36 |
| - """ |
37 |
| - Changes the color of the built-in NeoPixel using URL params. |
38 |
| - """ |
39 |
| - pixel.fill((int(r or 0), int(g or 0), int(b or 0))) |
| 48 | +@server.route("/change-neopixel-color/json", POST) |
| 49 | +def change_neopixel_color_handler_post_json(request: Request): |
| 50 | + """Changes the color of the built-in NeoPixel using JSON POST body.""" |
| 51 | + |
| 52 | + data = request.json() # e.g {"r": 255, "g": 0, "b": 0} |
| 53 | + r, g, b = data.get("r", 0), data.get("g", 0), data.get("b", 0) |
| 54 | + |
| 55 | + pixel.fill((r, g, b)) |
| 56 | + |
| 57 | + with Response(request, content_type="text/plain") as response: |
| 58 | + response.send(f"Changed NeoPixel to color ({r}, {g}, {b})") |
| 59 | + |
| 60 | + |
| 61 | +@server.route("/change-neopixel-color/<r>/<g>/<b>", GET) |
| 62 | +def change_neopixel_color_handler_url_params( |
| 63 | + request: Request, r: str = "0", g: str = "0", b: str = "0" |
| 64 | +): |
| 65 | + """Changes the color of the built-in NeoPixel using URL params.""" |
| 66 | + |
| 67 | + # e.g. /change-neopixel-color/255/0/0 |
| 68 | + |
| 69 | + pixel.fill((int(r), int(g), int(b))) |
40 | 70 |
|
41 | 71 | with Response(request, content_type="text/plain") as response:
|
42 | 72 | response.send(f"Changed NeoPixel to color ({r}, {g}, {b})")
|
|
0 commit comments