mirror of
https://github.com/ocogeclub/ocoge.git
synced 2024-11-21 15:19:48 +00:00
49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
||
|
||
import sys
|
||
import pyboard
|
||
import serial.tools.list_ports
|
||
|
||
args = sys.argv
|
||
|
||
# port = args[1]
|
||
mode = args[1]
|
||
src = sys.stdin.read()
|
||
|
||
dest = 'main.py'
|
||
chunk_size=256
|
||
|
||
# USBシリアルポート自動認識:アルファベット順で最初に見つけたポートを使用
|
||
for p in sorted(serial.tools.list_ports.comports()):
|
||
if p.hwid.startswith('USB'):
|
||
port = p.device
|
||
print ('Device found: ' + port)
|
||
break
|
||
else:
|
||
print('No devices found.', file=sys.stderr)
|
||
sys.exit(1)
|
||
|
||
pyb = pyboard.Pyboard(port)
|
||
pyb.enter_raw_repl(False)
|
||
|
||
if mode == 'r':
|
||
print('Run code', flush=True)
|
||
pyb.exec_raw_no_follow(src)
|
||
elif mode == 'd':
|
||
print('Deploy code', flush=True)
|
||
pyb.exec("f=open('%s','wb')\nw=f.write" % dest)
|
||
pos = 0
|
||
while True:
|
||
data = src[pos:pos+chunk_size]
|
||
if not data:
|
||
break
|
||
if sys.version_info < (3,):
|
||
pyb.exec("w(b" + repr(data) + ")")
|
||
else:
|
||
pyb.exec("w(" + repr(data) + ")")
|
||
pos += chunk_size
|
||
pyb.exec("f.close()")
|
||
|
||
pyb.exit_raw_repl()
|
||
pyb.close()
|