WIP: implement device logic for game #1

Draft
k.pietrak wants to merge 7 commits from feature/add-main-functionalities into develop
21 changed files with 508 additions and 13 deletions
+20 -1
View File
@@ -1 +1,20 @@
# Architektoniczne perełki Lublina - Master # 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
+28
View File
@@ -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
+2 -1
View File
@@ -12,5 +12,6 @@
platform = espressif32 platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = fastled/FastLED@^3.10.3
+118
View File
@@ -1,11 +1,129 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h>
#include <FastLED.h>
#include <Conts.h>
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() void setup()
{ {
Wire.begin(SDA_PIN, SCL_PIN);
Serial.begin(115200); Serial.begin(115200);
// Initialize FastLED with the specified LED type, data pin, and LED count
FastLED.addLeds<LED_TYPE, LED_DATA_PIN, COLOR_ORDER>(leds, LED_COUNT).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
// Initialize button interrupts
initializeButtonInterrupts();
testLedStrip();
} }
void loop() void loop()
{ {
// put your main code here, to run repeatedly: // 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");
} }
+28
View File
@@ -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": {}
}
+7
View File
@@ -1 +1,8 @@
# Architektoniczne perełki Lublina - Slave 1 # Architektoniczne perełki Lublina - Slave 1
## I2C
Pins:
- SDA -> 21
- SCL -> 22
Address: 0x08
+3 -1
View File
@@ -13,4 +13,6 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = miguelbalboa/MFRC522@^1.4.12
+167
View File
@@ -1,10 +1,177 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h>
#include <MFRC522.h>
#include <SPI.h>
// 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() void setup()
{ {
Serial.begin(115200); 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() 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);
}
+8 -1
View File
@@ -1 +1,8 @@
# Architektoniczne perełki Lublina - Slave 2 # Architektoniczne perełki Lublina - Slave 2
## I2C
Pins:
- SDA -> 21
- SCL -> 22
Address: 0x09
+3 -1
View File
@@ -13,4 +13,6 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = miguelbalboa/MFRC522@^1.4.12
+19 -1
View File
@@ -1,11 +1,29 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h>
#define SDA_PIN 21
#define SCL_PIN 22
#define SLAVE_ADDRESS 0x09
void requestEvent();
void setup() void setup()
{ {
Serial.begin(115200); 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 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!");
}
+8 -1
View File
@@ -1 +1,8 @@
# Architektoniczne perełki Lublina - Slave 3 # Architektoniczne perełki Lublina - Slave 3
## I2C
Pins:
- SDA -> 21
- SCL -> 22
Address: 0x0A
+3 -1
View File
@@ -13,4 +13,6 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = miguelbalboa/MFRC522@^1.4.12
+19 -1
View File
@@ -1,11 +1,29 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h>
#define SDA_PIN 21
#define SCL_PIN 22
#define SLAVE_ADDRESS 0x0A
void requestEvent();
void setup() void setup()
{ {
Serial.begin(115200); 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 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!");
} }
+8 -1
View File
@@ -1 +1,8 @@
# Architektoniczne perełki Lublina - Slave 4 # Architektoniczne perełki Lublina - Slave 4
## I2C
Pins:
- SDA -> 21
- SCL -> 22
Address: 0x0B
+3 -1
View File
@@ -13,4 +13,6 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = miguelbalboa/MFRC522@^1.4.12
+19
View File
@@ -1,10 +1,29 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h>
#define SDA_PIN 21
#define SCL_PIN 22
#define SLAVE_ADDRESS 0x0B
void requestEvent();
void setup() void setup()
{ {
Serial.begin(115200); 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 loop()
{ {
} }
void requestEvent()
{
// TODO: Send data to the master when requested
Serial.println("Master requested data from Slave.");
Wire.write("Hello from Slave!");
}
+8 -1
View File
@@ -1 +1,8 @@
# Architektoniczne perełki Lublina - Slave 5 # Architektoniczne perełki Lublina - Slave 5
## I2C
Pins:
- SDA -> 21
- SCL -> 22
Address: 0x0C
+3 -1
View File
@@ -13,4 +13,6 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = miguelbalboa/MFRC522@^1.4.12
+19
View File
@@ -1,10 +1,29 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h>
#define SDA_PIN 21
#define SCL_PIN 22
#define SLAVE_ADDRESS 0x0C
void requestEvent();
void setup() void setup()
{ {
Serial.begin(115200); 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 loop()
{ {
} }
void requestEvent()
{
// TODO: Send data to the master when requested
Serial.println("Master requested data from Slave.");
Wire.write("Hello from Slave!");
}
+15
View File
@@ -1,2 +1,17 @@
# Architektoniczne perełki Lublina # 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