NW27
December 19, 2021, 9:14pm
#1
Hi All,
How can I have a constant/variable that has the project name and compiled date/time accessible within the project?
Example
#define COMPILED $UNIX_TIME // or similar
#define PROJECTNAME $folder // or similar
printf(“Project compiled at %s/n”,COMPILED);
printf(“Project Name at %s/n”,PROJECTNAME);
Thanks,
Neil
As the documentation shows, you can inject the Unix time into the build system by writing
build_flags =
-D CURRENT_TIME=$UNIX_TIME
You can use this timestamp to convert it into a human readable time.
Alternatively and way more easier, use the __DATE__
and __TIME__
built-in macros that are compiler-defined and are already strings (https://www.cprogramming.com/reference/preprocessor/DATE .html ).
While the project name isn’t directly available, you can pass in $PROJECT_DIR
(string escaped ) to get the full ptath to the projects, e.g., C:\Users\Max\temp\atmega4809
.
NW27
December 20, 2021, 1:47am
#3
Hi and thanks for the quick reply.
I don’t understand how to use $PROJECT_DIR
I tried
[env]
build_flags =
-D NEIL=27
-D PROJECT_NAME=$PROJECT_DIR
But this just gives me strange errors.
Yeah the $PROJECT_DIR
is tricky because it is a string and it contains characters that need to be escaped, such as backslashes. You need to use scripting to convert that to an acceptible compiler argument for -D
.
The following is a minimal example:
[env:uno]
platform = atmelavr
board = uno
framework = arduino
build_flags =
-DCOMPILE_UNIX_TIME=$UNIX_TIME
extra_scripts = pre:inject_path.py
with
inject_path.py
being on the same directory level as platformio.ini
Import("env")
proj_path = env["PROJECT_DIR"]
proj_path = proj_path.replace("\\", "\\\\")
macro_value = "\\\"" + proj_path + "\\\""
env.Append(CPPDEFINES=[
("PROJECT_PATH", macro_value)
])
and src\main.cpp
#include <Arduino.h>
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("PlatformIO Unix compile time: ");
Serial.println(COMPILE_UNIX_TIME);
Serial.print("Project directory: ");
Serial.println(PROJECT_PATH);
Serial.print("__DATE__: ");
Serial.println(__DATE__);
Serial.print("__TIME__: ");
Serial.println(__TIME__);
delay(2000);
}
gives output
>pio run -t upload -t monitor
[...]
--- Miniterm on COM3 9600,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
PlatformIO Unix compile time: 1640016119
Project directory: C:\Users\Max\temp\time
__DATE__: Dec 20 2021
__TIME__: 17:02:00
1 Like