0

The problem:

I'm writing a general C library for an LCD in a microcontroller project. up to 8 LCDs with various sizes(e.g. 128*96 or 64*48) in various addresses may be added (e.g. LCD3 and LCD7). but only one of them is actively coded at a time. so I thought for a mechanism to do so.

in the code, there is a definition for CLCD_ROWS and CLCD_COLS which correspond to the Active display size.

#define CLCD_ROWS   // Active LCD rows
#define CLCD_COLS   // Active LCD columns

and there's definitions for the various LCDs. for example, if we have LCD3 and LCD7 connected, we define their sizes with :

#define CLCD_ROWS3  96
#define CLCD_COLS3  64

#define CLCD_ROWS7  128
#define CLCD_COLS7  32

The question:

I've written a [wrong] macro to redefine the values of CLCD_ROWS and CLCD_COLS :

#define cLcd_setActiveI2CcLcd(X)    \
CLCD_ROWS = CLCD_ROWS##X            \
CLCD_COLS = CLCD_COLS##X

and in my main code I call the macro:

cLcd_setActiveI2CcLcd(7);

which gives me an error of "missing ;".

it is easy to implement it with variables. but since these values are hardcoded , I thought they are "preprocessable" since need every bit of RAM in a low end MCU.

  1. Is my approach about preprocessing this values, correct ?
  2. What is the right way to write a macro for that purpose ?

I'm using a C99 compiler.

  • 1
    The error complains about a missing `;`. Why don't you insert one after the first assignment? I.e. before the second `\\`. – Yunnosch Oct 12 '19 at 08:15
  • Are you going to use the macro like `if(false) cLcd_setActiveI2CcLcd(7);` or `while(true) cLcd_setActiveI2CcLcd(7);` ? I.e. without `{}`. – Yunnosch Oct 12 '19 at 08:18
  • @Yunnosch actually I tested the code with placing `;` in various positions. the error still appeared (or twice 'd). no I'm not using it that way it's in the main function, without being embraced by anything. – Tirdad Sadri Nejad Oct 12 '19 at 08:37
  • This might help https://stackoverflow.com/questions/154136/why-use-apparently-meaningless-do-while-and-if-else-statements-in-macros – gstukelj Oct 12 '19 at 08:45

1 Answers1

0

First things first, your method of using function-type macro is wrong. Even if you fix the error you have, the macro will not do CLCD_ROWS equal to CLCD_ROWS7, but to CLCD_ROWSX (that is how macros work, it concatenates the thing you give, not its value). Instead if you want to use macros for reducing RAM usage you can change your code to:

1st Solution

#define ROW_COLS 7 // change this if you use different display

#if ROW_COLS == 7

#define CLCD_ROWS  128
#define CLCD_COLS  32

#elif ROW_COLS == 3

#define CLCD_ROWS  96
#define CLCD_COLS  64

#endif

2nd Solution If you want dynamically change the size of your display in the runtime, you can do it like this:

static int display_cnt;

#define CLCD_ROWS ((display_cnt == 3) ? 96 : 128)
#define CLCD_COLS ((display_cnt == 3) ? 64 : 32)

So when you change the value of display_cnt variable, the macro will automatically change its value.

Miradil Zeynalli
  • 423
  • 4
  • 18
  • this whole code you provided runs(gets preprocessed) just once. I want to reprocess it in various points of code. – Tirdad Sadri Nejad Oct 12 '19 at 09:31
  • is this possibe to assign a value to a definition by using `=` ? and is there any way to get the value of X to concatenate ? – Tirdad Sadri Nejad Oct 12 '19 at 09:32
  • There is not. Once, I also needed to have value of variable to get concatenated, I could not find any solution. Some suggested two-level macros, but it also did not work. If you want, you can use above code and insert it into function type macro – Miradil Zeynalli Oct 12 '19 at 09:35
  • @TirdadSadriNejad I edited my post and wrote 2nd solution. Check this one – Miradil Zeynalli Oct 12 '19 at 09:57