%% Modeling a CD Player/Radio Using Enumerated Data Types
% 
% This example shows a simple model of a CD Player/Radio logic that uses 
% enumerated data types in Simulink(R) and Stateflow(R) (new in R2008b).
%   Copyright 2007-2009 The MathWorks, Inc.

mdl = 'sf_cdplayer';
open_system(mdl);

rt = sfroot;

machine = rt.find('-isa','Stateflow.Machine','Name','sf_cdplayer');
userRequestChart = machine.find('-isa','Stateflow.Chart','Name','UserRequest');

cdPlayerModeManagerChart = machine.find('-isa','Stateflow.Chart','Name','CdPlayerModeManager');

cdPlayerBehaviorModelChart = machine.find('-isa','Stateflow.Chart','Name','CdPlayerBehaviorModel');

%% 
%
% The heart of the logic for controlling the CD Player/Radio is in the 
% "CdPlayerModeManager" chart. This chart receives user inputs, such as 
% whether a disk has been inserted and the choice for the radio mode (FM,
% AM, or CD).  Then the chart determines the mechanical command to output.
% For example, this chart is responsible for making sure that a 
% Rewind command is not issued in the absence of a disk in the player.
% Notice that the data types of input data "RadioReqMode" and "CdReqMode"
% as well as the output data "CurrentRadioMode" and "MechCmd"
% are defined as enumerated data types.  These types are defined in the 
% MATLAB(R) files |CdRequestMode.m| and |RadioRequestMode.m|. Notice that the 
% enumeration strings such as "CD" or "FM" are accessed directly in the 
% chart for comparisons and assignments.

cdPlayerModeManagerChart.visible = 1;


%% 
%
% The output command from the "CdPlayerModeManager" is processed
% by the chart "CdPlayerBehaviorModel" which models the behavior 
% of the CD Player mechanism. 

cdPlayerModeManagerChart.visible = 0;
cdPlayerBehaviorModelChart.visible = 1;


%% 
%
% The inputs to "CdPlayerModeManager" are provided by 
% the chart "UserRequestChart" which is woken up at a 
% periodic rate by Simulink and calls an external
% MATLAB(R) file named |sfcdplayerhelper.m|.
% This MATLAB(R) file acts as an intermediary
% between the Handle Graphics(R) panel and Simulink.

cdPlayerBehaviorModelChart.visible = 0;
userRequestChart.visible = 1;
sfcdplayergui;




