Simulate a Media Player by Using Strings

This example models a media player by using string data in two Stateflow® C action language charts. The model plays a clip of music and other pre-recorded sounds. During simulation, the charts use strings to control the behavior of the media player and provide natural language output messages.

Run the Media Player Model

1. Simulate the model. The Media Player Helper appears.

In the model, the Display blocks show the default settings of the media player:

  • CurrentRadioMode: "Standby (OFF)"

  • MechCmd: "STOP"

  • CdStatus: "EMPTY"

  • AlbumName: "None"

2. In the Radio Request section, click CD.

  • The String Constant block RR changes from "OFF" to "CD".

  • The Display block for CurrentRadioMode changes from "Standby (OFF)" to "CD Player".

3. Click Insert Disc.

  • The Display block for AlbumName changes from "None" to "Handel's Greatest Hits".

  • The Display block for CdStatus changes from "EMPTY" to "Reading: Handel's Greatest Hits" to "Stopped".

4. In the CD Request section, click PLAY.

  • The String Constant block CR and the Display block for MechCmd change from "STOP" to "PLAY".

  • The Display block for CdStatus changes from "Stopped" to "Playing: Handel's Greatest Hits".

  • Music begins to play.

5. In the CD Request section, click FF.

  • Music stops and chirping sounds begin.

  • The String Constant block CR and the Display block for MechCmd change from "PLAY" to "FF".

  • The Display block for CdStatus changes from "Playing: Handel's Greatest Hits" to "Forward >> Handel's Greatest Hits". The album name in this message scrolls forward across the display.

To see other changes in the Display blocks, use the Media Player Helper to select other operating modes or to enter a different album name.

ModeManager Chart

This chart activates the appropriate subcomponent of the media player (AM radio, FM radio, or CD player) depending on the inputs received from the Media Player Helper.

Key Features

  • String data RadioReq, CdReq, and MechCmd control chart behavior.

  • String data CurrentRadioMode provides natural language output.

  • Operators strcpy and = assign values to string data.

  • Operators strcmp and == compare values of string data.

  • Operator hasChanged detects changes in the value of RadioReq.

Chart Behavior

At the start of simulation, the NormalOperation state becomes active. If the Boolean data DiscEject is 1 (or 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) as 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 string data RadioReq. The inner transition inside the ON state continually scans for changes in the value of RadioReq.

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

  • If the value of RadioReq is "AM", then the substate AMMode becomes active, setting the media player to AM radio mode. The ModeManager chart outputs a "STOP" command to the CdPlayer chart through the string data MechCmd.

  • If the value of RadioReq is "FM", then the substate FMMode becomes active, setting the media player to FM radio mode. The ModeManager chart outputs a "STOP" command to the CdPlayer chart through the string data MechCmd.

CdPlayer Chart

This chart activates the appropriate operating mode for the CD player depending on the input received from the ModeManager chart.

Key Features

  • String data Cmd controls chart behavior.

  • String data NewAlbum, AlbumName, and CdStatus provide natural language output.

  • Operators = and == assign and compare values of string data.

  • Operators strcat, strlen, and substr produce text in output string CdStatus.

  • Temporal logic operator after controls the timing of transitions during disc insertion and ejection.

  • Namespace operator ml accesses MATLAB® play and stop functions to manage music and sounds.

Chart Behavior

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

If the Boolean data DiscInsert is 1 (or 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.

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