m5stack/M5StickC

Color defines causes compiler errors

corneliusmunz opened this issue · 2 comments

The color defines in

#define BLACK 0x0000 /* 0, 0, 0 */
#define NAVY 0x000F /* 0, 0, 128 */
#define DARKGREEN 0x03E0 /* 0, 128, 0 */
#define DARKCYAN 0x03EF /* 0, 128, 128 */
#define MAROON 0x7800 /* 128, 0, 0 */
#define PURPLE 0x780F /* 128, 0, 128 */
#define OLIVE 0x7BE0 /* 128, 128, 0 */
#define LIGHTGREY 0xC618 /* 192, 192, 192 */
#define DARKGREY 0x7BEF /* 128, 128, 128 */
#define BLUE 0x001F /* 0, 0, 255 */
#define GREEN 0x07E0 /* 0, 255, 0 */
#define CYAN 0x07FF /* 0, 255, 255 */
#define RED 0xF800 /* 255, 0, 0 */
#define MAGENTA 0xF81F /* 255, 0, 255 */
#define YELLOW 0xFFE0 /* 255, 255, 0 */
#define WHITE 0xFFFF /* 255, 255, 255 */
#define ORANGE 0xFD20 /* 255, 165, 0 */
#define GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define PINK 0xF81F

causes problems when compiling with other libraries or other code which uses the commonly names for colors as Variable or Enum names. The namespaced color defines in are much better and should be used only
#define ST7735_BLACK 0x0000 /* 0, 0, 0 */
#define ST7735_NAVY 0x000F /* 0, 0, 128 */
#define ST7735_DARKGREEN 0x03E0 /* 0, 128, 0 */
#define ST7735_DARKCYAN 0x03EF /* 0, 128, 128 */
#define ST7735_MAROON 0x7800 /* 128, 0, 0 */
#define ST7735_PURPLE 0x780F /* 128, 0, 128 */
#define ST7735_OLIVE 0x7BE0 /* 128, 128, 0 */
#define ST7735_LIGHTGREY 0xC618 /* 192, 192, 192 */
#define ST7735_DARKGREY 0x7BEF /* 128, 128, 128 */
#define ST7735_BLUE 0x001F /* 0, 0, 255 */
#define ST7735_GREEN 0x07E0 /* 0, 255, 0 */
#define ST7735_CYAN 0x07FF /* 0, 255, 255 */
#define ST7735_RED 0xF800 /* 255, 0, 0 */
#define ST7735_MAGENTA 0xF81F /* 255, 0, 255 */
#define ST7735_YELLOW 0xFFE0 /* 255, 255, 0 */
#define ST7735_WHITE 0xFFFF /* 255, 255, 255 */
#define ST7735_ORANGE 0xFD20 /* 255, 165, 0 */
#define ST7735_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define ST7735_PINK 0xF81F

Can you please delete the common color name defines in https://github.com/m5stack/M5StickC/blob/master/src/utility/ST7735_Defines.h to prevent compiler errors with other code?

In my case i have a local enum definition of color in a class of the following library and here the Enum names are replaced by the Macro definition of your library:
https://github.com/corneliusmunz/legoino/blob/edc40fa058e640bf455411be0708832a0e13b093/src/Lpf2Hub.h#L75-L89
This replacement leads to error messages like the following

In file included from /home/programmez/Arduino/libraries/M5StickC/src/utility/In_eSPI_Setup.h:17:0,
                 from /home/programmez/Arduino/libraries/M5StickC/src/utility/In_eSPI.h:24,
                 from /home/programmez/Arduino/libraries/M5StickC/src/M5Display.h:7,
                 from /home/programmez/Arduino/libraries/M5StickC/src/M5StickC.h:70,
                 from /tmp/arduino_modified_sketch_117376/sketch_jun02a.ino:1:
/home/programmez/Arduino/libraries/M5StickC/src/utility/ST7735_Defines.h:94:29: error: expected identifier before numeric constant
 #define BLACK               0x0000      /*   0,   0,   0 */
                             ^
/home/programmez/Arduino/libraries/Legoino/src/Lpf2Hub.h:77:3: note: in expansion of macro 'BLACK'
   BLACK = 0,
   ^
/home/programmez/Arduino/libraries/M5StickC/src/utility/ST7735_Defines.h:94:29: error: expected '}' before numeric constant
 #define BLACK               0x0000      /*   0,   0,   0 */
                             ^
/home/programmez/Arduino/libraries/Legoino/src/Lpf2Hub.h:77:3: note: in expansion of macro 'BLACK'
   BLACK = 0,
   ^
/home/programmez/Arduino/libraries/M5StickC/src/utility/ST7735_Defines.h:94:29: error: expected unqualified-id before numeric constant
 #define BLACK               0x0000      /*   0,   0,   0 */
                             ^
/home/programmez/Arduino/libraries/Legoino/src/Lpf2Hub.h:77:3: note: in expansion of macro 'BLACK'
   BLACK = 0,
   ^
In file included from /home/programmez/Arduino/libraries/Legoino/src/PoweredUpHub.h:14:0,
                 from /tmp/arduino_modified_sketch_117376/sketch_jun02a.ino:2:
/home/programmez/Arduino/libraries/Legoino/src/Lpf2Hub.h:89:1: error: expected declaration before '}' token
 };
 ^
exit status 1
Error compiling for board M5Stick-C.

Eidt this will cause some example or user code error

Maybe edit your code
or
used #undef will be better

Hi @EeeeBin! Thx for your fast response!
I can also change it in my library (https://github.com/corneliusmunz/legoino) but i am not happy about that because i have a enum definition inside a propper class and that names are hard replaced by the Macro of the m5stickc library. For the future it would be better to use a constant instead of a macro. The constant will not replace any "BLACK" or "RED" in the overall code by a value and is as efficient as a macro
const uint16_t BLACK = 0x0000;

See also: https://luckyresistor.me/knowledge/avoid-preprocessor-macros/

If i use #undef it is strongly dependent on the order of the header includes. Maybe you can think of replacing the macro defines with const definitions. This should be transparent for all your users and the example code