%% Configure Default Data and Function Code Generation Programmatically
% This example shows how to configure default data and function code generation for example model rtwdemo_configdefaults.The example uses the default mapping programming interface to specify code generation requirements for model data and functions. Use that interface to automate the configuration, or if you prefer to configure models programmatically. For information about configuring the default data and function code generation by using the Code Mappings editor, see Configure Default Code Generation for Categories of Model Data and Functions.
%
%% Open the Model
% The model rtwdemo_configdefaults uses multiple execution rates and is configured for single-instance usage.
%

open_system('rtwdemo_configdefaults')

%
%% Code Generation Requirements
% For this example, these are the code generation requirements:
% Import project type definition for data of type double, DBL_FLOAT, from header file exDblFloat.h.
% Import signal ex_input1 for computing a value stored in memory and used in an if-else condition in the Switch block. Import the signal data from header file exInDataMem.h.
% Import signals ex_input2, ex_input3, and ex_input4 for lookup tables Table1 and Table2. Import the signal data from header file exInDataLut.h.
% Data imported into the model from header files exInDataMem.h and exInDataLut.h is of type DBL_FLOAT, a project standard.
% Parameters UPPER, LOWER, K1, and K2 are parameter objects stored in the model workspace. Represent the parameters as global variables in the generated code.
% Data element X represents the delay for the Unit Delay block.
% Store data that is internal to the model in memory section internalDataMem.
% Store generated initialize and terminate functions in memory section functionSlowMem and execution functions in memory section functionFastMem.
% Use the identifier naming rule exSlow_$N to name initialize and terminate entry-point functions. Use the naming rule exFast_$N to name execution functions for .5 second and 1.5 second periodic functions. Set the name of the 1 second periodic function to exFast_1sec.
% Export output data declarations to header file exSysOut.h and definitions to exSysOut.c.
% For this example, someone, such as a system architect, has previously created these code definitions in an Embedded Coder Dictionary that is part of Simulink data dictionary exCodeDefs.sldd:
% Memory sections internalDataMem, functionFastMem, and functionSlowMem.
% Function customization templates exFastFunction and exSlowFunction.
%
%% Get Data and Function Code Mappings for Model
% Get the code mappings for example model rtwdemo_configdefaults by specifying the name of the model in a call to function coder.mapping.api.get. The function returns an object that represents the code mappings for the model.  You specify that object as the first argument in subsequent calls to other functions in the API.
% If code mappings do not exist for a model, create a code mappings object by calling coder.mapping.utils.create.

cm = coder.mapping.api.get('rtwdemo_configdefaults');

%
%% Set Relevant Category, Property, and Value Combinations
% Set relevant category, property, and value combinations with calls to setDataDefaults and setFunctionDefaults. The first two arguments that you specify for these functions are the code mappings object returned by coder.mapping.api.get and the name of a data or function category. In addition, you specify name-value pair arguments that specify the default configuration information that you want to set, such as the storage class and storage class properties.
%  In this example, you set the default:
% Storage class for Inport blocks, Outport blocks, and model parameters
% Memory section for internal data (for example, signald and block states)
% Header and definition files for Inport and Outport blocks
% Function customization template for initialize, terminate, and execution functions

% Configure data defaults
setDataDefault(cm,'Inports','StorageClass','ImportFromFile','HeaderFile','exInDataLut.h');
setDataDefault(cm,'Outports','StorageClass','ExportToFile','HeaderFile','exSysOut.h',...
    'DefinitionFile','exSysOut.c');
setDataDefault(cm,'ModelParameters','StorageClass','ExportedGlobal');
setDataDefault(cm,'InternalData','MemorySection','internalDataMem');

% Configure function defaults
setFunctionDefault(cm,'InitializeTerminate','FunctionCustomizationTemplate','exSlowFunction');
setFunctionDefault(cm,'Execution','FunctionCustomizationTemplate','exFastFunction');

%% Verify Default Mappings
% Verify default mappings with calls to getDataDefaults and getFunctionDefaults. The first two arguments that you specify for these functions are the code mappings object returned by coder.mapping.api.get and the name of a data or function category. In addition, you specify the name of the configuration information that you want the function to return, such as the name of the storage class or a storage class property.
% In this example, you verify these default settings:
% Storage class for Inport blocks, Outport blocks, and model parameters
% Memory section for internal data (for example, signal data and block states)
% Header and definition files for Inport and Outport blocks
% Function customization template for initialize, terminate, and execution functions

% Verify default data configurations
defscInports = getDataDefault(cm,'Inports','StorageClass')
defhfileInports = getDataDefault(cm,'Inports','HeaderFile')
defscOutport = getDataDefault(cm,'Outports','StorageClass')
defhfileOutport = getDataDefault(cm,'Outports','HeaderFile')
defdffileOutport = getDataDefault(cm,'Outports','DefinitionFile')
defscParams = getDataDefault(cm,'ModelParameters','StorageClass')
defmemInternal = getDataDefault(cm,'InternalData','MemorySection')

% Verify default function configurations
deftempInitTerm = getFunctionDefault(cm,'InitializeTerminate','FunctionCustomizationTemplate')
deftempExecution = getFunctionDefault(cm,'Execution','FunctionCustomizationTemplate')

%% Configure Individual Data Elements to Use Default Configuration Settings
% The storage class for each model data element is set to Auto, which means that the code generator might eliminate or change the representation of relevant code for optimization purposes. If optimizations are not possible, the code generator applies the model default configuration.
% To avoid optimizations and force the code generator to use the default configuration, set the storage class to Model default.
% To override the default configuration, specify the storage class that meets the code generation requirements for that inport.
% For this example, you configure the code generator to apply the default storage class setting to the Inport blocks, the Output block, model parameters, and state X for the Unit Delay block. Use the find function to get the names of the data elements in the model of the different categories. Then, use the values returned by find in calls to setInport, setOutport, setModelParameter, and setState to set the storage class to Model default.  Use calls to getInport, getOutport, getModelParameter, and getState to verify, the storage class settings.
% In each of the function calls, you specify the code mappings object returned by coder.mapping.api.get. In addition:
% In the call to the find function, you specify a value indicating code mapping information that you want the function to return.
% The calls to setInport, setOutport, setModelParameter, and setState identify data elements  that you want to configure and a name-value pair argument that specifies the configuration information that you want to set, such as the storage class or a storage class property.
% The calls to getInport, getOutport, getModelParameter, and getState identify a data element for which you want to return configuration information and the type of information that you want the function to return.

input = find(cm,'Inports');
setInport(cm,input,'StorageClass','Model default');

output = find(cm,'Outports');
setOutport(cm,output,'StorageClass','Model default');

params = find(cm,'ModelParameters');
setModelParameter(cm,params,'StorageClass','Model default');

states = find(cm,'States');
setState(cm,states,'StorageClass','Model default');

scIn1 = getInport(cm,'In1','StorageClass')
scIn2 = getInport(cm,'In2','StorageClass')
scIn3 = getInport(cm,'In3','StorageClass')
scIn4 = getInport(cm,'In4','StorageClass')

scK1 = getModelParameter(cm,'K1','StorageClass')
scTable2 = getModelParameter(cm,'Table2','StorageClass')
scK2 = getModelParameter(cm,'K2','StorageClass')

scX = getState(cm,'rtwdemo_configdefaults/Delay','StorageClass')

%% Override Default Header File Setting for Inport Block In1
% Previously, you set the default header file for inports to exInDataLut.h. The requirements specify that you import data for Inport block In1 from header file exInDataMem.h.
% For Inport block In1, override the default storage class to ImportFromFile and set the header file to exInDataMem.h with a call to setInport. The function call specifies the  code mappings object returned by coder.mapping.api.get, the name of the Inport block to configure, and name-value pair arguments specifying that the function set StorageClass to ImportFromFile and HeaderFile to exInDataMem.h.

setInport(cm,'In1','StorageClass','ImportFromFile','HeaderFile','exInDataMem.h');

%
%% Verify Header File Setting
% Verify the updated header file setting for Inport block In1 with a call to getInport.The function call specifies the  code mappings object returned by coder.mapping.api.get, the name of the Inport block of interest, and the configuration information to return.

hfileIn1 = getInport(cm,'In1','StorageClass')

%% Override Default Function Name Setting for Step Function
% Previously, you set the default function customization template for exeuction functions to functionFastMem, which applies the naming rule exFast_$N to the three step functions generated for the model. The requirements specify that the name for function Periodic:D2, which has a sample time of 1 seccond, be  named exFast_1sec.
% Set the function name with a call to setFunction.The function call specifies the code mappings object returned by coder.mapping.api.get, the source of the function, Periodic:D2, and the name-value pair argument specifying that the function set the function name to exFast_1sec.

setFunction(cm,'Periodic:D2','FunctionName','exFast_1sec')

%% Verify Function Name Setting
% Verify the updated function name setting for execution function Periodic:D2 with a call to getFunction.The function call specifies the  code mappings object returned by coder.mapping.api.get, the function of interest, and property FunctionName.

functionName = getFunction(cm,'Periodic:D2','FunctionName')

%% Related Links
% Configure Inport Blocks for C Code Generation
% Configure Outport Blocks for C Code Generation
% Configure Signal Data for C Code Generation
% Configure Parameters for C Code Generation
% Configure Block States for C Code Generation
% Configure Data Stores for C Code Generation
% Customize Generated C Function Interfaces
% Code Mapping Editor - C
% Embedded Coder Dictionary
% Copyright 2020 The MathWorks, Inc.