ocoge/lib/pyboard_ocoge.py

46 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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()
if mode == 'r':
print('Run code', flush=True)
pyb.exec(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()