Description
setValue()
– write a new value into a data pool variable. This function is overloaded and takes several variable types as value argument. Use explicit type casting to tell the library how to understand the new value and to avoid compiler errors (see examples below).
Syntax
setValue( ou8_variable_index, value );
setValue( ou8_variable_index, ou8_byte_pos, ou8_byte_value );
Parameters
uint8_t ou8_variable_index: the number of the variable that should be given a new value
float / uint32_t or int32_t value: the new value of the variable
uin8_t ou8_byte_pos: the position of the byte to change [0…3] – 0: LSB / 3: MSB
uin8_t ou8_byte_value: the new value of the byte
Syntax Examples
myPool.setValue(0, (float) 5.33); // set var0 - float value
myPool.setValue(1, 2, 0xAA); // set var1 - data byte 2
myPool.setValue(2, (uint32_t) 123); // set var2 - unsigned int
myPool.setValue(3, (int32_t) -234); // set var3 - signed int
Returns
uint8_t: 0: OK or
1: index out of range
2: pool not yet initialised (call begin() first)
Example Codes:
The following code example defines a data pool including an callback function that is called whenever a variable is modified by another peer.
#include "Datapool.h"
#define NB_OF_VARIABLES 3
#define POOL_NAME "A_SimplePool_V1_0"
void myCallBack(uint8_t ou8_NbOfUpdatedVariable);
DATAPOOL myPool( &MCAN0, &BOOTLOADER_SETTING, POOL_NAME, NB_OF_VARIABLES, myCallBack );
void setup() {
while(!Serial);
myPool.begin();
Serial.println("Enter new value of Var1");
}
int8_t s8_modVar = -1;
void loop()
{
if(Serial.peek() != -1)
{
int i_new = Serial.readStringUntil('n').toInt();
myPool.setValue( 1, (int32_t) i_new );
}
if(s8_modVar > -1)
{
Serial.println("Var"+String(s8_modVar)+": "+
String( myPool.getValue_int32(s8_modVar) ));
s8_modVar = -1;
}
}
void myCallBack(uint8_t ou8_NbOfUpdatedVariable)
{
s8_modVar = ou8_NbOfUpdatedVariable;
}
Notes and Warnings
—
Examples can be found in the Arduino IDE menu:
File -> Examples -> Datapool ->