Simulate a Media Player
This example shows how to create an interface between a Stateflow® chart that uses C as the action language and a MATLAB® app created in App Designer. For more information on connecting a Stateflow chart that uses MATLAB as the action language to a MATLAB app, seeModel a Power Window Controller.
In this example, a MATLAB app models the front end of a media player. During simulation, you can choose between the AM radio, FM radio, and CD player components of the media player. When the CD player is on, you can also select the playback mode.
The Stateflow chartApp Interface
provides a bidirectional connection between the MATLAB app and the control and plant systems in the Simulink® model. When you interact with the widgets in the app, the chart sends a corresponding command to the other charts in the model. These charts use strings to control the behavior of the media player and to provide natural language output messages that indicate its status. When the status of the media player changes, the chart changes the color of the buttons and updates the text field at the bottom of the app.
Run the Media Player Model
Open the Simulink model and clickRun. The Media Player Helper app opens. The text field at the bottom of the app shows the status of the media player,
Standby (OFF)
.In theRadio Requestsection, clickCD. The media player status changes to
CD Player: Empty
.ClickInsert Disc. The media player status briefly says
Reading: Handel's Greatest Hits
before changing toCD播放器:停止
.In theCD Requestsection, clickPLAY. The media player status changes to
Playing: Handel's Greatest Hits
and music begins to play.In theCD Requestsection, clickFF. The music stops and chirping sounds begin. The media status changes to
Forward >> Handel's Greatest Hits
. The album name in this message scrolls forward across the display.Use the Media Player Helper app to select other operating modes or to enter a different album name. For example, try playing to the albums
Training Deep Networks
或Fun With State Machines
. To stop the simulation, close the Media Player Helper app.
Connect Chart to MATLAB App
The chartApp Interface
is already configured to communicate with the MATLAB appsf_mediaplayer_app
. To create a bidirectional connection between your MATLAB app and a Stateflow chart that uses C as the action language, follow these steps. In the MATLAB app:
Create a custom property to interface with the chart during simulation. The app uses this property to access chart inputs, chart outputs, and local data. For more information, seeShare Data Within App Designer Apps.
Modify the
startupFcn
callback for the app by adding a new input argument and storing its value as the property that you created in the previous step. For more information, seeWrite Callbacks in App Designer.
In the Stateflow chart:
Create a local data object to interface with the app. The chart uses this local data object as an argument when it calls helper functions in the app.
设置本地数据对象创建的类型in the previous step to
ml
. For more information, seeSpecify Type of Stateflow Data.Run the app using the
ml
namespace operator to indicate that the app is extrinsic MATLAB code. Pass the keywordthis
as an argument to give the app access to the chart during simulation. Store the value returned by the function call to the app as the local data object that you created to interface with the app. For more information, seeAccess MATLAB Functions and Workspace Data in C Charts.
In this example, the Media Player Helper app uses a property calledchart
to interface with the chartApp Interface
. The app callbacks use this property to write to the chart outputs:
When you insert or eject a disc, the
EjectButtonPushed
callback sets the values ofinsert
,eject
, andAlbum
.When you click a button in theRadio Requestsection of the app, the corresponding callbacks set the value of
RadioReq
.When you click a button in theCD Requestsection of the app, the corresponding callbacks set the value of
CDReq
.When you close the app, the
UIFigureCloseRequest
callback sets the value ofStop
totrue
.
Conversely, in the chart, the entry actions in theInterfaceWithApp
state run the appsf_mediaplayer_app
and store the returned value as the local data objectapp
. The chart uses this local data object when it calls the helper functionsupdateButtons
andupdateStatus
. In the app, these helper functions change the color of the buttons and update the text field at the bottom of the app based on the value of the chart inputsRadioMode
,CDMode
, andCDStatus
.
Manage Media Player Modes
TheMode Manager
chart activates the appropriate subcomponent of the media player (AM radio, FM radio, or CD player) depending on the inputs received from theApp Interface
chart. The chart inputsRadioReq
andCDReq
包含字符串数据,控制t的行为he chart. To evaluate the string data, the chart uses the string operatorstrcmp
and its equivalent shorthand form==
. The chart outputCurrentRadioMode
provides natural language output to the app, whileMechCmd
controls the behavior of the CD player subcomponent. To assign values to these outputs, the chart uses the string operatorstrcpy
and its equivalent shorthand form=
.
At the start of simulation, theNormalOperation
state becomes active. If the Boolean dataDiscEject
istrue
, a transition to theEject
state occurs, followed by a transition back to theNormalOperation
state.
WhenNormalOperation
is active, the previously active substate (Standby
或ON
) recorded by the history junction becomes active. Subsequent transitions between theStandby
andON
substates depend on the value of the expressionstrcmp(RadioReq,"OFF")
:
If
strcmp
returns a value of zero, thenRadioReq
is"OFF"
and theStandby
substate is activated.If
strcmp
returns a nonzero value, thenRadioReq
is not "|OFF|" and theON
substate is activated.
In theON
substate, three substates represent the operating modes of the media player: CD player, AM radio, and FM radio. Each substate corresponds to a different value of the inputRadioReq
. The inner transition inside theON
state uses the operatorhasChanged
to continually scan for changes in the value ofRadioReq
.
If the value of
RadioReq
is"CD"
, then the substateCDMode
becomes active and the media player switches to CD player mode. TheMode Manager
chart outputs"PLAY"
,"REW"
,"FF"
, and"STOP"
commands to theCD Player
chart through the string dataMechCmd
.If the value of
RadioReq
is"AM"
, then the substateAMMode
becomes active and the media player switches to AM radio mode. TheMode Manager
chart outputs a"STOP"
command to theCD Player
chart through the string dataMechCmd
.If the value of
RadioReq
is"FM"
, then the substateFMMode
becomes active and the media player switches to FM radio mode. TheMode Manager
chart outputs a"STOP"
command to theCD Player
chart through the string dataMechCmd
.
Manage CD Player Modes
TheCD Player
chart activates the appropriate operating mode for the CD player depending on the input received from theApp Interface
andMode Manager
charts. The chart inputsCmd
andAlbum
包含字符串数据,控制t的行为he chart. The chart outputAlbumName
provides natural language output to the app. To assign and compare the values of string data, the chart uses the shorthand operations=
(seestrcpy) and==
(seestrcmp)。生产或者文本tput stringCDStatus
, the chart uses the string operatorsstrcat
,strlen
, andsubstr
.
At the start of simulation, theEmpty
state is activated.
If the Boolean dataDiscInsert
istrue
, a transition to theInserting
state occurs. After a short time delay, a transition to theDiscPresent
state occurs. TheDiscPresent
state remains active until the dataCmd
becomes"EJECT"
. At that point, a transition to theEjecting
state occurs. After a short time delay, a transition to theEmpty
state occurs. The temporal logic operatorafter
controls the timing of the transitions during disc insertion and ejection.
When a state transition occurs, the entry action in the new state changes the value ofCDStatus
to reflect the status of the CD player. In theFF
或REW
substates, the during actions continually change the value ofCDStatus
to produce a scrolling motion effect.
When the active state is
Empty
, the value ofCDStatus
is"CD Player: Empty"
.When the active state is
Inserting
, the value ofCDStatus
is"Reading:AlbumName"
.When the active state is
Ejecting
, the value ofCDStatus
is"Ejecting:AlbumName"
.When the active state is
DiscPresent.STOP
, the value ofCDStatus
is"CD Player: Stopped"
.When the active state is
DiscPresent.PLAY
, the value ofCDStatus
is"Playing:AlbumName"
.When the active state is
DiscPresent.REW
, the value ofCDStatus
is"Reverse <<AlbumName"
, whereAlbumName
scrolls backward across the display.When the active state is
DiscPresent.FF
, the value ofCDStatus
is"Forward >>AlbumName"
, whereAlbumName
scrolls forward across the display.
See Also
after|hasChanged|strcat|strcmp|strcpy|strlen|substr