ModuleData
Object containing data received from module.
Functions list
Result | Function | Description |
---|---|---|
bool |
is(command ) |
Check if data came of specified command |
bool |
isInt() | Check if there is valid integer data |
bool |
isString() | Check if there is valid string data |
int |
getInt() | Read integer from data |
string |
getString() | Read string from data |
bool |
getData(array ) |
Read array pointer of data |
bool |
getData(array , length ) |
Read array pointer and data length |
int |
getCmdHash() | Get hash of received command string |
API
Functions
is(command
)
- Check if received data is from specified
command
.
Returns: [true
:false
]
void onModuleData(ModuleData data) {
if (data.is("cfg/robot/name"))
Serial.println("Data came from 'cfg/robot/name' command");
if (data.is("version"))
Serial.println("Data came from 'version' command");
}
isInt()
- Check if data can be read with
getInt()
.
Returns: [true
:false
]
ModuleData data;
module.readWait("cfg/robot/name", data);
data.isInt(); // returns false. "cfg/robot/name" data contains string value
module.readWait("version", data);
data.isInt(); // returns true. "version" data contains integer value
isString()
- Check if data can be read with
getString()
.
Returns: [true
:false
]
ModuleData data;
module.readWait("cfg/robot/name", data);
data.isString(); // returns true. "cfg/robot/name" data contains string value
module.readWait("version", data);
data.isString(); // returns false. "version" data contains integer value
getInt()
- Read integer value from received data.
Returns: signed 32bitint
[-2147483648
:2147483647
]
ModuleData data;
// Read firmware version. Read result will be stored to "data".
if (module.readWait("version", data)) {
Serial.print("Value of 'version': ");
Serial.println(data.getInt()); // Prints: Value of 'version': 100
}
getString()
- Read string value from received data.
Returns: string array.
ModuleData data;
// Read robot name. Read result will be stored to "data".
if (module.readWait("cfg/robot/name", data)) {
Serial.print("'cfg/robot/name' is ");
Serial.println(data.getString()); // Prints: 'cfg/robot/name' is Totem X4
}
getData(array
)
- Get pointer to data array.
Value is stored in passed parameterarray
.
In case of printing received data, TotemBUS protocol always adds NULL terminator at the end of the data, so it's safe to use print and string manipulation functions.
Returns: data stored inarray
[true
:false
]
ModuleData data;
if (module.readWait("cfg/robot/name", data)) { // Read robot name
char *name;
data.getData(name); // Information is pointed to name variable
Serial.print("'cfg/robot/name' is ");
Serial.println(name); // Prints: 'cfg/robot/name' is Totem X4
}
getData(array
, length
)
- Get pointer to data array and its length.
Value is stored in passed parametersarray
,length
.
In case of printing received data, TotemBUS protocol always adds NULL terminator at the end of the data, so it's safe to use print and string manipulation functions.
Returns: data stored inarray
,length
[true
:false
]
ModuleData data;
if (module.readWait("cfg/robot/name", data)) {// Read robot name
char *name;
int length;
data.get(name, length); // Information is pointed to name and length variables
Serial.print("'cfg/robot/name' is ");
Serial.print(name);
Serial.print(", length: ");
Serial.println(length); // Prints: 'cfg/robot/name' is Totem X4, length: 8
}
getCmdHash()
- Get hash of result command.
Will return command identifier that can be generated with
TotemModule::hashCmd("command")
.
Returns: unsigned 32bit hash [0
:0xFFFFFFFF
]
void onModuleData(ModuleData data) {
if (data.getCmdHash() == TotemModule::hashCmd("cfg/robot/name")) {
// Got command "cfg/robot/name"
}
else if (data.getCmdHash() == TotemModule::hashCmd("version")) {
// Got command "version"
}
}