Misc

Applies to:

All variants

Random number generation

This is done using the following module:

vts.rand32()

This returns a random 32-bit number. Unlike other random functions, you may have used before this one does not allow you to set a range. However, this is easy to solve as you simply use the modulo functionality of python to set the range, for example:

random_num = vts.rand32()
ranged_random_num = vts.rand32()%10
print("Random: {}, Ranged: {}".format(random_num, ranged_random_num))

The ranged_random_num will be a value within 0-9. This does make it the smallest bit less random but it is likely random enough for whatever purpose it is going to be used for.

This function allows you to enter 2 integers of the start and range and it will return a random number that fits them.

def gen_rand_num(start = 0, range = 4294967296):
    rand_num = (vts.rand32()%range)+start
    return rand_num

Record

The record functions use the built-in mic on the bottom of the VBOX Touch next to the speaker to record audio.

To record sound you can use the built-in functionality and pass it a buffer and the gain you want. We recommend creating something like the function below to make this simpler.

def record_sound(time=5, gain=2):
    print('Recording...')
    record_buf = bytearray(16000 * time)
    vts.record(record_buf, gain)
    print('Recording finished.')
    return record_buf

This returns a buffer of the sound that you recorded in bytes. If you want to convert this into an mp3 file there are many free tools available to do so.

To check if it is currently recording you can use:

vts.recording()

Which will return a boolean of whether it is still recording or not.

To then play this sound you recorded you can use the following function:

def play(b):
    print('Uploading...')
    ft.wrbuf(ft.RAM_G, b) #load address of rec_buffer start
    ft.wr32(ft.REG_PLAYBACK_START, ft.RAM_G) #Set start point
    ft.wr32(ft.REG_PLAYBACK_LENGTH, len(b)) #Set length of data
    ft.wr32(ft.REG_PLAYBACK_FREQ, 16000) #Set Recording Frequency
    ft.wr32(ft.REG_PLAYBACK_FORMAT, 0)
    ft.wr32(ft.REG_PLAYBACK_LOOP, 0) #Disable Loop
    ft.wr32(ft.REG_VOL_PB, 255) #Set playback volume
    ft.wr32(ft.REG_PLAYBACK_PLAY, 1) #START PLAYBACK

It takes an input of the bytearray that you recorded earlier. So you could combine both using something like this:

def record_and_play(time=5, gain=2):
    record_buf = record_sound(time, gain)
    play(record_buf)

Garbage Collection

Manually collecting garbage

These functions are used to mainpulate garbage collection and to view when it is calling. This is useful for helping you to understand what is causing it to trigger and how you can trigger it at better times.

To set the flag for garbage collection to be triggered you can use the following function:

import gc
gc.collect()

This allows you to call a collect. Racelogic often uses this to call a collect in a place where a user would “expect” lag. For example, when changing from one screen to the next we often call gc_collect() as you expect a delay.

Disabling gc

You can disable garbage collection using the gc.disable() function.

gc.disable()

It is not advised to use this function as garbage collection should be taking place in your program. However, if certain sections are performance critical and cannot be stopped/slowed by a garbage collection then you can temporarily disable it.

Warning:

If you do not enable this again and it runs out of memory then this will cause a core panic crash.

Enabling gc

To enable garbage collection, after having disabled it, you can use the gc.enable() function:

gc.enable()

This will start the automatic garbage collection process again.

Check the status of gc

If you need to check the status of the automatic garbage collection of micropython you can use:

gc.isenabled()

This will return True if enabled and false otherwise.

Memory

Using the garbage collection functions you can check the amount of memory that is free and the amount that has been allocated.

gc.mem_alloc() # the amount of allocated memory from heap RAM
gc.mem_free() # the amount of free memory from heap RAM

They will each return their respective values however gc.memfree() will return -1 if the amount of free memory is not known.

Threshold

Normally when a new memory allocation cannot be satisfied, for example in the case of an out-of-memory (OOM) condition, then gc is collected. This function allows you to set another condition for garbage collection. This condition triggers when an amount of bytes has been allocated.

gc.threshold(amount)

amount is the number of bytes before a garbage collection. If this is None then it will return the amount currently assigned which by default is -1, this means that it is currently disabled.