Skip to content

The repository contains app demonstrates the use of threaded code for state machine on C

License

Notifications You must be signed in to change notification settings

mk590901/Switch-TC-C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SWITCH-TC-C

The application tests threaded code generated by the editor of hierarchical state machines. The original scheme can be seen in the attached movie. It's another model of a switch affected by two events: TURN and RESET. The first switches two states ON and OFF, the second resets the state machine to the OFF state regardless of what state it was in before.

Precondition

The editor's Planner module was supplemented with C code generator, which automatically generates the Switch_resetHelper.h and Switch_resetHelper.c files. A core has also been added to the application, which services the launch of threaded code and the impact of events on it. This is a set of next files: core.h and core.c.

Code modification

The generated switch_reset_helper.c file is a skeleton for the logical part of the application, namely the list and bodies of empty transfer functions that can and should be filled with some content. For example, with trace elements in the simplest case. Some functions may not be used and should be deleted or commented out:

//	File Switch_resetHelper.h automatically generated at 2024-12-23 08:21:34

#ifndef Switch_resetHelper_H
#define Switch_resetHelper_H

#include "core.h"

//	Transfer functions

	//void switchEntry(void* data);
	//void switchInit(void* data);
	void offEntry(void* data);
	void offReset(void* data);
	//void offExit(void* data);
	void offTurn(void* data);
	void onEntry(void* data);
	//void onExit(void* data);
	void onTurn(void* data);

	void create_helper(QHsmHelper* helper);

#endif	//	Switch_resetHelper_H
//	File Switch_resetHelper.c automatically generated at 2024-12-23 08:21:33

#include "Switch_resetHelper.h"
//	Transfer functions

//void switchEntry(void* data) {
//}

//void switchInit(void* data) {
//}

void offEntry(void* data) {
	printf("OFF\n");
}

void offReset(void* data) {
	printf("@RESET\n");
}

//void offExit(void* data) {
//}

void offTurn(void* data) {
	printf("OFF: TURN\n");
}

void onEntry(void* data) {
	printf("ON\n");
}

//void onExit(void* data) {
//}

void onTurn(void* data) {
	printf("ON: TURN\n");
}

void create_helper(QHsmHelper* helper) {
	void (*switch_init_functions[])(void*) = {
		//switchEntry,
		//switchInit,
		offEntry
	};
	insert(helper, "switch", "init", compose_threaded_code_executor("off", switch_init_functions, ARRAY_SIZE(switch_init_functions)));

	void (*off_reset_functions[])(void*) = {
		offReset,
		//offExit,
		//switchInit,
		offEntry
	};
	insert(helper, "off", "RESET", compose_threaded_code_executor("off", off_reset_functions, ARRAY_SIZE(off_reset_functions)));

	void (*off_turn_functions[])(void*) = {
		offTurn,
		onEntry
	};
	insert(helper, "off", "TURN", compose_threaded_code_executor("on", off_turn_functions, ARRAY_SIZE(off_turn_functions)));

	void (*on_reset_functions[])(void*) = {
		offReset,
		//onExit,
		//offExit,
		//switchInit,
		offEntry
	};
	insert(helper, "on", "RESET", compose_threaded_code_executor("off", on_reset_functions, ARRAY_SIZE(on_reset_functions)));

	void (*on_turn_functions[])(void*) = {
		onTurn,
		//onExit,
		//offExit,
		//switchInit,
		offEntry
	};
	insert(helper, "on", "TURN", compose_threaded_code_executor("off", on_turn_functions, ARRAY_SIZE(on_turn_functions)));

}

Description of the application

The application is created as a ubuntu console application and can be launched in terminal mode:

#include "Switch_resetHelper.h"

// Function to test the switch
void test_switch() {
    QHsmHelper* helper = compose_qhsm_helper("switch");
    Runner* runner = compose_runner(helper);
    create_helper(helper);

    post_(runner, "init");
    post_(runner, "TURN");
    post_(runner, "RESET");
    post_(runner, "TURN");
    post_(runner, "TURN");
    post_(runner, "RESET");

    freeAllAllocatedMemory(helper, runner);
}

int main() {
    test_switch();
    return 0;
}

Compilation and build

micrcx@micrcx-desktop:~/vs_cpp/tc_c$ make
cc    -c -o core.o core.c
cc    -c -o Switch_resetHelper.o Switch_resetHelper.c
cc    -c -o main.o main.c
g++ core.o Switch_resetHelper.o main.o -o switch

Run

micrcx@micrcx-desktop:~/vs_cpp/tc_c$ ./switch
OFF
OFF: TURN
ON
@RESET
OFF
OFF: TURN
ON
ON: TURN
OFF
@RESET
OFF
micrcx@micrcx-desktop:~/vs_cpp/tc_c$ 

Movie

c_sample.webm

About

The repository contains app demonstrates the use of threaded code for state machine on C

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published