From 58e6493ca0e0b37e9072973c511511d58f1ab0a6 Mon Sep 17 00:00:00 2001 From: Alija Bobija Date: Wed, 11 Sep 2024 12:16:14 +0200 Subject: [PATCH] UID as struct of bytes array and length --- examples/basic/main/basic.c | 5 +++-- examples/i2c/main/i2c.c | 5 +++-- include/rc522.h | 8 +++++++- src/rc522.c | 23 +++++------------------ 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/examples/basic/main/basic.c b/examples/basic/main/basic.c index eccec3e1..b0ce5e2e 100644 --- a/examples/basic/main/basic.c +++ b/examples/basic/main/basic.c @@ -1,5 +1,4 @@ #include -#include #include #include "rc522.h" @@ -67,8 +66,10 @@ static void rc522_event_handler(void *arg, esp_event_base_t base, int32_t event_ switch (event_id) { case RC522_EVENT_TAG_SCANNED: { + ESP_LOGI(TAG, "Tag scanned!"); + rc522_tag_t *tag = (rc522_tag_t *)data->ptr; - ESP_LOGI(TAG, "Tag scanned (UID: 0x%" PRIX64 ")", tag->uid); + ESP_LOG_BUFFER_HEX(TAG, tag->uid.bytes, tag->uid.length); } break; } } diff --git a/examples/i2c/main/i2c.c b/examples/i2c/main/i2c.c index f7a2b388..968d1a65 100644 --- a/examples/i2c/main/i2c.c +++ b/examples/i2c/main/i2c.c @@ -1,5 +1,4 @@ #include -#include #include #include "rc522.h" @@ -48,8 +47,10 @@ static void rc522_event_handler(void *arg, esp_event_base_t base, int32_t event_ switch (event_id) { case RC522_EVENT_TAG_SCANNED: { + ESP_LOGI(TAG, "Tag scanned!"); + rc522_tag_t *tag = (rc522_tag_t *)data->ptr; - ESP_LOGI(TAG, "Tag scanned (UID: 0x%" PRIX64 ")", tag->uid); + ESP_LOG_BUFFER_HEX(TAG, tag->uid.bytes, tag->uid.length); } break; } } diff --git a/include/rc522.h b/include/rc522.h index a81cd9ae..12cfe36b 100644 --- a/include/rc522.h +++ b/include/rc522.h @@ -41,7 +41,13 @@ typedef struct typedef struct { - uint64_t uid; + uint8_t *bytes; + uint8_t length; +} rc522_tag_uid_t; + +typedef struct +{ + rc522_tag_uid_t uid; } rc522_tag_t; /** diff --git a/src/rc522.c b/src/rc522.c index 5ccb2db6..1a6549a1 100644 --- a/src/rc522.c +++ b/src/rc522.c @@ -209,21 +209,6 @@ esp_err_t rc522_unregister_events(rc522_handle_t rc522, rc522_event_t event, esp return esp_event_handler_unregister_with(rc522->event_handle, RC522_EVENTS, event, event_handler); } -static uint64_t rc522_uid_to_u64(uint8_t *sn) -{ - uint64_t result = 0; - - if (!sn) { - return 0; - } - - for (int i = 4; i >= 0; i--) { - result |= ((uint64_t)sn[i] << (i * 8)); - } - - return result; -} - // Buffer should be length of 2, or more // Only first 2 elements will be used where the result will be stored // TODO: Use 2+ bytes data type instead of buffer array @@ -594,11 +579,13 @@ static void rc522_task(void *arg) rc522->tag_was_present_last_time = false; } else if (!rc522->tag_was_present_last_time) { - rc522_tag_t tag = { - .uid = rc522_uid_to_u64(uid_bytes), + rc522_tag_uid_t uid = { + .bytes = uid_bytes, + .length = 5, // TODO: Change once when we dynamicaly find the length of UID }; - FREE(uid_bytes); + rc522_tag_t tag = { .uid = uid }; rc522_dispatch_event(rc522, RC522_EVENT_TAG_SCANNED, &tag); + FREE(uid_bytes); rc522->tag_was_present_last_time = true; } else {