Skip to content

Commit

Permalink
BENB non-volatile toml configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ben committed Dec 2, 2024
1 parent 44ef561 commit bdc30e3
Show file tree
Hide file tree
Showing 18 changed files with 3,158 additions and 116 deletions.
2 changes: 2 additions & 0 deletions grid_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.10)
idf_component_register(
SRCS
"grid_protocol.c"
"grid_config.c"
"grid_cal.c"
"grid_ain.c"
"grid_led.c"
Expand Down Expand Up @@ -56,6 +57,7 @@ idf_component_register(
"lua-5.4.3/src/lvm.c"
"lua-5.4.3/src/lzio.c"

"tomlc99/toml.c"

INCLUDE_DIRS
"."
Expand Down
18 changes: 16 additions & 2 deletions grid_common/grid_cal.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ int grid_cal_init(struct grid_cal_model* cal, uint8_t resolution, uint8_t length
cal->enable = (uint8_t*)malloc(cal->length * sizeof(uint8_t));

const uint16_t half_value = cal->maximum / 2;

const uint16_t default_offset = +32 * 4.5;

for (uint8_t i = 0; i < length; ++i) {
for (uint8_t i = 0; i < cal->length; ++i) {
cal->value[i] = half_value + default_offset;
cal->center[i] = cal->value[i];
cal->enable[i] = 0;
Expand Down Expand Up @@ -52,6 +51,21 @@ int grid_cal_enable_range(struct grid_cal_model* cal, uint8_t start, uint8_t len
return 0;
}

int grid_cal_center_get(struct grid_cal_model* cal, uint8_t channel, uint16_t* center) {

if (!(channel < cal->length)) {
return 1;
}

if (cal->enable[channel]) {
*center = cal->center[channel];
} else {
*center = 0;
}

return 0;
}

int grid_cal_center_set(struct grid_cal_model* cal, uint8_t channel, uint16_t center) {

if (!(channel < cal->length)) {
Expand Down
1 change: 1 addition & 0 deletions grid_common/grid_cal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern struct grid_cal_model grid_cal_state;

int grid_cal_init(struct grid_cal_model* cal, uint8_t resolution, uint8_t length);
int grid_cal_enable_range(struct grid_cal_model* cal, uint8_t start, uint8_t length);
int grid_cal_center_get(struct grid_cal_model* cal, uint8_t channel, uint16_t* center);
int grid_cal_center_set(struct grid_cal_model* cal, uint8_t channel, uint16_t center);
int grid_cal_value_get(struct grid_cal_model* cal, uint8_t channel, uint16_t* value);
int grid_cal_enable_get(struct grid_cal_model* cal, uint8_t channel, uint8_t* enable);
Expand Down
127 changes: 127 additions & 0 deletions grid_common/grid_config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* grid_config.c
*
* Created: 29/11/2024 13:35:37 PM
* Author : BENB
*/

#include "grid_config.h"

extern void grid_platform_printf(char const* fmt, ...);

struct grid_config_model grid_config_state;

int grid_config_init(struct grid_config_model* config, struct grid_cal_model* cal) {

if (!cal) {
return 1;
}

config->cal = cal;

return 0;
}

int grid_config_parse_cal(struct grid_cal_model* cal, char* errbuf, toml_table_t* conf) {

if (!conf) {
return 1;
}

toml_table_t* calibration = toml_table_in(conf, "calibration");
if (!calibration) {
return 1;
}

toml_array_t* centers = toml_array_in(calibration, "centers");
if (!centers) {
return 1;
}

for (int i = 0; i < cal->length; ++i) {
toml_datum_t center = toml_int_at(centers, i);
if (center.ok) {
grid_cal_center_set(cal, i, (uint16_t)center.u.i);
}
}

return 0;
}

int grid_config_parse(struct grid_config_model* config, char* src) {

char errbuf[GRID_CONFIG_ERRBUF];

int status;

toml_table_t* conf = toml_parse(src, errbuf, GRID_CONFIG_ERRBUF);

if (!conf) {
return 1;
}

status = grid_config_parse_cal(config->cal, errbuf, conf); // HECK
if (status) {
return status;
}

toml_free(conf);
return 0;
}

void toml_cat_table_header(char* dest, char* name) {

strcat(dest, "[");
strcat(dest, name);
strcat(dest, "]\n");
}

void toml_cat_bare_key(char* dest, char* name) {

strcat(dest, name);
strcat(dest, " = ");
}

int grid_config_generate_cal(struct grid_cal_model* cal, char* dest) {

toml_cat_table_header(dest, "calibration");

toml_cat_bare_key(dest, "centers");

strcat(dest, "[ ");

char potcal[GRID_CONFIG_MAX_UINT16_ARRAYELEM + 1] = {0};
for (int i = 0; i < cal->length; ++i) {

uint16_t center;
int status = grid_cal_center_get(cal, i, &center);
if (status) {
return status;
}

snprintf(potcal, GRID_CONFIG_MAX_UINT16_ARRAYELEM + 1, "%hu, ", center);
strcat(dest, potcal);
}

strcat(dest, "]\n");

return 0;
}

int grid_config_generate(struct grid_config_model* config, char* dest) {

int status;

dest[0] = '\0';

status = grid_config_generate_cal(config->cal, dest);
if (status) {
return status;
}

return 0;
}

uint32_t grid_config_bytes_cal(struct grid_cal_model* cal) { return strlen("[calibration]\n") + strlen("centers = ") + strlen("[ ") + cal->length * GRID_CONFIG_MAX_UINT16_ARRAYELEM + strlen("]\n"); }

uint32_t grid_config_bytes(struct grid_config_model* config) { return grid_config_bytes_cal(config->cal) + 1 /*\0*/; }
30 changes: 30 additions & 0 deletions grid_common/grid_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#ifndef GRID_CONFIG_H_INCLUDED
#define GRID_CONFIG_H_INCLUDED

#include <stdio.h>
#include <string.h>

#include "tomlc99/toml.h"

#include "grid_cal.h"

#define GRID_CONFIG_MAX_UINT16_ARRAYELEM 7 // uint16_t + ',' + ' '

enum {
GRID_CONFIG_ERRBUF = 256,
};

struct grid_config_model {
struct grid_cal_model* cal;
};

extern struct grid_config_model grid_config_state;

int grid_config_init(struct grid_config_model* config, struct grid_cal_model* cal);
int grid_config_parse(struct grid_config_model* config, char* src);
int grid_config_generate(struct grid_config_model* config, char* dest);
uint32_t grid_config_bytes(struct grid_config_model* config);

#endif /* GRID_CONFIG_H_INCLUDED */
6 changes: 3 additions & 3 deletions grid_common/grid_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ uint8_t grid_decode_pagediscard_to_ui(char* header, char* chunk) {

if (msg_instr == GRID_INSTR_EXECUTE_code) {

if (!grid_ui_bulk_pageread_init(&grid_ui_state, grid_ui_page_get_activepage(&grid_ui_state), id, &grid_protocol_nvm_read_succcess_callback)) {
if (!grid_ui_bulk_page_init(&grid_ui_state, GRID_UI_BULK_READ_PROGRESS, grid_ui_page_get_activepage(&grid_ui_state), id, &grid_protocol_nvm_read_succcess_callback)) {
return 1;
}

Expand Down Expand Up @@ -871,7 +871,7 @@ uint8_t grid_decode_pagestore_to_ui(char* header, char* chunk) {

if (msg_instr == GRID_INSTR_EXECUTE_code) {

if (!grid_ui_bulk_pagestore_init(&grid_ui_state, grid_ui_page_get_activepage(&grid_ui_state), id, &grid_protocol_nvm_store_succcess_callback)) {
if (!grid_ui_bulk_page_init(&grid_ui_state, GRID_UI_BULK_STORE_PROGRESS, grid_ui_page_get_activepage(&grid_ui_state), id, &grid_protocol_nvm_store_succcess_callback)) {
return 1;
}

Expand Down Expand Up @@ -911,7 +911,7 @@ uint8_t grid_decode_pageclear_to_ui(char* header, char* chunk) {

if (msg_instr == GRID_INSTR_EXECUTE_code) {

if (!grid_ui_bulk_pageclear_init(&grid_ui_state, grid_ui_page_get_activepage(&grid_ui_state), id, &grid_protocol_nvm_clear_succcess_callback)) {
if (!grid_ui_bulk_page_init(&grid_ui_state, GRID_UI_BULK_CLEAR_PROGRESS, grid_ui_page_get_activepage(&grid_ui_state), id, &grid_protocol_nvm_clear_succcess_callback)) {
return 1;
}

Expand Down
5 changes: 2 additions & 3 deletions grid_common/grid_lua_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1630,8 +1630,6 @@
lua_settable(L, -3);
}

grid_platform_printf("potmeter_calibration_get()\n");

return 1;
}

Expand Down Expand Up @@ -1688,7 +1686,7 @@
}
}

grid_platform_printf("potmeter_calibration_set()\n");
grid_ui_bulk_conf_init(&grid_ui_state, GRID_UI_BULK_CONFSTORE_PROGRESS, 0, NULL);

return 0;
}
Expand Down Expand Up @@ -1755,6 +1753,7 @@
{GRID_LUA_FNC_G_IMMEDIATE_SEND_short, GRID_LUA_FNC_G_IMMEDIATE_SEND_fnptr},

{GRID_LUA_FNC_G_ELEMENT_COUNT_short, GRID_LUA_FNC_G_ELEMENT_COUNT_fnptr},

{GRID_LUA_FNC_G_POTMETER_CALIBRATION_GET_short, GRID_LUA_FNC_G_POTMETER_CALIBRATION_GET_fnptr},
{GRID_LUA_FNC_G_POTMETER_CALIBRATION_SET_short, GRID_LUA_FNC_G_POTMETER_CALIBRATION_SET_fnptr},

Expand Down
Loading

0 comments on commit bdc30e3

Please sign in to comment.