0

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.

3
  • It is not clear why you have this scattered across multiple h files to begin with. struct.h is obviously a meaningless nonsense name and no header should be named as such. So why is it not merged into adc.h? Is the struct thing meant to hold an hardware independent HAL? Doesn't look like one - rather it looks like part of the driver. Your driver should only have one single .h file which is the driver's public API. From there it may have any number of .c files and those may include other .h files. Commented Apr 17, 2024 at 12:36
  • @Lundin the adc.h is a custom header from another partner, it is responsible for holding all the stuff responsible for the ADC hardware setup and it is not supposed to be visible to other files and I am not allowed to make modifications in this file. the struct.h hold information in general that will be used in other files, error management, sensor data reading, etc. Commented Apr 17, 2024 at 12:48
  • 1
    So it is kind of a HAL then. In that case it should not contain anything from adc.h. You need to keep that internal to your .c file. Maybe consider opaque types with struct forward declaration, not to expose any internals of the struct to the caller. How to do private encapsulation in C? Commented Apr 17, 2024 at 13:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

C23 also introduces an option to name the size of the enum, so that we can create 8 bit enums for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.