Shop OBEX P1 Docs P2 Docs Learn Events
Micropython for P2 - Page 9 — Parallax Forums

Micropython for P2

1679111223

Comments

  • David Betz wrote: »
    I don't suppose there is any way to run Python code on more than one COG at the same time?

    Not yet, no. That's likely to be moderately complicated. Running PASM code (represented as a binary blob) in another COG launched from Python is much simpler and could probably be done pretty quickly with the existing tools. I just haven't had time to get to this.

    For those who are particularly ambitious, the source code is at https://github.com/totalspectrum/micropython. :).
  • For somebody wanting to try FlexGUI with MicroPython, are the most complete instructions the README.txt file found in the zip attached to the first post?

    Thanks, Ken Gracey
  • RaymanRayman Posts: 14,755
    Will this work with Rev.B? I have a feeling that USB kb/mouse won't work yet with Rev.B…
    But, I could be wrong, since this is a binary... Maybe it will work...
  • Ken Gracey wrote: »
    For somebody wanting to try FlexGUI with MicroPython, are the most complete instructions the README.txt file found in the zip attached to the first post?

    Thanks, Ken Gracey

    Well, the easiest way to use micropython from FlexGUI is to just select "Run uPython on P2" from the FlexGUI "Special" menu. But the details on things like attaching the VGA and USB are in the README.txt in the zip file (all of which is also in the FlexGUI 4.0.3 samples/upython directory, so you won't need to download the .zip if you already have FlexGUI).

    Note that FlexGUI version 4.0.3 has a small bug in its loadp2 loader which causes the default terminal to not be in ANSI mode. This doesn't matter if you use a connected keyboard and VGA monitor, but it is a pain when using micropython from the console. As a work-around until the next version of FlexGUI is released, you can replace the bin\loadp2.exe file in your flexgui folder with the one in the attached .zip. Or, you can close FlexGUI's terminal window and use a real terminal emulator instead.

  • Rayman wrote: »
    Will this work with Rev.B? I have a feeling that USB kb/mouse won't work yet with Rev.B…
    But, I could be wrong, since this is a binary... Maybe it will work...

    Yes, the VGA and USB will work with both Rev.A and Rev.B boads, the hardware type is detected at run time and both drivers are adjusted accordingly.

    The source code for this version of micropython is at https://github.com/totalspectrum/micropython.
  • Cluso99Cluso99 Posts: 18,069
    WOW :sunglasses::sunglasses::sunglasses:

    Just plugged my P2-EVAL (rev2) into the USB, ran flexgui (downloaded v4.0.4) and from the special menu clicked Run uPython on P2. Voila, the python prompt :)
    Then ran a few simple tests using print, for loops, etc.

    All running nicely :)
    Congrats guys - Eric & Brian ??
  • Cluso99Cluso99 Posts: 18,069
    Eric,
    I tried to list the SD directory contents using
    >>> import os
    >>> os.listdir()
    []
    >>>
    
    Am I missing something as there are files in the root directory and it is formatted as FAT32 ???
  • @Cluso99 There is a bunch of other SD card initialisation you need to do first, it should be buried somewhere in the Micropython threads. I think Tubular posted the sequence some time back.
  • Cluso99Cluso99 Posts: 18,069
    Thanks Roger. I'll go searching.
  • Tubular mentioned there are BS2-like functions designed accessible by this version of MicroPython.

    Is there any kind of reference for these examples, or a list of them that shows their syntax?

    Thanks,

    Ken Gracey
  • Here's the sequence for @Cluso99
    https://forums.parallax.com/discussion/comment/1473775/#Comment_1473775

    Ozpropdev wrote a library of some of the BS2 examples. I think they are posted but not sure where. They are MicroPython code (cut and paste into either micropython incarnation)
  • Cluso99Cluso99 Posts: 18,069
    @Tubular,

    Here is the info less the binary
    For those who want to check it out this is what I found I needed to do
    
    ## turn off the P123
    ## plug the VGA monitor into P123 vga board attached to P48~55
    ## plug USB keyboard to lower connector of P123 "serial host" board attached to P16~23
    ## rename Eric's v15 .binary to _BOOT_P2.BIX on an SD card
    ## edit/strip the last function (~15 lines) off pye.py and copy the remainder to pye2.py in root directory of the SD card
    ## (my pye2.py with lines already stripped off attached to this post, in zip file)
    ## turn off P123 flash switch (in case of difficulties)
    ## power up and wait for micropython prompt to appear on VGA screen (few seconds)
    ## type the following commands into the micropython repl
    import pyb
    sd=pyb.SDCard()
    sd.power(1)
    ## repl will echo 'True'
    
    import os
    os.mount(sd,'/sd')
    os.chdir('/sd')
    execfile("pye2.py")
    from pye import pye
    pye('pye2.py')
    ## that should bring up the editor and move around the ~970 line pye2 source.
    ## control-k-s to save back to SD card
    ## control-k-q to quit back to repl
    

    I am trying to just do a simple listdir without loading my SD card with anything but I am missing something.

    Can anyone give me a simple solution (python code) for running FlexGUI with Run uPython on P2?
    Is the '/sd' section trying to change to a directory called sd ???
    BTW I just want to use the PC USB connection.
  • Cluso99Cluso99 Posts: 18,069
    I copied Erics upython.binary to the SD root directory as _BOOT_P2.BIX

    I repowered the P2Evalrev2 board with PST running at 230400 on my PC,
    and python boots,
    sends a message to PST "started USB on cog 2" ,
    and then runs the default python program "main.py" on the SD.
    My "main.py" program does a few simple print messages which also works.

    I think my program then ended, and of course python ends.

    So, how to stay in python after "main.py" ends?
    And how do I perform a listdir() ?
  • rjo__rjo__ Posts: 2,114
    edited 2019-11-30 05:27
    Tubular wrote: »
    Here's the sequence for @Cluso99
    https://forums.parallax.com/discussion/comment/1473775/#Comment_1473775

    Can anyone give me a simple solution (python code) for running FlexGUI with Run uPython on P2?
    Is the '/sd' section trying to change to a directory called sd ???
    BTW I just want to use the PC USB connection.

    Sorry.

    Hotkeys won't work because there is no key combination for the edit->paste in the icon for the Propeller Output window. I tried writing directly into the Propeller output window using pyautogui.typewrite working on a copy of the code in the system clipboard … two major issues. If you clicked on a different window while the process was going on... that window got written to... a major problem. Had my computer going nuts for
    a while. and … it didn't work.

    I think there are some other wrinkles to worry about. I think I have the latest of everything and though the current copy/paste routine is now flawless I am not yet blinking any LEDs. I can turn one on and off... just can't make it blink. I tried all of the code snippets that supposedly work and they don't work for me. Prior to the last revision I thought it had failed and then after quite some period it started blinking... and continued to blink through a reset:)
    No kidding. Nothing since.

    I have attached a copy of my attempted solution. Don't run it... it is a monster waiting to eat your computer.







  • ersmithersmith Posts: 6,068
    edited 2019-11-30 07:41
    Cluso99 wrote: »
    Eric,
    I tried to list the SD directory contents using
    >>> import os
    >>> os.listdir()
    []
    >>>
    
    Am I missing something as there are files in the root directory and it is formatted as FAT32 ???

    You're probably not missing anything, but micropython is. It doesn't seem to detect all SD Cards automatically. If it doesn't find yours, then you'll have to mount it manually. Run micropython as usual (e.g. with FlexGUI "Special > Run uPython on P2") then at the prompt enter:
    import pyb
    sd=pyb.SDCard()
    sd.present()
    
    it should print `True`. If it doesn't, do:
    sd.power(1)
    
    Now you have to mount the SD card:
    import os
    os.mount(sd, '/sd')
    os.chdir('/sd')
    os.listdir()
    
    The path '/sd' in the os.mount function is arbitrary, you can call it whatever you want -- it's just a way to associate the root directory of this particular sd card with a name. The os.chdir('/sd') says to make that sd card the default directory.

    Another alternative is to try a different SD card. Of the 3 I've tried, one doesn't seem to work very well (sometimes micropython hangs trying to access it).
  • ersmithersmith Posts: 6,068
    edited 2019-11-30 07:55
    rjo__ wrote: »
    Hotkeys won't work because there is no key combination for the edit->paste in the icon for the Propeller Output window.
    On Windows 10 you can turn on a hot key combination for any console window (including the Propeller Output window):

    Right click on the title bar of the window. You'll get a popup menu one of whose options is "Properties". Selecting this gives a dialog box that says something like "Propeller Output Properties". Under Options there are a bunch of "Edit Options" checkboxes. "Use Ctrl+Shift+C/V as Copy/Paste" enables ctrl+shift copy paste keys (which work even when the program is in raw mode, as loadp2 is in the Propeller Output window).

    There's also "QuickEdit Mode". which enables right-click for pasting.

    You can make these options the default for future invocations by right-clicking on the window title and selecting "Default" instead of "Properties".
    I think there are some other wrinkles to worry about. I think I have the latest of everything and though the current copy/paste routine is now flawless I am not yet blinking any LEDs. I can turn one on and off... just can't make it blink. I tried all of the code snippets that supposedly work and they don't work for me. Prior to the last revision I thought it had failed and then after quite some period it started blinking... and continued to blink through a reset:)
    No kidding. Nothing since.

    Have you tried the smartpin blinking sequence?
    import pyb
    p=pyb.Pin(56)
    p.makeinput()   # makes the pin an input, do this before setting up smartpin
    p.mode(0x4c)    # set to NCO frequency mode
    p.xval(16000)   # set bit period
    p.yval(858993)  # set increment
    p.makeoutput()  # enables smartpin
    

  • Cluso99Cluso99 Posts: 18,069
    ersmith wrote: »
    Cluso99 wrote: »
    Eric,
    I tried to list the SD directory contents using
    >>> import os
    >>> os.listdir()
    []
    >>>
    
    Am I missing something as there are files in the root directory and it is formatted as FAT32 ???

    You're probably not missing anything, but micropython is. It doesn't seem to detect all SD Cards automatically. If it doesn't find yours, then you'll have to mount it manually. Run micropython as usual (e.g. with FlexGUI "Special > Run uPython on P2") then at the prompt enter:
    import pyb
    sd=pyb.SDCard()
    sd.present()
    
    it should print `True`. If it doesn't, do:
    sd.power(1)
    
    Now you have to mount the SD card:
    import os
    os.mount(sd, '/sd')
    os.chdir('/sd')
    os.listdir()
    
    The path '/sd' in the os.mount function is arbitrary, you can call it whatever you want -- it's just a way to associate the root directory of this particular sd card with a name. The os.chdir('/sd') says to make that sd card the default directory.

    Another alternative is to try a different SD card. Of the 3 I've tried, one doesn't seem to work very well (sometimes micropython hangs trying to access it).
    @ersmith
    Thanks for your help.

    This is what I had to do...

    * Remove the SD card from the P2-EVAL (removing the _BOOT_P2.BIX and _BOOT_P2.BIY files did not work)
    * Re-power the P2-EVAL
    * From FlexGUI, click special, click Run upython on P2
    * Insert the SD card (with P2-EVAL powered and connected)
    * then enter the python sequence as you detailed above...

    Then the os.listdir() prints a list of the files :smiley:

    Here is the terminal window trace
    ( Entering terminal mode.  Press Ctrl-] to exit. )
    started USB on cog 2
    MicroPython eric_v19 on 2019-11-05; P2-Eval-Board with p2-cpu
    Type "help()" for more information.
    >>> import pyb
    >>> sd=pyb.SDCard()
    >>> sd.present()
    True
    >>> import os
    >>> os.mount(sd,'/sd')
    >>> os.chdir('/sd')
    >>> os.listdir()
    ['System Volume Information', '_clear.cmd', '_cmd.cmd', '_cmd_.cmd', '_codegen.cmd', '_copy.cmd', '_del.cmd', '_diff.cmd', '_dir.cmd', '_dircpm.cmd', '_dnload.cmd', '_dumpfil.cmd', '_dumphub.cmd', '_dumpsob.cmd', '_dumptok.cmd', '_echo.cmd', '_free.cmd', '_getcpm.cmd', '_getfat.cmd', '_help.cmd', '_HELP.TXT', '_hubdump.cmd', '_lex.cmd', '_lf.cmd', '_link.cmd', '_ls.cmd', '_mapcpm.cmd', '_os.cmd', '_program.cmd', '_putcpm.cmd', '_putfat.cmd', '_reboot.cmd', '_ren.cmd', '_run.cmd', '_sobdump.cmd', '_testsd.cmd', '_tokdump.cmd', '_type.cmd', '_used.cmd', '_ver.cmd', 'Boot05M0.EEP', 'Boot10M0.EEP', 'BootP8X2.EEP', 'FIRMWARE.ROM', 'LED3.SOB', 'LED3.spn', 'LED3.TOK', 'os.bin', 'OSv110sd.zip', 'TACHYON3.BIN', 'TIMESTMP.D8A', '_eeprom.cmd', 'TACHYONL.BIN', 'TACHYONU.BIN', 'LOWER.EEP', 'BOOTTACH.BIN', 'BOOTTACH.LOW', '_P2_LED_.BIY', '_P2_LED_.BIX', '_BOOT_P2.BLU', '_BOOT_P2.GRN', '_BOOT_P2.RED', '_BOOT_P2.BIB', '_BOOT_SD.OBJ', '_BOOTROM.OBJ', 'pin_005a.obj', 'RESET_SD.OBJ', 'OSv110sc.zip', '_BOOT_P2.BoX', '_BOOT_P2.BI2', '_BOOT_P2.B56', '_BOOT_P2.BIA', 'TESTDATA.TXT', 'hello.py', '_BOOT_P2.BuX', 'main.py', 'sd', 'README.txt', '_BOOT_P2.BIX', 'pye.py', 'pye2.py']
    >>>
    

    So, it's necessary to prevent the P2-EVAL from booting the SD card by inserting it after FlexGUI is running and the P2-EVAL is running.
  • Cluso99Cluso99 Posts: 18,069
    edited 2019-11-30 09:26
    And here is a dir listing with one file per line...
    >>> lst = os.listdir()
    >>> for i in range(0,len(lst)):
    ...     print(lst[i])
    ...
    System Volume Information
    _clear.cmd
    _cmd.cmd
    _cmd_.cmd
    _codegen.cmd
    _copy.cmd
    _del.cmd
    _diff.cmd
    _dir.cmd
    _dircpm.cmd
    _dnload.cmd
    _dumpfil.cmd
    _dumphub.cmd
    _dumpsob.cmd
    _dumptok.cmd
    _echo.cmd
    _free.cmd
    _getcpm.cmd
    _getfat.cmd
    _help.cmd
    _HELP.TXT
    _hubdump.cmd
    _lex.cmd
    _lf.cmd
    _link.cmd
    _ls.cmd
    _mapcpm.cmd
    _os.cmd
    _program.cmd
    _putcpm.cmd
    _putfat.cmd
    _reboot.cmd
    _ren.cmd
    _run.cmd
    _sobdump.cmd
    _testsd.cmd
    _tokdump.cmd
    _type.cmd
    _used.cmd
    _ver.cmd
    Boot05M0.EEP
    Boot10M0.EEP
    BootP8X2.EEP
    FIRMWARE.ROM
    LED3.SOB
    LED3.spn
    LED3.TOK
    os.bin
    OSv110sd.zip
    TACHYON3.BIN
    TIMESTMP.D8A
    _eeprom.cmd
    TACHYONL.BIN
    TACHYONU.BIN
    LOWER.EEP
    BOOTTACH.BIN
    BOOTTACH.LOW
    _P2_LED_.BIY
    _P2_LED_.BIX
    _BOOT_P2.BLU
    _BOOT_P2.GRN
    _BOOT_P2.RED
    _BOOT_P2.BIB
    _BOOT_SD.OBJ
    _BOOTROM.OBJ
    pin_005a.obj
    RESET_SD.OBJ
    OSv110sc.zip
    _BOOT_P2.BoX
    _BOOT_P2.BI2
    _BOOT_P2.B56
    _BOOT_P2.BIA
    TESTDATA.TXT
    hello.py
    _BOOT_P2.BuX
    main.py
    sd
    README.txt
    _BOOT_P2.BIX
    pye.py
    pye2.py
    >>>
    
    Note: For non-python programmers...
    In a FOR loop with a range, python includes the first and excludes the last.
    eg for i in range(0,5) will do 0,1,2,3,4 but not 5.
    There are a few gotchas where this is not the case - eg when using a pandas dataframe and df.loc but df.iloc excludes the last.
  • Cluso99Cluso99 Posts: 18,069
    And a nicer listing...
    >>> s14='              '
    >>> for i in range(0,len(lst),4):
    ...     s=(lst[i]+s14)[:14]
    ...     if i+1<len(lst):
    ...         s=(s+lst[i+1]+s14)[:28]
    ...     if i+2<len(lst):
    ...         s=(s+lst[i+2]+s14)[:42]
    ...     if i+3<len(lst):
    ...         s=(s+lst[i+3]+s14)[:56]
    ...     print(s)
    ...
    
    System Volume _clear.cmd    _cmd.cmd      _cmd_.cmd
    _codegen.cmd  _copy.cmd     _del.cmd      _diff.cmd
    _dir.cmd      _dircpm.cmd   _dnload.cmd   _dumpfil.cmd
    _dumphub.cmd  _dumpsob.cmd  _dumptok.cmd  _echo.cmd
    _free.cmd     _getcpm.cmd   _getfat.cmd   _help.cmd
    _HELP.TXT     _hubdump.cmd  _lex.cmd      _lf.cmd
    _link.cmd     _ls.cmd       _mapcpm.cmd   _os.cmd
    _program.cmd  _putcpm.cmd   _putfat.cmd   _reboot.cmd
    _ren.cmd      _run.cmd      _sobdump.cmd  _testsd.cmd
    _tokdump.cmd  _type.cmd     _used.cmd     _ver.cmd
    Boot05M0.EEP  Boot10M0.EEP  BootP8X2.EEP  FIRMWARE.ROM
    LED3.SOB      LED3.spn      LED3.TOK      os.bin
    OSv110sd.zip  TACHYON3.BIN  TIMESTMP.D8A  _eeprom.cmd
    TACHYONL.BIN  TACHYONU.BIN  LOWER.EEP     BOOTTACH.BIN
    BOOTTACH.LOW  _P2_LED_.BIY  _P2_LED_.BIX  _BOOT_P2.BLU
    _BOOT_P2.GRN  _BOOT_P2.RED  _BOOT_P2.BIB  _BOOT_SD.OBJ
    _BOOTROM.OBJ  pin_005a.obj  RESET_SD.OBJ  OSv110sc.zip
    _BOOT_P2.BoX  _BOOT_P2.BI2  _BOOT_P2.B56  _BOOT_P2.BIA
    TESTDATA.TXT  hello.py      _BOOT_P2.BuX  main.py
    sd            README.txt    _BOOT_P2.BIX  pye.py
    pye2.py
    >>>
    
  • Cluso99Cluso99 Posts: 18,069
    edited 2019-11-30 11:07
    Even better using functions...
    ( Entering terminal mode.  Press Ctrl-] to exit. )
    started USB on cog 2
    MicroPython eric_v19 on 2019-11-05; P2-Eval-Board with p2-cpu
    Type "help()" for more information.
    >>> import pyb
    >>> sd=pyb.SDCard()
    >>> sd.present()
    True
    >>> import os
    >>> os.mount(sd,'/sd')
    >>> os.chdir('/sd')
    >>> lst=os.listdir()
    >>> def padfname(f):
    ...     lst=f.split('.')
    ...     if len(lst)>1:
    ...         s=(lst[0]+'        ')[:8]+'.'+(lst[1]+'     ')[:5]
    ...     else:
    ...         s=(f+'              ')[:14]
    ...     return(s)
    ...
    >>> def ls(lst):
    ...     for i in range(0,len(lst),4):
    ...         s=padfname(lst[i])
    ...         if i+1<len(lst):
    ...             s=s+padfname(lst[i+1])
    ...         if i+2<len(lst):
    ...             s=s+padfname(lst[i+2])
    ...         if i+3<len(lst):
    ...             s=s+padfname(lst[i+3])
    ...         print(s)
    ...
    >>> ls(lst)
    
    And the output...
    System Volume _clear  .cmd  _cmd    .cmd  _cmd_   .cmd
    _codegen.cmd  _copy   .cmd  _del    .cmd  _diff   .cmd
    _dir    .cmd  _dircpm .cmd  _dnload .cmd  _dumpfil.cmd
    _dumphub.cmd  _dumpsob.cmd  _dumptok.cmd  _echo   .cmd
    _free   .cmd  _getcpm .cmd  _getfat .cmd  _help   .cmd
    _HELP   .TXT  _hubdump.cmd  _lex    .cmd  _lf     .cmd
    _link   .cmd  _ls     .cmd  _mapcpm .cmd  _os     .cmd
    _program.cmd  _putcpm .cmd  _putfat .cmd  _reboot .cmd
    _ren    .cmd  _run    .cmd  _sobdump.cmd  _testsd .cmd
    _tokdump.cmd  _type   .cmd  _used   .cmd  _ver    .cmd
    Boot05M0.EEP  Boot10M0.EEP  BootP8X2.EEP  FIRMWARE.ROM
    LED3    .SOB  LED3    .spn  LED3    .TOK  os      .bin
    OSv110sd.zip  TACHYON3.BIN  TIMESTMP.D8A  _eeprom .cmd
    TACHYONL.BIN  TACHYONU.BIN  LOWER   .EEP  BOOTTACH.BIN
    BOOTTACH.LOW  _P2_LED_.BIY  _P2_LED_.BIX  _BOOT_P2.BLU
    _BOOT_P2.GRN  _BOOT_P2.RED  _BOOT_P2.BIB  _BOOT_SD.OBJ
    _BOOTROM.OBJ  pin_005a.obj  RESET_SD.OBJ  OSv110sc.zip
    _BOOT_P2.BoX  _BOOT_P2.BI2  _BOOT_P2.B56  _BOOT_P2.BIA
    TESTDATA.TXT  hello   .py   _BOOT_P2.BuX  main    .py
    sd            README  .txt  _BOOT_P2.BIX  pye     .py
    pye2    .py
    >>>
    
  • rjo__rjo__ Posts: 2,114
    ersmith,

    thanks. I'll get to it later in the day.
  • Cluso99Cluso99 Posts: 18,069
    I have upython working nicely.

    Suppose I have upython.py on my SD as _BOOT_P2.BIX
    When the P2 boots it will load and run this binary from the SD card. All good so far.
    If there is a python file called main.py then upython will load and run it. Again, all good.

    So presume I don't have main.py so I am in upython...
    Can I run a python program from upython? and if so, how?
    Presume the program is called myprog.py
    How can I make this program go back to upython when it ends running?

    Postedit:
    I found this set of instructions. I presume this is the basis of what I need to try...
    ## power up and wait for micropython prompt to appear on VGA screen (few seconds)
    ## type the following commands into the micropython repl
    import pyb
    sd=pyb.SDCard()
    sd.power(1)
    ## repl will echo 'True'
    
    import os
    os.mount(sd,'/sd')
    os.chdir('/sd')
    execfile("pye2.py")
    from pye import pye
    pye('pye2.py')
    ## that should bring up the editor and move around the ~970 line pye2 source.
    ## control-k-s to save back to SD card
    ## control-k-q to quit back to repl
    

    So, I presume the
    execfile('myfile.py')
    is what is required to run a upython program.

    And I also presume the preamble is required too. This is the real part I'd like to replace if possible - is there any way??
  • Cluso99Cluso99 Posts: 18,069
    When upython boots from the SD card (as _BOOT_P2.BIX)...
    If "main.py" exists, it will load and run :)

    If "main.py" does not exist, then it goes to REPL...
    From here I can just type execfile("hello.py") which works fine.
    How to I revert back into REPL from...
    1. The program "hello.py" ?
    2. Is there any way to recover from the terminal program?
  • The pye editor returns to Repl when you abort by typing ^k ^q, so just check what code it uses

    One of the controls (^c ^d ^x etc) may abort back to repl too
  • Cluso99Cluso99 Posts: 18,069
    I want to return to repl programmatically, so will a print(^k^q) work??
    I’ll try this tomorrow
  • Cluso99 wrote: »
    When upython boots from the SD card (as _BOOT_P2.BIX)...
    If "main.py" exists, it will load and run :)
    And then when main.py finishes it will go to the REPL.
    If "main.py" does not exist, then it goes to REPL...
    From here I can just type execfile("hello.py") which works fine.
    How to I revert back into REPL from...
    1. The program "hello.py" ?
    2. Is there any way to recover from the terminal program?

    When "hello.py" finishes the REPL will automatically resume.

    At the moment there's no way to make the terminal program interrupt hello.py. Control-C is supposed to do that, but it relies on some interrupts being set up that we don't have right now. So basically your hello.py program will have to check for input itself, or else avoid endless loops.
  • Cluso99Cluso99 Posts: 18,069
    When the hello.py program finishes, it does not appear to return to repl. I’ve tried nothing and quit().
    I don’t get the repl prompt and nothing I type gets displayed.
    Btw I’m using PST as the terminal program currently.
  • Maybe PST is confused somehow? I've always seen the REPL prompt after my scripts finish.

    Here's the exact sequence I run in FlexGUI:

    - Turn on P2 board. All dip switches are in "off" position, and no SD card is inserted.
    - Start FlexGUI and select "Special > Run uPython on P2"
    - Insert an SD card and mount it by entering:
    import pyb
    sd = pyb.SDCard()
    import os
    os.mount(sd, '/mysd')
    os.chdir('/mysd')
    
    - Now create hello.py:
    f=open('hello.py', 'w')
    f.write('print("hello, world")\n')
    f.write('print("goodbye")\n')
    f.close()
    
    - Now run it:
    execfile('hello.py')
    
    It comes back to the REPL prompt after printing its messages. Here's the complete transcript:
    ( Entering terminal mode.  Press Ctrl-] to exit. )
    started USB on cog 2
    MicroPython eric_v19 on 2019-11-05; P2-Eval-Board with p2-cpu
    Type "help()" for more information.
    >>> import pyb
    >>> sd = pyb.SDCard()
    >>> import os
    >>> os.mount(sd, '/mysd')
    >>> os.chdir('/mysd')
    >>> f=open('hello.py', 'w')
    >>> f.write('print("hello, world")\n')
    22
    >>> f.write('print("goodbye")\n')
    17
    >>> f.close()
    >>> execfile('hello.py')
    hello, world
    goodbye
    >>> 
    

  • Cluso99Cluso99 Posts: 18,069
    edited 2019-12-06 09:08
    Eric,
    Here is my problem...

    When I invoke the os I mount the sd as '/sdc'.
    My sd file "ls.py" mounts the sd as '/sd'.
    This works once only. Any further tries to run "ls.py" fails.

    It is to do with the os.mount name. If it is the same, then the next time it fails.
    So if instead, the first time I mount with '/sd' then ls.py fails straight up.

    Any ideas???
    ( Entering terminal mode.  Press Ctrl-] to exit. )
    started USB on cog 2
    MicroPython eric_v19 on 2019-11-05; P2-Eval-Board with p2-cpu
    Type "help()" for more information.
    >>> import pyb
    >>> sd=pyb.SDCard()
    >>> import os
    >>> os.mount(sd,'/sdc')
    >>> os.chdir('/sdc')
    >>> os.listdir()
    ['System Volume Information', '_clear.cmd', '_cmd.cmd', .....
    'pye2.py', 'ls.py', '_BOOT_P2.pyX', 'upython.bin', '_BOOT_P2.BIX', 'lss.py']
    >>> execfile('ls.py')
    System Volume _clear  .cmd  _cmd    .cmd  _cmd_   .cmd
    .....
    pye2    .py   ls      .py   _BOOT_P2.pyX  upython .bin
    _BOOT_P2.BIX  lss     .py
    >>> execfile('ls.py')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "ls.py", line 4, in <module>
    OSError: 1
    >>>
    
    And my file on sd card "ls.py"
    import pyb
    import os
    sd=pyb.SDCard()
    os.mount(sd,'/sd')
    os.chdir('/sd')
    lst=os.listdir()
    def padfname(f):
        lst=f.split('.')
        if len(lst)>1:
            s=(lst[0]+'        ')[:8]+'.'+(lst[1]+'     ')[:5]
        else:
            s=(f+'              ')[:14]
        return(s)
    def ls(lst):
        for i in range(0,len(lst),4):
            s=padfname(lst[i])
            if i+1<len(lst):
                s=s+padfname(lst[i+1])
            if i+2<len(lst):
                s=s+padfname(lst[i+2])
            if i+3<len(lst):
                s=s+padfname(lst[i+3])
            print(s)
    ls(lst)
    
  • The mount should only be done once. It's a "permanent" thing (well, permanent for the duration of the micropython session). I think I suggested earlier that you could mount twice with different names. You can, but it's a bad idea, and I'm sorry I mentioned it. Don't do that. Mount the sd card once only, right after micropython starts up, and never try to do it again. If your ls.py script is running from sd card it can assume the card is already mounted (otherwise it wouldn't be able to run!) and it doesn't need to do the mount or the chdir.
Sign In or Register to comment.