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 |
Example
#include <Totem.h>
// Declare that we are willing to communicate with X4 module
TotemModule module(04);
// Receiver function for result of read() function
void onModuleData(ModuleData data) {
if (data.is("cfg/robot/name")) { // cfg/robot/name received
Serial.print("'cfg/robot/name' is ");
Serial.println(data.getString()); // Prints: 'cfg/robot/name' is Totem Robot
}
else if (data.is("version")) { // version received
Serial.print("'version' is");
Serial.println(data.get()); // Prints: 'version' is 100
}
}
// Arduino initialization function
void setup() {
// put your setup code here, to run once:
Totem.X4.begin(); // Initialize X4 module
// Register module read() data receiver function
module.attachOnData(onModuleData);
// Read cfg/robot/name from module X4. Result will be passed to onModuleData
module.read("cfg/robot/name");
// Read firmware version from module X4. Result will be passed to onModuleData
module.read("version");
}
// Arduino loop function
void loop() {
// put your main code here, to run repeatedly:
}
Description
bool
is(command
)
Description: Check if received data is from specified command
.
Result: True if command
and result command match.
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");
}
bool
isInt()
Description: Check if received data contains integer value.
Result: True if valid data can be received with getInt()
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
bool
isString()
Description: Check if received data contains string value.
Result: True if valid data can be received with getString()
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
int
getInt()
Description: Read integer value from received data.
Result: Up to 32-bit signed value.
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
}
string
getString()
Description: Read string value from received data.
Result: Pointer to first string character with NULL terminator.
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
}
bool
getData(array
)
Description: Get pointer to data array.
In case of printing received data, TotemBUS protocol always adds null terminator at the end of the data, so it's safe not to check the total length.
Result: Pointer to data array.
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
}
bool
getData(array
,length
)
Description: Get pointer to data array and it's length.
In case of printing received data, TotemBUS protocol always adds null terminator at the end of the data, so it's safe not to check the total length.
Result: Pointer to data array and it's length.
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
}
int
getCmdHash()
Description: Get hash of result command.
Will return command identifier that can be generated with TotemModule::hashCmd("command")
.
Result: Command hash.
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"
}
}