Home > Mobile >  Determine which variable use at runtime in Embedded C
Determine which variable use at runtime in Embedded C

Time:03-07

There are two different makefiles for this firmware that I am working on. Data Version 1 and Data Version 2. Both versions are using the same file called ble_communication.c in each own make file.

To differentiate between the two versions, we have two variables declared inside ble_communication.c

static uint8_t data_version1[] = {0x00, 0x00, 0x00, 0x00, 0x00};
static uint8_t data_version2[] = {0x01, 0x00, 0x00, 0x00, 0x00};

Inside the ble_communication.c file we have a function called

uint32_t start_broadcasting(void)
{
send_beacon(OPCODE, 0, /* determine data version here to send at runtime */, key);
}

My question is since we are using the same file ble_communication.c for both versions of the build, how can the code select which variable to use for its build during the runtime of the code? If it's Data Version 1, I want it to use data_version1[] and if's is Data Version 2 it uses data_version2[].

I can't use #ifndef switch statements as I am not allowed to use them due to the new design guidelines

CodePudding user response:

To be honest, I would prefer to use the #ifdef, but here is a workaround for you.

Create two files with the desired data and select the required file at build time, using the makefile.

First, prepare two C files, ble_communication_data1.c and ble_communication_data2.c. Feel free to choose a clearer name.

Place the required data in each C file, but keep the names the same.

ble_communication_data1.c

uint8_t ble_communication_data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};

ble_communication_data2.c

uint8_t ble_communication_data[5] = {0x01, 0x00, 0x00, 0x00, 0x00};

Create a header file to access the data:

ble_communication_data.h

extern uint8_t ble_communication_data[5];

Modify the ble_communication.c so that it uses the common variable name:

#include "ble_communication_data.h"

uint32_t start_broadcasting(void)
{
    send_beacon(OPCODE, 0, ble_communication_data, key);
}

Finally, in each of the makefiles, add the correct ble_communication_data C file to the list of files to be compiled.

  • Related