63 points · by ingve · 8y ago · 26 comments
It's actually pretty cheap on ARM (2 cycles [1]), if the message IDs are consecutive, due to TBB/TBH [2], and GCC will use it when appropriate. Not sure if x86 has an equivalent.
[1] http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.... [2] http://infocenter.arm.com/help/topic/com.arm.doc.dui0553a/BA...
That statement about switch inefficiency is simply not true, except for sparse case values.
But since one can generally choose those values (and they're often a running enum anyways), it's really a non-issue.
For sparse case, it's better to use a hash table. Perhaps a build phase to generate a perfect hash (https://en.wikipedia.org/wiki/Perfect_hash_function) to map each possible input value to continuous range of integer values.
Even for sparse case values the compiler will emit a binary search once the list of labels is long enough.
#include "Protocol.h"
#include "comms/comms.h"
namespace cc = comms_champion;
namespace demo
{
namespace cc_plugin
{
Protocol::~Protocol() noexcept = default;
const QString& Protocol::nameImpl() const
{
static const QString& Str("Demo");
return Str;
}
} // namespace cc_plugin
} // namespace demohttps://github.com/arobenko/comms_all_protocols
the ublox example seems a reasonably complex protocol of messages.
However, I'm not really sold on the framework to do it this way.
Using C/C++ for embedded systems external communications feels like building a house out of matches.
Sure, there's tooling to help fire proofing things a bit, like static analysis and so on. You see, it's important to spray some fire retardant on the match house.
But wouldn't it be so much nicer to have a construction material other than matches for this purpose...
Currently limited microcontroller architecture support is one of the biggest issues with it.
Rust based RTOS would also be helpful for medium size devices. Of course small devices are fine on bare metal.
[1]: https://github.com/tock/tock/blob/master/doc/Getting_Started... [2]: https://github.com/hashmismatch/freertos.rs
I would have guessed C or C++. Maybe they're not great for the purpose but they're also just about the only game in town, no?
Actually, does anyone know if FORTRAN is ever used for embedded applications?
It was used for a fair number of control systems in the 1980s and 1990s.
[0] https://webhome.weizmann.ac.il/home/fhlevins/RTF/RTF-TOC.htm...