cenumeration

Managing enumeration members in header files


In an application I am developing, there is a header file responsible for the entire functions of an ADC, let's call it adc.h.

Where there is an enumeration indicating the input PINs used in the ADC.

typedef enum
{
    a_in_1         = adc1 | 0x00,   //TEMP_SENSOR_1
    a_in_2         = adc1 | 0x01,   //TEMP_SENSOR_2
    a_in_3         = adc1 | 0x07,   //TEMP_SENSOR_3
    a_in_4         = adc1 | 0x08,   //TEMP_SENSOR_4
}adc_Input_t;

Another header file is responsible for declaring the structures that will be used in the software, it is called struct.h.

In there, I want to define a structure responsible for holding the information of a sensor.

typedef struct
{
    uint16_t adc_value;             // raw value
    uint16_t counter_breakline;     // counter for line fault
    uint16_t counter_shortcircuit;  // counter for short circuit
    uint8_t  error_adc;             // flag to indicate an error in ADC
    uint8_t  input;                 // ADC input pin
    float    temperature;           // current temperature
    
}sensor_temp_t;

My idea for the member input is to hold the ADC pin, described in the first structure. I could define this member as:

adc_Input_t input;

But I do not want to include the header adc.h, because the struct.h is included in several other files, and the ADC header is not, and I do not want to make it visible to the other files, what would happen if I include the adc.h in the struct.h.

My doubt is, is it right to define the input member as uint8_t instead of adc_Input_t ? Could it lead to some bug if I defined this member as I did?

During my tests, it seems to be working fine, however, I am unsure if it is the right way to deal in this case.


Solution

  • An enumeration is considered an integer type.

    So assuming the range of valid values for the enumeration fit in a uint8_t, this should work as expected.