/***********************************************************************************
/***********************************************************************************
//
/*
* Programme: Simulated chess board for arduino mega-2560
* Description: Le code ci-dessous est un code servat de simulation de prise de donner
* sur le positionnement de pion du un échequier 8x8
* Author: Mohamed Marou Belko
* Cadre Acadéique : Génie électrique Polytechnique Montréal
* Date: 13 Mars 2023
* Inspiration du code :
* 74HC165 Shift register input example
* pau: Uri Shaked, https://wokwi.com/arduino/projects/306031380875182657
*
*
*------------------------------------------------------------------------------------------
* //***** Representation des 64 cases de l'échéquier *********************************
* 0- |07|06|05|04|03|02|01|00|
* 1- |15|14|13|12|11|10|09|08|
* 2- |23|22|21|20|10|18|17|16|
* 3- |31|30|29|28|27|26|25|24|
* 4- |39|38|37|36|35|34|33|32|
* 5- |47|46|45|44|43|43|41|40|
* 6- |55|54|53|52|51|50|49|48|
* 7- |63|63|61|60|59|58|57|56|
-------------------------
7 6 5 4 3 2 1 0
//****** Representation corrigé par invertion de bit( invert(y)) et ('h'-x ) *****************
* 8- |07|06|05|04|03|02|01|00|
* 7- |15|14|13|12|11|10|09|08|
* 6- |23|22|21|20|10|18|17|16|
* 5- |31|30|29|28|27|26|25|24|
* 4- |39|38|37|36|35|34|33|32|
* 3- |47|46|45|44|43|43|41|40|
* 2- |55|54|53|52|51|50|49|48|
* 1- |63|63|61|60|59|58|57|56|
-------------------------
a b c d e f g h
*------------------------------------------------------------------------------------
*/
//*********************************************************************************///
const byte latchPin = 9; // Signal d'envoie des bit dans la bascule
const byte clockPin[2] = {13,10}; // pin 13 et 10 fourmissent le signal d'horloge
const byte dataPin[2] = {12,8}; // pin 12 et 8 utiliser pour lire les bits provenant des bascule 74HC165
// pour eviter d'utiliser des variables de uint64_t (64 bits) j'ai subdiviser les 64
// bits en deux matrice de 32 bits
uint32_t etatAnterieur[2]={0xFFFFFFFF,0xFFFFFFFF}; // previous state of all the input
const int t_Balayage = 10; // 1/10ms --> 100HZ
/*bool expectedMatrix[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};*/
// Tableau des coordonnée de l'échequier
String board[8][8] = {
{"a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8"},
{"a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7"},
{"a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6"},
{"a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5"},
{"a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4"},
{"a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3"},
{"a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2"},
{"a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"}};
/* Somme pour verifier que un état à la fois est modifier
Si la somme anterieur est est >=2 --> deux piece on été lever (erreur à implementer)
Si la somme
*/
int sommeAnterieur = 0;
int somme=0;
void setup ()
{
Serial.begin( 115200);
Serial.println( "Turn on and off the switches");
Serial.println( "Top row is switch 0 (right) to switch 7 (left)");
Serial.println( "Second row is 8 to 15, and so on");
pinMode( clockPin[0], OUTPUT); // clock signal, idle LOW
pinMode( latchPin, OUTPUT); // latch (copy input into registers), idle HIGH
pinMode( clockPin[1], OUTPUT); // clock signal, idle LOW
//digitalWrite( latchPin, HIGH);
// pulsion d'entrée des de donnés dans la bascule 74HC165
digitalWrite( latchPin, LOW);
delayMicroseconds(t_Balayage);
digitalWrite( latchPin, HIGH);
uint32_t etatCase[2]={0x0,0x0};
bool matrix1[8][8];
bool matrix2[8][8];
buildInputMatrices(etatCase,matrix1,matrix2,etatAnterieur);
sommeAnterieur = sumBooleanMatrix(matrix1); // call the function to get the sum of elements
Serial.print("Sum of elements: ");
Serial.println(sommeAnterieur);
caseState(matrix1,matrix2);
for ( int j=0; j<2; j++) etatAnterieur[j] = etatCase[j];
}
void loop ()
{
// pulsion d'entrée des de donnés dans la bascule 74HC165
digitalWrite( latchPin, LOW);
delayMicroseconds(t_Balayage);
digitalWrite( latchPin, HIGH);
//*****************************************************************************//
//*************** Fonction de lecture et d'enregistrement des états **********//
//*****************************************************************************//
uint32_t etatCase[2]={0x0,0x0};
bool matrix1[8][8];
bool matrix2[8][8];
buildInputMatrices(etatCase,matrix1,matrix2,etatAnterieur);
somme = sumBooleanMatrix(matrix1);
caseState(matrix1,matrix2);
for ( int j=0; j<2; j++) etatAnterieur[j] = etatCase[j];
//*****************************************************************************//
//**** Fontion associant chaque bit à une coordonnée ********//
//*****************************************************************************//
uint32_t num_low[2] = {etatCase[0] & 0xFFFFFFFF, etatCase[1] & 0xFFFFFFFF};
uint32_t num_high[2] = {etatCase[0] >> 32, etatCase[1] >> 32};
/* Serial.println("Enter the x coordinate (0-7): ");
while (!Serial.available()) {} // Wait for user input
int x = Serial.parseInt(); // Read user input for x coordinate
Serial.read(); // Discard newline character
Serial.println("Enter the y coordinate (0-7): ");
while (!Serial.available()) {} // Wait for user input
int y = Serial.parseInt(); // Read user input for y coordinate
Serial.read(); // Discard newline character
if (x >= 0 && x < 8 && y >= 0 && y < 8) { // Check if coordinates are within range
int bit_index = y * 8 + x; // Compute bit index from x and y coordinates
bool bit = ((bit_index < 32) ? (num_low[0] >> bit_index) : (num_low[1] >> (bit_index - 32))) & 1; // Extract the bit value
Serial.print("Bit value at (");
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.print("): ");
Serial.println(bit);
}
else {
Serial.println("Invalid coordinates.");
}
delay(1000); // Delay for 1 second before repeating the loop*/
//*********************************************************************************************
//********** Fonction associant une coordonnée (x,y) un valeur alphanum
//*********************************************************************************************
/* byte x, y;
char col;
Serial.println("Enter x coordinate (0-7): ");
while (!Serial.available());
x = Serial.parseInt();
Serial.read(); // Discard newline character
Serial.println("Enter y coordinate (0-7): ");
while (!Serial.available());
y = Serial.parseInt();
Serial.read(); // Discard newline character
if (x >= 0 && x < 8 && y >= 0 && y < 8) { // Check if coordinates are within range
int bit_index = y * 8 + x; // Compute bit index from x and y coordinates
bool bit = ((bit_index < 32) ? (num_low[0] >> bit_index) : (num_low[1] >> (bit_index - 32))) & 1; // Extract the bit value
col = x+'a';
Serial.print(col);
Serial.print(invert(y)+1);
Serial.println();
}
else {
Serial.println("Invalid coordinates.");
}
delay(1000); // Delay for 1 second before repeating the loop */
}
byte invert(byte num) {
return ~num & 0b00000111;
}
//************************ Fonctions: **************************************************//
byte readMatrix(const byte clock, const byte data)
{
byte ret = 0x00;
// The first one that is read is the highest bit (input D7 of the 74HC165).
for( int i=7; i>=0; i--)
{
if( digitalRead(data) == HIGH)
bitSet( ret, i);
digitalWrite( clock, HIGH);
delayMicroseconds( t_Balayage);
digitalWrite( clock, LOW);
}
return( ret);
}
void bitsToMatrix(uint32_t bits1, uint32_t bits2, bool matrix[][8]) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
int bitIndex = i * 8 + j;
bool bitValue;
if (bitIndex < 32) {
bitValue = ((bits1 >> (31 - bitIndex)) & 1);
} else {
bitValue = ((bits2 >> (31 - (bitIndex - 32))) & 1);
}
matrix[i][j] = bitValue;
}
}
}
bool compareMatrices(bool matrix1[][8], bool matrix2[][8]) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (matrix1[i][j] != matrix2[i][j]) {
return false;
}
}
}
return true;
}
void printMatrix(bool matrix[][8]) {
Serial.println();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
Serial.print(matrix[i][j]);
Serial.print(" ");
}
Serial.println();
}
}
void caseState(bool matrix1[8][8], bool matrix2[8][8]){
for( int i = 0; i<8; i++)
{
for ( int j=0; j<8; j++)
{
if( matrix1[i][j] != matrix2[i][j] )
{
Serial.print( "Case");
if( i < 10) Serial.print( " ");
Serial.print(board[i][j]);
Serial.print( " est maintenant ");
Serial.println( matrix1[i][j] == 0 ? " Occupé " : "vide");
}
}
}
}
void buildInputMatrices(uint32_t etatCase[2],bool matrix1[8][8],bool matrix2[8][8],uint32_t etatAnterieur[2]){
for ( int j=0; j<2; j++)
{
for( int i=24; i>=0; i-=8)
etatCase[j] |= ((uint32_t) readMatrix(clockPin[j],dataPin[j])) << i;
}
bitsToMatrix(etatCase[0], etatCase[1], matrix1);
bitsToMatrix(etatAnterieur[0], etatAnterieur[1], matrix2);
//bool isEqual = compareMatrices(matrix1, expectedMatrix);
}
int sumBooleanMatrix(bool matrix[8][8]) {
int sum = 0; // initialize sum variable to zero
// loop through each element of the matrix and add it to the sum
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
sum += matrix[i][j];
}
}
return sum; // return the sum of the elements
}