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, 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 Interfaceprovides 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 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).

  2. In theRadio Requestsection, clickCD. The media player status changes toCD Player: Empty.

  3. ClickInsert Disc. The media player status briefly saysReading: Handel's Greatest Hitsbefore changing toCD播放器:停止.

  4. In theCD Requestsection, clickPLAY. The media player status changes toPlaying: Handel's Greatest Hitsand music begins to play.

  5. In theCD Requestsection, clickFF. The music stops and chirping sounds begin. The media status changes toForward >> 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 albumsTraining Deep NetworksFun With State Machines. To stop the simulation, close the Media Player Helper app.

Connect Chart to MATLAB App

The chartApp Interfaceis 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:

  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, seeShare Data Within App Designer Apps.

  2. Modify thestartupFcncallback 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:

  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. 设置本地数据对象创建的类型in the previous step toml. For more information, seeSpecify Type of Stateflow Data.

  3. Run the app using themlnamespace operator to indicate that the app is extrinsic MATLAB code. Pass the keywordthisas 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 calledchartto interface with the chartApp Interface. The app callbacks use this property to write to the chart outputs:

  • When you insert or eject a disc, theEjectButtonPushedcallback sets the values ofinsert,eject, andAlbum.

  • When you click a button in theRadio Requestsection of the app, the corresponding callbacks set the value ofRadioReq.

  • When you click a button in theCD Requestsection of the app, the corresponding callbacks set the value ofCDReq.

  • When you close the app, theUIFigureCloseRequestcallback sets the value ofStoptotrue.

Conversely, in the chart, the entry actions in theInterfaceWithAppstate run the appsf_mediaplayer_appand store the returned value as the local data objectapp. The chart uses this local data object when it calls the helper functionsupdateButtonsandupdateStatus. 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 Managerchart activates the appropriate subcomponent of the media player (AM radio, FM radio, or CD player) depending on the inputs received from theApp Interfacechart. The chart inputsRadioReqandCDReq包含字符串数据,控制t的行为he chart. To evaluate the string data, the chart uses the string operatorstrcmpand its equivalent shorthand form==. The chart outputCurrentRadioModeprovides natural language output to the app, whileMechCmdcontrols the behavior of the CD player subcomponent. To assign values to these outputs, the chart uses the string operatorstrcpyand its equivalent shorthand form=.

At the start of simulation, theNormalOperationstate becomes active. If the Boolean dataDiscEjectistrue, a transition to theEjectstate occurs, followed by a transition back to theNormalOperationstate.

WhenNormalOperationis active, the previously active substate (StandbyON) recorded by the history junction becomes active. Subsequent transitions between theStandbyandONsubstates depend on the value of the expressionstrcmp(RadioReq,"OFF"):

  • Ifstrcmpreturns a value of zero, thenRadioReqis"OFF"and theStandbysubstate is activated.

  • Ifstrcmpreturns a nonzero value, thenRadioReqis not "|OFF|" and theONsubstate is activated.

In theONsubstate, 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 theONstate uses the operatorhasChangedto continually scan for changes in the value ofRadioReq.

  • If the value ofRadioReqis"CD", then the substateCDModebecomes active and the media player switches to CD player mode. TheMode Managerchart outputs"PLAY","REW","FF", and"STOP"commands to theCD Playerchart through the string dataMechCmd.

  • If the value ofRadioReqis"AM", then the substateAMModebecomes active and the media player switches to AM radio mode. TheMode Managerchart outputs a"STOP"command to theCD Playerchart through the string dataMechCmd.

  • If the value ofRadioReqis"FM", then the substateFMModebecomes active and the media player switches to FM radio mode. TheMode Managerchart outputs a"STOP"command to theCD Playerchart through the string dataMechCmd.

Manage CD Player Modes

TheCD Playerchart activates the appropriate operating mode for the CD player depending on the input received from theApp InterfaceandMode Managercharts. The chart inputsCmdandAlbum包含字符串数据,控制t的行为he chart. The chart outputAlbumNameprovides 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, theEmptystate is activated.

If the Boolean dataDiscInsertistrue, a transition to theInsertingstate occurs. After a short time delay, a transition to theDiscPresentstate occurs. TheDiscPresentstate remains active until the dataCmdbecomes"EJECT". At that point, a transition to theEjectingstate occurs. After a short time delay, a transition to theEmptystate occurs. The temporal logic operatoraftercontrols 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 ofCDStatusto reflect the status of the CD player. In theFFREWsubstates, the during actions continually change the value ofCDStatusto produce a scrolling motion effect.

  • When the active state isEmpty, the value ofCDStatusis"CD Player: Empty".

  • When the active state isInserting, the value ofCDStatusis"Reading:AlbumName".

  • When the active state isEjecting, the value ofCDStatusis"Ejecting:AlbumName".

  • When the active state isDiscPresent.STOP, the value ofCDStatusis"CD Player: Stopped".

  • When the active state isDiscPresent.PLAY, the value ofCDStatusis"Playing:AlbumName".

  • When the active state isDiscPresent.REW, the value ofCDStatusis"Reverse <<AlbumName", whereAlbumNamescrolls backward across the display.

  • When the active state isDiscPresent.FF, the value ofCDStatusis"Forward >>AlbumName", whereAlbumNamescrolls forward across the display.

See Also

||||||

Related Topics