Use Simulink Strings inside C Function Block to Model Scrolling Display
This example shows how to use Simulink® strings inside the C Function block to model a scrolling display for a user-defined Simulink® string.
Open the model. In this model, DisplayController
subsystem receives a custom string and outputs a fixed-size Simulink® string with a scrolling effect.
mdl = "CFunctionStringExample.slx";
open_system(mdl)
The DisplayController
subsystem contains two C Function blocks, Scrolling Display
and Send to Hardware
.
The mask parameter Display Size indicates the size of the scrolling display. The C Function block named Scrolling Display
uses this mask parameter in conjunction with the input string length to achieve the desired string output. This model is configured to output 20 characters at each time step.
Scrolling Display
The C Function block named Scrolling Display
receives a dynamic Simulink® string named inputStr
at its input port and outputs a fixed-size Simulink® string named outputStr
. The output code, initial values of the parameters, and ports and parameter names are defined in the Output section, Start section, and Ports and Parameters table, respectively.
This block interprets a Simulink® string of length n
as a C character array of length of n+1
. The Buffer size of dynamically-sized string (bytes) parameter under Advanced parameters section of Simulation Target pane of Configuration Parameters dialog box determines number of character buffer generated for dynamic string signals. This allows the C Function block to use string library functions and zero-based indexing to output a fixed-size Simulink® string with a scrolling effect.
Send to Hardware
The C Function block named Send to Hardware
propagates the fixed-size string outputStr
to an external hardware display board. This block uses a hardware function call set_display
that uses outputStr
as an input argument to generate the code for the external hardware display board.
Simulate Model and Visualize Results
Simulate the model and visualize the display in the Display block named Simulation
Display
. To change the display size, double-click the DisplayController
subsystem and change the value of the Display Size parameter. To change the input string, double-click the String Constant block and change the string.
out = sim (mdl);