Last active
July 21, 2022 03:26
-
-
Save mokjpn/80b3d61f30316f67b280b12fc7e6bea5 to your computer and use it in GitHub Desktop.
M5Stack sample code: Download a JPEG image from given URL, then display it on LCD.
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
from m5stack import lcd | |
import socket | |
import os | |
def split_list(l, s): | |
count = 0 | |
ni = [] | |
for i in range(0, len(l)-len(s)): | |
if l[i] == s[count]: | |
count=count+1 | |
if count == len(s): | |
lcd.println("Matched") | |
ni.append(i+1) | |
count = 0 | |
else: | |
count = 0 | |
if len(ni) == 0: | |
return l | |
else: | |
r = [] | |
pi = 0 | |
for j in range(0, len(ni)): | |
r.append(l[pi:(ni[j]-len(s))]) | |
pi = ni[j] | |
r.append(l[pi:len(l)]) | |
return r | |
def http_getFile(url,file): | |
_, _, host, path = url.split('/', 3) | |
addr = socket.getaddrinfo(host, 80)[0][-1] | |
lcd.println(str(path)) | |
s = socket.socket() | |
s.connect(addr) | |
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')) | |
while True: | |
data = s.recv(200) | |
lcd.println(str(len(data))) | |
if data: | |
l = split_list(data,[13,10,13,10]) | |
if len(l) == 2: | |
lcd.println(str(len(l[0]))) | |
lcd.println(str(len(l[1]))) | |
file.write(l[1]) | |
break | |
while True: | |
data = s.recv(100) | |
if data: | |
file.write(data) | |
else: | |
break | |
lcd.println("File got") | |
s.close() | |
file.close() | |
lcd.clear() | |
lcd.setCursor(0, 0) | |
lcd.setColor(lcd.WHITE) | |
os.remove("logo.jpg") | |
f = open('logo.jpg','w') | |
http_getFile("http://www.okadajp.org/RWiki/Rlogo-old.jpg", f) | |
#lcd.clear() | |
lcd.image(0, 0, file="logo.jpg", scale=0, type=lcd.JPG) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment