Main Content

Generate Code for an LED Control Function That Uses Enumerated Types

This example shows how to generate code for a function that uses enumerated types. In this example, the enumerated types inherit from base type int32. The base type can be int8, uint8, int16, uint16, int32, or uint32.

Define the enumerated type sysMode. Store it in sysMode.m on the MATLAB® path.

type sysMode.m
classdef sysMode < int32
    enumeration
        OFF(0),
        ON(1)
    end
end

Define the enumerated type LEDcolor. Store it in LEDcolor.m on the MATLAB path.

type LEDcolor.m
classdef LEDcolor < int32
    enumeration
        GREEN(1),
        RED(2)
    end
end

Define the function displayState, which uses enumerated data to activate an LED display, based on the state of a device. displayState lights a green LED display to indicate the ON state. It lights a red LED display to indicate the OFF state.

type displayState.m
function led = displayState(state)
%#codegen

if state == sysMode.ON
    led = LEDcolor.GREEN;
else
    led = LEDcolor.RED;
end

Generate a MEX function for displayState. Specify that displayState takes one input argument that has an enumerated data type sysMode.

codegen displayState -args {sysMode.ON}
Code generation successful.

Test the MEX function.

displayState_mex(sysMode.OFF)
ans = 
  LEDcolor enumeration

    RED

Generate a static library for the function displayState. Specify that displayState takes one input argument that has an enumerated data type sysMode.

codegen -config:lib displayState -args {sysMode.ON}
Code generation successful.

codegen generates a C static library with the default name, displayState. It generates supporting files in the default folder, codegen/lib/displayState.

View the header file displayState_types.h.

type codegen/lib/displayState/displayState_types.h
/*
 * Prerelease License - for engineering feedback and testing purposes
 * only. Not for sale.
 * File: displayState_types.h
 *
 * MATLAB Coder version            : 24.2
 * C/C++ source code generated on  : 20-Jul-2024 12:21:26
 */

#ifndef DISPLAYSTATE_TYPES_H
#define DISPLAYSTATE_TYPES_H

/* Include Files */
#include "rtwtypes.h"

/* Type Definitions */
#ifndef enum_sysMode
#define enum_sysMode
enum sysMode
{
  OFF = 0, /* Default value */
  ON
};
#endif /* enum_sysMode */
#ifndef typedef_sysMode
#define typedef_sysMode
typedef enum sysMode sysMode;
#endif /* typedef_sysMode */

#ifndef enum_LEDcolor
#define enum_LEDcolor
enum LEDcolor
{
  GREEN = 1, /* Default value */
  RED
};
#endif /* enum_LEDcolor */
#ifndef typedef_LEDcolor
#define typedef_LEDcolor
typedef enum LEDcolor LEDcolor;
#endif /* typedef_LEDcolor */

#endif
/*
 * File trailer for displayState_types.h
 *
 * [EOF]
 */

The enumerated type LEDcolor is represented as a C enumerated type because the base type in the class definition for LEDcolor is int32. When the base type is int8, uint8, int16, or uint16, the code generator produces a typedef for the enumerated type. It produces #define statements for the enumerated type values. For example:

typedef short LEDcolor;
#define GREEN ((LEDcolor)1)
#define RED ((LEDcolor)2)

Related Topics