#DEFINE placement

Lancsrick
Posts: 30
Joined: Mon Apr 10, 2023 5:48 pm

#DEFINE placement

Postby Lancsrick » Mon Apr 08, 2024 9:15 pm

Thought I'd put in here as it's not so much an ESP IDF question as a general C question. For the sake of making my code logical/easy to read I'd like to put some #DEFINE statements in with the gpio_config statements I have in a void pin_setup() function. Is it legitimate to have a #DEFINE at this level or must it be outside of any functions to avoid future issues?

Thanks in advance.

Code: Select all

void pin_setup()
{
/////////////////////////////////////////////////////////////////////

//Pin definition on ESP DOIT Devkit V1 and pin config

//D1 - Red lane Dog 1 Fault light
  gpio_config_t io_conf1;
    io_conf1.intr_type = GPIO_INTR_DISABLE;//disable interrupt
    io_conf1.mode = GPIO_MODE_OUTPUT;//set as output mode
    io_conf1.pin_bit_mask = (1ULL<<1);//bit mask of the pins that you want to set,e.g. GPIO 1
    io_conf1.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
    io_conf1.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
    esp_err_t error=gpio_config(&io_conf1);//configure GPIO with the given settings
    if(error!=ESP_OK){
        printf("error configuring GPIO1 \n");
    }
   gpio_set_direction(GPIO_NUM_1, GPIO_MODE_OUTPUT);
   #define RED_FAULT_1 GPIO_NUM_1
   //more code beyond this but cut it for simplicity of post
   }
   

ESP_Sprite
Posts: 9709
Joined: Thu Nov 26, 2015 4:08 am

Re: #DEFINE placement

Postby ESP_Sprite » Tue Apr 09, 2024 2:01 am

A define is a preprocessor thing, and at the stage that gets resolved the system isn't aware of anything deep like 'functions' yet; the preprocessor sees your C code as plain text where it needs to do find&replace operations defined by the #define statement. In other words: as long as you put the #define before anywhere you use it, it should be fine.

MicroController
Posts: 1696
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: #DEFINE placement

Postby MicroController » Tue Apr 09, 2024 9:50 am

Is it legitimate to have a #DEFINE at this level
It's not illegal ;-)
It is rather uncommon though, and for reasons.
Note that #defined values are only visible/available inside the very compilation unit in which they are defined, and only 'below' the point at which they were #defined.
There are several use cases for #defining macros, but if a macro is 1) defined in code and 2) only serves as a placeholder for some constant value and 3) is not used in preprocessing for e.g. conditional compilation, I recommend using C const's instead. Like the preprocessor #defines, you can declare/define them in header files as static const and get some benefits of them being C objects (type, scope/visibility,...). If you only need them inside a single source file, declare+define them in that source file (still static const). Generally, aim to group 'related' constants together at a single location, like "All pin number definitions are here:" so that you won't have to search for individual constants/#defines through the code base if you want to change a value.

Who is online

Users browsing this forum: Google [Bot] and 133 guests