billvaglienti / ProtoGen

Communications protocol generation software
MIT License
30 stars 16 forks source link

Name lookup for enumerated values #37

Closed SchrodingersGat closed 7 years ago

SchrodingersGat commented 7 years ago

This PR adds the ability to (optionally) generate code to lookup the label for an enumerated value based on the integer value.

The lookup tag should be added to an enum to enable this functionality.

Example:

<Enum name="CorvidPackets" prefix="STARTER_PKT_" lookup="true">
        <!-- Starter Status Packets -->
        <Value name="STATUS_A" value="0x50" comment="Status information"/>
        <Value name="STATUS_B" comment="Reserved for future use"/>

        <!-- DRV8308 Register Values -->
        <Value name="WRITE_REG" value="0x60" comment="Write data to a DRV8308 register"/>
        <Value name="READ_REG" comment="Request DRV8308 register data"/>
        <Value name="REG_DATA" comment="DRV8308 register data"/>

        <!-- Other Settings Packets -->
        <Value name="MOTOR_SETTINGS" value="0x80" comment="Motor configuration" required="true"/>

        <!-- Starter Config Packets -->
        <Value name="ADDRESS" value="0x90" comment="Starter address information" required="true"/>
        <Value name="FIRMWARE" comment="Starter firmware information" required="true"/>
        <Value name="SYSTEM_INFO" comment="Starter system info" required="true"/>
        <Value name="TELEMETRY_CFG" comment="Starter telemetry configuration" required="true"/>
        <Value name="EEPROM_INFO" comment="Non-volatile settings info" required="true"/>
    </Enum>

produces:

/*!
 * \brief Lookup label for 'CorvidPackets' enum entry
 * 
 * \param value is the integer value of the enum entry
 * \return string label of the given entry (emptry string if not found)
 */
char* CorvidPackets_Label(int value)
{
    switch (value)
    {
    default:
        return "";
    case STARTER_PKT_STATUS_A:
        return "STARTER_PKT_STATUS_A";
    case STARTER_PKT_STATUS_B:
        return "STARTER_PKT_STATUS_B";
    case STARTER_PKT_WRITE_REG:
        return "STARTER_PKT_WRITE_REG";
    case STARTER_PKT_READ_REG:
        return "STARTER_PKT_READ_REG";
    case STARTER_PKT_REG_DATA:
        return "STARTER_PKT_REG_DATA";
    case STARTER_PKT_MOTOR_SETTINGS:
        return "STARTER_PKT_MOTOR_SETTINGS";
    case STARTER_PKT_ADDRESS:
        return "STARTER_PKT_ADDRESS";
    case STARTER_PKT_FIRMWARE:
        return "STARTER_PKT_FIRMWARE";
    case STARTER_PKT_SYSTEM_INFO:
        return "STARTER_PKT_SYSTEM_INFO";
    case STARTER_PKT_TELEMETRY_CFG:
        return "STARTER_PKT_TELEMETRY_CFG";
    case STARTER_PKT_EEPROM_INFO:
        return "STARTER_PKT_EEPROM_INFO";
    }
}