VEXcode V5 2.0.7 Python VM updates

The latest VEXcode release (2.0.7) did include an updated Python VM with a few new features.

1 - This release is based on micropython V1.13
we previously used 1.12, so several months worth of bug fixes to the core of the Python VM

2 - improved file support
you can now use standard Python file IO.
the open() function is supported.

3 - import of modules from SD Card

an example of this.

import and file IO demo
# Library imports
from vex import *

# import functions from james.py on SD Card
from james import * # pylint: disable=import-error

# open a file for writing
f = open("test.txt", 'w')

# write to file using print
print("Hello", file=f)
print("Goodbye", file=f)
f.close()

# now open the same file for reading
f = open("test.txt", 'r')
# and read lines, show on terminal
f.readlines()
f.close()

# functions from the james module
# pylint: disable=undefined-variable
fib(5)      
fib2(8)

Note: I am having to disable some of the linting we now do on the Python source to build this without showing an error dialog in VEXcode before the download.

the james.py module that is imported
# Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()

def fib2(n):   # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while a < n:
        result.append(a)
        a, b = b, a+b
    return result

place this in a file “james.py” (or whatever you want to call it) on the SD card

This is primarily for import of your own code, most publicly available modules will probably not import without modification.

image

supporting standard fileio has some interesting side effects, for example, as V5 ports can be now opened using this API, generic serial comms is now supported in Python in the same way as it is in C++.

f = open("/dev/port1",'w')

would allow output on port1 with default baud rate (115200)

4 - asyncio support
As micropython now has asyncio as an available module, we can support this.

asyncio example
# Library imports
from vex import *

# Begin project code
try:
    import uasyncio as asyncio
except ImportError:
    try:
        import asyncio
    except ImportError:
        print("asyncio not available")
        raise SystemExit

try:
    import utime

    ticks = utime.ticks_ms
    ticks_diff = utime.ticks_diff
except:
    import time

    ticks = lambda: int(time.time() * 1000)
    ticks_diff = lambda t1, t0: t1 - t0


async def delay_print(t, s):
    await asyncio.sleep(t)
    print(s)


async def main():
    print("start")

    await asyncio.sleep(0.001)
    print("after sleep")

    t0 = ticks()
    await delay_print(0.2, "short")
    t1 = ticks()
    await delay_print(0.8, "long")
    t2 = ticks()
    await delay_print(-1, "negative")
    t3 = ticks()

    print(
        "took {} {} {}".format(
            round(ticks_diff(t1, t0), -1),
            round(ticks_diff(t2, t1), -1),
            round(ticks_diff(t3, t2), -1),
        )
    )


asyncio.run(main())

image

15 Likes