Skip to content

Commit e426cc1

Browse files
authored
Merge pull request #2262 from makermelissa/main
Added some handy utility scripts for Web Workflow Quickstart guide
2 parents 15d9d0f + e50396a commit e426cc1

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

Web_Workflow_Quickstart/boot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-FileCopyrightText: 2022 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import storage
6+
import microcontroller
7+
8+
if microcontroller.nvm[0] != 1:
9+
storage.disable_usb_drive()

Web_Workflow_Quickstart/env.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# SPDX-FileCopyrightText: 2022 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import os
6+
import storage
7+
import microcontroller
8+
9+
# Get all files in the format of .env.xxxxxxxxxx
10+
def enumerate_env_files():
11+
found_files = []
12+
all_files = os.listdir("/")
13+
for current_file in all_files:
14+
if current_file[:4] == ".env" and len(current_file) > 4:
15+
found_files.append(current_file)
16+
return found_files
17+
18+
19+
# Compare .env to enumerated env files
20+
def get_current_env_file(enumerated_files):
21+
with open(".env") as env:
22+
env_lines = env.readlines()
23+
for env_file in enumerated_files:
24+
with open(env_file) as f:
25+
lines = f.readlines()
26+
if len(env_lines) != len(lines):
27+
continue
28+
file_may_match = True
29+
for line_no, env_line in enumerate(env_lines):
30+
if env_line != lines[line_no]:
31+
file_may_match = False
32+
break
33+
if not file_may_match:
34+
continue
35+
return env_file
36+
return None
37+
38+
39+
# Erase .env then write the contents of the new env file
40+
def change_env_file(env_file):
41+
try:
42+
storage.remount("/", False)
43+
open(".env", "w").close()
44+
with open(".env", "w") as env, open(env_file) as f:
45+
for line in f.readlines():
46+
env.write(line)
47+
env.close()
48+
print("Done. Hard resetting board...")
49+
microcontroller.reset()
50+
except RuntimeError:
51+
print("You can't change the env file with this script while USB is mounted")
52+
53+
54+
# Return a prettier name than the env file
55+
def pretty_name(env_file):
56+
name = env_file[5:]
57+
name = name[0].upper() + name[1:]
58+
return f"{name} .env file"
59+
60+
env_files = enumerate_env_files()
61+
62+
if len(env_files) < 2:
63+
print("You need to have at least 2 env files to change")
64+
65+
result = get_current_env_file(env_files)
66+
if result:
67+
env_files.remove(result)
68+
print("WARNING: This will overwrite all of your current .env file settings.")
69+
if len(env_files) == 1:
70+
answer = input(f"Change to {pretty_name(env_files[0])}? ")
71+
answer = answer.lower()
72+
if answer in ("y", "yes"):
73+
change_env_file(env_files[0])
74+
else:
75+
valid_selection = False
76+
while not valid_selection:
77+
print("Select an option:")
78+
for index, file in enumerate(env_files):
79+
print(f"{index + 1}: {pretty_name(file)}")
80+
answer = input("Which option would you like? ")
81+
if answer.isdigit() and 0 < int(answer) <= len(env_files):
82+
valid_selection = True
83+
change_env_file(env_files[int(answer) - 1])
84+
print(f"{answer} was an invalid selection.\n")

Web_Workflow_Quickstart/usb/check.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-FileCopyrightText: 2022 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import microcontroller
6+
status = 'enabled' if microcontroller.nvm[0] == 1 else 'disabled'
7+
print(f"USB Mass Storage is {status} on this device")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-FileCopyrightText: 2022 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import microcontroller
6+
microcontroller.nvm[0] = 0
7+
microcontroller.reset()

Web_Workflow_Quickstart/usb/enable.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-FileCopyrightText: 2022 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import microcontroller
6+
microcontroller.nvm[0] = 1
7+
microcontroller.reset()

0 commit comments

Comments
 (0)