Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs1-24fa
lecture-code
Commits
3a418a7a
Commit
3a418a7a
authored
5 months ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
autocommit
parent
22eecf31
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
07/transfer.py
+61
-24
07/transfer.py
with
61 additions
and
24 deletions
+61
-24
07/transfer.py
View file @
3a418a7a
import
ports
import
psutil
import
urllib.request
import
os
import
shutil
import
platform
import
time
import
subprocess
def
get_windows_drive_name
(
letter
):
return
subprocess
.
check_output
([
"cmd"
,
"/c vol "
+
letter
]).
decode
().
split
(
"
\r\n
"
)[
0
].
split
(
" "
).
pop
()
def
get_drive
():
def
get_windows_drive_name
(
letter
):
return
subprocess
.
check_output
([
"cmd"
,
"/c vol "
+
letter
]).
decode
().
split
(
"
\r\n
"
)[
0
].
split
(
" "
).
pop
()
if
platform
.
system
().
lower
()
==
'windows'
:
disks
=
[(
x
.
mountpoint
,
get_windows_drive_name
(
x
.
mountpoint
.
strip
(
'
\\
'
)))
for
x
in
psutil
.
disk_partitions
()]
disks
=
[(
x
,
name
)
for
x
,
name
in
disks
if
'TRINKEY'
in
name
or
'CIRCUIT'
in
name
]
else
:
disks
=
[(
x
.
mountpoint
,
x
.
mountpoint
)
for
x
in
psutil
.
disk_partitions
(
)
if
'TRINKEY'
in
x
.
mountpoint
.
upper
()
or
'CIRCUIT'
in
x
.
mountpoint
.
upper
()]
if
platform
.
system
().
lower
()
==
'windows'
:
disks
=
[(
x
.
mountpoint
,
get_windows_drive_name
(
x
.
mountpoint
.
strip
(
'
\\
'
)))
for
x
in
psutil
.
disk_partitions
()]
disks
=
[(
x
,
name
)
for
x
,
name
in
disks
if
'TRINKEY'
in
name
or
'CIRCUIT'
in
name
]
else
:
disks
=
[(
x
.
mountpoint
,
x
.
mountpoint
)
for
x
in
psutil
.
disk_partitions
(
)
if
'TRINKEY'
in
x
.
mountpoint
.
upper
()
or
'CIRCUIT'
in
x
.
mountpoint
.
upper
()]
if
len
(
disks
)
!=
1
:
print
(
"Could not find the
t
rinkey. Did you click the reset button twice and connect it?"
)
exit
(
1
)
if
len
(
disks
)
!=
1
:
print
(
"Could not find the
T
rinkey. Did you click the reset button twice and connect it?"
)
exit
(
1
)
disk
,
name
=
disks
[
0
]
disk
,
name
=
disks
[
0
]
return
disk
,
name
if
'TRINKEY'
in
name
:
def
reset_drive
():
urllib
.
request
.
urlretrieve
(
"https://downloads.circuitpython.org/bin/adafruit_proxlight_trinkey_m0/en_US/adafruit-circuitpython-adafruit_proxlight_trinkey_m0-en_US-9.1.4.uf2"
,
os
.
path
.
join
(
disk
,
'trinkey.uf2'
))
print
(
"The drive will now eject. You can quit any keyboard popups that appear. Run the transfer.py file again for it to be bootable."
)
else
:
shutil
.
rmtree
(
os
.
path
.
join
(
disk
,
'lib'
),
True
)
shutil
.
rmtree
(
os
.
path
.
join
(
disk
,
'boot_out.txt'
),
True
)
shutil
.
copy
(
'07/code.py'
,
os
.
path
.
join
(
disk
,
'code.py'
))
shutil
.
copy
(
'07/interact.py'
,
os
.
path
.
join
(
disk
,
'interact.py'
))
shutil
.
copy
(
'07/abstract.py'
,
os
.
path
.
join
(
disk
,
'abstract.py'
))
shutil
.
copy
(
'07/colorsys.py'
,
os
.
path
.
join
(
disk
,
'colorsys.py'
))
print
(
"Done! Switch back to the Serial Monitor!"
)
print
(
"The drive will now eject and then re-attach. Please wait 10 seconds."
)
time
.
sleep
(
10
)
disk
,
name
=
get_drive
()
if
'TRINKEY'
in
name
:
reset_drive
()
disk
,
name
=
get_drive
()
trinkey
=
ports
.
get_trinkey_port
()
if
not
trinkey
:
print
(
"Could not find the Trinkey to connect to...try removing it from the USB drive and plugging it back in."
)
exit
(
1
)
# Kill any running program
trinkey
.
write
(
bytes
(
chr
(
0x03
),
'utf-8'
))
trinkey
.
flush
()
print
(
"Connected to Trinkey! Transfering files..."
)
shutil
.
rmtree
(
os
.
path
.
join
(
disk
,
'src'
),
True
)
shutil
.
rmtree
(
os
.
path
.
join
(
disk
,
'__pycache__'
),
True
)
for
x
in
os
.
listdir
(
disk
):
try
:
os
.
unlink
(
os
.
path
.
join
(
disk
,
x
))
except
(
PermissionError
,
FileNotFoundError
,
OSError
):
pass
shutil
.
copytree
(
'src'
,
os
.
path
.
join
(
disk
,
'src'
),
dirs_exist_ok
=
True
)
shutil
.
copytree
(
'support'
,
os
.
path
.
join
(
disk
,
'support'
),
dirs_exist_ok
=
True
)
shutil
.
copy
(
'code.py'
,
os
.
path
.
join
(
disk
,
'code.py'
))
print
(
"Files transfered...connecting to Trinkey..."
)
time
.
sleep
(
1
)
print
(
"Restarting Trinkey!"
)
# Kill any running program
trinkey
.
write
(
bytes
(
chr
(
0x04
),
'utf-8'
))
trinkey
.
flush
()
time
.
sleep
(
1
)
miniterm
=
ports
.
connect_to_serial
(
trinkey
)
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help