An archive of community.esquilo.io as of Saturday January 26, 2019.

# Want to Read and Write a config file to save configuration information for my program

JefferyS

I think the problem I'm having is that I'm not sure how to write a table to a file if that can be done and I want multiple configuration settings so maybe a table is not the best way to go. Any ideas?
Also not sure how to read it back. I don't know why but Squirrel seems to confuse me. I can write C# but this I guess doesn't make since or there are not enough examples that I can find.

Here is the code I'm playing with.

// Write table to SD card
function WriteConfigSD(sdFile, data){
try {
// If the file already exists, open for append
configFile <- file(sdFile, "r+");
configFile.seek(0, 'e');
} catch (error) {
// if it doesn't exist, create and open for write
configFile <- file(sdFile, "w+");
}
configFile.writestr(data);
configFile.flush();
}

// Read table to SD card
}

local config = {
id = 1,
name = "Anodizing",
minTemp = 25,
maxTemp = 72
}

WriteConfigSD("sd:/config.txt", config);

Thanks,

Jeffery

Scott_Shumate

You can certainly write a config file if you want to but an easier way to do things like persistent configuration is to use the nv table. The nv table is automatically restored from EEPROM every time Esquilo boots. You can easily store Squirrel data structures like tables and arrays to the nv table and have them back when you reboot. Here's an example adapted from your code:

require("nv");

// Create a default config if it does not exist
if (!("config" in nv)) {
nv.config <- {
// Anodizing Config
Anodizing = {
minTemp = 25,
maxTemp = 72
},
// Soaking Config
Soaking = {
minTemp = 35,
maxTemp = 82
}
};

// Default to the anodizing
nv.current <- "Anodizing";

// Save the nv table
nvsave();
}

print("Config: " + nv.current + "\n");

// Get a reference to the current config
config <- nv.config[nv.current];

// Print the config
print("minTemp: " + config.minTemp + "\n");
print("maxTemp: " + config.maxTemp + "\n");
JefferyS

That is awesome Scott, I'll give it a go.

Thanks,

Jeffery

JefferyS

Hi Scott, I see you are using nv.current, I'm guessing current is a class method/function. I didn't see it in the documentation so I was wondering if there was an easy way to find out all the available methods for NV.

Thanks,

Jeffery

Scott_Shumate

nv is just a global table that magically reappears every time you boot. It is not a class like the other Squirrel objects we added. You can store anything you want in the nv table and call it anything you want. nv.current is just a table slot named "current" I used to illustrate how to pick from multiple configs. You could call it something else or omit it entirely and pick the config some other way. The only function that goes along with the nv table is nvsave() which saves the changes to the nv table back to the EEPROM.

We'll look into adding a search to the docs.

-Scott

JefferyS

Oh, I see it now.

Thanks Scott

gwittie

Is there a way to clear the nv values in the EEPROM once a project is finished or during development?

Scott_Shumate

You can clear individual values with delete:

delete nv.config;
nvsave();

Or if you want to clear the whole thing, assign it to an empty table:

nv = {};
nvsave();

-Scott