In [1]:
import os
import shutil
In [2]:
baud_rate = 115200
# baud_rate = 230400
# baud_rate = 460800
# baud_rate = 921600
com_port = 'COM3'
com_port = 'COM6'
# com_port = 'COM7'
# com_port = 'COM12'
# com_port = 'COM13'
# com_port = 'COM15'
# com_port = 'COM16'
# com_port = 'COM17'
# com_port = '/dev/ttyUSB0'
In [3]:
def clear_local_folder(folder):
print('\n[Clearing folder {}]'.format(folder))
for file in os.listdir(folder):
os.remove(os.path.join(folder, file))
def copy_one_file_to_local_folder(folder, file, destination_folder):
print('Copying {} to {}'.format(file, destination_folder))
shutil.copy(os.path.join(folder, file), destination_folder)
def copy_all_files_to_local_folder(folders, destination_folder):
print('\n[Copying all files to upload folder {}]'.format(destination_folder))
clear_local_folder(destination_folder)
for folder in folders:
for file in os.listdir(folder):
if (file.endswith('.py') or file.endswith('.mpy')) and not file.startswith('_'):
copy_one_file_to_local_folder(folder, file, destination_folder)
In [4]:
def list_files_in_device(com_port):
files = !ampy --port {com_port} --baud {baud_rate} ls
return sorted(files)
def cat_file_from_device(com_port, file):
!ampy --port {com_port} --baud {baud_rate} get {file}
def delete_file_in_device(com_port, file):
print('Deleting {}'.format(file))
!ampy --port {com_port} --baud {baud_rate} rm {file}
def delete_files_in_device(com_port):
print('\n[Deleting all files in device {}]'.format(com_port))
for file in list_files_in_device(com_port):
delete_file_in_device(com_port, file)
# try:
# !ampy --port {com_port} --baud {baud_rate} rmdir {'/'}
# except Exception as e:
# print(e)
def copy_one_file_to_device(com_port, folder, file, mpy_only = False):
if mpy_only:
if file.endswith('.mpy'):
print('Copying {}'.format(file))
!ampy --port {com_port} --baud {baud_rate} put {os.path.join(folder, file)}
elif file.endswith('.py'):
print('Copying {}'.format(file))
!ampy --port {com_port} --baud {baud_rate} put {os.path.join(folder, file)}
def delet_main_in_device(com_port, main_file_names):
print('Deleting {}'.format(main_file_names))
files = list_files_in_device(com_port)
for file in main_file_names:
if file in files:
delete_file_in_device(com_port, file)
def delete_main_and_files_in_device(com_port, main_file_names = ['main.py', 'main.mpy'], delete_first = True, mpy_only = False):
print('\n[Copying all files to device {}]'.format(com_port))
delet_main_in_device(com_port, main_file_names)
if mpy_only: delete_first = True
if delete_first: delete_files_in_device(com_port)
def copy_all_files_to_device(com_port, folder, main_file_names = ['main.py', 'main.mpy'], delete_first = True, mpy_only = False):
for file in os.listdir(folder):
if (file.endswith('.py') or file.endswith('.mpy')) and not file.startswith('_') and not file in main_file_names:
copy_one_file_to_device(com_port, folder, file, mpy_only)
for file in main_file_names:
if os.path.isfile(os.path.join(folder, file)):
copy_one_file_to_device(com_port, folder, file, mpy_only)
def do_all_to_device(com_port, folder, main_file_names = ['main.py', 'main.mpy'], delete_first = True, mpy_only = False):
delete_main_and_files_in_device(com_port, main_file_names, delete_first, mpy_only)
copy_all_files_to_device(com_port, folder, main_file_names, delete_first, mpy_only)
In [5]:
folders = [os.path.sep.join(['..', '..', 'codes', 'controller']),
os.path.sep.join(['..', '..', 'codes', 'demo']),
os.path.sep.join(['..', '..', 'codes', 'display']),
os.path.sep.join(['..', '..', 'codes', 'sx127x']),]
upload_folder = os.path.sep.join(['upload', 'py'])
copy_all_files_to_local_folder(folders, upload_folder)
In [6]:
upload_folder_py = os.path.sep.join(['upload', 'py'])
do_all_to_device(com_port, upload_folder, delete_first = True)
print('\n[All done!]')
In [ ]:
upload_folder_py = os.path.sep.join(['upload', 'py'])
upload_folder_mpy = os.path.sep.join(['upload', 'mpy'])
do_all_to_device(com_port, upload_folder_mpy, mpy_only = True)
copy_one_file_to_device(com_port, upload_folder_py, 'boot.py')
copy_one_file_to_device(com_port, upload_folder_py, 'main.py')
print('\n[All done!]')
In [38]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'controller']), 'controller.py')
In [39]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'controller']), 'controller_esp.py')
In [13]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'controller']), 'controller_esp_ttgo_lora_oled.py')
In [39]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'controller']), 'main.py')
In [8]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'controller']), 'config_lora.py')
In [7]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'sx127x']), 'sx127x.py')
In [42]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'test.py')
In [28]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'test_dual_channels.py')
In [6]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaSender.py')
In [165]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaReceiver.py')
In [187]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaSetSpread.py')
In [44]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaSetSyncWord.py')
In [94]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaReceiverCallback.py')
In [109]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaDuplex.py')
In [35]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaDuplexCallback.py')
In [33]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaPingPong.py')
In [14]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'display']), 'ssd1306.py')
In [10]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'display']), 'display_ssd1306_i2c.py')
In [ ]:
# list_files_in_device(com_port)
In [9]:
# cat_file_from_device(com_port, 'config_lora.py')
In [ ]:
# 連上網路
# import network; nic=network.WLAN(network.STA_IF); nic.active(False); # disable network
# import network; nic=network.WLAN(network.STA_IF); nic.active(True); nic.connect('SSID','password');nic.ifconfig()
# import network; nic=network.WLAN(network.STA_IF); nic.active(True); nic.connect('Kingnet-70M-$370', '');nic.ifconfig()
# import network; nic=network.WLAN(network.STA_IF); nic.ifconfig()
# import network; nic=network.WLAN(network.STA_IF);nic.ifconfig();nic.config('mac');nic.ifconfig((['mac',])
In [ ]:
# Delete all files
# import u_python;u_python.del_all_files();import os;os.listdir()