Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stellar Paint doesn't work. #1037

Open
arizonagroovejet opened this issue Dec 15, 2024 · 0 comments
Open

Stellar Paint doesn't work. #1037

arizonagroovejet opened this issue Dec 15, 2024 · 0 comments

Comments

@arizonagroovejet
Copy link

I've spent several hours figuring out how to make the Stellar Paint example work because it simply doesn't work at all. I'm just going to dump here what I did to get it to work.

My first problem was that this

You will also have to install micropython-phew and microdot through Thonny's Tools -> Manage Packages.

didn't work to the extent that searching for those in Thonny didn't even return any results.

My Stellar Unicorn was running MicroPython v1.21.0, stellar_unicorn v1.21.0 so I downloaded the currently newest stable release v1.23.0-1 and installed that. That didn't result in Thonny being able to find either of the packages. I've still no idea why Thonny won't find the packages (it finds others like if I search for "http" I get a bunch of results). In the end I ended up using something called pipkin to get them installed. On my laptop, with the Stellar Unicorn plugged in and Thonny not running:


$ pip install pipkin
$ pipkin  --port /dev/ttyACM0 install microdot
$ pipkin  --port /dev/ttyACM0 install micropython-phew

Stellar Paint now chucked different errors at me and I eventually figured out that it's written to work with Microdot 1 and Microdot 2 has breaking changes, so, barely knowing what I was doing, fiddled around with stellar_paint.py until it looked like this.

import os
from microdot import Microdot, send_file, websocket
from phew import connect_to_wifi
from stellar import StellarUnicorn
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
from WIFI_CONFIG import SSID, PSK


su = StellarUnicorn()
graphics = PicoGraphics(DISPLAY)
mv_graphics = memoryview(graphics)
su.set_brightness(0.5)

WIDTH, HEIGHT = graphics.get_bounds()

ip = connect_to_wifi(SSID, PSK)

print(f"Start painting at: http://{ip}")


server = Microdot()


@server.route("/", methods=["GET"])
def route_index(request):
    return send_file("stellar_paint/index.html")


@server.route("/static/<path:path>", methods=["GET"])
def route_static(request, path):
    return send_file(f"stellar_paint/static/{path}")


def get_pixel(x, y):
    if x < WIDTH and y < HEIGHT and x >= 0 and y >= 0:
        o = (y * WIDTH + x) * 4
        return tuple(mv_graphics[o:o + 3])
    return None


def flood_fill(x, y, r, g, b):
    todo = []

    def fill(x, y, c):
        if get_pixel(x, y) != c:
            return

        graphics.pixel(x, y)

        up = get_pixel(x, y - 1)
        dn = get_pixel(x, y + 1)
        lf = get_pixel(x - 1, y)
        ri = get_pixel(x + 1, y)

        if up == c:
            todo.append((x, y - 1))
        if dn == c:
            todo.append((x, y + 1))
        if lf == c:
            todo.append((x - 1, y))
        if ri == c:
            todo.append((x + 1, y))

    c = get_pixel(x, y)

    if c is None:
        return

    fill(x, y, c)

    while len(todo):
        x, y = todo.pop(0)
        fill(x, y, c)


@server.route("/paint")
@websocket.with_websocket
async def echo(request, ws):
    while True:
        data = await ws.receive()
        try:
            x, y, r, g, b = [int(n) for n in data[0:5]]
            graphics.set_pen(graphics.create_pen(r, g, b))
            graphics.pixel(x, y)

        except ValueError:
            if data == "show":
                su.update(graphics)

            if data == "fill":
                data = await ws.receive()
                x, y, r, g, b = [int(n) for n in data[0:5]]
                graphics.set_pen(graphics.create_pen(r, g, b))
                flood_fill(x, y, r, g, b)

            if data == "clear":
                graphics.set_pen(graphics.create_pen(0, 0, 0))
                graphics.clear()

            if data == "save":
                filename = await ws.receive()
                print(f"Saving to {filename}.bin")
                try:
                    os.mkdir("saves")
                except OSError:
                    pass
                with open(f"saves/{filename}.bin", "wb") as f:
                    f.write(graphics)
                await ws.send(f"alert: Saved to saves/{filename}.bin")


server.run(host="0.0.0.0", port=80)

And that works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant