将枚举类型转换为C++

发布于 2025-01-20 05:11:08 字数 1089 浏览 1 评论 0原文

编写返回8位六角值数组的函数的最佳和最短方法是基于枚举参数分配的。

我需要硬代码命令,该命令将通过蓝牙发送,并且仍然可以轻松地使用代码访问它们。因此,我有一个以人类可读方式列出的所有可能命令的枚举。

enum eventType {
    MODIFY_A,
    MODIFY_B,
    MODIFY_C,
    MODIFY_D
}

我尝试调整的通信协议使用具有预定义十六进制值的自定义数据包。因此,例如,发送0x00、0x01、0x02、0x03可以更改接收设备的LED颜色。

用于BLE通信的库接受所述hexes或其他单词的数组,只是一个普通的c字符串。

声明之后,简单地在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;
}

然后我可以简单地调用: sendoverble(getCommandof(eventType :: modify_a));

不幸的是,我们不能在C ++中这样做。 我愿意接受建议。您将如何解决?尽管我有一些解决方案,但我不喜欢他们中的任何一个。

也许在2D数组中定义它们?还是自定义容器类?

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.

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.

The library used for ble communication accepts arrays of said hexes, or other words, just a plain C string.

It would be easiest to simply define an array in a switch() after declaring it, just like this:

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));

Unfortunately, we can't do that in C++.
I'm open to suggestions. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温折酒 2025-01-27 05:11:08

我建议使用并返回 std :: array 。 将您的功能更改为

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");
}

如果您需要将此数组传递到采用uint8_t*的函数,则可以 >成员可以得到这样的指针

my_uint8_t_ptr_func(getCommandOf(eventType::MODIFY_A).data());

I would suggest using and returning a std::array. That would change your function to

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

my_uint8_t_ptr_func(getCommandOf(eventType::MODIFY_A).data());
故乡的云 2025-01-27 05:11:08

如果您完全控制您的enum,您可以将其定义如下:

enum eventType : uint8_t {
    MODIFY_A = 1,
    MODIFY_B,
    MODIFY_C,
    MODIFY_D
}

那么您的函数可以是:

std::array<uint8_t, 4> getCommandOf(eventType event) {
    return { event, 0, 0, 0 };
}

If you're in complete control over your enum you could define it as follows:

enum eventType : uint8_t {
    MODIFY_A = 1,
    MODIFY_B,
    MODIFY_C,
    MODIFY_D
}

Then your function could be:

std::array<uint8_t, 4> getCommandOf(eventType event) {
    return { event, 0, 0, 0 };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文