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, see Model a Power Window Controller.
In this example, a MATLAB app models the front end of a media player. During simulation, you can choose between an FM radio and an internet stream player.
A Simulink® model defines the logic for the media player. The model contains these Stateflow charts:
App Interface
provides a bidirectional connection between the MATLAB app and the other charts in the Simulink® model.Mode Manager
determines whether the media player is on or off, whether the FM radio or stream player is active, and the whether the stream player is operating in normal, reverse, or fast-forward playback mode.Stream Player
simulates the internal behavior of the stream player component.
When you interact with the buttons and knobs in the app, App Interface
sends a corresponding command to the other charts in the model. These charts use enumerated data to control the behavior of the media player and string data to provide natural language output messages that indicate the status of the media player. When the state of the media player changes, App Interface
updates the status message and changes the appearance of the buttons in the app.
Run the Media Player Model
Open the Simulink model and click Run. The Media Player App opens. The media player is initially off. At the top of the app, the Media Player Status box shows the message
Standby (Off)
.Turn the Component Selection knob to Stream. The status message briefly displays
Connecting to Handel's Greatest Hits
. After a short pause, the status message changes toPlaying: Handel's Greatest Hits
and music begins to play.Click the Fast-Forward button. The music stops and chirping sounds begin. The status message changes to
Forward >> Handel's Greatest Hits
. The name of the stream scrolls forward across the display. To resume normal play mode, click the Play button.Click the Reverse button. Chirping sounds play and the status message changes to
Reverse >> Handel's Greatest Hits
. The name of the stream scrolls backward across the display. To resume normal play mode, click the Play button.In the Stream Name box, enter the name of a new stream and click Connect. For example, try connecting to the streams
Training Deep Networks
orFun With State Machines
.Turn the Component Selection knob to Radio. The status message displays
Playing: 99.5 FM
. To select another station, turn the FM Radio Station knob.To stop the simulation, close the Media Player App.
Connect Chart to MATLAB App
The chart App Interface
is already configured to communicate with the MATLAB app sfMediaPlayerApp
. 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, see Share Data Within App Designer Apps.
Modify the
startupFcn
callback for the app by adding a new input argument. Store the value of this input argument as the property that you created in the previous step. For more information, see 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.
Set the type of the local data object you created in the previous step to
ml
. For more information, see Specify Scope and 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, see Access MATLAB Functions and Workspace Data in C Charts.
In this example, the Media Player App uses a property called chart
to interface with the chart App Interface
. The app callbacks use this property to write to the chart outputs:
When you turn the Component Selection knob to select a component of the media player, the callback
ComponentKnobValueChanged
sets the value ofCompRequest
.When you click the Pause, Play, Reverse, or Fast-Forward buttons, the corresponding callbacks set the value of
StreamRequest
.When you click Connect, the callback
ConnectButtonPushed
sets the value ofStreamName
.When you close the app, the callback
UIFigureCloseRequest
sets the value ofStop
totrue
.
Conversely, in the chart, the entry actions in the InterfaceWithApp
state run the app sfMediaPlayerApp
and store the returned value as the local data object app
. The chart uses this local data object when it calls the helper functions updateStatus
and updateButtons
. In the app, these helper functions update the media player status message and enable, disable, or change the color of the playback buttons based on the value of the chart inputs Component
, StreamMode
, and StreamStatus
.
Manage Media Player Modes by Using Enumerated Data
The Mode Manager
chart determines the behavior of the media player based on the inputs received from the App Interface
chart. The inputs and outputs for this chart are enumerated data that describe the operating modes of the media player and playback modes of the stream player component.
The chart input
CompRequest
and the chart outputComponent
use the enumerated data typeMediaComponentMode
, which has values that correspond to the components of the media player:
classdef MediaComponentMode < Simulink.IntEnumType enumeration OFF(0), RADIO(1), STREAM(2) end end
The chart input
StreamRequest
and the chart outputStreamMode
use the enumerated data typeStreamingMode
, which has values that correspond to the operating modes of the stream player:
classdef StreamingMode < Simulink.IntEnumType enumeration PAUSE(0), PLAY(1), REW(2), FF(3) end end
By using enumerated data to group related values into separate data types, you can enhance the readability of your chart and reduce the number of constant data objects in your model. For more information, see Reference Values by Name by Using Enumerated Data.
The chart contains two top-level states, On
and Off
, that define the activity of the media player. At the start of simulation, the state Off
is active. The transitions between On
and Off
depend on whether or not the chart input CompRequest
equals the enumerated value OFF
.
In the On
state, the substates Stream
and Radio
represent the operating modes of the media player. The inner transition in this state uses the operator hasChanged
to continually scan for changes in the value of CompRequest
and activate one of these substates:
The substate
Stream
becomes active when you select the stream player and the value ofCompRequest
changes toSTREAM
.The substate
Radio
becomes active when you select the FM radio and the value ofCompRequest
changes toRADIO
.
In the Stream
state, the substates Play
and Pause
determine the activity of the stream player. The transitions between these substates depend on the value of the chart input StreamRequest
:
The substate
Play
becomes active when you play a stream andStreamRequest
changes to the enumerated valuePLAY
.The substate
Pause
becomes active when you pause the stream player andStreamRequest
changes to the enumerated valuePAUSE
.
Finally, in the Play
state, the substates Normal
, Reverse
, and FastForward
represent the play modes for the stream player. The inner transitions in this state monitor the value of StreamRequest
:
The substate
Normal
is active during normal play mode, when the value ofStreamRequest
equalsPLAY
.The substate
Reverse
becomes active when you click the Reverse button and the value ofStreamRequest
changes toREW
.The substate
FastForward
becomes active when you click the Fast-Forward button and the value ofStreamRequest
changes toFF
.
The Mode Manager
chart communicates with the App Interface
chart by setting the value of the output Component
to OFF
when the state Off
is active, or to STREAM
or RADIO
depending on which substate of On
is active. Similarly, the chart communicates with the Stream Player
chart by setting the value of the output StreamMode
to PAUSE
when the state Pause
is active, or to PLAY
, REW
, or FF
depending on which substate of Play
is active.
Compose Stream Player Status by Using String Data
The Stream Player
chart simulates the internal behavior of the stream player and provides natural language output to the app by using string data. The chart uses the string operators strcat
, strlen
, and substr
to concatenate strings and create the effect of scrolling text in the output string StreamStatus
. For more information, see Manage Textual Information by Using Strings.
The chart output StreamStatus
contains a status message that depends on the chart inputs StreamMode
and StreamName
:
When the value of
StreamMode
equalsPLAY
, the entry action in the statePLAY
sets the status message to the string"Playing:"
followed by the value ofStreamName
.When the value of
StreamMode
equalsREW
, the entry and during actions of the stateREW
set the status message to the string"Reverse <<"
followed by a substring ofStreamName
. The substring continually changes length to produce a backward scrolling effect.When the value of
StreamMode
equalsFF
, the entry and during actions of the stateFF
set the status message to the string"Forward >>"
followed by a substring ofStreamName
. The substring continually changes length to produce a forward scrolling effect.When the value of
StreamMode
equalsPAUSE
, the entry action in the statePAUSE
sets the status message to the string"Paused:"
followed by the value ofStreamName
.When the value of
StreamName
changes, the entry action in the stateConnecting
sets the status message to the string"Connecting to"
followed by the value ofStreamName
.
The Stream Player
chart outputs this status message to the App Interface
chart. When the stream player is on, the App Interface
chart calls the helper function updateStatus
, which displays the status message in the Media Player Status box at the top of the Media Player App.
See Also
this | hasChanged
| strcat
| strlen
| substr