SD Card
Applies to:
All variants
Introduction
All VBOX Touch products have an external SD Card port. There are only certain file formats that will work. You should have been supplied with an 8 GB SDHC card in a FAT32 file format when you received your VBOX Touch unit. This is the only file format supported. If you did not receive this please contact your distributor or Racelogic Support.
If you are getting a new SD card be sure it has a write speed of above 7 MB/s (4 MB/s absolute minimum) and a speed class of 10 (4 minimum). It needs to be a SDHC card (2 GB to 32 GB) and need to be in the FAT32 file format.
For more information about reformatting SD cards and which ones work see here.
The SD Card is where all the data is stored during a log. As well as this it is used to install new apps, instructions for which can be found in Getting started. It can also be used to run your files by calling the file to run main.py. This is what we often use for development.
Checking if an SD Card is present
To check if an sd card is present you need to use the vts library:
import vts
To check its current state use:
vts.get_sdcard_state()
Which will return one of 5 values:
-2 = Error
-1 = Removing
0 = No Card
1 = Detected
2 = Initialised
The SD Card is therefore present when the function equals both 1 and 2 however the SD Card is only in a state to be read and written to when it is equal to 2.
There is a callback for when there is a state change in the SD card:
vts.set_sd_callback(sd_card_cb)
Which takes a function as an input. You can then use this with some flags to determine the state you are in and when you exit that state.
Reading data from the SD Card
As stated earlier you can only read from the SD Card when it is in the initialised state (2). If you try reading anything from the SD Card before it has been initialised, it will cause an error. Other than this reading to the SD Card is the same as writing a file to any normal document except with the sd card prefix. Hence it is opened and closed using the functions shown below:
f = open("/sd/HELLOWORLD.txt", "r")
file_data = f.read()
f.close()
This stores the data from the file in a var called file_data.
Reading JSON files
Python allows the reading of JSON files using the built-in JSON library which we have imported into micropython.
import json
The main function that is used by Racelogic from this library is the load function which can be used on a JSON file to convert it to a dictionary.
f = open("/sd/Customisation.json", "r")
file_data = json.load(f)
f.close()
A very good tutorial on this can be found here.
For a further example see the CONSTANT.py file in the Event_marker app as this does some formatting of the dictionary returned.
Writing data to the SD Card
As stated earlier you can only log to the SD Card when it is in the initialised state (2). If you try logging/writing anything to the SD Card before it has been initialised, it will cause an error. Other than this writing to the SD Card is the same as writing a file to any normal document except with the sd card prefix. Hence it is opened and closed using the functions shown below:
log = open("/sd/HELLOWORLD.txt", "w")
log.close()
and then to write to the file it is simply:
log.write("HELLO WORLD")
Note:
Do not forget to close the file after writing is complete as otherwise, you could get file corruption if the SD Card is removed, however, this is unlikely to happen due to some of the low-level code we have implemented, it is more likely that there will just be some form of data loss.
Further examples
The following scripts all have examples of using the SD Card.
Event Marker - Reads a .json file from the SD Card and writes data back to it of data from the sats + custom data to do with the button presses.
Torque Display - Example of a fully featured application with lots of UI, examples of writing to the SD card, displaying data and reading data over CAN.