Control Color of Lamp on Instrument Panel
This example shows how to control the color of a lamp indicator on an instrument panel that connects to a Simulink Real-Time application.
The example operations are:
Create uifigure, add lamps, and add labels
Open model and build real-time application
Connect lamps and add instrument
Observe color cycle of lamps
Remove instrument
Close model
Create uifigure and Add Components
f = uifigure; lamp1 = uilamp(f); lamp1.Position = [10 300 20 20]; tlabel1 = uilabel(f); tlabel1.Position = [40 298 100 22]; tlabel1.Text = 'Lamp 1'; lamp2 = uilamp(f); lamp2.Position = [10 200 20 20]; tlabel2 = uilabel(f); tlabel2.Position = [40 198 100 22]; tlabel2.Text = 'Lamp 2';
Build Real-Time Application
open_system('slrt_ex_lamp_instrument'); model = 'slrt_ex_lamp_instrument'; set_param('slrt_ex_lamp_instrument','FixedStep','.01'); evalc('slbuild(model)');
Run Real-Time Application
tg = slrealtime; load(tg,model); start(tg);
Wait for Application Start on Target Computer
pause(2);
Add Instruments to UI Figure
inst = slrealtime.Instrument; inst.connectScalar(lamp1, 'lamp1', 'Property', 'Color', 'Callback', @setLampColor); inst.connectScalar(lamp2, 'lamp2', 'Property', 'Color'); addInstrument(tg,inst);
Wait and Observe Lamp Color Changes
pause(10);
Stop Real-Time Application
stop(tg);
Remove Instruments
removeInstrument(tg,inst);
Close All Windows
bdclose('all');
Callback for changing Lamp Color
function color = setLampColor(~,d) switch uint8(d(end)) case 5 color = 'green'; case 4 color = 'yellow'; case 3 color = 'cyan'; case 2 color = 'magenta'; case 1 color = 'red'; otherwise color = 'white'; end end