%% Function Reuse in Generated Code
% This example shows how to configure an atomic subsystem for generating reusable
% code. To specify that the code generated for a subsystem execute as an atomic 
% unit, on the Block Parameters dialog box, select the *Treat as atomic unit* 
% parameter . That parameter enables the *Function Packaging* parameter on the 
% *Code Generation* tab. The *Function Packaging* parameter has these four settings:
%
% * |Inline|:  Inline the subsystem code
% * |Nonreusable function|:  Function with I/O passed as global data
% * |Reusable function|:  Function with I/O passed as function arguments
% * |Auto|:  Let Simulink Coder optimize based on context
% 
% The |Reusable function| and |Auto| settings permit the code generator to
% reuse subsystem code. The |Reusable function| and |Nonreusable function|
% settings enable the *Function name options*, *Function name*, and *File name*
% *options* parameters.
%
% If you have an Embedded Coder license, you can configure a nonreusable
% subsystem to accept arguments.

%% Example Model
% The |rtwdemo_ssreuse| model contains two identical subsystems, |SS1| and |SS2|.
% For these subsystems, the *Function packaging* parameter is set to |Reusable|
% |function|, and the *Function name* parameter is |myfun|. The subsystems are
% parameterized masked subsystems. To see the contents of the masked
% subsystems, right-click the subsystem blocks and select *Mask* > *Look Under*
% *Mask*.
model = 'rtwdemo_ssreuse';
open_system(model);

%% Generate and Inspect Code 
% Create a temporary folder for the build and inspection process. Open the
% *Simulink Coder* or *Embedded Coder* app. Then, generate and inspect the
% code.
%
currentDir=pwd;
[~,cgDir]=rtwdemodir();
rtwbuild(model)

%%
cfile=fullfile(cgDir, 'rtwdemo_ssreuse_grt_rtw', 'rtwdemo_ssreuse.c');
rtwdemodbtype(cfile, '/* Model step', '/* Model initialize', 1, 0);

%% 
% In the model step function, there are two calls to the reusable function,
% |myfun|. The mask parameters, |T1Break|, |T1Data|, |T2Break|, and |T2Data|, are
% function arguments.

%%
% Change the *Function Packaging* parameter to |Inline|.
set_param('rtwdemo_ssreuse/SS1','RTWSystemCode','Inline')
set_param('rtwdemo_ssreuse/SS2','RTWSystemCode','Inline')
%%
% Generate and inspect the code.
rtwbuild(model)

%%
cfile=fullfile(cgDir, 'rtwdemo_ssreuse_grt_rtw', 'rtwdemo_ssreuse.c');
rtwdemodbtype(cfile, '/* Model step', '/* Model initialize', 1, 0);

%%
% In the model step function, the subsystem code is inlined.

%%
% Change the *Function Packaging* parameter to |Nonreusable function|.
% For |SS2| , change the *Function name* parameter to |myfun2|.
set_param('rtwdemo_ssreuse/SS1','RTWSystemCode','Nonreusable function')
set_param('rtwdemo_ssreuse/SS2','RTWSystemCode','Nonreusable function')
set_param('rtwdemo_ssreuse/SS2','RTWFcnName','myfun2')
%%
% Generate and inspect the code.
rtwbuild(model)

%%
cfile=fullfile(cgDir, 'rtwdemo_ssreuse_grt_rtw', 'rtwdemo_ssreuse.c');
rtwdemodbtype(cfile, '/* Model step', '/* Model initialize', 1, 0);

%%
% The model step function contains calls to the functions |myfun| and |myfun2|.
% These functions have a void-void interface.

%%
% Change the *Function Packaging* parameter to |Auto|.
set_param('rtwdemo_ssreuse/SS1','RTWSystemCode','Auto')
set_param('rtwdemo_ssreuse/SS2','RTWSystemCode','Auto')

%%
% For the |auto| setting, Simulink Coder chooses the optimal format. For this
% model, the optimal format is a reusable function. 
%%
% Close the model and clean up.
bdclose(model)
rtwdemoclean;
cd(currentDir)

%% 
% Copyright 2012-2019 The MathWorks, Inc.