The test modifies main.py in micro python

Introduction: The python script program is written, which can automatically generate the internal execution program of micropthon. With the help of STM32-BOOTLOADER, micropthon is sent through REPL, and micropthon completes the emptying or updating of the main.py file in the SD card. This requires that the original main.py can be returned to REPL to download the program together with STM32-BOOTLOADER. Otherwise, you need to modify the main.py in the SD card through the USB SD card reader / writer.

Keywords: micropathon, MM32F3277, mainpy

 

 

§ 01 micro Python execution main.py

1, Micro Python startup process

  in Design mm32f3277 micro Python experimental board with SD card Mm32f3277 micro Python experimental board with SD card was tested. The circuit is transplanted from MindMotion Su Yong (version 2021-11-5). After power on, confirm whether the SDIO port is connected with an SD card (now this version requires the capacity of the SD card to be greater than 4Gb), and check whether there is a main.py file. If main.py is found, execute main.py.

   for the SD card main.py file, you often need another SD card reader to be written and deleted by the computer. The following tests whether the modification and stored procedure of main.py can be completed with the help of micro Python software.

▲ figure 1.1.1 USB socket for reading and writing SD card

2, Delete main.py

  in the current version, the instruction to delete files in SD card is not directly supported. However, you can empty the original main.py file with the help of the write function.

1. Test starting conditions

  first, use the SD card USB reader to write a test main.py program in the SD card.

from machine                import Pin
import utime

for _ in range(10):
    print(_)
    utime.sleep_ms(100)

   after the test circuit is clicked, it will execute main.py first, and return to the REPL state after displaying 10 numbers.

[Y] sdcard ready.
[Y] file system on sdcard ready.
[Y] run the main.py on disk ...

0
1
2
3
4
5
6
7
8
9
[Y] done. 1
MicroPython v1.16 on 2021-11-05; MB_F3270 with MM32F3277G7P
>>> 

2. Empty main.py

  write a test program to empty the main.py in the SD card.

(1) Clear code

from machine                import Pin
import utime

with open('main.py', 'w') as f:
    f.write('')

(2) Execution process

Reset MicroPython...
Wait for MicroPython coming back...
Download MicroPython : 22 lines/507 characters.
Begin to download program...
-------------------------------------------------------------------------

0
>>> 

(3) Check emptying effect

   power on and execute. The following is the display information. It can be seen that although micropyron detects the existence of main.py, it returns to REPL directly because main.py is an empty file

[Y] sdcard ready.
[Y] file system on sdcard ready.
[Y] run the main.py on disk ...
[Y] done. 1
MicroPython v1.16 on 2021-11-05; MB_F3270 with MM32F3277G7P
>>> 

3, Write main.py

   to write the file to main.py in the SD card, you need to program the program to be written into an executable program. Use the main.py program as the parameter to be written after microphoton opens main.py.

1. Test main.py to be written

   the following is the main.py code to be written on the SD card.

from machine                import Pin
import utime

for _ in range(10):
    print(_)
    utime.sleep_ms(100)

2. Python commands

  write Python script pymain.py. Its functions:

  • Determine whether to generate an executive program to clear main.py or write the current editing window program to SD card main.py according to whether there are parameters;
  • Generate micro Python execution code;
  • Copy it to the clipboard, send the "MPDLD" command to STM32 bootloader, and send it to micropathon through REPL by stm32-bootloader to clear main.py or write the program to main.py

(1) pymain.py code

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# PYMAIN.PY                    -- by Dr. ZhuoQing 2021-11-07
#
# Note:
#============================================================
from head import *
from tsmodule.tsstm32       import *
#------------------------------------------------------------
pastestr = ''
if len(sys.argv) == 1:
    tspcopyclipboard()
    pastestr = clipboard.paste().replace('\r', '').replace('\n', '\\n')
writestr = 'with open("main.py", "w") as f:\n    f.write("%s")\n\n'%pastestr
clipboard.copy(writestr)
stm32cmd("MPDLD")
tspfocuswindow("TEASOFT:1")
printf('\a')
#------------------------------------------------------------
#        END OF FILE : PYMAIN.PY
#============================================================

(2) Function test

   edit the downloaded main.py program with TEASOFT and execute the following pymain program.

➤ I. clear main.py
  • Send command: pymain+
➤ II. Write main.py
  • Send command: pymain (no parameters)

  after testing, the above process meets the design function.

4, Existing problems

   to complete the above functions, it is necessary for micropthon to return REPL. If the original main.py is an endless loop and external conditions cannot be returned to REPL, the above program cannot complete the work. The clearing of main.py can only be completed through the USB SD reader / writer.

 

※ test summary ※

   the python script program is written, which can automatically generate the internal execution program of micropthon. With the help of STM32-BOOTLOADER, micropthon is sent through REPL, and micropthon completes the emptying or updating of the main.py file in the SD card.

  this requires that the original main.py can be returned to REPL to download the program in cooperation with STM32-BOOTLOADER. Otherwise, you need to modify the main.py in the SD card through the USB SD card reader / writer.

1, Complete pymain.py program

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# PYMAIN.PY                    -- by Dr. ZhuoQing 2021-11-07
#
#    Usage:
#       pymain *                    # Clear main.py content
#       pymain                      # Write the current edit file into micropython main.py
#
# Note:
#============================================================

from head import *
from tsmodule.tsstm32       import *

#------------------------------------------------------------

pastestr = ''

if len(sys.argv) == 1:
    tspcopyclipboard()
    pastestr = clipboard.paste().replace('\r', '').replace('\n', '\\n')
    printf("Write the main.py code.")
else:
    printf("Clear MicroPython main.py.")

writestr = 'with open("main.py", "w") as f:\n    f.write("%s")\n\n'%pastestr


clipboard.copy(writestr)
stm32cmd("MPDLD")
tspfocuswindow("TEASOFT:1")

printf('\a')



#------------------------------------------------------------
#        END OF FILE : PYMAIN.PY
#============================================================

■ links to relevant literature:

● relevant chart links:

Keywords: Python micropython

Added by Edison on Sun, 07 Nov 2021 22:30:24 +0200