From a82a2e2b191b60f69ca905c75d257ec49142d706 Mon Sep 17 00:00:00 2001
From: Jimmy <me@jimmy.nz>
Date: Fri, 11 Dec 2020 15:57:10 +1300
Subject: [PATCH] Add bin folder. Tidy up main.cpp

---
 .gitignore    |  2 +-
 test/main.cpp | 84 +++++++++++++++++++++------------------------------
 2 files changed, 36 insertions(+), 50 deletions(-)

diff --git a/.gitignore b/.gitignore
index 662f6d5..7ab120d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,2 @@
 .vscode/
-bin/
+bin/*
diff --git a/test/main.cpp b/test/main.cpp
index f9c09a2..4f281d3 100644
--- a/test/main.cpp
+++ b/test/main.cpp
@@ -12,62 +12,48 @@ const int NRSTPD = 5;
 uchar serNum[5];
 uchar  writeDate[16] ={'T', 'e', 'n', 'g', ' ', 'B', 'o', 0, 0, 0, 0, 0, 0, 0, 0,0};
 
-uchar sectorKeyA[16][16] = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
-                             {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
-                             {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
-                            };
- uchar sectorNewKeyA[16][16] = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
-                                {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
-                                {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
-                               };
-
+uchar sectorKeyA[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 int main() {                
 	usart::init(1000000);
 	printf("Hello");
-   	MFRC522_Init();
+   	PORTC |= (1<<PC6)|(1<<PC7);
 	while(1) {
+		MFRC522_Init();
 		uchar i;
 		uchar status;
-			uchar str[MAX_LEN];
-			uchar RC_size;
-			uchar blockAddr;	//Select the address of the operation 0~63
+		uchar str[MAX_LEN];
+		uchar RC_size;
+		uchar blockAddr;	//Select the address of the operation 0~63
 
 
-			// searching card, return card type	
-			status = MFRC522_Request(PICC_REQIDL, str);	
-			if (status == MI_OK)
-			{
-			}
+		// searching card, return card type	
+		while(MFRC522_Request(PICC_REQIDL, str) != MI_OK);	
 
-			
-			status = MFRC522_Anticoll(str);
-			memcpy(serNum, str, 5);
-			if (status == MI_OK)
-			{
-				printf("The card's number is  : %x%x%x%x%x%x\n\r",
-					serNum[0],
-					serNum[1],
-					serNum[2],
-					serNum[3],
-					serNum[4]);
-			}
+		
+		status = MFRC522_Anticoll(str);
+		memcpy(serNum, str, 5);
+		if (status == MI_OK)
+		{
+			printf("The card's number is  : %x%x%x%x%x%x\n\r",
+				serNum[0],
+				serNum[1],
+				serNum[2],
+				serNum[3],
+				serNum[4]);
+		}
 
-			// select card, return card capacity
-			RC_size = MFRC522_SelectTag(serNum);
-			if (RC_size != 0)
-			{}
-					
+		// select card, return card capacity
+		RC_size = MFRC522_SelectTag(serNum);
+		if (RC_size != 0)
+		{}
+
+		if(PINC & (1<<PC6)) {		
 			// write data card
 			blockAddr = 7;		// data block 7		
-			status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[blockAddr/4], serNum);	// authentication 
+			status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA, serNum);	// authentication 
 			if (status == MI_OK)
 
 			{
-				// write data
-				status = MFRC522_Write(blockAddr, sectorNewKeyA[blockAddr/4]);
-				printf("set the new card password, and can modify the data of the Sector: ");
-				printf("%i", blockAddr/4);
-
 				// write data
 				blockAddr = blockAddr - 3 ; 
 				status = MFRC522_Write(blockAddr, writeDate);
@@ -76,29 +62,29 @@ int main() {
 					printf("OK!\n\r");
 				}
 			}
-
+		} else {
 			// read card
 			blockAddr = 7;		// data block 7		
-			status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, 
-
-	sectorNewKeyA[blockAddr/4], serNum);	// authentication 
+			status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA, serNum);	// authentication 
 			if (status == MI_OK)
 			{
 				// read data
-							blockAddr = blockAddr - 3 ; 
-							status = MFRC522_Read(blockAddr, str);
+				blockAddr = blockAddr - 3 ; 
+				status = MFRC522_Read(blockAddr, str);
 				if (status == MI_OK)
 				{
 					printf("Read from the card ,the data is : \n\r");
 					for (i=0; i<16; i++)
 					{
 						printf("%c", str[i]);
+						if(writeDate[i] !=  str[i]) printf("Not equal");
 					}
 						printf("\n\r");
 				}
 			}
-			printf("\n\r");
-			MFRC522_Halt();			// command card into sleeping mode              
+		}
+		printf("\n\r");
+		MFRC522_Halt();			// command card into sleeping mode              
 			
 	}