xxAtrain223 / EmbMessenger

Command based communication library for embedded devices.
MIT License
2 stars 1 forks source link

Command Callback #2

Closed xxAtrain223 closed 4 years ago

xxAtrain223 commented 5 years ago

Instead of storing each command in an array of size MaxCommands, they could be called from a single callback which then selects which command to call.

  1. Saves SRAM on devices by moving the selection logic into program space.
  2. Allows for all user EmbMessenger logic to be in one place, decluttering user code, although the EmbMessenger logic could still be kept in individual commands or adaptors.
  3. Allows for arbitrary command IDs instead of 0-indexed.

Example usage:

void commandCallback(uint8_t command_id)
{
    switch (command_id)
    {
        case 0: {
            ping();
        } break;
        case 1: {
            bool state;
            messenger.read(state); // Can read data in callback instead of command
            setLed(state);
        } break;
        case 2: {
            bool rv;
            rv = toggleLed();
            messenger.write(rv); // Can write data in callback instead of command
        } break;
        case 77: { // Note Arbitrary CommandID
            int16_t a = 0, b = 0, rv = 0;

            messenger.read_and_validate(a, [](int16_t val){ return val > -16 && val < 128; });
            messenger.read_and_validate(b, [](int16_t val){ return val > -16 && val < 128; });

            add(a, b, rv);

            messenger.write(rv);
        } break;
    }
}

void setup()
{
    messenger.registerCommandCallback(commandCallback);
}

This feature is on hold until EmbGen is finished.

xxAtrain223 commented 4 years ago

Closing in favor of #8.