简体   繁体   中英

Allocate a type to a uint8_t value

In my project I read the unique ID from an RFID tag, the result is in the form uint8_t TagRead[4] .

The result is compared with a number of predefined tag ID values to establish which tag has been read.

For example:

uint8_t RED1[4] = { 0x73, 0xD5, 0xB7, 0xAC };
uint8_t RED2[4] = { 0x7E, 0x27, 0x49, 0x4E };
uint8_t RED3[4] = { 0x02, 0xFD, 0x06, 0x40 };
uint8_t GREEN1[4] = { 0xAB, 0xEC, 0x68, 0x80 };
uint8_t GREEN2[4] = { 0xEE, 0x20, 0x50, 0x4E };
uint8_t GREEN3[4] = { 0x27, 0x06, 0x40, 0x73 };

if (*((uint32_t *)TagRead) == *((uint32_t *)RED2)) {
    // RED2 tag has been read
}

else if (*((uint32_t *)TagRead) == *((uint32_t *)GREEN3)) {
    // GREEN3 tag has been read
}

My question relates to being able to assign a type/category to a group of tags so that an action can be performed based on the colour of the tag that has been scanned.

It may be that when a RED tag is scanned we switch on a red LED and when a GREEN tag is scanned we switch on a blue LED.

Because there are approximately 50 tags of each colour, I don't want to to have to list all the tag names in the If statement. Instead, is it possible to assign the colour to the tag?

It would then be possible to do:

If scanned tag is of type RED, do red action. If scanned tag is of type GREEN do green action.

Thanks for your help.

First, your comparison is undefined behaviour. The right way to go is with a std::memcmp . You also need to take care of endianness.

In order to attach properties (like color) to your tags, simply define a struct:

struct rfid_tag
{
    uint8_t value[4];
    enum { ... } color;
};

Once you got a struct, you can enrich it with operator== so you can use std::find() to lookup the appropriate tag in one line:

#include <iostream>
#include <array>
#include <algorithm>
#include <cstring>

struct rfid_tag
{
    enum color_type { red = 10, blue = 11 };

    std::array<uint8_t, 4> value;
    color_type             color;
};

bool operator==(std::array<uint8_t, 4> const& tagvalue, rfid_tag const& rhs)
{
    return std::memcmp(tagvalue.data(), rhs.value.data(), rhs.value.size()) == 0;
}

bool operator==(rfid_tag const& lhs, std::array<uint8_t, 4> const& tagvalue)
{
    return tagvalue == lhs;
}


static const std::array<rfid_tag, 3> known_tags = {
    rfid_tag{ { 0x00, 0x01, 0x02, 0x03 }, rfid_tag::red },
    rfid_tag{ { 0x10, 0x11, 0x12, 0x13 }, rfid_tag::blue },
    rfid_tag{ { 0x20, 0x21, 0x22, 0x23 }, rfid_tag::red }
};

int main()
{
    const std::array<uint8_t, 4> tag_to_find{ 0x10, 0x11, 0x12, 0x13 };
    std::cout << std::find(begin(known_tags), end(known_tags), tag_to_find)->color << "\n"; // outputs "11" as expected
}

demo

You can create a structure with id and a color enum:

enum class Color { red, green };

struct Tag
{
    uint8_t id[4];
    Color color;
};

Tag RED1 = { { 0x73, 0xD5, 0xB7, 0xAC }, Color::red } ;
Tag RED2 = { { 0x7E, 0x27, 0x49, 0x4E }, Color::red } ;
Tag RED3 = { { 0x02, 0xFD, 0x06, 0x40 }, Color::red } ;
Tag GREEN1 = { { 0xAB, 0xEC, 0x68, 0x80 }, Color::green } ;
Tag GREEN2 = { { 0xEE, 0x20, 0x50, 0x4E }, Color::green } ;
Tag GREEN3 = { { 0x27, 0x06, 0x40, 0x73 }, Color::green } ;

void test(Tag tag)
{
    if (tag.color == Color::red)
    {
        //
    }
    else if (tag.color == Color::green)
    {

    }
}

There are multiple ways.

You could write a struct which contains your tag along with your color, like this:

struct ColoredTag
{
    uint8_t[4] value;
    std::string color;

} typename ColoredTag_t;

ColoredTag_t RED1 = {{ 0x73, 0xD5, 0xB7, 0xAC }, "Red"};
ColoredTag_t RED2 = {{ 0x7E, 0x27, 0x49, 0x4E }, "Red"};
ColoredTag_t RED3 = {{ 0x02, 0xFD, 0x06, 0x40  }, "Red"};
ColoredTag_t GREEN1 = {{ 0xAB, 0xEC, 0x68, 0x80 }, "Green"};
ColoredTag_t GREEN2 = {{ 0xEE, 0x20, 0x50, 0x4E }, "Green"};
ColoredTag_t GREEN3 = {{ 0x27, 0x06, 0x40, 0x73 }, "Green"};

Or, you can use a std::map to assign a color to a tag, like this

std map<uint8_t[4], std::string> tags;

public void fillTags()
{
    tags[RED1] = "Red";
    tags[RED2] = "Red";
    //...

}

std::string getColor(uint8_t tag)
{
    return tags[tag];

}

There might be some more solutions for this issue, but these are the ones that came to my mind first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM