Skip to content

Commit

Permalink
factor out enums in a separate header
Browse files Browse the repository at this point in the history
  • Loading branch information
yconst committed Feb 5, 2024
1 parent 5c2fce3 commit 5403b65
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 41 deletions.
12 changes: 12 additions & 0 deletions avlos/generators/generator_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ def process(instance, config):
env.filters["bitmask_eps"] = avlos_bitmask_eps
env.filters["as_include"] = as_include

template = env.get_template("tm_enums.h.jinja")
try:
includes = config["header_includes"]
except KeyError:
includes = []
os.makedirs(os.path.dirname(config["paths"]["output_enums"]), exist_ok=True)
with open(config["paths"]["output_enums"], "w") as output_file:
print(
template.render(instance=instance, includes=includes),
file=output_file,
)

template = env.get_template("fw_endpoints.h.jinja")
try:
includes = config["header_includes"]
Expand Down
39 changes: 1 addition & 38 deletions avlos/templates/fw_endpoints.h.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,6 @@
#include {{ include | as_include }}
{%- endfor %}

typedef enum
{
AVLOS_RET_NOACTION,
AVLOS_RET_READ = 1,
AVLOS_RET_WRITE = 2,
AVLOS_RET_CALL = 3
} Avlos_Return;

typedef enum
{
AVLOS_CMD_WRITE,
AVLOS_CMD_READ = 1
} Avlos_Command;

{%- for ep in instance | bitmask_eps %}

typedef enum
{
{%- set comma = joiner(", ") %}
{{ ep.full_name | replace(".", "_") | upper }}_NONE = 0,
{%- for flag in ep.bitmask %}{{ comma() }}
{{ ep.full_name | replace(".", "_") | upper }}_{{ flag.name }} = (1 << {{ loop.index0 }})
{%- endfor %}
} {{ ep.full_name | replace(".", "_") }}_flags;
{%- endfor %}

{%- for ep in instance | enum_eps %}

typedef enum
{
{%- set comma = joiner(", ") %}
{%- for option in ep.options %}{{ comma() }}
{{ ep.full_name | replace(".", "_") | upper }}_{{ option.name }} = {{ loop.index0 }}
{%- endfor %}
} {{ ep.full_name | replace(".", "_") }}_options;
{%- endfor %}

extern uint32_t avlos_proto_hash;
extern uint8_t (*avlos_endpoints[{{ instance | endpoints | length }}])(uint8_t * buffer, uint8_t * buffer_len, Avlos_Command cmd);
extern uint32_t _avlos_get_proto_hash(void);
Expand All @@ -67,4 +30,4 @@ extern uint32_t _avlos_get_proto_hash(void);
*/
uint8_t avlos_{{attr.full_name | replace(".", "_") }}(uint8_t * buffer, uint8_t * buffer_len, Avlos_Command cmd);

{%- endfor %}
{%- endfor %}
46 changes: 46 additions & 0 deletions avlos/templates/tm_enums.h.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file was automatically generated using Avlos.
* https://github.com/tinymovr/avlos
*
* Any changes to this file will be overwritten when
* content is regenerated.
*/

#pragma once

typedef enum
{
AVLOS_RET_NOACTION,
AVLOS_RET_READ = 1,
AVLOS_RET_WRITE = 2,
AVLOS_RET_CALL = 3
} Avlos_Return;

typedef enum
{
AVLOS_CMD_WRITE,
AVLOS_CMD_READ = 1
} Avlos_Command;

{%- for ep in instance | bitmask_eps %}

typedef enum
{
{%- set comma = joiner(", ") %}
{{ ep.full_name | replace(".", "_") | upper }}_NONE = 0,
{%- for flag in ep.bitmask %}{{ comma() }}
{{ ep.full_name | replace(".", "_") | upper }}_{{ flag.name }} = (1 << {{ loop.index0 }})
{%- endfor %}
} {{ ep.full_name | replace(".", "_") }}_flags;
{%- endfor %}

{%- for ep in instance | enum_eps %}

typedef enum
{
{%- set comma = joiner(", ") %}
{%- for option in ep.options %}{{ comma() }}
{{ ep.full_name | replace(".", "_") | upper }}_{{ option.name }} = {{ loop.index0 }}
{%- endfor %}
} {{ ep.full_name | replace(".", "_") }}_options;
{%- endfor %}
5 changes: 3 additions & 2 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ The output config defines the output modules that will be used and their options
generator_c:
enabled: true
paths:
output_enums: outputs/tm_enums.h
output_header: outputs/header.h
output_impl: outputs/header.c
header_includes:
- src/header.h
- src/tm_enums.h
impl_includes:
- src/test.h
- src/header.h
CLI Usage
---------
Expand Down
2 changes: 2 additions & 0 deletions tests/definition/avlos_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ generators:
generator_c:
enabled: true
paths:
output_enums: ../outputs/enums.h
output_header: ../outputs/test.h
output_impl: ../outputs/test.c
header_includes:
- src/common.h
impl_includes:
- src/dust.h
- src/test.h
- src/tm_enums.h
generator_rst:
enabled: true
paths:
Expand Down
6 changes: 5 additions & 1 deletion tests/test_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def test_c_output_manual(self):
def_path_str = str(
importlib.resources.files("tests").joinpath("definition/good_device.yaml")
)
enum_path_str = str(
importlib.resources.files("tests").joinpath("outputs/tm_enums.h")
)
header_path_str = str(
importlib.resources.files("tests").joinpath("outputs/test.h")
)
Expand All @@ -27,10 +30,11 @@ def test_c_output_manual(self):
config = {
"hash_string": "0x9e8dc7ac",
"paths": {
"output_enums": enum_path_str,
"output_header": header_path_str,
"output_impl": impl_path_str,
},
"c_includes": {"src/common.h"},
"c_includes": {"src/common.h", "src/tm_enums.h"},
}
generator_c.process(obj, config)

Expand Down

0 comments on commit 5403b65

Please sign in to comment.