[英]Converting enum type to a specific, predefined array of hex values in C++
What would be the most optimal and shortest way to write a function returning an array of 8-bit hex values, which are assigned based on an enumerated parameter.编写返回 8 位十六进制值数组的 function 的最佳和最短方法是什么,这些值是根据枚举参数分配的。
I need to hard code commands, which will be sent over bluetooth and still have an easy access to them in code.我需要对命令进行硬编码,这些命令将通过蓝牙发送,并且仍然可以在代码中轻松访问它们。 So I have an enum with all possible commands listed in a human-readable way.所以我有一个枚举,其中以人类可读的方式列出了所有可能的命令。
enum eventType {
MODIFY_A,
MODIFY_B,
MODIFY_C,
MODIFY_D
}
The communication protocol I try to adapt uses custom packets with predefined hex values.我尝试采用的通信协议使用具有预定义十六进制值的自定义数据包。 So for example, sending 0x00, 0x01, 0x02, 0x03
could change led colour of a receiving device.因此,例如,发送0x00, 0x01, 0x02, 0x03
可能会改变接收设备的 LED 颜色。
The library used for ble communication accepts arrays of said hexes, or other words, just a plain C string.用于 ble 通信的库接受 arrays 的所述十六进制,或者换句话说,只是一个普通的 C 字符串。
It would be easiest to simply define an array in a switch() after declaring it, just like this:最简单的方法是在声明后在 switch() 中简单地定义一个数组,就像这样:
uint8_t* getCommandOf(eventType event) {
uint8_t command[4];
switch(event) {
case eventType::MODIFY_A:
command = {0x01, 0x00, 0x00, 0x00}; break;
case eventType::MODIFY_B:
command = {0x02, 0x00, 0x00, 0x00}; break;
case eventType::MODIFY_C:
command = {0x03, 0x00, 0x00, 0x00}; break;
case eventType::MODIFY_D:
command = {0x04, 0x00, 0x00, 0x00}; break;
}
return command;
}
Then I could simply call: sendOverBLE(getCommandOf(eventType::MODIFY_A));
然后我可以简单地调用: sendOverBLE(getCommandOf(eventType::MODIFY_A));
Unfortunately, we can't do that in C++. I'm open to suggestions.不幸的是,我们不能在 C++ 中这样做。我愿意接受建议。 How would you solve it?你会怎么解决? I though of a few solutions, butI don't like any of them.我想到了一些解决方案,但我不喜欢其中任何一个。
Maybe define them in a 2d array?也许在二维数组中定义它们? Or a custom container class?还是自定义容器 class?
I would suggest using and returning a std::array
.我建议使用并返回一个std::array
。 That would change your function to这会将您的 function 更改为
std::array<uint8_t, 4> getCommandOf(eventType event) {
switch(event) {
case eventType::MODIFY_A:
return {0x01, 0x00, 0x00, 0x00};
case eventType::MODIFY_B:
return {0x02, 0x00, 0x00, 0x00};
case eventType::MODIFY_C:
return {0x03, 0x00, 0x00, 0x00};
case eventType::MODIFY_D:
return {0x04, 0x00, 0x00, 0x00};
}
// for bad input
return {}; // for all zeros
// or use an exception
throw std::invalid_argument("some error message here");
}
If you need to pass this array to a function that takes a uint8_t*
, then you can use array
's data
member to get such a pointer like如果您需要将此数组传递给采用uint8_t*
的 function,那么您可以使用array
的data
成员来获取这样的指针
my_uint8_t_ptr_func(getCommandOf(eventType::MODIFY_A).data());
If you're in complete control over your enum
you could define it as follows:如果您完全控制您的enum
,您可以按如下方式定义它:
enum eventType : uint8_t {
MODIFY_A = 1,
MODIFY_B,
MODIFY_C,
MODIFY_D
}
Then your function could be:那么您的 function 可能是:
std::array<uint8_t, 4> getCommandOf(eventType event) {
return { event, 0, 0, 0 };
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.