Skip to content

Commit

Permalink
Example that uses multiple readers (#58)
Browse files Browse the repository at this point in the history
- Added example that uses 2 readers
- Use term 'reader' in other examples as well
  • Loading branch information
abobija authored Sep 22, 2024
1 parent 0657687 commit 8bfcaf5
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 50 deletions.
18 changes: 9 additions & 9 deletions examples/basic/main/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ static const char *TAG = "rc522-basic-example";
#define RC522_SPI_BUS_GPIO_MISO (25)
#define RC522_SPI_BUS_GPIO_MOSI (23)
#define RC522_SPI_BUS_GPIO_SCLK (19)
#define RC522_SPI_DEVICE_GPIO_SDA (22)
#define RC522_GPIO_RST (-1) // soft-reset
#define RC522_SPI_READER_GPIO_SDA (22)
#define RC522_READER_GPIO_RST (-1) // soft-reset

static rc522_spi_config_t driver_config = {
.host_id = VSPI_HOST,
Expand All @@ -19,13 +19,13 @@ static rc522_spi_config_t driver_config = {
.sclk_io_num = RC522_SPI_BUS_GPIO_SCLK,
},
.dev_config = {
.spics_io_num = RC522_SPI_DEVICE_GPIO_SDA,
.spics_io_num = RC522_SPI_READER_GPIO_SDA,
},
.rst_io_num = RC522_GPIO_RST,
.rst_io_num = RC522_READER_GPIO_RST,
};

static rc522_driver_handle_t driver;
static rc522_handle_t rc522;
static rc522_handle_t reader;

static void on_picc_state_changed(void *arg, esp_event_base_t base, int32_t event_id, void *data)
{
Expand All @@ -45,11 +45,11 @@ void app_main()
rc522_spi_create(&driver_config, &driver);
rc522_driver_install(driver);

rc522_config_t config = {
rc522_config_t reader_config = {
.driver = driver,
};

rc522_create(&config, &rc522);
rc522_register_events(rc522, RC522_EVENT_PICC_STATE_CHANGED, on_picc_state_changed, NULL);
rc522_start(rc522);
rc522_create(&reader_config, &reader);
rc522_register_events(reader, RC522_EVENT_PICC_STATE_CHANGED, on_picc_state_changed, NULL);
rc522_start(reader);
}
20 changes: 10 additions & 10 deletions examples/basic_i2c/main/basic_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

static const char *TAG = "rc522-basic-i2c-example";

#define RC522_I2C_ADDRESS (0x28)
#define RC522_I2C_GPIO_SDA (18)
#define RC522_I2C_GPIO_SCL (21)
#define RC522_GPIO_RST (-1) // soft-reset
#define RC522_I2C_ADDRESS (0x28)
#define RC522_I2C_GPIO_SDA (18)
#define RC522_I2C_GPIO_SCL (21)
#define RC522_READER_GPIO_RST (-1) // soft-reset

static rc522_i2c_config_t driver_config = {
.port = I2C_NUM_0,
Expand All @@ -22,11 +22,11 @@ static rc522_i2c_config_t driver_config = {
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000,
},
.rst_io_num = RC522_GPIO_RST,
.rst_io_num = RC522_READER_GPIO_RST,
};

static rc522_driver_handle_t driver;
static rc522_handle_t rc522;
static rc522_handle_t reader;

static void on_picc_state_changed(void *arg, esp_event_base_t base, int32_t event_id, void *data)
{
Expand All @@ -44,11 +44,11 @@ void app_main()
rc522_i2c_create(&driver_config, &driver);
rc522_driver_install(driver);

rc522_config_t config = {
rc522_config_t reader_config = {
.driver = driver,
};

rc522_create(&config, &rc522);
rc522_register_events(rc522, RC522_EVENT_PICC_STATE_CHANGED, on_picc_state_changed, NULL);
rc522_start(rc522);
rc522_create(&reader_config, &reader);
rc522_register_events(reader, RC522_EVENT_PICC_STATE_CHANGED, on_picc_state_changed, NULL);
rc522_start(reader);
}
30 changes: 15 additions & 15 deletions examples/memory_dump/main/memory_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ static const char *TAG = "rc522-memory-dump-example";
#define RC522_SPI_BUS_GPIO_MISO (25)
#define RC522_SPI_BUS_GPIO_MOSI (23)
#define RC522_SPI_BUS_GPIO_SCLK (19)
#define RC522_SPI_DEVICE_GPIO_SDA (22)
#define RC522_GPIO_RST (-1) // soft-reset
#define RC522_SPI_READER_GPIO_SDA (22)
#define RC522_READER_GPIO_RST (-1) // soft-reset

static rc522_spi_config_t driver_config = {
.host_id = VSPI_HOST,
Expand All @@ -21,13 +21,13 @@ static rc522_spi_config_t driver_config = {
.sclk_io_num = RC522_SPI_BUS_GPIO_SCLK,
},
.dev_config = {
.spics_io_num = RC522_SPI_DEVICE_GPIO_SDA,
.spics_io_num = RC522_SPI_READER_GPIO_SDA,
},
.rst_io_num = RC522_GPIO_RST,
.rst_io_num = RC522_READER_GPIO_RST,
};

static rc522_driver_handle_t driver;
static rc522_handle_t rc522;
static rc522_handle_t reader;

#define DUMP(format, ...) esp_log_write(ESP_LOG_INFO, TAG, format, ##__VA_ARGS__)

Expand Down Expand Up @@ -99,7 +99,7 @@ static void dump_block(rc522_mifare_sector_block_t *block, uint8_t sector_index)
DUMP("\n");
}

static esp_err_t dump_memory(rc522_handle_t rc522, rc522_picc_t *picc)
static esp_err_t dump_memory(rc522_handle_t reader, rc522_picc_t *picc)
{
rc522_mifare_key_t key = {
.value = { RC522_MIFARE_KEY_VALUE_DEFAULT },
Expand All @@ -117,10 +117,10 @@ static esp_err_t dump_memory(rc522_handle_t rc522, rc522_picc_t *picc)
do {
rc522_mifare_sector_desc_t sector;
ESP_RETURN_ON_ERROR(rc522_mifare_get_sector_desc(sector_index, &sector), TAG, "");
ESP_RETURN_ON_ERROR(rc522_mifare_auth_sector(rc522, picc, &sector, &key), TAG, "");
ESP_RETURN_ON_ERROR(rc522_mifare_auth_sector(reader, picc, &sector, &key), TAG, "");

rc522_mifare_sector_block_t trailer;
ESP_RETURN_ON_ERROR(rc522_mifare_read_sector_trailer_block(rc522, picc, &sector, &trailer), TAG, "");
ESP_RETURN_ON_ERROR(rc522_mifare_read_sector_trailer_block(reader, picc, &sector, &trailer), TAG, "");

dump_block(&trailer, sector_index);

Expand All @@ -129,7 +129,7 @@ static esp_err_t dump_memory(rc522_handle_t rc522, rc522_picc_t *picc)

do {
rc522_mifare_sector_block_t block;
ESP_RETURN_ON_ERROR(rc522_mifare_read_sector_block(rc522, picc, &sector, &trailer, block_offset, &block),
ESP_RETURN_ON_ERROR(rc522_mifare_read_sector_block(reader, picc, &sector, &trailer, block_offset, &block),
TAG,
"");

Expand Down Expand Up @@ -160,14 +160,14 @@ static void on_picc_state_changed(void *arg, esp_event_base_t base, int32_t even
return;
}

if (dump_memory(rc522, picc) == ESP_OK) {
if (dump_memory(reader, picc) == ESP_OK) {
ESP_LOGI(TAG, "Memory dump success");
}
else {
ESP_LOGE(TAG, "Memory dump failed");
}

if (rc522_mifare_deauth(rc522, picc) != ESP_OK) {
if (rc522_mifare_deauth(reader, picc) != ESP_OK) {
ESP_LOGW(TAG, "Deauth failed");
}
}
Expand All @@ -177,11 +177,11 @@ void app_main()
rc522_spi_create(&driver_config, &driver);
rc522_driver_install(driver);

rc522_config_t config = {
rc522_config_t reader_config = {
.driver = driver,
};

rc522_create(&config, &rc522);
rc522_register_events(rc522, RC522_EVENT_PICC_STATE_CHANGED, on_picc_state_changed, NULL);
rc522_start(rc522);
rc522_create(&reader_config, &reader);
rc522_register_events(reader, RC522_EVENT_PICC_STATE_CHANGED, on_picc_state_changed, NULL);
rc522_start(reader);
}
8 changes: 8 additions & 0 deletions examples/multiple_readers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(multiple_readers)
2 changes: 2 additions & 0 deletions examples/multiple_readers/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
idf_component_register(SRCS "multiple_readers.c"
INCLUDE_DIRS ".")
4 changes: 4 additions & 0 deletions examples/multiple_readers/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies:
abobija/rc522:
version: "*"
override_path: '../../../'
117 changes: 117 additions & 0 deletions examples/multiple_readers/main/multiple_readers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <esp_log.h>
#include "rc522.h"
#include "driver/rc522_spi.h"
#include "rc522_picc.h"

static const char *TAG = "rc522-multiple-readers-example";

// SPI bus GPIOs

#define RC522_SPI_BUS_GPIO_MISO (25)
#define RC522_SPI_BUS_GPIO_MOSI (23)
#define RC522_SPI_BUS_GPIO_SCLK (19)

// CS GPIOs for each reader

#define RC522_SPI_READER_1_GPIO_SDA (22)
#define RC522_SPI_READER_2_GPIO_SDA (26)

// {{ Reader configurations

static rc522_spi_config_t reader_1_config = {
.host_id = VSPI_HOST,
.bus_config = &(spi_bus_config_t){
.miso_io_num = RC522_SPI_BUS_GPIO_MISO,
.mosi_io_num = RC522_SPI_BUS_GPIO_MOSI,
.sclk_io_num = RC522_SPI_BUS_GPIO_SCLK,
},
.dev_config = {
.spics_io_num = RC522_SPI_READER_1_GPIO_SDA,
},
.rst_io_num = -1, // soft-reset
};

// Second reader does not need bus configuration,
// since first reader will initialize the bus.

static rc522_spi_config_t reader_2_config = {
.host_id = VSPI_HOST,
.dev_config = {
.spics_io_num = RC522_SPI_READER_2_GPIO_SDA,
},
.rst_io_num = -1, // soft-reset
};

// }}

// Drivers

static rc522_driver_handle_t driver_1;
static rc522_driver_handle_t driver_2;

// Readers

static rc522_handle_t reader_1;
static rc522_handle_t reader_2;

// Event handler

static void on_picc_state_changed(void *arg, esp_event_base_t base, int32_t event_id, void *data)
{
rc522_handle_t reader = (rc522_handle_t)arg;
rc522_picc_state_changed_event_t *event = (rc522_picc_state_changed_event_t *)data;
rc522_picc_t *picc = event->picc;

uint8_t reader_no = 1;

if (reader == reader_2) {
reader_no = 2;
}

if (picc->state == RC522_PICC_STATE_ACTIVE) {
ESP_LOGI(TAG, "Card detected on the scanner #%d", reader_no);
rc522_picc_print(picc);
}
else if (picc->state == RC522_PICC_STATE_IDLE && event->old_state >= RC522_PICC_STATE_ACTIVE) {
ESP_LOGI(TAG, "Card has been removed from the scanner #%d", reader_no);
}
}

// App entry point

void app_main()
{
// Create drivers

rc522_spi_create(&reader_1_config, &driver_1);
rc522_spi_create(&reader_2_config, &driver_2);

// Install drivers

rc522_driver_install(driver_1);
rc522_driver_install(driver_2);

// Create readers

rc522_create(
&(rc522_config_t) {
.driver = driver_1,
},
&reader_1);

rc522_create(
&(rc522_config_t) {
.driver = driver_2,
},
&reader_2);

// Register events for each reader

rc522_register_events(reader_1, RC522_EVENT_PICC_STATE_CHANGED, on_picc_state_changed, reader_1);
rc522_register_events(reader_2, RC522_EVENT_PICC_STATE_CHANGED, on_picc_state_changed, reader_2);

// Start readers

rc522_start(reader_1);
rc522_start(reader_2);
}
Loading

0 comments on commit 8bfcaf5

Please sign in to comment.