3rd year group project. Electronic and Electrical Engineering. Heriot-Watt University. This is the code for the mbed for the Automatic Little Object Organiser (ALOO).

Dependencies:   MCP23017 TCS3472_I2C WattBob_TextLCD mbed

Revision:
8:e1da2ae62885
Parent:
7:b6e31bfdb2af
Child:
9:dc8f155b71c8
--- a/main.cpp	Mon Nov 16 18:46:31 2015 +0000
+++ b/main.cpp	Mon Nov 16 22:20:11 2015 +0000
@@ -27,7 +27,8 @@
 
 TCS3472_I2C rgb_sensor(p28, p27);
 Serial      pc(USBTX, USBRX);
-uint8_t     rxBuffer[5 + 1];
+uint8_t     rxBuffer[kSmallBufferSize + 1];
+int 		rxIndex = 0;
 
 Commander 	_commander = Commander();
 Commander 	*commander = &_commander;
@@ -37,8 +38,6 @@
 extern Block _HazBlock;
 Block *HazBlock = &_HazBlock;
 
-bool connectedToPC = false;
-
 void detectPC();
 void initInternal();
 void initPort(int baudRate=kDefaultBaudRate);
@@ -106,7 +105,11 @@
 				if (aboardOperation == true) {
 					break;
 				}
+
 				// Cannot aboard any longer. Block is inserted.
+				// Detach rx interrupt until block processed.
+				NVIC_DisableIRQ(UART1_IRQn);
+
 				int blockSize = fpga->checkForSize();
 
 				if (blockSize == HazBlock->size) {
@@ -114,33 +117,38 @@
 					lcd->cls();
 					lcd->printf("Detecting Colour");
 				}
+
+				// Re-Attach rx interrupt
+				NVIC_EnableIRQ(UART1_IRQn);
 			}
 
 		} else if (selection == 2) {
-			// Wait for PC to send '!<pc>init;' command.
-			lcd->cls();
-			LCDFL();
-			lcd->printf("Waiting for PC...");
-			LCDSL();
-			lcd->printf("4: Main Menu");
-
-			int aboardOperation = false;
-			while (connectedToPC == false && aboardOperation == false){
-				aboardOperation = readSwitches() == 4;
-			}
-			if (aboardOperation == true) { break; }
-			else if (connectedToPC == true){
+			for (;;){
+				// Wait for PC to send '!<pc>connect;' command.
 				lcd->cls();
 				LCDFL();
-				lcd->printf("Connected to PC");
+				lcd->printf("Waiting for PC...");
 				LCDSL();
-				lcd->printf("4: Disconnect.");
-			}
+				lcd->printf("4: Main Menu");
 
-			while (aboardOperation == false){
-				aboardOperation = readSwitches() == 4;
+				int aboardOperation = false;
+				while (connectedToPC == false && aboardOperation == false){
+					aboardOperation = readSwitches() == 4;
+				}
+				if (aboardOperation == true) { break; }
+				else if (connectedToPC == true){
+					lcd->cls();
+					LCDFL();
+					lcd->printf("Connected to PC");
+					LCDSL();
+					lcd->printf("4: Disconnect.");
+				}
+
+				while (aboardOperation == false && connectedToPC == true){
+					aboardOperation = readSwitches() == 4;
+				}
+				if (aboardOperation == true ) { break; }
 			}
-			if (aboardOperation == true ) { break; }
 		}
 
 	}
@@ -148,7 +156,19 @@
 
 /// Called every-time it receives an char from PC.
 void Rx_interrupt(){
+	char interruptChar = pc.getc();
 
+	NVIC_DisableIRQ(UART1_IRQn);
+
+	if (interruptChar == CommandTypeValue[Query]){
+		commander->decodeCommand(Query);
+	}else if (interruptChar == CommandTypeValue[Set]){
+		commander->decodeCommand(Set);
+	}else if (interruptChar== CommandTypeValue[Reply]){
+		commander->decodeCommand(Reply);
+	}
+
+	NVIC_EnableIRQ(UART1_IRQn);
 }
 
 void initInternal()