Initializing Sensors with variables
19. 5. 28 오전 9:24

Hey everyone,

I'm trying to enable every sensor on its own. But I dont want to make it hardcoded. I want to get a message from e.g. a REST client and with the result configure the sensors.

Therefore i took the "StreamSensorDataOverUsb" as basis and tried to modify it that the initialization is done with variable that i can modify later on on the process.

First i defined the variables for each Sensor if it should be enabled

bool AccelerationEnabled = true;
bool MagnetometerEnabled = true;
bool GyrometerEnabled = true;
bool HumidityEnabled = true;
bool TemperaturEnabled = true;
bool PressureEnabled = true;
bool LightEnabled = true;
bool NoiseEnabled = true;

and then i initialized the Configurationstructs:

Sensor_AccelConfiguration_T AccelerationConfiguration = {
		.Type = SENSOR_ACCEL_BMA280,
		.IsRawData = false,
		.IsInteruptEnabled = true,
		.Callback = AccelAppCallback

and in the end i want to initialize the Sensor_Setup_T:

static Sensor_Setup_T SensorSetup = {
		.CmdProcessorHandle = NULL,
		.Enable = {
				.Accel = AccelerationEnabled,
				.Mag = MagnetometerEnabled,
				.Gyro = GyrometerEnabled,
				.Humidity = HumidityEnabled,
				.Temp = true,
				.Pressure = true,
				.Light = true,
				.Noise = false
		.Config = {
			.Accel = AccelerationConfiguration,
			.Gyro = GyrometerConfiguration,
			.Mag = MagnometerConfiguration,
			.Light = LightConfiguration,
			.Temp = TemperaturConfiguration,

So in that way i can configure the variables before and just initialize the Setup struct and the sensor upfront.

But i get the errors:

"initializer element is not constant"

"near initialization for '[Varablename]'

in all codelines.


The only kind of "solution" that would come to my mind, is to hardcode initialize the struct with all false values and them somewhere, somehow go between initialization of the struct and the initialization of the sensor itself, and that way do something like:

SensorSetup.Enable.Accel = AccelerationEnabled;

the big questions here would be, is that possible and if so where do you have to dig in?


But the Keyquestion is: How can you "varibale" initialize the sensor?

0 (0 투표)
RE: Initializing Sensors with variables
19. 5. 28 오후 3:31 as a reply to Martin Scheck.

Hi Martin,

You are doing everything right, but initialization should be done before you execute the Sensor_Setup function. see the picture below.



+1 (1 투표)
RE: Initializing Sensors with variables
19. 5. 29 오전 11:20 as a reply to Saeid Kajlar.
Works perfectly. Thank you very much ! :)
+1 (1 투표)