%% Use Custom Code to Define Constants, Variables, and Functions
% This example show how to use custom C code to define constants,
% variables, and functions that apply to all charts in your model.

% Copyright 2018 The MathWorks, Inc.

%%
% The model contains a Stateflow chart with an input signal from a Sine
% Wave block.

mdl = 'sf_custom_code_constants_vars_fcns';
open_system(mdl)

%%
% The chart contains two states |A| and |B|, and three data objects:
% |input_data|, |local_data|, and |out_data|. The chart accesses a custom
% variable named |my_global| and calls a custom function named
% |my_function|.

open_system('sf_custom_code_constants_vars_fcns/Chart')

%%
% To configure the model to access the custom code:
% 
% # Open the Model Configuration Parameters dialog box.
% # Select the *Simulation Target* pane.
% # In the *Header file* subpane, enter |#define| and |#include|
% statements. When you include a custom header file, you must enclose the
% file name in double quotes.
% # In the *Include directories* subpane, enter the name of the folder that
% contains your custom code files. To access custom code files in a
% subfolder of the model folder, use a relative path name of the form
% |.\subfolder_name|.
% # In the *Source files* subpane, enter the name of the source file that
% contains your custom code. To access a source file that resides in a
% subfolder of the model folder, use a relative path name of the form
% |.\subfolder_name\source_file.c|.

%% 
% In this example, the custom code defines three constants, a variable, and
% a function by using these configurations:
%
% * The *Header file* subpane contains this statement: |#include
% "sf_custom_code_constants_vars_fcns_hdr.h"|
% * The *Include directories* subpane contains a single period (|.|) to
% indicate that all of the custom code files reside in the same folder as
% the model.
% * The *Source file* subpane contains this file name:
% |sf_custom_code_constants_vars_fcns_src.c|
%
% <<../custom_code_constants_vars_fcns_header_file.png>>

%%
% The custom header file |sf_custom_code_constants_vars_fcns_hdr.h|
% contains the definitions of three constants:
%
%  #define TRUE 1
%  #define FALSE 0
%  #define MAYBE 2
% 
% The header file also contains declarations for the variable |my_global|
% and the function |my_function|:
%
%  extern int myglobal;
%  extern int my_function(int var1, double var2);

%%
% The custom source file |sf_custom_code_constants_vars_fcns_src.c| is
% compiled in conjunction with the Stateflow generated code into a single
% S-function MEX file.

%%
% Because the custom definitions appear at the top of the generated machine
% header file |sf_custom_code_constants_vars_fcns_sfun.h|, you can access
% them in all charts that belong to this model.
