Build Flags not working on Raspberry Pi 3b

I have the following platformio.ini file

[env]
build_flags = -D $PIOENV
          
[env:adafruit_feather_m0_rf95_client_env]
platform = atmelsam
board = adafruit_feather_m0
src_filter = +<LoraxClient.cpp>
framework = arduino
lib_deps = Time

[env:raspberrypi_3b_dragino_pi_hat__server_env]
platform = linux_arm
board = raspberrypi_3b
src_filter = +<LoraxServer.cpp>
build_flags = -D RASPBERRY_PI

Note the [env] section. If I am understanding it correctly, -D $PIOENV should define the macro #define adafruit_feather_m0_rf95_client_env for the first environment and #define raspberrypi_3b_dragino_pi_hat__server_env for the second. I am trying to use these macros in the following C++ file. Note the #if define(...) near the beginning.

#include "LoraxRF95.h"
#include <stdlib.h>
#include <RH_RF95.h>

static RH_RF95 *__driver = NULL; //Initialized by init()

#if defined(adafruit_feather_m0_rf95_client_env)

#define SLAVE_SELECT 8
#define INTERRUPT_PIN 3

#elif defined(raspberrypi_3b_dragino_pi_hat__server_env)

#define SLAVE_SELECT 25
#define INTERRUPT_PIN 4

#else

#error "Environment not supported. Please either define SLAVE_SELECT and INTERRUPT_PIN for the environment or add this library to lib_ignore in platformio.ini file"

#endif

bool rf95Init()
{

    if (__driver == NULL)
    {
        static RH_RF95 driver(SLAVE_SELECT, INTERRUPT_PIN);
        __driver = &driver;
    }

    return __driver->init(); 
}

This works for the first environment but not for the raspberry pi

Is there any reason why this is not working?

Just for giggles, does it make any difference if you reverse the order of the environments? i.e. does putting the raspberry pi env block before the feather env block make the feather fail?

However, I suspect the culprit is probably fact you have build_flags for the raspberry pi, and it’s not adding to the two together… you probably need to do build_flags = ${env.build_flags} -D RASPBERRY_PI instead so it adds the global flags rather than overrides them… that’s what the docs are suggestive of when working with a global lib_deps parameter that is added to, anyway.

2 Likes

The addition of ${env.build_flags} worked thankfully, but not switching the order of the environments.

1 Like

Excellent… at least it’s not a bug then, just behaviour you weren’t expecting! :slight_smile:

2 Likes