Exercise 6 - Code generation (C)
In this exercise we will create a generator model and generate C code from our example model. The generated
Java code is then used in a simple command line application that allows to raise events and track power changes.
Lesson 6A - Generate code
Creating a new code generator model
- Create a new code generator model via New → File → Code Generator Model.
- Select the statechart you want to generate code for (Light.sct).
- Select the YAKINDU C Code Generator from the drop-down list at the top.
- Check which features are available to tailor the code generation process (try [Ctrl]+[Space]).
- Define the options i a way that the state machine code is generated to the folder "src/sc".
Using the generated code
Open the light_main.c class and make yourself familiar with the code. We will discuss the required
integrattion code jointly.
Running the example
Build an run the example app (right-click the project and select "Run As > Local C/C++ application").
Please be arware that you require an installed comipler toolchain (best is Cygwin on Windows systems).
Lesson 6B (Add-On) - Trace State Changes
The C code generator provides the option to trace state changes. This is implemented as a generator feature.
- Go to the sgen file and add the tracing feature.
Please be aware, that you can use Ctrl-Space in the sgen editor to get proposals.
- Regenerate the code and take a look at LightReqired.h, light_trace.c to get understand what is going on.
- We will jointly discuss this feature.
Lesson 6C (Add-On) - Use Operations
The statechart allows to specify operations in its interface. The code generator will define function prototypes
for these operations that must be implemented by the 'client code'.
- Add an setPower operation to the statechart that takes the power value as an argument.
- Replace the assignments of the power value by calls to the operation.
- Regenerate the code.
- Take a look at the file LightRequired.h .
- Implement the new function prototype that was added to the header file and update the power value
using the functions defined in light_power_control.h .
- Take a loom at the run_cycle function in light_main.c . What can be simplified?