Plymouth ELEC351 Group T
/
ELEC351_Group_T
FINAL PROJECT isn't it
Fork of ELEC351 by
Diff: main.cpp
- Revision:
- 41:859b5e1e3d9a
- Parent:
- 39:5c499989d2b9
- Child:
- 42:ae1640bca2e1
--- a/main.cpp Sun Jan 07 23:01:38 2018 +0000 +++ b/main.cpp Mon Jan 08 14:20:30 2018 +0000 @@ -12,7 +12,6 @@ #include "SETUP.hpp" #include "NETWORK.hpp" -#include "FIFO.hpp" #define Do_Read_Data 1 #define Dont_Read_Data 0 @@ -22,50 +21,84 @@ Mutex Time_Lock_Main; Mutex Data_Buffer_Lock; -int Write_Data(DATA Data_Store, int Write_Pointer) +int Write_Data(DATA Data_Store, int Write_Pointer)//Writes data passed in to the array at address Write Pointer { - Data_Buffer_Lock.lock();//Mutex Lock - + Data_Buffer_Lock.lock();//Appling lock for critial section Data_Buffer[Write_Pointer] = Data_Store; - if(Write_Pointer < mailsize - 1) + if(Write_Pointer < mailsize - 1)//Checks if write pointer will be greater than mail size { - Write_Pointer = Write_Pointer + 1; + Write_Pointer = Write_Pointer + 1;//Increment write pointer } else { - Write_Pointer = 0; + Write_Pointer = 0;//Else set it to 0 thus a FIFO system } - Data_Buffer_Lock.unlock(); - return Write_Pointer; + Data_Buffer_Lock.unlock();//Releasing lock for critial section + return Write_Pointer;//New address of Write pointer } DATA Read_Data(int Read_Pointer) { - Data_Buffer_Lock.lock(); + Data_Buffer_Lock.lock();//Appling lock for critial section DATA Temp_Data = Data_Buffer[Read_Pointer]; - Data_Buffer_Lock.unlock(); + Data_Buffer_Lock.unlock();//Releasing lock for critial section return Temp_Data; } void Delete_Data(int Delete_Pointer) { Thread::signal_wait(Do_Delete_Data); - Data_Buffer_Lock.lock(); + Data_Buffer_Lock.lock();//Appling lock for critial section Data_Buffer[Delete_Pointer].set_all_zero(); - Data_Buffer_Lock.unlock(); + Data_Buffer_Lock.unlock();//Releasing lock for critial section +} + +void SD_Card()//Writes data to the SD card +{ + while(1) + { + Thread::signal_wait(SD_Data_Ready); //Waits till the sampler has data avaliable + if(Log_Value==1){cout << "In SD_Card Thread" << endl;} + + //Open to WRITE + FILE* fp = fopen("/sd/test.csv","a"); + time_t Time = Data_Active.get_time(); + tm* Time_Pointer = localtime(&Time); + int temp_day = Time_Pointer->tm_mday; + int temp_month = (Time_Pointer->tm_mon+1);//Set to current month + int temp_year = (Time_Pointer->tm_year+1900);//Set to current year + + int temp_hours = Time_Pointer->tm_hour; + int temp_minute = Time_Pointer->tm_min; + + float temp_temperature = Data_Active.get_temperature(); + float temp_pressure = Data_Active.get_pressure(); + float temp_light = Data_Active.get_light(); + + fprintf(fp, "Date: %d/%d/%d,",temp_day,temp_month,temp_year);//Date + fprintf(fp, "Time: %d:%d,",temp_hours,temp_minute);//Time + fprintf(fp, "Temperature: %1.1f,",temp_temperature);//Temperature + fprintf(fp, "Pressure: %1.1f,",temp_pressure);//Pressure + fprintf(fp, "Light: %5.3f,",temp_light);//Light + fprintf(fp, "End of Data \n");//End of data + + //Close File + fclose(fp); + //dump all Data_Buffer[mailsize]; + } } void Network() { while(1) { - Thread::wait(5000);//Waits 5 seconds + Thread::wait(NetworkWait);//Waits Network Wait amount of time if(Log_Value==1){cout << "In Network Thread" << endl;} //Log this time_t Time = Data_Active.get_time(); tm* Time_Pointer = localtime(&Time); int temp_day = Time_Pointer->tm_mday; - int temp_month = (Time_Pointer->tm_mon+1); - int temp_year = (Time_Pointer->tm_year+1900); + int temp_month = (Time_Pointer->tm_mon+1);//Set to current month + int temp_year = (Time_Pointer->tm_year+1900);//Set to current year int temp_hours = Time_Pointer->tm_hour; int temp_minute = Time_Pointer->tm_min; @@ -75,17 +108,17 @@ float temp_light = Data_Active.get_light(); - Networking(temp_day,temp_month,temp_year,temp_hours,temp_minute,temp_temperature,temp_pressure,temp_light); + Networking(temp_day,temp_month,temp_year,temp_hours,temp_minute,temp_temperature,temp_pressure,temp_light);//Pass in networking data and time values } } void LCD_Write_Year() { - Time_Lock_Main.lock(); + Time_Lock_Main.lock();//Appling lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Year Time lock taken"<<endl;} time_t Time = time(NULL); tm* Time_Pointer = localtime(&Time); int Years = 1900 + Time_Pointer->tm_year; - Time_Lock_Main.unlock(); + Time_Lock_Main.unlock();//Releasing lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Year Time lock released"<<endl;} stringstream ss; ss << Years; @@ -95,15 +128,14 @@ LCD.DDRAM_Address(0x40); LCD.Write_String(Year_String); } - void LCD_Write_Month() { - Time_Lock_Main.lock(); + Time_Lock_Main.lock();//Appling lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Month Time lock taken"<<endl;} time_t Time = time(NULL); tm* Time_Pointer = localtime(&Time); int Months = 1 + Time_Pointer->tm_mon; - Time_Lock_Main.unlock(); + Time_Lock_Main.unlock();//Releasing lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Month Time lock released"<<endl;} stringstream ss; ss << Months; @@ -115,12 +147,12 @@ } void LCD_Write_Day() { - Time_Lock_Main.lock(); + Time_Lock_Main.lock();//Appling lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Day Time lock taken"<<endl;} time_t Time = time(NULL); tm* Time_Pointer = localtime(&Time); int Days = Time_Pointer->tm_mday; - Time_Lock_Main.unlock(); + Time_Lock_Main.unlock();//Releasing lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Day Time lock released"<<endl;} stringstream ss; ss << Days; @@ -130,16 +162,14 @@ LCD.DDRAM_Address(0x40); LCD.Write_String(Day_String); } - - void LCD_Write_Hour() { - Time_Lock_Main.lock(); + Time_Lock_Main.lock();//Appling lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Hour Time lock taken"<<endl;} time_t Time = time(NULL); tm* Time_Pointer = localtime(&Time); int Hours = Time_Pointer->tm_hour; - Time_Lock_Main.unlock(); + Time_Lock_Main.unlock();//Releasing lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Hour Time lock released"<<endl;} stringstream ss; ss << Hours; @@ -151,12 +181,12 @@ } void LCD_Write_Minute() { - Time_Lock_Main.lock(); + Time_Lock_Main.lock();//Appling lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Minute Time lock taken"<<endl;} time_t Time = time(NULL); tm* Time_Pointer = localtime(&Time); int Minutes = Time_Pointer->tm_min; - Time_Lock_Main.unlock(); + Time_Lock_Main.unlock();//Releasing lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Minute Time lock released"<<endl;} stringstream ss; ss << Minutes; @@ -168,12 +198,12 @@ } void LCD_Write_Seconds() { - Time_Lock_Main.lock(); + Time_Lock_Main.lock();//Appling lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Seconds Time lock taken"<<endl;} time_t Time = time(NULL); tm* Time_Pointer = localtime(&Time); int Seconds = Time_Pointer->tm_sec; - Time_Lock_Main.unlock(); + Time_Lock_Main.unlock();//Releasing lock for critial section if(Log_Value == 1){cout<<"In LCD_Write_Seconds Time lock released"<<endl;} stringstream ss; ss << Seconds; @@ -188,33 +218,33 @@ { while(1) { - Thread::wait(10);//Dont Delete + Thread::wait(10);//Small wait if(mode == 0)//Default mode { if(Log_Value == 1){cout << "In mode 0 " << endl;}//Log this - Thread::wait(Default_Mode_Toggle_Time); + Thread::wait(Default_Mode_Toggle_Time);//Wait for this amount of time if(Log_Value == 1){cout<<"Writing Data to LCD"<<endl;} sprintf (LCD_buffer, "%1.1f %1.1f %1.1f",Data_Active.get_temperature(),Data_Active.get_pressure(),Data_Active.get_light());//Used for converting to a sting LCD.DDRAM_Address(0x00); LCD.Write_String("Temp Pres li"); LCD.DDRAM_Address(0x40); - LCD.Write_String(LCD_buffer); + LCD.Write_String(LCD_buffer);//Print out current data values to the LCD - Thread::wait(Default_Mode_Toggle_Time); + Thread::wait(Default_Mode_Toggle_Time); //Wait for this amount of time if(Log_Value == 1){cout<<"Writing Time and Date to LCD"<<endl;} - Time_Lock_Main.lock(); + Time_Lock_Main.lock();//lock Time_lock for critial section time_t Time = Data_Active.get_time(); tm* Time_Pointer = localtime(&Time); LCD.Display_Clear(); sprintf (LCD_buffer, "%d:%d %d,%d",Time_Pointer->tm_hour,Time_Pointer->tm_min,(Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));//Used for converting to a sting - Time_Lock_Main.unlock(); + Time_Lock_Main.unlock();//unlock Time_lock for critial section LCD.DDRAM_Address(0x00); LCD.Write_String("Current Time:"); LCD.DDRAM_Address(0x40); - LCD.Write_String(LCD_buffer); + LCD.Write_String(LCD_buffer);//Print the current time to the LCD if(Log_Value == 1){cout<<"Checking Switches for next mode"<<endl;} if(SW1.read() & SW2.read() == 1) @@ -414,7 +444,6 @@ } void Serial_Commands()//Used for getting input from the user to determine the opperations to perform { - char input[100]; while(1) { @@ -428,12 +457,11 @@ //READ ALL FIX THIS if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ' & input[5] == 'A' & input[6] == 'L' & input[7] == 'L') { - if(Log_Value == 1){cout<<"READ ALL Confirmed"<<endl;} - Data_Buffer_Lock.lock(); + Data_Buffer_Lock.lock();//Lock data buffer due to critical section int Start_Address_Read_All = Write_Pointer; - Data_Buffer_Lock.unlock(); + Data_Buffer_Lock.unlock();//unLock data buffer due to critical section for(int x_Read_All = (Start_Address_Read_All - mailsize); x_Read_All != Start_Address_Read_All; x_Read_All = x_Read_All ) { @@ -467,7 +495,7 @@ if(Log_Value == 1){cout<<"DELETE ALL Confirmed"<<endl;} } - //READ look into this + //READ else if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ') { int NumberOfChars = 0; int ArrayAddress = 0; string ReadNumber; int NumberToRead; @@ -479,9 +507,9 @@ if(Log_Value == 1){cout<<"Getting Data"<<endl;} - Data_Buffer_Lock.lock(); + Data_Buffer_Lock.lock();//Lock data buffer due to critical section int Start_Address = Write_Pointer; - Data_Buffer_Lock.unlock(); + Data_Buffer_Lock.unlock();//unLock data buffer due to critical section for(int x = (Start_Address - NumberToRead); x != Start_Address; x = x ) { @@ -513,8 +541,8 @@ //DELETE look into this else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ') { - Sampling_Timer.detach(); - Console_Output_Timer.detach(); + Sampling_Timer.detach();//Stop the Sample timer + Console_Output_Timer.detach();//Stop Console Printing int NumberOfChars = 0; int ArrayAddress = 0; string DeleteNumber; int NumberToDelete; @@ -525,7 +553,7 @@ if(Log_Value == 1){cout << "Deleted " << NumberToDelete << " samples" << endl;} - Sampling_Timer.attach(&Sampling_ISR,Sample_Rate); + Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);//Start Sample timer cout << "Sampling Restarted" << endl; } @@ -538,14 +566,14 @@ stringstream Number(StateNumber); Number >> NumberToState; - if(NumberToState==0){Sampling_Timer.detach(); Console_Output_Timer.detach();} - if(NumberToState==1){Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);} + if(NumberToState==0){Sampling_Timer.detach(); Console_Output_Timer.detach();}//Stop Sampling + if(NumberToState==1){Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);}//Start Sampling } //SETDATE else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'D' & input[4] == 'A' & input[5] == 'T' & input[6] == 'E' & input[7] == ' ' & input[10] == ' ' & input[13] == ' ') { - int NumberOfChars = 0; int ArrayAddress = 0; + //int NumberOfChars = 0; int ArrayAddress = 0; string DayNumber, MonthNumber, YearNumber; int NumberToDay, NumberToMonth, NumberToYear; @@ -567,7 +595,7 @@ //SETTIME else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'T' & input[4] == 'I' & input[5] == 'M' & input[6] == 'E' & input[7] == ' ' & input[10] == ' ' & input[13] == ' ') { - int NumberOfChars = 0; int ArrayAddress = 0; + //int NumberOfChars = 0; int ArrayAddress = 0; string HourNumber, MinuteNumber, SecondNumber; int NumberToHour, NumberToMinute, NumberToSecond; @@ -620,7 +648,7 @@ //HELP else if(input[0] == 'H' & input[1] == 'E' & input[2] == 'L' & input[3] == 'P')// Use this to display all of the availble commands { - HELP(); + HELP();//Run HELP Command } //STOP @@ -650,7 +678,7 @@ } -void Sampling_ISR() {t1.signal_set(SamplingTime);} +void Sampling_ISR(){t1.signal_set(SamplingTime);} void Sample()//Samples the hardware and prints the result to the LCD @@ -673,6 +701,7 @@ Write_Pointer = Write_Data(Data_Active,Write_Pointer); t1.signal_set(NotSamplingTime); if(Log_Value == 1){cout<<"New Sample avaliable"<<endl;} + t2.signal_set(SD_Data_Ready); } } int main() @@ -697,13 +726,15 @@ FATFileSystem fs("sd", &sd); //Open to WRITE - FILE* fp = fopen("/sd/test.csv","a"); + FILE* fp = fopen("/sd/test.csv","a");//test.csv is created in the SD Card if (fp == NULL) { error("Could not open file for write\n"); LCD.Display_Clear(); LCD.Write_String("CANNOT OPEN FILE"); errorCode(FATAL); } + //Close File + fclose(fp); int network_temp; network_temp = Network_Init(); if(network_temp == 1)//Sets up the network and checks if the network cable is not pluged in @@ -711,7 +742,7 @@ error("Could not access network"); LCD.Display_Clear(); LCD.Write_String("Could not access network"); - errorCode(FATAL); + errorCode(NETWORK_FATAL); } //Last message before sampling begins LCD.Display_Clear(); @@ -725,6 +756,7 @@ Sampling_Timer.attach(&Sampling_ISR,Sample_Rate); t1.start(Sample); + t2.start(SD_Card); t3.start(LCD_Output); t4.start(Network); t5.start(Serial_Commands); @@ -740,25 +772,22 @@ id4 = t4.gettid(); id5 = t5.gettid(); - while (onBoardSwitch == 0) { - - } - - //Close File - /* - fclose(fp); - - //Close down - sd.deinit(); - printf("Unmounted...\n"); - lcd.cls(); - lcd.printf("Unmounted...\n\n"); - */ - while(true) { + if(onBoardSwitch == 1) + { + fclose(fp);//Close File + sd.deinit();//Close down + LCD.Display_Clear(); + LCD.DDRAM_Address(0x00); + LCD.Write_String("SD Card"); + LCD.DDRAM_Address(0x40); + LCD.Write_String("Unmounted"); + LCD.DDRAM_Address(0x00); + cout << "SD Card Unmounted" << endl; + } greenLED = 1; wait(0.5); greenLED = 0; wait(0.1); } -} +}//End of Main