WIP: implement device logic for game #1

Draft
k.pietrak wants to merge 7 commits from feature/add-main-functionalities into develop
3 changed files with 92 additions and 60 deletions
Showing only changes of commit 0a34467ccb - Show all commits
+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
+1 -27
View File
@@ -1,33 +1,7 @@
#include <Arduino.h>
#include <Wire.h>
#include <FastLED.h>
#define SDA_PIN 21
#define SCL_PIN 22
#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
#include <Conts.h>
CRGB leds[LED_COUNT];
+63 -33
View File
@@ -3,42 +3,50 @@
#include <MFRC522.h>
#include <SPI.h>
// I2C Pin definitions for ESP32
#define SDA_PIN 21
#define SCL_PIN 22
#define SLAVE_ADDRESS 0x08
// Card reader pins
// 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
#define RST_PIN 27
#define SS_ONE_PIN 26
#define SS_TWO_PIN 5
// 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 2
#define SS_FIVE_PIN 15
#define SS_SIX_PIN 13
#define SS_FOUR_PIN 33
#define SS_FIVE_PIN 25
#define SS_SIX_PIN 26
// Card status definitions
// 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
// Slave select pins for each reader
// 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};
// I2C addresses for each reader
// TODO: Update the addresses to specific setup
// Addresses for each reader (for I2C communication)
const byte READER_ADDRESSES[READER_COUNT] = {
0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
// List of proper cards uuid's
// TODO: Update this UUID's to the specific cards that will be used
// 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
@@ -48,7 +56,7 @@ const byte CORRECT_UIDS[READER_COUNT][4] = {
{0xC0, 0xFF, 0xEE, 0x00} // Reader 6
};
// Reader statuses: 0 - not detected, 1 - okey, 2 - wrong card
// Reader statuses: 0 - card not detected, 1 - OK, 2 - wrong card
volatile byte readerStatus[READER_COUNT] = {
CARD_NOT_DETECTED,
CARD_NOT_DETECTED,
@@ -57,47 +65,57 @@ volatile byte readerStatus[READER_COUNT] = {
CARD_NOT_DETECTED,
CARD_NOT_DETECTED};
MFRC522 mfrc522[READER_COUNT]; // Create MFRC522 instances for each reader
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); // Initialize I2C with specified SDA and SCL pins
Wire.begin(SDA_PIN, SCL_PIN, SLAVE_ADDRESS);
Wire.onRequest(requestEvent);
// Initialize SPI with specified pins
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())
{
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;
}
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;
@@ -107,18 +125,30 @@ void loop()
readerStatus[i] = CARD_WRONG;
}
// Close communication with the card and stop encryption on PCD
mfrc522[i].PICC_HaltA();
mfrc522[i].PCD_StopCrypto1();
}
delay(50);
delay(100);
}
void initializeReaders()
{
for (byte i = 0; i < READER_COUNT; i++)
{
mfrc522[i].PCD_Init(SS_PINS[i], RST_PIN);
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);
}
}