The config_schema
section of the mos.yml
provides a way to define configuration options for the device and override options defined elsewehre (e.g. in alibrary).
Example:
config_schema:
- ["app", "o", {title: "My app settings"}] # "o"=object
- ["app.name", "s", "My app name", {title: "Application's name"}] # "s"=string
- ["app.led_pin", "i", 2, {title: "LED pin. Default GPIO2"}] # "i"=int
- ["app.led_enable", "b", true, {title: "LED enable. Default true"}] # "b"=bool
- ["app.interval", "d", 123.45, {title: "A double value"}] # "d"=double
- ["app.uint_val", "ui", 12345678, {title: "An unsigned integer value"}] # "ui"=unsigned int
- ["i2c.enable", true] # overrides i2c.enable from the i2c library
During the build process, each object defined in the config_schema
will be compiled in a C structure (the boolean type is translated to int with values 0 for false and 1 for true):
struct mgos_config_app {
const char * name;
int led_pin;
int led_enable;
double interval;
unsigned int uint_val;
};
The default values can be changed at runtime running, e.g
mos config-set app.led_enable=false
mos config-set app.name="New app"
These settings can be used in your code to do an action according to their values:
if((mgos_sys_config_get_app_led_pin() > 0) && mgos_sys_config_get_app_led_enable()){
// toggle the led
mgos_gpio_toggle(mgos_sys_config_get_app_led_pin());
}