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

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

  2. 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 to Playing: Handel's Greatest Hits and music begins to play.

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

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

  5. 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 or Fun With State Machines.

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

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

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

  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 Scope and 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 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 of CompRequest.

  • 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 of StreamName.

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

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 output Component use the enumerated data type MediaComponentMode, 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 output StreamMode use the enumerated data type StreamingMode, 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 of CompRequest changes to STREAM.

  • The substate Radio becomes active when you select the FM radio and the value of CompRequest changes to RADIO.

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 and StreamRequest changes to the enumerated value PLAY.

  • The substate Pause becomes active when you pause the stream player and StreamRequest changes to the enumerated value PAUSE.

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 of StreamRequest equals PLAY.

  • The substate Reverse becomes active when you click the Reverse button and the value of StreamRequest changes to REW.

  • The substate FastForward becomes active when you click the Fast-Forward button and the value of StreamRequest changes to FF.

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 equals PLAY, the entry action in the state PLAY sets the status message to the string "Playing:" followed by the value of StreamName.

  • When the value of StreamMode equals REW, the entry and during actions of the state REW set the status message to the string "Reverse <<" followed by a substring of StreamName. The substring continually changes length to produce a backward scrolling effect.

  • When the value of StreamMode equals FF, the entry and during actions of the state FF set the status message to the string "Forward >>" followed by a substring of StreamName. The substring continually changes length to produce a forward scrolling effect.

  • When the value of StreamMode equals PAUSE, the entry action in the state PAUSE sets the status message to the string "Paused:" followed by the value of StreamName.

  • When the value of StreamName changes, the entry action in the state Connecting sets the status message to the string "Connecting to" followed by the value of StreamName.

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

| | | |

Related Topics