C Macros - avoiding macro expansion

Viewed 550

I have the following macros

#define REG_PWR_CTRL 0x2D  
#define REG_FIFO_CTL 0x38

#define VERBOSE(...) \
    if(verbose) \
            printf(__VA_ARGS__);

#define READ_REGISTER(i2c_dev_file, REGISTER, variable) \
{ \
    variable = i2c_smbus_read_byte_data(i2c_dev_file, REGISTER); \
}

#define WRITE_REGISTER_VERBOSE(i2c_dev_file, REGISTER, value) \
{ \
    short int var = 0; \
    i2c_smbus_write_byte_data(i2c_dev_file, REGISTER, value); \
    usleep(100); \
    READ_REGISTER(i2c_dev_file, REGISTER, var); \
    VERBOSE(#REGISTER "    :0x%02X\n", var); \
}

I would like the REGISTER field to not be expanded in the following line
VERBOSE(#REGISTER " :0x%02X\n", var); \

For example, When I write
WRITE_REGISTER_VERBOSE(i2c_dev_fd, REG_PWR_CTRL, 0x1A);
WRITE_REGISTER_VERBOSE(i2c_dev_fd, REG_FIFO_CTL, 0xC6);
I get the output
0x2D :0x1A
0x38 :0xC6
I would like to obtain
REG_PWR_CTRL :0x1A
REG_FIFO_CTL :0xC6

I came across a lot of posts that spoke about adding an extra level of indirection.
I tried the answer described here https://stackoverflow.com/a/2653351/1761555 ..although I believe that that answer is for a different problem altogether..

What I did was

#define STRINGIFY(label) (#label)

#define WRITE_REGISTER_VERBOSE(i2c_dev_file, REGISTER, value) \
{ \
    short int var = 0; \
    i2c_smbus_write_byte_data(i2c_dev_file, REGISTER, value); \
    usleep(100); \
    READ_REGISTER(i2c_dev_file, REGISTER, var); \
    VERBOSE("%s    :0x%02X\n", STRINGIFY(REGISTER), var); \
}

But this still gives me the same output as before

Is there any way to achieve this?

3 Answers
Related