diff --git a/XFTGenerator.py b/XFTGenerator.py index 504f947..2759c0f 100644 --- a/XFTGenerator.py +++ b/XFTGenerator.py @@ -111,6 +111,8 @@ def main(): variables["filename_lc"] = variables["filename"].lower() variables["fn"] = get_input("Filename (short)", None, True).upper() + fn_uc = variables["filename"] + ########## # States # ########## @@ -126,17 +128,76 @@ def main(): else: break - states = ["ST"+variables["fn"]+"_"+s for s in states] - variables["STATES_ENUM"] = ",\n".join(states) + prefix = "ST"+variables["fn"]+"_" + variables["STATES_ENUM"] = ",\n".join([prefix+s for s in states]) states_cases = [] + states_cases_entry = [] + states_cbs_struct = [] + states_cbs_init = [] for state in states: - case_ = f"case {state}:\n" + case_ = "case {STATE}:\n" case_ += " break;" + case_ = case_.replace("{STATE}", prefix+state) states_cases.append(case_) + case_ = "case {STATE}:\n" + case_ += " if (me->{state}.f != NULL) {\n" + case_ += " me->{state}.f(me->{state}.p);\n" + case_ += " }\n" + case_ += " break;" + case_ = case_.replace("{state}", state.lower()).replace("{STATE}", prefix+state) + states_cases_entry.append(case_) + + states_cbs_struct.append(f"{fn_uc}_CALLBACK {state.lower()};") + states_cbs_init.append(f"me->{state.lower()}.f = NULL;") + variables["STATES_CASES"] = "\n\n".join(states_cases) + variables["STATES_CASES_ENTRY"] = "\n\n".join(states_cases_entry) + variables["STATES_CBS_STRUCT"] = "\n".join(states_cbs_struct) + variables["STATES_CBS_INIT"] = "\n".join(states_cbs_init) + + ############# + # Callbacks # + ############# + + callbacks_dec = [] + + callback_dec = "" + callback_dec += "/**\n" + callback_dec += " * Set the callback function to call when the {filename} is entering state {state}\n" + callback_dec += " * @param me the {filename} itself\n" + callback_dec += " * @param f the function to call\n" + callback_dec += " * @param p the param(s) to pass to the function\n" + callback_dec += " */\n" + callback_dec += "void {filename}_on{State}({filename}* me, {filename}_CALLBACK_FUNCTION f, void* p);" + callback_dec = callback_dec.replace("{filename}", fn_uc) + + for state in states: + callbacks_dec.append( + callback_dec.replace("{state}", state.lower()) + .replace("{State}", state.capitalize()) + ) + + variables["CALLBACKS_DEC"] = "\n\n".join(callbacks_dec) + + callbacks_def = [] + + callback_def = "" + callback_def += "void {filename}_on{State}({filename}* me, {filename}_CALLBACK_FUNCTION f, void* p) {\n" + callback_def += " me->{state}.f = f;\n" + callback_def += " me->{state}.p = p;\n" + callback_def += "}" + callback_def = callback_def.replace("{filename}", fn_uc) + + for state in states: + callbacks_def.append( + callback_def.replace("{state}", state.lower()) + .replace("{State}", state.capitalize()) + ) + + variables["CALLBACKS_DEF"] = "\n\n".join(callbacks_def) ########## # Events # @@ -165,7 +226,7 @@ def main(): emit_def += "void {filename}_emit{Event}({filename}* me, uint16_t t) {\n" emit_def += " POST(me, &{filename}_processEvent, ev{fn}{event}, t, 0);\n" emit_def += "}" - emit_def = emit_def.replace("{filename}", variables["filename"]).replace("{fn}", variables["fn"]) + emit_def = emit_def.replace("{filename}", fn_uc).replace("{fn}", variables["fn"]) for event in events[1:]: events_emits_def.append( @@ -182,7 +243,7 @@ def main(): emit_dec += " * @param t time to wait in ms before triggering event\n" emit_dec += " */" emit_dec += "void {filename}_emit{Event}({filename}* me, uint16_t t);" - emit_dec = emit_dec.replace("{filename}", variables["filename"]) + emit_dec = emit_dec.replace("{filename}", fn_uc) for event in events[1:]: events_emits_dec.append( @@ -204,10 +265,10 @@ def main(): setter_def += "void {filename}_set{Name}({filename}* me, {type} v) {\n" setter_def += " me->{name} = v;\n" setter_def += "}" - setter_def = setter_def.replace("{filename}", variables["filename"]) + setter_def = setter_def.replace("{filename}", fn_uc) setter_dec = "void {filename}_set{Name}({filename}* me, {type} v);" - setter_dec = setter_dec.replace("{filename}", variables["filename"]) + setter_dec = setter_dec.replace("{filename}", fn_uc) print("Variables:") print("(leave name empty to end)") diff --git a/templates/file.c b/templates/file.c index 8689cc2..08eceb8 100644 --- a/templates/file.c +++ b/templates/file.c @@ -10,6 +10,7 @@ void ${filename}_init(${filename}* me){ me->state = ST${fn}_INIT; ${VARS_INIT} + ${STATES_CBS_INIT} } void ${filename}_startBehaviour(${filename}* me){ @@ -44,7 +45,7 @@ bool ${filename}_processEvent(Event* ev) { case ST${fn}_INIT: break; - ${STATES_CASES} + ${STATES_CASES_ENTRY} } processed = true; @@ -52,6 +53,12 @@ bool ${filename}_processEvent(Event* ev) { return processed; } +/************* + * Callbacks * + *************/ + +${CALLBACKS_DEF} + /************ * EMITTERS * ************/ diff --git a/templates/file.h b/templates/file.h index b8f8bbb..4b49b81 100644 --- a/templates/file.h +++ b/templates/file.h @@ -18,9 +18,16 @@ typedef enum { ${EVENTS_ENUM} } ${filename}_EVENTS; +typedef void (*${filename}_CALLBACK_FUNCTION)(void*); +typedef struct { + ${filename}_CALLBACK_FUNCTION f; // function + void* p; // param(s) +} ${filename}_CALLBACK; + typedef struct { ${filename}_STATES state; ${VARS_STRUCT} + ${STATES_CBS_STRUCT} } ${filename}; /** @@ -42,6 +49,12 @@ void ${filename}_startBehaviour(${filename}* me); */ bool ${filename}_processEvent(Event* ev); +/************* + * Callbacks * + *************/ + +${CALLBACKS_DEC} + /************ * EMITTERS * ************/