-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
206 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,6 @@ struct entry_key{ | |
}; | ||
typedef struct entry_key *entry_key_t; | ||
|
||
|
||
/** | ||
* @brief Regist the key object | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
Oops, something went wrong.