-
Hello, community. I am using the "wiegand" example to handle the reader and it works fine, but I would like to connect multiple readers to one ESP32 controller. I tried to just create another task, having previously edited the "task" function a little so that it receives arguments indicating pins:
But it obviously doesn't work. Is it possible to use the driver in multitasking mode? |
Beta Was this translation helpful? Give feedback.
Answered by
UncleRus
Feb 3, 2023
Replies: 1 comment 2 replies
-
Try something like this: #define READERS_COUNT 4 // for example
static QueueHandle_t queue = NULL;
// Single data packet
typedef struct
{
int reader_idx;
uint8_t data[BUF_SIZE];
size_t bits;
} data_packet_t;
// struct to hold reader description
typedef struct {
gpio_num_t d0, d1;
wiegand_order_t bit_order, byte_order;
} reader_descr_t;
// array to describe all readers
// replace GPIO_NUM_xx with real GPIOs
static const reader_descr_t readers_descr[READERS_COUNT] = {
{ .d0 = GPIO_NUM_xx, .d1 = GPIO_NUM_xx, .bit_order = WIEGAND_MSB_FIRST, .byte_order = WIEGAND_LSB_FIRST },
{ .d0 = GPIO_NUM_xx, .d1 = GPIO_NUM_xx, .bit_order = WIEGAND_MSB_FIRST, .byte_order = WIEGAND_LSB_FIRST },
{ .d0 = GPIO_NUM_xx, .d1 = GPIO_NUM_xx, .bit_order = WIEGAND_MSB_FIRST, .byte_order = WIEGAND_LSB_FIRST },
{ .d0 = GPIO_NUM_xx, .d1 = GPIO_NUM_xx, .bit_order = WIEGAND_MSB_FIRST, .byte_order = WIEGAND_LSB_FIRST },
};
// array for descriptors
static wiegand_reader_t readers[READERS_COUNT] = { 0 };
// single callback for all readers
static void callback(wiegand_reader_t *r)
{
// create simple undecoded data packet
data_packet_t p;
p.reader_idx = -1;
for (size_t i = 0; i < READERS_COUNT; i++)
if (r == readers_descr + i)
{
p.reader_idx = i;
break;
}
p.bits = r->bits;
memcpy(p.data, r->buf, CONFIG_EXAMPLE_BUF_SIZE);
// Send it to the queue
xQueueSendToBack(queue, &p, 0);
}
// initialize readers
static void init_readers()
{
for (size_t i = 0, i < READERS_COUNT; i++)
{
esp_err_t res = wiegand_reader_init(readers + i, readers_descr[i].d0, readers_descr[i].d1, true, BUF_SIZE, callback, readers_descr[i].bit_order, readers_descr[i].byte_order);
if (res != ESP_OK)
ESP_LOGE(TAG, "Error while initializing wiegand reader");
}
}
static void task(void *arg)
{
// Create queue
queue = xQueueCreate(5, sizeof(data_packet_t));
if (!queue)
{
ESP_LOGE(TAG, "Error creating queue");
ESP_ERROR_CHECK(ESP_ERR_NO_MEM);
}
// Initialize readers
init_readers();
data_packet_t p;
while (1)
{
ESP_LOGI(TAG, "Waiting for Wiegand data...");
xQueueReceive(queue, &p, portMAX_DELAY);
// dump received data
printf("==========================================\n");
printf("Reader: %d\n", p.reader_idx);
printf("Bits received: %d\n", p.bits);
printf("Received data:");
int bytes = p.bits / 8;
int tail = p.bits % 8;
for (size_t i = 0; i < bytes + (tail ? 1 : 0); i++)
printf(" 0x%02x", p.data[i]);
printf("\n==========================================\n");
}
}
void app_main()
{
xTaskCreate(task, TAG, configMINIMAL_STACK_SIZE * 4, NULL, 5, NULL);
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
UncleRus
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try something like this: