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.

The light

Lesson 6A - Generate code

Creating a new code generator model

  1. Create a new code generator model via New → File → Code Generator Model.
  2. Select the statechart you want to generate code for (Light.sct).
  3. Select the YAKINDU C Code Generator from the drop-down list at the top.
  4. Check which features are available to tailor the code generation process (try [Ctrl]+[Space]).
  5. 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.
  1. 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.
  2. Regenerate the code and take a look at LightReqired.h, light_trace.c to get understand what is going on.
  3. 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'.
  1. Add an setPower operation to the statechart that takes the power value as an argument.
  2. Replace the assignments of the power value by calls to the operation.
  3. Regenerate the code.
  4. Take a look at the file LightRequired.h .
  5. 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 .
  6. Take a loom at the run_cycle function in light_main.c . What can be simplified?