Main Content

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 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 chart App 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

  1. Open the Simulink model and click Run. The Media Player Helper app opens. The text field at the bottom of the app shows the status of the media player, Standby (OFF).

  2. In the Radio Request section, click CD. The media player status changes to CD Player: Empty.

  3. Click Insert Disc. The media player status briefly says Reading: Handel's Greatest Hits before changing to CD Player: Stopped.

  4. In the CD Request section, click PLAY. The media player status changes to Playing: Handel's Greatest Hits and music begins to play.

  5. In the CD Request section, click FF. 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.

  6. 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 or Fun With State Machines. To stop the simulation, close the Media Player Helper app.

Connect Chart to MATLAB App

The chart App Interface is already configured to communicate with the MATLAB app sf_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:

  1. 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.

  2. 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, see Write Callbacks in App Designer.

In the Stateflow chart:

  1. 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.

  2. Set the type of the local data object you created in the previous step to ml. For more information, see Specify Type of Stateflow Data.

  3. Run the app using the ml namespace operator to indicate that the app is extrinsic MATLAB code. Pass the keyword this 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 Helper 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 insert or eject a disc, the EjectButtonPushed callback sets the values of insert, eject, and Album.

  • When you click a button in the Radio Request section of the app, the corresponding callbacks set the value of RadioReq.

  • When you click a button in the CD Request section of the app, the corresponding callbacks set the value of CDReq.

  • When you close the app, the UIFigureCloseRequest callback sets the value of Stop to true.

Conversely, in the chart, the entry actions in the InterfaceWithApp state run the app sf_mediaplayer_app and store the returned value as the local data object app. The chart uses this local data object when it calls the helper functions updateButtons and updateStatus. 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 inputs RadioMode, CDMode, and CDStatus.

Manage Media Player Modes

The Mode Manager chart activates the appropriate subcomponent of the media player (AM radio, FM radio, or CD player) depending on the inputs received from the App Interface chart. The chart inputs RadioReq and CDReq contain string data that control the behavior of the chart. To evaluate the string data, the chart uses the string operator strcmp and its equivalent shorthand form ==. The chart output CurrentRadioMode provides natural language output to the app, while MechCmd controls the behavior of the CD player subcomponent. To assign values to these outputs, the chart uses the string operator strcpy and its equivalent shorthand form =.

At the start of simulation, the NormalOperation state becomes active. If the Boolean data DiscEject is true, a transition to the Eject state occurs, followed by a transition back to the NormalOperation state.

When NormalOperation is active, the previously active substate (Standby or ON) recorded by the history junction becomes active. Subsequent transitions between the Standby and ON substates depend on the value of the expression strcmp(RadioReq,"OFF"):

  • If strcmp returns a value of zero, then RadioReq is "OFF" and the Standby substate is activated.

  • If strcmp returns a nonzero value, then RadioReq is not "|OFF|" and the ON substate is activated.

In the ON 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 input RadioReq. The inner transition inside the ON state uses the operator hasChanged to continually scan for changes in the value of RadioReq.

  • If the value of RadioReq is "CD", then the substate CDMode becomes active and the media player switches to CD player mode. The Mode Manager chart outputs "PLAY", "REW", "FF", and "STOP" commands to the CD Player chart through the string data MechCmd.

  • If the value of RadioReq is "AM", then the substate AMMode becomes active and the media player switches to AM radio mode. The Mode Manager chart outputs a "STOP" command to the CD Player chart through the string data MechCmd.

  • If the value of RadioReq is "FM", then the substate FMMode becomes active and the media player switches to FM radio mode. The Mode Manager chart outputs a "STOP" command to the CD Player chart through the string data MechCmd.

Manage CD Player Modes

The CD Player chart activates the appropriate operating mode for the CD player depending on the input received from the App Interface and Mode Manager charts. The chart inputs Cmd and Album contain string data that control the behavior of the chart. The chart output AlbumName provides natural language output to the app. To assign and compare the values of string data, the chart uses the shorthand operations = (see strcpy) and == (see strcmp). To produce text in the output string CDStatus, the chart uses the string operators strcat, strlen, and substr.

At the start of simulation, the Empty state is activated.

If the Boolean data DiscInsert is true, a transition to the Inserting state occurs. After a short time delay, a transition to the DiscPresent state occurs. The DiscPresent state remains active until the data Cmd becomes "EJECT". At that point, a transition to the Ejecting state occurs. After a short time delay, a transition to the Empty state occurs. The temporal logic operator after 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 of CDStatus to reflect the status of the CD player. In the FF or REW substates, the during actions continually change the value of CDStatus to produce a scrolling motion effect.

  • When the active state is Empty, the value of CDStatus is "CD Player: Empty".

  • When the active state is Inserting, the value of CDStatus is "Reading: AlbumName".

  • When the active state is Ejecting, the value of CDStatus is "Ejecting: AlbumName".

  • When the active state is DiscPresent.STOP, the value of CDStatus is "CD Player: Stopped".

  • When the active state is DiscPresent.PLAY, the value of CDStatus is "Playing: AlbumName".

  • When the active state is DiscPresent.REW, the value of CDStatus is "Reverse << AlbumName", where AlbumName scrolls backward across the display.

  • When the active state is DiscPresent.FF, the value of CDStatus is "Forward >> AlbumName", where AlbumName scrolls forward across the display.

See Also

| | | | | |

Related Topics