Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Revision:
53:71f59e195f06
Parent:
52:99915f5240b2
Child:
54:a4c5949707ca
--- a/main.cpp	Tue Jan 09 22:27:49 2018 +0000
+++ b/main.cpp	Mon May 07 15:44:34 2018 +0000
@@ -1,11 +1,11 @@
-/*
-ELEC 351 Group T
-Team Members : Christopher Hills, Thomas Morris
-Christopher Student Number : 10467175 
-Thomas Student Number : 10427070
-*/
+
 #include "SETUP.hpp"
 
+int SPI_RX_DATA = 0;
+int Cubelet_Colours[9] = {0,0,0,0,0,0,0,0,0};//9 cubelets store colours here
+int Received_data = 0;
+bool Data_from_slave[16] = {0};
+
 //Interrupt service routine for handling the timeout of SW1
 void SW1TimeOutHandler() {
     SW1TimeOut.detach();        //Stop the timeout counter firing
@@ -26,28 +26,11 @@
     SW2.fall(NULL);                             //Disable this interrupt
     SW2TimeOut.attach(&SW2TimeOutHandler, SW1_SW2_Timeout_Time); //Start timeout counter    
 }
-void SD_Card()                              //Writes data to the SD card
-{
-    while(1)
-    {
-        Thread::signal_wait(SD_CARD_WRITE); //Wait till the SD Card Signal is set
-        Thread::wait(Sample_Rate*1000);     //Waits until a new is taken before checking again
-        SD_Card_Write();                    //Write Data to the SD Card
-    }
-}
-void Network()
-{
-    while(1)
-    {
-        Thread::wait(NetworkWaitTime);      //Waits Network Wait amount of time
-        Networking();                       //Write to the Network
-    }
-}
 void LCD_Output()
 {
     while(1)
     {
-        LCD_Print_Output();                 //Writes to the LCD
+        //Write to the LCD
     }
 }
 void Serial_Commands()
@@ -64,28 +47,89 @@
        Log_Leds();                          //Flashes the yellow led to indicate the logging mode
     }
 }
-void Sample()
+
+void SPI_INTERFACE()
+{
+    //pc.printf("SPI Test \n");
+    Thread::wait(1000);
+    while(1)
+    {
+        //Do stuff
+        cs= 0;
+        SPI_RX_DATA = spi.write(0xCB);
+        wait_us(3);
+        cs= 1;
+        //SPI_RX_DATA = SPI_RX_DATA >> 7;
+        //SPI_RX_DATA = SPI_RX_DATA << 1;
+        //pc.printf("Received data : %x\n",SPI_RX_DATA);
+
+        std::bitset<16> bits(SPI_RX_DATA);
+        cout << bits << endl;
+        pc.printf("Received data = %d\n", SPI_RX_DATA);
+        //wait_us(3);
+         Thread::wait(1000);
+     
+    }   
+}
+void Motor_Control()
 {
-    Sample_Event();                         //Run Sample Event
+    while(1)
+    {
+        if(Motor_To_Select !=0 and steps !=0)
+        {
+            if(Motor_To_Select == 1)
+            {
+                STEPPER_MOTOR_1.Rotate_Steps(steps ,direction);
+                //Motor_To_Select = 0;
+            }
+            else if(Motor_To_Select == 2)
+            {
+                STEPPER_MOTOR_2.Rotate_Steps(steps ,direction);
+                //Motor_To_Select = 0;
+            }
+             else if(Motor_To_Select == 3)
+            {
+                STEPPER_MOTOR_3.Rotate_Steps(steps ,direction);
+                //Motor_To_Select = 0;
+            }
+             else if(Motor_To_Select == 4)
+            {
+                STEPPER_MOTOR_4.Rotate_Steps(steps ,direction);
+                //Motor_To_Select = 0;
+            }
+             else if(Motor_To_Select == 5)
+            {
+                STEPPER_MOTOR_5.Rotate_Steps(steps ,direction);
+                //Motor_To_Select = 0;
+            }
+            
+             else if(Motor_To_Select == 6)
+            {
+                STEPPER_MOTOR_6.Rotate_Steps(steps ,direction);
+                //Motor_To_Select = 0;
+            }
+            
+        }
+    }
 }
+
 int main()
 {
     set_time(1515530152);                                   //Sets time
     pc.baud(9600);                                          //Sets the Serial Comms Baud Rate
-    LCD.Initialise();                                       //Initialise the LCD 
-    LCD.DDRAM_Address(0x00);                                //Set the LCD string entry point to top left
-    post();                                                 //Power on Self Test
-    Sample_Rate = TimerInterval;                            //Define Sample Rate
-    Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);       //Start the sampler
+    //SPI_INIT();
+    cs = 1; //Active Low
+
+    // Setup the spi for 8 bit data, high steady state clock,
+    // second edge capture, with a 1MHz clock rate
+    spi.format(16,1); // 8 Data bits phase 0 polarity 0
+    spi.frequency(1000000);//Output clock frequency 1Mhz
+    //post();                                                 //Power on Self Test
 
     //Start Threads
-    t1.start(Sample);
-    t2.start(SD_Card);
-    t3.start(LCD_Output);
-    t4.start(Network);
-    t5.start(Serial_Commands);
-    t6.start(LED_Logging);
-    
+    t1.start(Motor_Control);
+    t2.start(SPI_INTERFACE);
+    t3.start(Serial_Commands);
     //Interrupts
     SW1.fall(&SW1FallingEdge);
     SW2.fall(&SW2FallingEdge);
@@ -93,6 +137,7 @@
     //Main thread ID
     idMain = osThreadGetId();   //CMSIS RTOS call
 
+
     //Thread ID
     id1 = t1.gettid();
     id2 = t2.gettid();
@@ -100,34 +145,9 @@
     id4 = t4.gettid();
     id5 = t5.gettid();
     id6 = t6.gettid();
-
+    
     while(true) 
     {
-        if(SD_CARD_DETECT.read() == 1)              //Check if SD Card has been manually ejected
-        {
-            SD_CARD_PRESENT = 0;                    //Sets the SD card to not present
-            t2.signal_set(DONT_WRITE_TO_SD_CARD);   //Stop writing to SD Card
-            errorCode(SD_CARD_REMOVED);             //Throws an error code
-        } 
-        else if(SD_CARD_DETECT.read() == 0)         //Check if SD Card is present
-        {
-            if(SD_CARD_PRESENT == 0)                //Checks if the SD card is set to present
-            {
-                SD_Init();//                        //Inits the SD card if it was not already initialised
-                SD_CARD_PRESENT = 1;                //Makes SD Card Present 1 to prevent the SD init from running over and over
-            }
-            t2.signal_set(SD_CARD_WRITE);           //Enables the SD card to Write
-        }
-        else
-        {
-            t2.signal_set(DONT_WRITE_TO_SD_CARD);   //Else Dont Write to SD Card an Error has occured   
-        }
-        if(onBoardSwitch == 1)
-        {   
-            SD_CARD_PRESENT = 0;                    //Sets the SD card to not present
-            t2.signal_set(DONT_WRITE_TO_SD_CARD);   //Stop writing to SD Card
-            SD_Card_Eject();                        //Ejects the SD Card
-            Green_led.switchOn();                   //Turns on the green led 
-        }
+        //Do nothing main thread will sleep        
     }
 }
\ No newline at end of file