Skip to content

Commit

Permalink
UID as struct of bytes array and length
Browse files Browse the repository at this point in the history
  • Loading branch information
abobija committed Sep 11, 2024
1 parent 8b41e98 commit 58e6493
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
5 changes: 3 additions & 2 deletions examples/basic/main/basic.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <esp_log.h>
#include <inttypes.h>
#include <driver/spi_master.h>
#include "rc522.h"

Expand Down Expand Up @@ -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;
}
}
Expand Down
5 changes: 3 additions & 2 deletions examples/i2c/main/i2c.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <esp_log.h>
#include <inttypes.h>
#include <driver/i2c.h>
#include "rc522.h"

Expand Down Expand Up @@ -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;
}
}
Expand Down
8 changes: 7 additions & 1 deletion include/rc522.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
23 changes: 5 additions & 18 deletions src/rc522.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 58e6493

Please sign in to comment.