From a3cfa44d82b6132c61223da0a8b908e789fa9a29 Mon Sep 17 00:00:00 2001 From: SWETRAK Date: Fri, 3 Jul 2026 23:50:58 +0200 Subject: [PATCH] feat: implemented simple logic to detect cards and send information to master via I2C --- APL_Master/README.md | 9 ++- APL_Master/src/main.cpp | 3 + APL_Slave_1/platformio.ini | 4 +- APL_Slave_1/src/main.cpp | 115 ++++++++++++++++++++++++++++++++++++- APL_Slave_2/platformio.ini | 4 +- APL_Slave_3/platformio.ini | 4 +- APL_Slave_4/platformio.ini | 4 +- APL_Slave_5/platformio.ini | 4 +- 8 files changed, 138 insertions(+), 9 deletions(-) diff --git a/APL_Master/README.md b/APL_Master/README.md index 66ab445..759e39a 100644 --- a/APL_Master/README.md +++ b/APL_Master/README.md @@ -10,4 +10,11 @@ Addresses: - Slave 2 -> `0x09` - Slave 3 -> `0x0A` - Slave 4 -> `0x0B` - - Slave 5 -> `0x0C` \ No newline at end of file + - Slave 5 -> `0x0C` + +# LED config + +Led Type: WS2812B +Pin: 5 +Count: 29 +Color Order: GRB diff --git a/APL_Master/src/main.cpp b/APL_Master/src/main.cpp index 9feee3d..3ff2a1f 100644 --- a/APL_Master/src/main.cpp +++ b/APL_Master/src/main.cpp @@ -38,6 +38,9 @@ void setup() void loop() { // put your main code here, to run repeatedly: + // TODO: Implement the main loop logic for I2C communication with slave devices + // TODO: Implement the logic to control the LED strip based on the received data from slave devices + // TODO: Send informations to PC via Serial communication, needs to be implemented based on the previous code snippets. } void testLedStrip() diff --git a/APL_Slave_1/platformio.ini b/APL_Slave_1/platformio.ini index 7d842a1..2283aef 100644 --- a/APL_Slave_1/platformio.ini +++ b/APL_Slave_1/platformio.ini @@ -13,4 +13,6 @@ platform = espressif32 board = esp32dev framework = arduino -monitor_speed = 115200 \ No newline at end of file +monitor_speed = 115200 + +lib_deps = miguelbalboa/MFRC522@^1.4.12 diff --git a/APL_Slave_1/src/main.cpp b/APL_Slave_1/src/main.cpp index 895c624..cadbf52 100644 --- a/APL_Slave_1/src/main.cpp +++ b/APL_Slave_1/src/main.cpp @@ -1,13 +1,64 @@ #include #include +#include #define SDA_PIN 21 #define SCL_PIN 22 #define SLAVE_ADDRESS 0x08 +// Card reader pins +#define READER_COUNT 6 + +#define RST_PIN 9 + +#define SS_ONE_PIN 10 +#define SS_TWO_PIN 5 +#define SS_THREE_PIN 4 +#define SS_FOUR_PIN 2 +#define SS_FIVE_PIN 15 +#define SS_SIX_PIN 13 + +// Card status definitions +#define CARD_NOT_DETECTED 0 +#define CARD_OK 1 +#define CARD_WRONG 2 + +// Slave select pins for each reader +const byte SS_PINS[READER_COUNT] = {SS_ONE_PIN, SS_TWO_PIN, SS_THREE_PIN, SS_FOUR_PIN, SS_FIVE_PIN, SS_SIX_PIN}; + +// I2C addresses for each reader +// TODO: Update the addresses to specific setup +const byte READER_ADDRESSES[READER_COUNT] = { + 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; + +// List of proper cards uuid's +const byte CORRECT_UIDS[READER_COUNT][4] = { + {0xDE, 0xAD, 0xBE, 0xEF}, // Reader 1 + {0xCA, 0xFE, 0xBA, 0xBE}, // Reader 2 + {0xFA, 0xCE, 0xB0, 0x0C}, // Reader 3 + {0xBA, 0xAD, 0xF0, 0x0D}, // Reader 4 + {0xFE, 0xED, 0xFA, 0xCE}, // Reader 5 + {0xC0, 0xFF, 0xEE, 0x00} // Reader 6 +}; + +// Reader statuses: 0 - not detected, 1 - okey, 2 - wrong card +volatile byte readerStatus[READER_COUNT] = { + CARD_NOT_DETECTED, + CARD_NOT_DETECTED, + CARD_NOT_DETECTED, + CARD_NOT_DETECTED, + CARD_NOT_DETECTED, + CARD_NOT_DETECTED}; + +MFRC522 mfrc522[READER_COUNT]; // Create MFRC522 instances for each reader + void requestEvent(); +void initializeReaders(); + +bool compareUID(byte *readUID, const byte *correctUID, byte size); + void setup() { Serial.begin(115200); @@ -15,15 +66,73 @@ void setup() Wire.begin(SDA_PIN, SCL_PIN, SLAVE_ADDRESS); // Initialize I2C with specified SDA and SCL pins Wire.onRequest(requestEvent); + + initializeReaders(); } void loop() { + for (byte i = 0; i < READER_COUNT; i++) + { + if (!mfrc522[i].PICC_IsNewCardPresent() || !mfrc522[i].PICC_ReadCardSerial()) + { + MFRC522::StatusCode buffer_status; + byte buffer_atqa[2]; + byte buffer_size = sizeof(buffer_atqa); + + buffer_status = mfrc522[i].PICC_WakeupA(buffer_atqa, &buffer_size); + + if (buffer_status != MFRC522::STATUS_OK) + { + readerStatus[i] = CARD_NOT_DETECTED; + } + continue; + } + + if (compareUID(mfrc522[i].uid.uidByte, CORRECT_UIDS[i], mfrc522[i].uid.size)) + { + readerStatus[i] = CARD_OK; + } + else + { + readerStatus[i] = CARD_WRONG; + } + + mfrc522[i].PICC_HaltA(); + mfrc522[i].PCD_StopCrypto1(); + } + + delay(50); +} + +void initializeReaders() +{ + for (byte i = 0; i < READER_COUNT; i++) + { + mfrc522[i].PCD_Init(SS_PINS[i], RST_PIN); + } +} + +bool compareUID(byte *readUID, const byte *correctUID, byte size) +{ + for (byte i = 0; i < size; i++) + { + if (readUID[i] != correctUID[i]) + { + return false; + } + } + return true; } void requestEvent() { - // TODO: Send data to the master when requested - Serial.println("Master requested data from Slave."); - Wire.write("Hello from Slave!"); + byte outputData[READER_COUNT]; + + for (byte i = 0; i < READER_COUNT; i++) + { + outputData[i] = READER_ADDRESSES[i] + readerStatus[i]; + } + + Wire.write(outputData, READER_COUNT); } \ No newline at end of file diff --git a/APL_Slave_2/platformio.ini b/APL_Slave_2/platformio.ini index 7d842a1..2f6d349 100644 --- a/APL_Slave_2/platformio.ini +++ b/APL_Slave_2/platformio.ini @@ -13,4 +13,6 @@ platform = espressif32 board = esp32dev framework = arduino -monitor_speed = 115200 \ No newline at end of file +monitor_speed = 115200 + +lib_deps = miguelbalboa/MFRC522@^1.4.12 \ No newline at end of file diff --git a/APL_Slave_3/platformio.ini b/APL_Slave_3/platformio.ini index 7d842a1..2f6d349 100644 --- a/APL_Slave_3/platformio.ini +++ b/APL_Slave_3/platformio.ini @@ -13,4 +13,6 @@ platform = espressif32 board = esp32dev framework = arduino -monitor_speed = 115200 \ No newline at end of file +monitor_speed = 115200 + +lib_deps = miguelbalboa/MFRC522@^1.4.12 \ No newline at end of file diff --git a/APL_Slave_4/platformio.ini b/APL_Slave_4/platformio.ini index 7d842a1..2f6d349 100644 --- a/APL_Slave_4/platformio.ini +++ b/APL_Slave_4/platformio.ini @@ -13,4 +13,6 @@ platform = espressif32 board = esp32dev framework = arduino -monitor_speed = 115200 \ No newline at end of file +monitor_speed = 115200 + +lib_deps = miguelbalboa/MFRC522@^1.4.12 \ No newline at end of file diff --git a/APL_Slave_5/platformio.ini b/APL_Slave_5/platformio.ini index 7d842a1..2f6d349 100644 --- a/APL_Slave_5/platformio.ini +++ b/APL_Slave_5/platformio.ini @@ -13,4 +13,6 @@ platform = espressif32 board = esp32dev framework = arduino -monitor_speed = 115200 \ No newline at end of file +monitor_speed = 115200 + +lib_deps = miguelbalboa/MFRC522@^1.4.12 \ No newline at end of file