%% Optimize Generated Code By Packing Boolean Data Into Bitfields
% This example shows how to optimize the generated code by packing Boolean 
% data into bitfields. 
% When you select the model configuration parameter *Pack 
% Boolean data into bitfields*, Embedded Coder(R) packs the Boolean signals 
% into 1-bit bitfields, reducing RAM consumption. By  default,
% the optimization is enabled. This optimization reduces the RAM consumption. 
% Be aware that this optimization can potentially increase code size and 
% execution speed.

% Copyright 2015 The MathWorks, Inc.


%% Example Model
% Consider the model <matlab:rtwdemo_pack_boolean rtwdemo_pack_boolean>.
model = 'rtwdemo_pack_boolean';
open_system(model);

%% Disable Optimization
% # Open the Configuration Parameters dialog box.  
% # On the *Optimization > Signals and Parameters* pane, clear *Pack Boolean 
% data into bitfields*. 
%
% Alternatively, you can use the command-line API to disable the optimization:
set_param(model,'BooleansAsBitfields','off');
%%
% Create a temporary folder (in your system temporary folder) for the
% build and inspection process.
currentDir = pwd;
[~,cgDir] = rtwdemodir();

%% Generate Code Without Optimization
% Build the model using Embedded Coder(R).
rtwbuild(model)

%%
% View the generated code without the optimization. These lines of code are 
% in |rtwdemo_pack_boolean.h|.
hfile = fullfile(cgDir,'rtwdemo_pack_boolean_ert_rtw','rtwdemo_pack_boolean.h');
rtwdemodbtype(hfile,'/* Block signals and states','/* External inputs',1,0);

%% Enable Optimization
% # Open the Configuration Parameters dialog box.  
% # On the *Optimization > Signals and Parameters* pane, select *Pack Boolean 
% data into bitfields*. 
%
% Alternatively, you can use the command-line API to enable the optimization:
set_param(model,'BooleansAsBitfields','on');

%% Generate Code with Optimization
% Build the model using Embedded Coder(R).
rtwbuild(model)

%%
% View the generated code with the optimization. These lines of code are 
% in |rtwdemo_pack_boolean.h|.
hfile = fullfile(cgDir,'rtwdemo_pack_boolean_ert_rtw','rtwdemo_pack_boolean.h');
rtwdemodbtype(hfile,'/* Block signals and states','/* External inputs',1,0);
%%
% Selecting *Pack Boolean data into bitfields* enables model configuration 
% parameter *Bitfield declarator type specifier*. To optimize your code 
% further, select |uchar_t|. However, the optimization benefit of the *Bitfield 
% declarator type specifier* setting depends on your choice of target.
%%
% Close the model and code generation report.
bdclose(model)
rtwdemoclean;
cd(currentDir)
