Last active
May 16, 2022 03:33
-
-
Save shapr/8a5aefb3b85dd440de8ae663ee7e72d8 to your computer and use it in GitHub Desktop.
adafruit funhouse with httpserver for setting fibonacci 256 colors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries | |
| # | |
| # SPDX-License-Identifier: Unlicense | |
| from secrets import secrets # pylint: disable=no-name-in-module | |
| from rainbowio import colorwheel | |
| import binascii | |
| import board | |
| import microcontroller | |
| import neopixel | |
| import socketpool | |
| import wifi | |
| import math | |
| pixel_pin = board.A0 | |
| num_pixels = 256 | |
| pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3) | |
| pixels.fill(0x01FF01) | |
| from adafruit_httpserver import HTTPServer, HTTPResponse | |
| ssid = secrets["ssid"] | |
| print("Connecting to", ssid) | |
| wifi.radio.connect(ssid, secrets["password"]) | |
| print("Connected to", ssid) | |
| print(f"Listening on http://{wifi.radio.ipv4_address}:80") | |
| pool = socketpool.SocketPool(wifi.radio) | |
| server = HTTPServer(pool) | |
| @server.route("/temperature") | |
| def base(request): # pylint: disable=unused-argument | |
| """Return the current temperature""" | |
| # pylint: disable=no-member | |
| return HTTPResponse(body=f"{str(microcontroller.cpu.temperature)}") | |
| @server.route("/debug") | |
| def debug(request): | |
| return HTTPResponse(status=200, body=f"{request.params}") | |
| @server.route("/brightness") | |
| def brightness(request): | |
| new_brightness = request.params['brightness'] | |
| pixels.brightness = float(new_brightness) | |
| return HTTPResponse(status=200, body=f"{request.params}") | |
| @server.route("/unsafe") | |
| def unsafe(request): | |
| neopixel_values = request.params['runthis'] | |
| neopixel_values = binascii.a2b_base64(neopixel_values).decode('utf-8') | |
| pixels[:] = eval(neopixel_values) | |
| return HTTPResponse(status=200, body=f"probly worked, good luck") | |
| @server.route("/rainbow") | |
| def rainbow(request): | |
| for r in range(256): | |
| pixels[:] = [colorwheel((d+r*10) % 256) for d in range(256)] | |
| return HTTPResponse(status=200, body=f"probly worked, good luck") | |
| # Never returns | |
| server.serve_forever(str(wifi.radio.ipv4_address)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment