On save upload to pico

This commit is contained in:
jimmy 2023-06-06 22:44:21 +12:00
parent fd4b2047ae
commit 05de449532
2 changed files with 96 additions and 0 deletions

View File

@ -5,6 +5,17 @@ from pygame import Color, Rect
from pygame.locals import *
import datetime
import array
import requests
def upload_file(url, filename):
with open(filename, "rb") as file:
response = requests.post(url, files={"file": file})
# Check the response status code and handle accordingly
if response.status_code == 200:
print("File upload successful")
else:
print("File upload failed")
# Initialize pygame
pygame.init()
@ -120,6 +131,11 @@ while running:
with open(filename, "wb") as file:
file.write(bitstream)
upload_file("http://192.168.1.106", "art.grb")
print("Uploaded")
print(f"Pixel art saved as {filename}!")
if event.key == K_u and pygame.key.get_mods() & KMOD_CTRL:

80
web.py Normal file
View File

@ -0,0 +1,80 @@
#
# This is an example of a (sub)application, which can be made a part of
# bigger site using "app mount" feature, see example_app_router.py.
#
import picoweb
import network
from time import sleep
import ulogging
import traceback
import sdcard
import uos
from ws2812b import WS2812B
ssid = 'XCHC1'
password = 'workspace'
ws = WS2812B(256, 28, 0.1)
# Assign chip select (CS) pin (and start it high)
cs = machine.Pin(13, machine.Pin.OUT)
# Intialize SPI peripheral (start with 1 MHz)
spi = machine.SPI(1,
baudrate=1000000,
polarity=0,
phase=0,
bits=8,
firstbit=machine.SPI.MSB,
sck=machine.Pin(10),
mosi=machine.Pin(11),
miso=machine.Pin(12))
# Initialize SD card
sd = sdcard.SDCard(spi, cs)
# Mount filesystem
vfs = uos.VfsFat(sd)
uos.mount(vfs, "/sd")
def connect():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
i = 0
while wlan.isconnected() == False:
if i>10:
print("Failed to connect")
break
i += 1
print('Waiting for connection...')
sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip
ip = connect()
app = picoweb.WebApp(__name__)
@app.route("/")
def index(request, response):
size = int(request.headers[b"Content-Length"])
data = yield from request.reader.readexactly(size)
data = data.split(b'\r\n\r\n')[-1].split(b'------')[0]
for i in range(256):
offset = i * 3
green, red, blue = data[offset:offset + 3]
#print(red, green, blue)
ws.pixels_set(i, (red, green, blue))
ws.pixels_show()
yield from picoweb.start_response(response)
yield from response.awrite("HTTP/1.0 200 OK\r\n")
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=80)