Skip to content
Snippets Groups Projects
Commit da47ae05 authored by ave's avatar ave Committed by ave
Browse files

gr33nhouse: use sd card

parent cde11207
Branches
Tags
No related merge requests found
from st3m.input import InputState from st3m.input import InputState
from st3m.goose import Optional, List from st3m.goose import Optional, List
from st3m.ui import colours from st3m.ui import colours
from st3m.utils import sd_card_plugged
import urequests import urequests
import gzip import gzip
from utarfile import TarFile, DIRTYPE from utarfile import TarFile, DIRTYPE
...@@ -36,6 +37,18 @@ class DownloadView(BaseView): ...@@ -36,6 +37,18 @@ class DownloadView(BaseView):
self.response = b"" self.response = b""
self._download_instance = None self._download_instance = None
def _get_app_folder(self, tar_size: int) -> Optional[str]:
if sd_card_plugged():
sd_statvfs = os.statvfs("/sd")
if tar_size < sd_statvfs[1] * sd_statvfs[3]:
return "/sd/apps/"
flash_statvfs = os.statvfs("/flash")
if tar_size < flash_statvfs[1] * flash_statvfs[3]:
return "/flash/apps/"
return None
def draw(self, ctx: Context) -> None: def draw(self, ctx: Context) -> None:
ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill() ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill()
...@@ -157,23 +170,31 @@ class DownloadView(BaseView): ...@@ -157,23 +170,31 @@ class DownloadView(BaseView):
self._state = 6 self._state = 6
return return
app_folder = self._get_app_folder(len(tar))
if not app_folder:
gc.collect()
self.response = None
self.error_message = f"Not Enough Space\nSD/flash lack:\n{len(tar)}b"
self._state = 6
return
if not os.path.exists(app_folder):
print(f"making {app_folder}")
os.mkdir(app_folder)
for i in t: for i in t:
print(i.name) print(i.name)
if not os.path.exists("/flash/apps/"):
print("making /flash/apps/")
os.mkdir("/flash/apps/")
if i.type == DIRTYPE: if i.type == DIRTYPE:
print("dirtype") print("dirtype")
dirname = "/flash/apps/" + i.name dirname = app_folder + i.name
if not os.path.exists(dirname): if not os.path.exists(dirname):
print("making", dirname) print("making", dirname)
os.mkdir(dirname) os.mkdir(dirname)
else: else:
print("dir", dirname, "exists") print("dir", dirname, "exists")
else: else:
filename = "/flash/apps/" + i.name filename = app_folder + i.name
print("writing to", filename) print("writing to", filename)
f = t.extractfile(i) f = t.extractfile(i)
with open(filename, "wb") as of: with open(filename, "wb") as of:
......
...@@ -3,7 +3,7 @@ from st3m.input import InputState ...@@ -3,7 +3,7 @@ from st3m.input import InputState
from st3m.goose import Optional from st3m.goose import Optional
from st3m.ui.view import ViewManager from st3m.ui.view import ViewManager
from st3m.settings import SETTINGS_JSON_FILE from st3m.settings import SETTINGS_JSON_FILE
from st3m.utils import save_file_if_changed from st3m.utils import save_file_if_changed, sd_card_plugged
from ctx import Context from ctx import Context
import network import network
import leds import leds
...@@ -12,7 +12,6 @@ import json ...@@ -12,7 +12,6 @@ import json
import math import math
from .k3yboard import TextInputModel, KeyboardView from .k3yboard import TextInputModel, KeyboardView
from .helpers import ( from .helpers import (
sd_card_plugged,
set_direction_leds, set_direction_leds,
copy_across_devices, copy_across_devices,
mark_unknown_characters, mark_unknown_characters,
......
import os
import leds import leds
def sd_card_plugged() -> bool:
try:
os.listdir("/sd")
return True
except OSError:
# OSError: [Errno 19] ENODEV
return False
def copy_across_devices(src: str, dst: str): def copy_across_devices(src: str, dst: str):
""" """
copy a file across devices (flash->SD etc). copy a file across devices (flash->SD etc).
......
...@@ -137,4 +137,13 @@ def save_file_if_changed(filename: str, desired_contents: str) -> bool: ...@@ -137,4 +137,13 @@ def save_file_if_changed(filename: str, desired_contents: str) -> bool:
return save return save
def sd_card_plugged() -> bool:
try:
os.listdir("/sd")
return True
except OSError:
# OSError: [Errno 19] ENODEV
return False
tau = math.pi * 2 tau = math.pi * 2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment