-
Notifications
You must be signed in to change notification settings - Fork 1
Command Structure
xxAtrain223 edited this page Mar 10, 2019
·
1 revision
EmbMessenger is command based, intended to be used where each command does one simple action. For example, toggle an LED, get the value of an ultrasonic sensor, or set the position for a servo motor. Commands must be defined and registered in two places. On the Device, where the command is implemented; and on the Host, where the command interface is implemented.
In this example, we'll make three commands.
- Ping - An empty command, does nothing. Used for testing purposes.
-
Set Led - Set the new state for the LED. Takes one
boolparameter, the new LED state. -
Toggle Led - Toggle the LED. Returns one
boolparameter, the new LED state.
bool ledState = false;
// Do nothing, EmbMessenger will just send a message received.
void ping()
{
}
// Set the LED using the parameter from EmbMessenger
void setLed()
{
// Receive the new LED state
messenger.read(ledState);
digitalWrite(LED_BUILTIN, ledState ? HIGH : LOW);
}
// Toggle the LED and return the new state to EmbMessenger
void toggleLed()
{
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState ? HIGH : LOW);
// Send the new LED state
messenger.write(ledState);
}// Doesn't send or receive anything
class Ping : public emb::host::Command
{
};
// Set the new state of the LED
class SetLed : public emb::host::Command
{
bool m_ledState;
public:
// Take the new state as a constructor parameter
SetLed(bool ledState)
{
m_ledState = ledState;
}
// Override the default send method
void send(emb::host::EmbMessenger* messenger)
{
// Send the new LED state
messenger->write(m_ledState);
}
};
// Toggle the state of the LED
class ToggleLed : public emb::host::Command
{
bool m_ledState;
public:
// Override the default receive method
void receive(emb::host::EmbMessenger* messenger)
{
// Receive the new LED state
messenger->read(m_ledState);
}
// Get the new LED state
bool getLedState() const
{
return m_ledState;
}
};