Skip to content

Commit eef64d1

Browse files
Enzo Fresehubmartin
Enzo Frese
authored andcommitted
subsys: ctr_config: Refactor, west: Add CHESTER project generator
1 parent d695c1c commit eef64d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+7928
-1976
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ build/
44
doc/html
55
doc/latex
66
scripts/west_commands/__pycache__/*.pyc
7+
scripts/west_commands/project_generator/__pycache__/*.pyc
78
twister-out*
89
.vscode/*

include/chester/ctr_config.h

+103
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/* Zephyr includes */
1111
#include <zephyr/shell/shell.h>
12+
#include <zephyr/settings/settings.h>
1213

1314
/* Standard include */
1415
#include <stddef.h>
@@ -22,12 +23,114 @@ extern "C" {
2223
* @{
2324
*/
2425

26+
enum ctr_config_item_type {
27+
CTR_CONFIG_TYPE_INT,
28+
CTR_CONFIG_TYPE_FLOAT,
29+
CTR_CONFIG_TYPE_BOOL,
30+
CTR_CONFIG_TYPE_ENUM,
31+
CTR_CONFIG_TYPE_STRING,
32+
};
33+
34+
#define CTR_CONFIG_ITEM_INT(_name_d, _var, _min, _max, _help, _default) \
35+
{ \
36+
.module = SETTINGS_PFX, \
37+
.name = _name_d, \
38+
.type = CTR_CONFIG_TYPE_INT, \
39+
.variable = &_var, \
40+
.size = sizeof(_var), \
41+
.min = _min, \
42+
.max = _max, \
43+
.help = _help, \
44+
.default_int = _default, \
45+
}
46+
47+
#define CTR_CONFIG_ITEM_FLOAT(_name_d, _var, _min, _max, _help, _default) \
48+
{ \
49+
.module = SETTINGS_PFX, \
50+
.name = _name_d, \
51+
.type = CTR_CONFIG_TYPE_FLOAT, \
52+
.variable = &_var, \
53+
.size = sizeof(_var), \
54+
.min = _min, \
55+
.max = _max, \
56+
.help = _help, \
57+
.default_float = _default, \
58+
}
59+
60+
#define CTR_CONFIG_ITEM_BOOL(_name_d, _var, _help, _default) \
61+
{ \
62+
.module = SETTINGS_PFX, \
63+
.name = _name_d, \
64+
.type = CTR_CONFIG_TYPE_BOOL, \
65+
.variable = &_var, \
66+
.size = sizeof(_var), \
67+
.help = _help, \
68+
.default_bool = _default, \
69+
}
70+
71+
#define CTR_CONFIG_ITEM_ENUM(_name_d, _var, _items_str, _help, _default) \
72+
{ \
73+
.module = SETTINGS_PFX, \
74+
.name = _name_d, \
75+
.type = CTR_CONFIG_TYPE_ENUM, \
76+
.variable = &_var, \
77+
.size = sizeof(_var), \
78+
.min = 0, \
79+
.max = ARRAY_SIZE(_items_str), \
80+
.help = _help, \
81+
.enums = _items_str, \
82+
.default_enum = _default, \
83+
}
84+
85+
#define CTR_CONFIG_ITEM_STRING(_name_d, _var, _help, _default) \
86+
{ \
87+
.module = SETTINGS_PFX, \
88+
.name = _name_d, \
89+
.type = CTR_CONFIG_TYPE_STRING, \
90+
.variable = _var, \
91+
.size = ARRAY_SIZE(_var), \
92+
.help = _help, \
93+
.default_string = _default, \
94+
}
95+
96+
struct ctr_config_item {
97+
const char *module;
98+
const char *name;
99+
enum ctr_config_item_type type;
100+
void *variable;
101+
size_t size;
102+
int min;
103+
int max;
104+
const char *help;
105+
const char **enums;
106+
union {
107+
int default_int;
108+
float default_float;
109+
bool default_bool;
110+
int default_enum;
111+
const char *default_string;
112+
};
113+
};
114+
25115
typedef int (*ctr_config_show_cb)(const struct shell *shell, size_t argc, char **argv);
26116

27117
int ctr_config_save(bool reboot);
28118
int ctr_config_reset(bool reboot);
29119
void ctr_config_append_show(const char *name, ctr_config_show_cb cb);
30120

121+
int ctr_config_show_item(const struct shell *shell, const struct ctr_config_item *item);
122+
int ctr_config_help_item(const struct shell *shell, const struct ctr_config_item *item);
123+
int ctr_config_parse_item(const struct shell *shell, char *argv,
124+
const struct ctr_config_item *item);
125+
int ctr_config_init_item(const struct ctr_config_item *item);
126+
127+
int ctr_config_cmd_config(const struct ctr_config_item *items, int nitems,
128+
const struct shell *shell, size_t argc, char **argv);
129+
int ctr_config_h_export(const struct ctr_config_item *items, int nitems,
130+
int (*export_func)(const char *name, const void *val, size_t val_len));
131+
int ctr_config_h_set(const struct ctr_config_item *items, int nitems, const char *key, size_t len,
132+
settings_read_cb read_cb, void *cb_arg);
133+
31134
/** @} */
32135

33136
#ifdef __cplusplus

samples/ctr_config/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright (c) 2023 HARDWARIO a.s.
3+
#
4+
# SPDX-License-Identifier: LicenseRef-HARDWARIO-5-Clause
5+
#
6+
7+
cmake_minimum_required(VERSION 3.20.0)
8+
9+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
10+
project(blinky)
11+
12+
target_sources(app PRIVATE src/main.c)

samples/ctr_config/prj.conf

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright (c) 2023 HARDWARIO a.s.
3+
#
4+
# SPDX-License-Identifier: LicenseRef-HARDWARIO-5-Clause
5+
#
6+
7+
CONFIG_CTR_BUTTON=y
8+
CONFIG_CTR_DEFAULTS=y
9+
CONFIG_CTR_LED=y
10+
CONFIG_CTR_LOG=y
11+
CONFIG_CTR_CONFIG=y
12+
CONFIG_CTR_SHELL=y

samples/ctr_config/src/main.c

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright (c) 2023 HARDWARIO a.s.
3+
*
4+
* SPDX-License-Identifier: LicenseRef-HARDWARIO-5-Clause
5+
*/
6+
7+
/* CHESTER includes */
8+
#include <chester/ctr_led.h>
9+
#include <chester/ctr_config.h>
10+
11+
/* Zephyr includes */
12+
#include <zephyr/kernel.h>
13+
#include <zephyr/logging/log.h>
14+
15+
LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);
16+
17+
#define SETTINGS_PFX "sample"
18+
19+
struct config {
20+
int interval;
21+
enum ctr_led_channel channel;
22+
char greeting[16 + 1];
23+
};
24+
25+
static struct config m_config;
26+
static struct config m_config_interim;
27+
28+
static const char *m_enum_ctr_led_channel_str[] = {"red", "green", "yellow", "external", "load"};
29+
30+
static const struct ctr_config_item m_config_items[] = {
31+
CTR_CONFIG_ITEM_INT("interval", m_config_interim.interval, 200, 5000, "blink interval",
32+
500),
33+
CTR_CONFIG_ITEM_ENUM("channel", m_config_interim.channel, m_enum_ctr_led_channel_str,
34+
"LED channel", CTR_LED_CHANNEL_R),
35+
CTR_CONFIG_ITEM_STRING("greeting", m_config_interim.greeting, "greeting text", "Alive"),
36+
};
37+
38+
static int cmd_config_show(const struct shell *shell, size_t argc, char **argv)
39+
{
40+
for (int i = 0; i < ARRAY_SIZE(m_config_items); i++) {
41+
ctr_config_show_item(shell, &m_config_items[i]);
42+
}
43+
44+
return 0;
45+
}
46+
47+
static int cmd_config(const struct shell *shell, size_t argc, char **argv)
48+
{
49+
return ctr_config_cmd_config(m_config_items, ARRAY_SIZE(m_config_items), shell, argc, argv);
50+
}
51+
52+
static int h_set(const char *key, size_t len, settings_read_cb read_cb, void *cb_arg)
53+
{
54+
return ctr_config_h_set(m_config_items, ARRAY_SIZE(m_config_items), key, len, read_cb,
55+
cb_arg);
56+
}
57+
58+
static int h_commit(void)
59+
{
60+
LOG_DBG("Loaded settings in full");
61+
memcpy(&m_config, &m_config_interim, sizeof(struct config));
62+
return 0;
63+
}
64+
65+
static int h_export(int (*export_func)(const char *name, const void *val, size_t val_len))
66+
{
67+
return ctr_config_h_export(m_config_items, ARRAY_SIZE(m_config_items), export_func);
68+
}
69+
70+
static int print_help(const struct shell *shell, size_t argc, char **argv)
71+
{
72+
if (argc > 1) {
73+
shell_error(shell, "command not found: %s", argv[1]);
74+
shell_help(shell);
75+
return -EINVAL;
76+
}
77+
78+
shell_help(shell);
79+
80+
return 0;
81+
}
82+
83+
/* clang-format off */
84+
85+
SHELL_STATIC_SUBCMD_SET_CREATE(
86+
sub_sample,
87+
88+
SHELL_CMD_ARG(config, NULL,
89+
"Configuration commands.",
90+
cmd_config, 1, 3),
91+
92+
SHELL_SUBCMD_SET_END
93+
);
94+
95+
/* clang-format on */
96+
97+
SHELL_CMD_REGISTER(sample, &sub_sample, "Sample commands.", print_help);
98+
99+
int main(void)
100+
{
101+
int ret;
102+
103+
LOG_INF("Build time: " __DATE__ " " __TIME__);
104+
105+
for (int i = 0; i < ARRAY_SIZE(m_config_items); i++) {
106+
ctr_config_init_item(&m_config_items[i]);
107+
}
108+
109+
static struct settings_handler sh = {
110+
.name = SETTINGS_PFX,
111+
.h_set = h_set,
112+
.h_commit = h_commit,
113+
.h_export = h_export,
114+
};
115+
116+
ret = settings_register(&sh);
117+
if (ret) {
118+
LOG_ERR("Call `settings_register` failed: %d", ret);
119+
return ret;
120+
}
121+
122+
ret = settings_load_subtree(SETTINGS_PFX);
123+
if (ret) {
124+
LOG_ERR("Call `settings_load_subtree` failed: %d", ret);
125+
return ret;
126+
}
127+
128+
ctr_config_append_show(SETTINGS_PFX, cmd_config_show);
129+
130+
bool state = false;
131+
132+
for (;;) {
133+
LOG_INF("%s", m_config.greeting);
134+
135+
/* Invert state variable */
136+
state = !state;
137+
138+
/* Control LED */
139+
ctr_led_set(m_config.channel, state);
140+
141+
/* Wait 500 ms */
142+
k_sleep(K_MSEC(m_config.interval));
143+
}
144+
145+
return 0;
146+
}

scripts/deploy.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ deploy_clime() {
5151
# Build CHESTER Clime IAQ
5252
#
5353
cp CMakeLists.txt.bak CMakeLists.txt
54-
gawk -i inplace '{ gsub(/set\(SHIELD ctr_lrw ctr_lte ctr_s2)*/, "set(SHIELD ctr_lrw ctr_lte ctr_s1 ctr_x10)"); print }' CMakeLists.txt
54+
gawk -i inplace '{ gsub(/set\(SHIELD ctr_lrw ctr_lte ctr_s2)*/, "set(SHIELD ctr_lrw ctr_lte ctr_s1)"); print }' CMakeLists.txt
5555
rm -rf build/
5656
FW_NAME="CHESTER Clime IAQ" FW_VERSION=$FW_VERSION west build
5757
hardwario chester app fw upload --name "hio-chester-clime-iaq" --version $FW_VERSION

scripts/west-commands.yml

+20
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,23 @@ west-commands:
2020
- name: gen-codec
2121
class: GenerateCodec
2222
help: generate codec
23+
- file: scripts/west_commands/project_generator/project_generator_west.py
24+
commands:
25+
- name: chester-init
26+
class: ProjectInit
27+
help: initialize Hardwario Project Application
28+
- file: scripts/west_commands/project_generator/project_generator_west.py
29+
commands:
30+
- name: chester-update
31+
class: ProjectUpdate
32+
help: update Hardwario Project Application
33+
- file: scripts/west_commands/project_generator/project_generator_west.py
34+
commands:
35+
- name: chester-cbor
36+
class: ProjectCbor
37+
help: create/update CBOR to Hardwario Project Application
38+
- file: scripts/west_commands/project_generator/project_generator_west.py
39+
commands:
40+
- name: chester-create
41+
class: ProjectCreate
42+
help: create Hardwario Project Application
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{#- COPYRIGHT AND LICENSE #}
2+
{%- if data['project']['company'] or data['project']['license'] -%}
3+
#
4+
{%- if data['project']['company'] %}
5+
# Copyright (c) {{data['project']['company']}}
6+
{%- endif %}
7+
{%- if data['project']['company'] and data['project']['license'] %}
8+
#
9+
{%- endif %}
10+
{%- if data['project']['license'] %}
11+
# {{data['project']['license'] }}
12+
{%- endif %}
13+
#
14+
{% raw %}
15+
{% endraw %}
16+
{%- endif -%}
17+
cmake_minimum_required(VERSION 3.20.0)
18+
19+
set(ENV{FW_BUNDLE} "{{data['project']['fw_bundle']}}")
20+
set(ENV{FW_VERSION} "{{data['project']['fw_version']}}")
21+
set(ENV{FW_NAME} "{{data['project']['fw_name']}}")
22+
23+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
24+
25+
project({{ project_name }})
26+
{%- if 'subsystem_lte' in data['features']%}
27+
28+
add_custom_command(
29+
COMMAND ../../../scripts/gen-msg-key.py ../codec/cbor-decoder.yaml ../src/msg_key.h
30+
OUTPUT ../src/msg_key.h
31+
MAIN_DEPENDENCY ../codec/cbor-decoder.yaml
32+
)
33+
{%- endif %}
34+
{% for file in sources %}
35+
{%- if file != 'app_ble_svc.c'%}
36+
target_sources(app PRIVATE src/{{ file }})
37+
{%- endif %}
38+
{%- if file == 'app_ble_svc.c' %}
39+
target_sources_ifdef(CONFIG_CTR_BLE app PRIVATE src/app_ble_svc.c)
40+
{%- endif%}
41+
{%- endfor %}
42+
43+
target_precompile_headers(app PRIVATE src/feature.h)
44+
target_precompile_headers(app PRIVATE src/variant.h)
45+
46+
# ### Preserved code "cmake" (begin)
47+
# ^^^ Preserved code "cmake" (end)
48+
{%- raw %}
49+
{% endraw%}

0 commit comments

Comments
 (0)