Skip to content

Commit

Permalink
[keyobj] add rc522 key obj
Browse files Browse the repository at this point in the history
  • Loading branch information
greedyhao committed Feb 1, 2020
1 parent 2af2ef8 commit 3b5c668
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 87 deletions.
1 change: 1 addition & 0 deletions software/applications/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ src = Split("""
main.c
entry_key.c
reg_key_fp.c
reg_key_rc.c
""")

group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH)
Expand Down
1 change: 0 additions & 1 deletion software/applications/entry_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ struct entry_key{
};
typedef struct entry_key *entry_key_t;


/**
* @brief Regist the key object
*
Expand Down
102 changes: 102 additions & 0 deletions software/applications/reg_key_rc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "entry_key.h"
#include "mfrc522.h" // rc522 packages

#define EVENT_SCAN_START (1 << 0)
#define EVENT_SCAN_END (1 << 1)

static Uid *uid;
static rt_event_t rc_evt = RT_NULL;
static uint32_t tmp_id = 0;

static struct entry_key en_key_rc = {0};
static uint32_t rc_id[KEY_ID_MAX] = {0};

static void rc_add_key(uint16_t id)
{
uint32_t rec = 0;
if (id > KEY_ID_MAX)
{
rt_kprintf("id out of range!\n");
return;
}
rt_event_send(rc_evt, EVENT_SCAN_START);
rt_event_recv(rc_evt, EVENT_SCAN_END, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &rec);
rc_id[id] = tmp_id;
}

static void rc_del_key(uint16_t id)
{
rc_id[id] = 0;
}

static uint16_t rc_ver_key(void)
{
uint32_t rec = 0;
uint16_t i = 0;
rt_event_send(rc_evt, EVENT_SCAN_START);
rt_event_recv(rc_evt, EVENT_SCAN_END, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &rec);
for (i = 0; i < KEY_ID_MAX; i++)
{
if (rc_id[i] == tmp_id)
return i;
}
return KEY_VER_ERROR;
}

static uint8_t rc_has_key(uint16_t id)
{
return (rc_id[id] != 0) ? 1 : 0;
}

static void rc_init_thread(void *param)
{
uint32_t rec = 0;
MFRC522(MFRC522_SS_PIN, MFRC522_RST_PIN);
PCD_Init(); // Init MFRC522
rt_thread_mdelay(4);
uid = get_uid();

while (1)
{
rt_event_recv(rc_evt, EVENT_SCAN_START, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &rec);
while (1)
{
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. And if present, select one.
if ( ! PICC_IsNewCardPresent() || ! PICC_ReadCardSerial())
{
rt_thread_mdelay(50);
continue;
}

// // Dump debug info about the card; PICC_HaltA() is automatically called
// PICC_DumpToSerial(uid);
tmp_id = uid->uidByte[0] << 24 + uid->uidByte[1] << 16 + uid->uidByte[2] << 8 + uid->uidByte[3]; // change uid format form 8bit to 32bit
rt_event_send(rc_evt, EVENT_SCAN_END);
break;
}
}

// PCD_End();
}

static int rt_hw_rc_port(void)
{
en_key_rc.add_key = rc_add_key;
en_key_rc.del_key = rc_del_key;
en_key_rc.ver_key = rc_ver_key;
en_key_rc.has_key = rc_has_key;
reg_key_obj(ENTRY_KEY_FP, &en_key_rc);

rc_evt = rt_event_create("rck-evt", RT_IPC_FLAG_FIFO);
if (RT_NULL == rc_evt)
rt_kprintf("create rc_evt failed!\n");

rt_thread_t rc_tid = RT_NULL;
rc_tid = rt_thread_create("rck-tid", rc_init_thread, RT_NULL, 1024, 15, 10);
if (rc_tid != RT_NULL)
rt_thread_startup(rc_tid);

return 0;
}
INIT_COMPONENT_EXPORT(rt_hw_rc_port);

Loading

0 comments on commit 3b5c668

Please sign in to comment.