diff --git a/APL_Master/README.md b/APL_Master/README.md index f7a9d96..759e39a 100644 --- a/APL_Master/README.md +++ b/APL_Master/README.md @@ -1 +1,20 @@ -# Architektoniczne perełki Lublina - Master \ No newline at end of file +# Architektoniczne perełki Lublina - Master + +## I2C +Pins: + - SDA -> 21 + - SCL -> 22 + +Addresses: + - Slave 1 -> `0x08` + - Slave 2 -> `0x09` + - Slave 3 -> `0x0A` + - Slave 4 -> `0x0B` + - Slave 5 -> `0x0C` + +# LED config + +Led Type: WS2812B +Pin: 5 +Count: 29 +Color Order: GRB diff --git a/APL_Master/lib/Conts/Conts.h b/APL_Master/lib/Conts/Conts.h new file mode 100644 index 0000000..dda76d2 --- /dev/null +++ b/APL_Master/lib/Conts/Conts.h @@ -0,0 +1,28 @@ +// I2C pins +#define SDA_PIN 21 +#define SCL_PIN 22 + +// I2C slave addresses +#define SLAVE_ONE_ADDRESS 0x08 +#define SLAVE_TWO_ADDRESS 0x09 +#define SLAVE_THREE_ADDRESS 0x0A +#define SLAVE_FOUR_ADDRESS 0x0B +#define SLAVE_FIVE_ADDRESS 0x0C + +// Buttons pins +#define PLAYER_ONE_BUTTON_PIN 32 +#define PLAYER_ONE_A_BUTTON_PIN 33 +#define PLAYER_ONE_B_BUTTON_PIN 25 +#define PLAYER_ONE_C_BUTTON_PIN 26 + +#define PLAYER_TWO_BUTTON_PIN 27 +#define PLAYER_TWO_A_BUTTON_PIN 14 +#define PLAYER_TWO_B_BUTTON_PIN 12 +#define PLAYER_TWO_C_BUTTON_PIN 13 + +// LEDs pins and configuration +#define LED_DATA_PIN 5 +#define LED_COUNT 29 +#define BRIGHTNESS 64 +#define LED_TYPE WS2812B +#define COLOR_ORDER GRB \ No newline at end of file diff --git a/APL_Master/platformio.ini b/APL_Master/platformio.ini index 86818e0..e45e6ea 100644 --- a/APL_Master/platformio.ini +++ b/APL_Master/platformio.ini @@ -12,5 +12,6 @@ platform = espressif32 board = esp32dev framework = arduino - monitor_speed = 115200 + +lib_deps = fastled/FastLED@^3.10.3 diff --git a/APL_Master/src/main.cpp b/APL_Master/src/main.cpp index 37a1561..b197db0 100644 --- a/APL_Master/src/main.cpp +++ b/APL_Master/src/main.cpp @@ -1,11 +1,129 @@ #include +#include +#include +#include + +CRGB leds[LED_COUNT]; + +const CRGB SUCCESS_COLOR = CRGB::Green; +const CRGB ERROR_COLOR = CRGB::Red; + +void testLedStrip(); + +void initializeButtonInterrupts(); + +void playerOneButtonPressedInterrupt(); +void playerOneAButtonPressedInterrupt(); +void playerOneBButtonPressedInterrupt(); +void playerOneCButtonPressedInterrupt(); + +void playerTwoButtonPressedInterrupt(); +void playerTwoAButtonPressedInterrupt(); +void playerTwoBButtonPressedInterrupt(); +void playerTwoCButtonPressedInterrupt(); void setup() { + Wire.begin(SDA_PIN, SCL_PIN); Serial.begin(115200); + + // Initialize FastLED with the specified LED type, data pin, and LED count + FastLED.addLeds(leds, LED_COUNT).setCorrection(TypicalLEDStrip); + FastLED.setBrightness(BRIGHTNESS); + + // Initialize button interrupts + initializeButtonInterrupts(); + + testLedStrip(); } 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() +{ + // Test the LED strip by turning on each LED one by one + for (int i = 0; i < LED_COUNT; i++) + { + leds[i] = SUCCESS_COLOR; // Set the LED to green + FastLED.show(); + delay(100); // Wait for a short duration + leds[i] = CRGB::Black; // Turn off the LED + } +} + +void initializeButtonInterrupts() +{ + pinMode(PLAYER_ONE_BUTTON_PIN, INPUT_PULLUP); + pinMode(PLAYER_ONE_A_BUTTON_PIN, INPUT_PULLUP); + pinMode(PLAYER_ONE_B_BUTTON_PIN, INPUT_PULLUP); + pinMode(PLAYER_ONE_C_BUTTON_PIN, INPUT_PULLUP); + + pinMode(PLAYER_TWO_BUTTON_PIN, INPUT_PULLUP); + pinMode(PLAYER_TWO_A_BUTTON_PIN, INPUT_PULLUP); + pinMode(PLAYER_TWO_B_BUTTON_PIN, INPUT_PULLUP); + pinMode(PLAYER_TWO_C_BUTTON_PIN, INPUT_PULLUP); + + attachInterrupt(digitalPinToInterrupt(PLAYER_ONE_BUTTON_PIN), playerOneButtonPressedInterrupt, FALLING); + attachInterrupt(digitalPinToInterrupt(PLAYER_ONE_A_BUTTON_PIN), playerOneAButtonPressedInterrupt, FALLING); + attachInterrupt(digitalPinToInterrupt(PLAYER_ONE_B_BUTTON_PIN), playerOneBButtonPressedInterrupt, FALLING); + attachInterrupt(digitalPinToInterrupt(PLAYER_ONE_C_BUTTON_PIN), playerOneCButtonPressedInterrupt, FALLING); + + attachInterrupt(digitalPinToInterrupt(PLAYER_TWO_BUTTON_PIN), playerTwoButtonPressedInterrupt, FALLING); + attachInterrupt(digitalPinToInterrupt(PLAYER_TWO_A_BUTTON_PIN), playerTwoAButtonPressedInterrupt, FALLING); + attachInterrupt(digitalPinToInterrupt(PLAYER_TWO_B_BUTTON_PIN), playerTwoBButtonPressedInterrupt, FALLING); + attachInterrupt(digitalPinToInterrupt(PLAYER_TWO_C_BUTTON_PIN), playerTwoCButtonPressedInterrupt, FALLING); +} + +void playerOneButtonPressedInterrupt() +{ + // Handle Player One button press interrupt + Serial.println("Player One button pressed"); +} + +void playerOneAButtonPressedInterrupt() +{ + // Handle Player One A button press interrupt + Serial.println("Player One A button pressed"); +} + +void playerOneBButtonPressedInterrupt() +{ + // Handle Player One B button press interrupt + Serial.println("Player One B button pressed"); +} + +void playerOneCButtonPressedInterrupt() +{ + // Handle Player One C button press interrupt + Serial.println("Player One C button pressed"); +} + +void playerTwoButtonPressedInterrupt() +{ + // Handle Player Two button press interrupt + Serial.println("Player Two button pressed"); +} + +void playerTwoAButtonPressedInterrupt() +{ + // Handle Player Two A button press interrupt + Serial.println("Player Two A button pressed"); +} + +void playerTwoBButtonPressedInterrupt() +{ + // Handle Player Two B button press interrupt + Serial.println("Player Two B button pressed"); +} + +void playerTwoCButtonPressedInterrupt() +{ + // Handle Player Two C button press interrupt + Serial.println("Player Two C button pressed"); } diff --git a/APL_PlatformIO.code-workspace b/APL_PlatformIO.code-workspace new file mode 100644 index 0000000..e324da6 --- /dev/null +++ b/APL_PlatformIO.code-workspace @@ -0,0 +1,28 @@ +{ + "folders": [ + { + "path": "APL_Master" + }, + { + "name": "APL_Slave_1", + "path": "APL_Slave_1" + }, + { + "name": "APL_Slave_2", + "path": "APL_Slave_2" + }, + { + "name": "APL_Slave_3", + "path": "APL_Slave_3" + }, + { + "name": "APL_Slave_4", + "path": "APL_Slave_4" + }, + { + "name": "APL_Slave_5", + "path": "APL_Slave_5" + } +], + "settings": {} +} diff --git a/APL_Slave_1/README.md b/APL_Slave_1/README.md index 0a16a38..a59945c 100644 --- a/APL_Slave_1/README.md +++ b/APL_Slave_1/README.md @@ -1 +1,8 @@ # Architektoniczne perełki Lublina - Slave 1 + +## I2C +Pins: + - SDA -> 21 + - SCL -> 22 + +Address: 0x08 \ No newline at end of file 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 b1cef02..c874e11 100644 --- a/APL_Slave_1/src/main.cpp +++ b/APL_Slave_1/src/main.cpp @@ -1,10 +1,177 @@ #include +#include +#include +#include + +// I2C Pin definitions for ESP32 +#define SDA_PIN 21 +#define SCL_PIN 22 + +#define SLAVE_ADDRESS 0x08 + +// Count of MFRC522 readers connected to the ESP32 +#define READER_COUNT 6 + +// SPI pins for MFRC522 readers +#define MISO_PIN 19 +#define MOSI_PIN 23 +#define SCK_PIN 18 + +// Dedicated Slave Select (SS) pins for each reader +#define SS_ONE_PIN 14 +#define SS_TWO_PIN 32 +#define SS_THREE_PIN 4 +#define SS_FOUR_PIN 33 +#define SS_FIVE_PIN 25 +#define SS_SIX_PIN 26 + +// Dedicated reset pins for each reader +#define RST_ONE_PIN 27 +#define RST_TWO_PIN 13 +#define RST_THREE_PIN 2 +#define RST_FOUR_PIN 15 +#define RST_FIVE_PIN 12 +#define RST_SIX_PIN 17 + +// Definitions for card statuses +#define CARD_NOT_DETECTED 0 +#define CARD_OK 1 +#define CARD_WRONG 2 + +// Arrays for SS and RST 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}; +const byte RST_PINS[READER_COUNT] = {RST_ONE_PIN, RST_TWO_PIN, RST_THREE_PIN, RST_FOUR_PIN, RST_FIVE_PIN, RST_SIX_PIN}; + +// Addresses for each reader (for I2C communication) +const byte READER_ADDRESSES[READER_COUNT] = { + 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; + +// List of correct UIDs for each reader (4 bytes each) +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 - card not detected, 1 - OK, 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]; + +void requestEvent(); +void initializeReaders(); +bool compareUID(byte *readUID, const byte *correctUID, byte size); void setup() { Serial.begin(115200); + delay(1000); // Daj czas na zainicjalizowanie portu USB w komputerze + Serial.println("Starting ESP32..."); + + Wire.begin(SDA_PIN, SCL_PIN, SLAVE_ADDRESS); + Wire.onRequest(requestEvent); + + SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, -1); + Serial.println("SPI initialized successfully."); + + // SPI.setFrequency(1000000); // Set 1 MHz for whole SPI bus, but MFRC522 can handle up to 10 MHz. Adjust as needed. + // Serial.println("SPI set to 1 MHz"); + + initializeReaders(); + Serial.println("System initialized successfully. Waiting for card detection..."); } void loop() { + for (byte i = 0; i < READER_COUNT; i++) + { + // Check if a new card is present and read its UID + if (!mfrc522[i].PICC_IsNewCardPresent() || !mfrc522[i].PICC_ReadCardSerial()) + { + readerStatus[i] = CARD_NOT_DETECTED; // Reset reader status if no card is present + continue; + } + + // Card is detected, print its UID + Serial.print("Reader "); + Serial.print(i + 1); + Serial.print(" detected card with UID: "); + for (byte j = 0; j < mfrc522[i].uid.size; j++) + { + Serial.print(mfrc522[i].uid.uidByte[j], HEX); + if (j < mfrc522[i].uid.size - 1) + { + Serial.print(":"); + } + } + Serial.println(); + + // Check if the read UID matches the correct UID for this reader + if (compareUID(mfrc522[i].uid.uidByte, CORRECT_UIDS[i], mfrc522[i].uid.size)) + { + readerStatus[i] = CARD_OK; + } + else + { + readerStatus[i] = CARD_WRONG; + } + + // Close communication with the card and stop encryption on PCD + mfrc522[i].PICC_HaltA(); + mfrc522[i].PCD_StopCrypto1(); + } + + delay(100); } + +void initializeReaders() +{ + for (byte i = 0; i < READER_COUNT; i++) + { + pinMode(SS_PINS[i], OUTPUT); + digitalWrite(SS_PINS[i], HIGH); + + // Usage of the constructor with both SS and RST pins + mfrc522[i].PCD_Init(SS_PINS[i], RST_PINS[i]); + + Serial.print("Reader "); + Serial.print(i + 1); + Serial.print(" firmware version: "); + mfrc522[i].PCD_DumpVersionToSerial(); + + delay(100); + } +} + +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() +{ + 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/README.md b/APL_Slave_2/README.md index 20462a5..cdb1853 100644 --- a/APL_Slave_2/README.md +++ b/APL_Slave_2/README.md @@ -1 +1,8 @@ -# Architektoniczne perełki Lublina - Slave 2 \ No newline at end of file +# Architektoniczne perełki Lublina - Slave 2 + +## I2C +Pins: + - SDA -> 21 + - SCL -> 22 + +Address: 0x09 \ 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_2/src/main.cpp b/APL_Slave_2/src/main.cpp index 37a1561..9c79bed 100644 --- a/APL_Slave_2/src/main.cpp +++ b/APL_Slave_2/src/main.cpp @@ -1,11 +1,29 @@ #include +#include + +#define SDA_PIN 21 +#define SCL_PIN 22 + +#define SLAVE_ADDRESS 0x09 + +void requestEvent(); void setup() { Serial.begin(115200); + + Wire.begin(SDA_PIN, SCL_PIN, SLAVE_ADDRESS); // Initialize I2C with specified SDA and SCL pins + + Wire.onRequest(requestEvent); } void loop() { - // put your main code here, to run repeatedly: } + +void requestEvent() +{ + // TODO: Send data to the master when requested + Serial.println("Master requested data from Slave."); + Wire.write("Hello from Slave!"); +} \ No newline at end of file diff --git a/APL_Slave_3/README.md b/APL_Slave_3/README.md index b88f3af..ca39350 100644 --- a/APL_Slave_3/README.md +++ b/APL_Slave_3/README.md @@ -1 +1,8 @@ -# Architektoniczne perełki Lublina - Slave 3 \ No newline at end of file +# Architektoniczne perełki Lublina - Slave 3 + +## I2C +Pins: + - SDA -> 21 + - SCL -> 22 + +Address: 0x0A \ 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_3/src/main.cpp b/APL_Slave_3/src/main.cpp index f76fc99..510f019 100644 --- a/APL_Slave_3/src/main.cpp +++ b/APL_Slave_3/src/main.cpp @@ -1,11 +1,29 @@ #include +#include + +#define SDA_PIN 21 +#define SCL_PIN 22 + +#define SLAVE_ADDRESS 0x0A + +void requestEvent(); void setup() { Serial.begin(115200); + + Wire.begin(SDA_PIN, SCL_PIN, SLAVE_ADDRESS); // Initialize I2C with specified SDA and SCL pins + + Wire.onRequest(requestEvent); } void loop() { - // put your main code here, to run repeatedly: +} + +void requestEvent() +{ + // TODO: Send data to the master when requested + Serial.println("Master requested data from Slave."); + Wire.write("Hello from Slave!"); } \ No newline at end of file diff --git a/APL_Slave_4/README.md b/APL_Slave_4/README.md index a90f8e7..8fd5c9b 100644 --- a/APL_Slave_4/README.md +++ b/APL_Slave_4/README.md @@ -1 +1,8 @@ -# Architektoniczne perełki Lublina - Slave 4 \ No newline at end of file +# Architektoniczne perełki Lublina - Slave 4 + +## I2C +Pins: + - SDA -> 21 + - SCL -> 22 + +Address: 0x0B \ 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_4/src/main.cpp b/APL_Slave_4/src/main.cpp index b1cef02..b2b60bf 100644 --- a/APL_Slave_4/src/main.cpp +++ b/APL_Slave_4/src/main.cpp @@ -1,10 +1,29 @@ #include +#include + +#define SDA_PIN 21 +#define SCL_PIN 22 + +#define SLAVE_ADDRESS 0x0B + +void requestEvent(); void setup() { Serial.begin(115200); + + Wire.begin(SDA_PIN, SCL_PIN, SLAVE_ADDRESS); // Initialize I2C with specified SDA and SCL pins + + Wire.onRequest(requestEvent); } void loop() { } + +void requestEvent() +{ + // TODO: Send data to the master when requested + Serial.println("Master requested data from Slave."); + Wire.write("Hello from Slave!"); +} \ No newline at end of file diff --git a/APL_Slave_5/README.md b/APL_Slave_5/README.md index 44d036d..2299fc1 100644 --- a/APL_Slave_5/README.md +++ b/APL_Slave_5/README.md @@ -1 +1,8 @@ -# Architektoniczne perełki Lublina - Slave 5 \ No newline at end of file +# Architektoniczne perełki Lublina - Slave 5 + +## I2C +Pins: + - SDA -> 21 + - SCL -> 22 + +Address: 0x0C \ 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 diff --git a/APL_Slave_5/src/main.cpp b/APL_Slave_5/src/main.cpp index b1cef02..ef13d54 100644 --- a/APL_Slave_5/src/main.cpp +++ b/APL_Slave_5/src/main.cpp @@ -1,10 +1,29 @@ #include +#include + +#define SDA_PIN 21 +#define SCL_PIN 22 + +#define SLAVE_ADDRESS 0x0C + +void requestEvent(); void setup() { Serial.begin(115200); + + Wire.begin(SDA_PIN, SCL_PIN, SLAVE_ADDRESS); // Initialize I2C with specified SDA and SCL pins + + Wire.onRequest(requestEvent); } void loop() { } + +void requestEvent() +{ + // TODO: Send data to the master when requested + Serial.println("Master requested data from Slave."); + Wire.write("Hello from Slave!"); +} \ No newline at end of file diff --git a/README.md b/README.md index da4fe1d..2cc8bdc 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,17 @@ # Architektoniczne perełki Lublina +## Master device + +Insert for this device is in directory `API_Master`. This device communicates with PC. + +## Slave + +### Slave 1 + +### Slave 2 + +### Slave 3 + +### Slave 4 + +### Slave 5 \ No newline at end of file