有没有一种比较这些数组的有效方法?

时间:2019-04-06 23:14:24

标签: arrays arduino byte arduino-c++

我有一台NFC扫描仪,该扫描仪获取标签的UID并将其存储在byte TagReadB[4]中。

然后我需要将其与大量其他字节数组进行比较,以查看其是否与它们匹配(即,是否为公认的标签)。

识别出的标签就是这样存储的。

uint8_t RED1[4] = {0x15, 0x4C, 0xA9, 0xFB};
uint8_t RED2[4] = {0x67, 0xFC, 0x8A, 0x16};
uint8_t RED3[4] = {0xB7, 0xC4, 0xEA, 0xA4};
uint8_t RED4[4] = {0xE7, 0x32, 0xE7, 0xA4};
uint8_t RED5[4] = {0x87, 0x33, 0xED, 0x74};
uint8_t RED6[4] = {0xB7, 0x56, 0xFA, 0xA4};
uint8_t RED7[4] = {0xB5, 0x8A, 0xB6, 0xFB};
uint8_t RED8[4] = {0x07, 0xA6, 0xF3, 0xA4};
uint8_t RED9[4] = {0xE5, 0xB2, 0x9E, 0xFB};
uint8_t RED10[4] = {0x87, 0xF8, 0x8A, 0x16};
uint8_t RED11[4] = {0x47, 0x93, 0xDA, 0xA4};
uint8_t RED12[4] = {0x87, 0x1D, 0x8A, 0x16};
uint8_t RED13[4] = {0x85, 0x83, 0xB1, 0xFB};
uint8_t RED14[4] = {0x55, 0x33, 0x9C, 0xFB};
uint8_t RED15[4] = {0x45, 0x39, 0xB7, 0xFB};
uint8_t RED16[4] = {0xA5, 0xED, 0xA5, 0xFB};
uint8_t RED17[4] = {0x85, 0x6E, 0xA1, 0xFB};
uint8_t RED18[4] = {0x17, 0xC3, 0xEF, 0xA4};
uint8_t RED19[4] = {0x37, 0xCA, 0xEB, 0xA4};
uint8_t RED20[4] = {0x15, 0x66, 0xA0, 0xFB};
uint8_t YELLOW1[4] = {0x27, 0x44, 0x73, 0xE5};
uint8_t YELLOW2[4] = {0xD7, 0x44, 0xCB, 0xE2};
uint8_t YELLOW3[4] = {0x87, 0x6E, 0x78, 0xE5};
uint8_t YELLOW4[4] = {0x27, 0x49, 0x63, 0xE5};
uint8_t YELLOW5[4] = {0xA7, 0x0D, 0x6C, 0xE5};
uint8_t YELLOW6[4] = {0x87, 0x96, 0x69, 0xE5};
uint8_t YELLOW7[4] = {0x37, 0xF9, 0xCA, 0xE2};
uint8_t YELLOW8[4] = {0xD7, 0xAE, 0xCB, 0xE2};
uint8_t YELLOW9[4] = {0xA7, 0xE3, 0x78, 0xE5};
uint8_t YELLOW10[4] = {0x47, 0x72, 0x90, 0xE5};
uint8_t BLUE1[4] = {0x73, 0xD5, 0xB7, 0xAC};
uint8_t BLUE2[4] = {0x1D, 0x5D, 0xFC, 0x69};
uint8_t BLUE3[4] = {0xBD, 0xEC, 0x8F, 0x12};
uint8_t BLUE4[4] = {0xE6, 0x9F, 0x54, 0x73};
uint8_t BLUE5[4] = {0x38, 0x74, 0x7E, 0x33};
uint8_t BLUE6[4] = {0x6D, 0x05, 0x3E, 0x12};
uint8_t BLUE7[4] = {0x9C, 0x6B, 0x4A, 0x73};

我可以执行以下操作,但是必须有一种更简单的方法,而不必重复20条OR语句?

if (!memcmp(TagReadB, RED1, 4) || !memcmp(TagReadB, RED2, 4) || ...)    {

        //do something

    }

更好的是,最好测试与之匹配的识别标签的颜色,因为每种颜色都有相同的作用。

例如如果是红色标签,则执行x,黄色标签则执行y,蓝色标签则执行z。

非常感谢。

2 个答案:

答案 0 :(得分:1)

您可以将数据扩展到

struct {uint8_t tag[4], uint8_t type}
data[] = {
   { {0x15, 0x4C, 0xA9, 0xFB}, RED}, 
// ...
   { {0xD7, 0xAE, 0xCB, 0xE2}, YELLOW },
// ...
};

使用

#define RED 1
// etc. 

(或const字节或枚举)

然后循环播放此列表,直到找到匹配项,然后返回结果data[i].type 或为“未找到”返回0

答案 1 :(得分:0)

您可以将标签存储在多维数组中:

uint8_t RED_TAGS[][4] = {
    {0x15, 0x4C, 0xA9, 0xFB},
    {0x67, 0xFC, 0x8A, 0x16},
    ...
};

uint8_t YELLOW_TAGS[][4] = {
    {0x15, 0x4C, 0xA9, 0xFB},
    {0x67, 0xFC, 0x8A, 0x16},
    ...
};

uint8_t BLUE_TAGS[][4] = {
    {0x15, 0x4C, 0xA9, 0xFB},
    {0x67, 0xFC, 0x8A, 0x16},
    ...
}

现在您可以使用for循环检查标记是否在多维数组之一中:

int is_tag_in_array(uint8_t array[][4], int size, uint8_t tag[4]) {
    for (int i=0;i<size;i++) {
        if (memcmp(array[i], tag, 4) == 0)
            return 1;
    }
    return 0;
}

这样,您可以像这样检查颜色:

if (is_tag_in_array(RED_TAGS, 20, TagReadB)) {
    // Do something for a red tag
} else if (is_tag_in_array(YELLOW_TAGS, 10, TagReadB)) {
    // Do something for a yellow tag
} else if (is_tag_in_array(BLUE_TAGS, 7, TagReadB) {
    // Do something for a blue tag
} else {
    // Didn't match any color
}