I am working on a compact output / modulation debugging utility that will be used in embedded systems.
I created a system in which I can transfer messages via a serial port to a PC in a compact way. To save the memory / serial port throughput, I deleted the message lines from the embedded system by giving them unique 16-bit identifiers.
This was pretty straightforward because I put all the messages in 1 list. Several macros put this on the list:
projectdefs.h:
#define MESSAGE_TABLE(MSG) \ MSG(HELLO, "Hello World!") \ MSG(TEST, "Second message #ID 1") \ MSG(TEST2, "Third message #ID 2")
messages.h:
#define MACRO_STR_CONCAT(a,b) a##b #define MESSAGE_ENUM(codeName, str) MACRO_STR_CONCAT(MSG_, codeName) typedef enum messageNumbers_e { MESSAGE_TABLE(MESSAGE_ENUM), MESSAGE_COUNT }; #define MESSAGE(codeName) messageSend(MACRO_STR_CONCAT(MSG_, codeName), __LINE__, file_number);
The only data transmitted through the serial port is the message identifier, line number and file number (note, not line!).
I am having a problem with how I can assign a unique identifier to each file using the C preprocessor / compiler. I do not want to store every line of the file name inside the firmware. It uses (too much) memory or bandwidth for the serial port.
My idea is to define the file_number constant in each file using a macro. I would use this definition at the top of each source file:
#define ASSIGN_FILENUMBER() enum { file_number = __COUNTER__ };
However, since each file is compiled separately, this means that the __COUNTER__ operator always starts at 0 when called and does not know the presence of other files or its own identifier.
Another consideration was to edit the MakeFile (script) and add the file identification numbers. However, this will closely link the project / project functionality with my IDE configuration, which is undesirable. Moreover, I am not sure of the possibility with my current IDE (XC16 / XC32 compiler on the Mplab X IDE or IAR Embedded Workbench).
I wonder if there are any other creative ways for the standard C preprocessor to complete the task?