%% Generate Separate Code for an Atomic Subchart
% This example shows how to use atomic subcharts to generate code for
% individual parts of a Stateflow(R) chart. When you generate code for your
% chart, a separate file stores the code for the atomic subchart. Atomic
% subcharts are supported only in Stateflow charts in Simulink(R) models.
% For more information, see <docid:stateflow_ug#bsbm40z-2>.

% Copyright 2019-2020 The MathWorks, Inc.

%% Original Model Without Atomic Subcharts
% This model contains two <docid:simulink_ref#f8-893976 Sine Wave> blocks:
% one with a frequency of 1 radian per second, and the other with a
% frequency of 2 radians per second.

model = 'ex_reuse_states';
load_system(model)

% Delete all annotations at the top level of the model
ah = find_system(model, 'FindAll', 'on', 'type', 'annotation');
for i = 1:length(ah)
   ao = get_param(ah(i), 'Object');
   ao.delete;
end

open_system(model)

%%
% In the chart, each state uses saturator logic to convert the input sine
% wave to an output square wave of the same frequency.
%
% <<../sf_identical_states_chart.png>>

% g2074387
% open_system([model '/Chart'])

%%
% Because this example does not use atomic subcharts, generated code for
% the entire model is stored in one file. To find code for a specific part
% of the chart, you have to look through the entire file.
%
% In contrast, you can convert state |A| to an atomic subchart and specify
% that the code for the subchart appears in a separate file. This method of
% code generation enables unit testing for a specific part of a chart. You
% avoid searching through unrelated code and focus only on the code that
% interests you.

%% Edit Model to Use Atomic Subcharts
% *Step 1: Convert a State to an Atomic Subchart*
%
% Right-click state |A| and select *Group & Subchart > Atomic Subchart*.
% State |A| changes to an atomic subchart and displays the label *Atomic*
% in the upper-left corner.
%
% <<../sf_identical_states_chart_with_one_atomic_subchart.png>>

% g2074387
% bdclose
% model = 'ex_reuse_states_with_one_atomic_subchart';
% load_system(model)
% open_system([model '/Chart'])
% 
% %%
% close_system(model)
 
%%
% *Step 2: Set Up a Standalone C File for the Atomic Subchart*
% 
% # Open the properties dialog box for subchart |A| by right-clicking the
% subchart and selecting *Properties*.
% # Set the *Code generation function packaging* property to |Reusable
% function|.
% # Set the *Code generation file name options* property to |User
% specified|.
% # In the *Code generation file name* box, enter |saturator| as the name
% of the file.
% # Click *OK*.
%%
% *Step 3: Set Up the Code Generation Report*
%
% # In the *Modeling* tab, click *Chart Properties > Model Settings* to
% open the Configuration parameters dialog box.
% # In the *Code Generation* tab, set the *System target file* parameter to
% |ert.tlc|.
% # Under *Code Generation > Report*, select *Create code generation
% report*.
% # Under *Advanced parameters*, select *Model-to-code*.
% # Click *Apply*.

%%
% *Step 4: Customize the Generated Function Names*
%
% In the Configuration Parameters dialog box, in the *Code Generation >
% Identifiers* tab, set the *Subsystem methods* parameter to the format
% scheme |$R$N$M$F|, where:
% 
% * |$R| is the root model name.
% * |$N| is the block name.
% * |$M| is the mangle token.
% * |$F| is the type of interface function for the atomic subchart.

%% Generate Code
% To generate code for your modelby using Embedded Coder(R), press
% *Ctrl+B*.
%
% The code generation report contains links to the code generated from the
% chart (*Model files*) and the atomic subchart (*Subsystem files*). To
% inspect the code for the subchart, click the |saturator.c| hyperlink.
%
% <<../sf_identical_states_saturator_code.png>>
%
% Line 31 shows that the |during| function generated for the atomic
% subchart has the name |ex_reuse_states_A_during|. This name follows the
% format scheme |$R$N$M$F| specified for Subsystem methods:
%
% * The root model name is |ex_reuse_states|.
% * The block name is |A|.
% * The mangle token is empty.
% * The type of interface function for the atomic subchart is |during|.
%
% *Note:* The line numbers that appear in your code generation report can
% differ from the numbers shown.
