Plymouth ELEC351 Group T
/
ELEC351_Group_T
FINAL PROJECT isn't it
Fork of ELEC351 by
main.cpp@42:ae1640bca2e1, 2018-01-08 (annotated)
- Committer:
- thomasmorris
- Date:
- Mon Jan 08 15:38:53 2018 +0000
- Revision:
- 42:ae1640bca2e1
- Parent:
- 41:859b5e1e3d9a
- Child:
- 43:fb9faec1dc72
- Child:
- 45:875f7e18a386
Edit me
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 6:97f586597310 | 1 | /* |
thomasmorris | 6:97f586597310 | 2 | ELEC 351 Group T |
thomasmorris | 6:97f586597310 | 3 | Team Members : Christopher Hills, Thomas Morris |
thomasmorris | 42:ae1640bca2e1 | 4 | Current Verision 19 |
thomasmorris | 42:ae1640bca2e1 | 5 | Overiew: Working Tasks 1,2,3,5,6,7,8,9,10,11,12,13,14,15 |
thomasmorris | 8:0e4481b64353 | 6 | |
thomasmorris | 12:536eca338ae8 | 7 | Last Revision: Added Mail Box to serial |
thomasmorris | 8:0e4481b64353 | 8 | Todo: |
thomasmorris | 42:ae1640bca2e1 | 9 | Add functionality to SD card import chris's work on delete serial commnads |
thomasmorris | 42:ae1640bca2e1 | 10 | Commnet code |
thomasmorris | 42:ae1640bca2e1 | 11 | Check for race conditions |
thomasmorris | 42:ae1640bca2e1 | 12 | Move code around |
thomasmorris | 42:ae1640bca2e1 | 13 | Other fixes |
thomasmorris | 6:97f586597310 | 14 | */ |
thomasmorris | 6:97f586597310 | 15 | |
thomasmorris | 25:36699ed589ab | 16 | #include "SETUP.hpp" |
thomasmorris | 35:26b0a9b55d82 | 17 | #include "NETWORK.hpp" |
thomasmorris | 38:8d86e0d8a816 | 18 | |
thomasmorris | 38:8d86e0d8a816 | 19 | #define Do_Read_Data 1 |
thomasmorris | 38:8d86e0d8a816 | 20 | #define Dont_Read_Data 0 |
thomasmorris | 38:8d86e0d8a816 | 21 | #define Do_Delete_Data 1 |
thomasmorris | 38:8d86e0d8a816 | 22 | #define Dont_Delete_Data 0 |
chills | 24:7d2da96e05ad | 23 | |
chills | 33:3b5096f0126a | 24 | Mutex Time_Lock_Main; |
thomasmorris | 38:8d86e0d8a816 | 25 | Mutex Data_Buffer_Lock; |
thomasmorris | 38:8d86e0d8a816 | 26 | |
thomasmorris | 41:859b5e1e3d9a | 27 | int Write_Data(DATA Data_Store, int Write_Pointer)//Writes data passed in to the array at address Write Pointer |
thomasmorris | 38:8d86e0d8a816 | 28 | { |
thomasmorris | 41:859b5e1e3d9a | 29 | Data_Buffer_Lock.lock();//Appling lock for critial section |
thomasmorris | 38:8d86e0d8a816 | 30 | Data_Buffer[Write_Pointer] = Data_Store; |
thomasmorris | 41:859b5e1e3d9a | 31 | if(Write_Pointer < mailsize - 1)//Checks if write pointer will be greater than mail size |
thomasmorris | 38:8d86e0d8a816 | 32 | { |
thomasmorris | 41:859b5e1e3d9a | 33 | Write_Pointer = Write_Pointer + 1;//Increment write pointer |
thomasmorris | 38:8d86e0d8a816 | 34 | } |
thomasmorris | 38:8d86e0d8a816 | 35 | else |
thomasmorris | 38:8d86e0d8a816 | 36 | { |
thomasmorris | 41:859b5e1e3d9a | 37 | Write_Pointer = 0;//Else set it to 0 thus a FIFO system |
thomasmorris | 38:8d86e0d8a816 | 38 | } |
thomasmorris | 41:859b5e1e3d9a | 39 | Data_Buffer_Lock.unlock();//Releasing lock for critial section |
thomasmorris | 41:859b5e1e3d9a | 40 | return Write_Pointer;//New address of Write pointer |
thomasmorris | 38:8d86e0d8a816 | 41 | |
thomasmorris | 38:8d86e0d8a816 | 42 | } |
thomasmorris | 38:8d86e0d8a816 | 43 | DATA Read_Data(int Read_Pointer) |
thomasmorris | 38:8d86e0d8a816 | 44 | { |
thomasmorris | 41:859b5e1e3d9a | 45 | Data_Buffer_Lock.lock();//Appling lock for critial section |
thomasmorris | 38:8d86e0d8a816 | 46 | DATA Temp_Data = Data_Buffer[Read_Pointer]; |
thomasmorris | 41:859b5e1e3d9a | 47 | Data_Buffer_Lock.unlock();//Releasing lock for critial section |
thomasmorris | 38:8d86e0d8a816 | 48 | |
thomasmorris | 38:8d86e0d8a816 | 49 | return Temp_Data; |
thomasmorris | 38:8d86e0d8a816 | 50 | } |
thomasmorris | 38:8d86e0d8a816 | 51 | void Delete_Data(int Delete_Pointer) |
thomasmorris | 38:8d86e0d8a816 | 52 | { |
thomasmorris | 41:859b5e1e3d9a | 53 | Data_Buffer_Lock.lock();//Appling lock for critial section |
thomasmorris | 38:8d86e0d8a816 | 54 | Data_Buffer[Delete_Pointer].set_all_zero(); |
thomasmorris | 41:859b5e1e3d9a | 55 | Data_Buffer_Lock.unlock();//Releasing lock for critial section |
thomasmorris | 41:859b5e1e3d9a | 56 | } |
thomasmorris | 41:859b5e1e3d9a | 57 | |
thomasmorris | 41:859b5e1e3d9a | 58 | void SD_Card()//Writes data to the SD card |
thomasmorris | 41:859b5e1e3d9a | 59 | { |
thomasmorris | 41:859b5e1e3d9a | 60 | while(1) |
thomasmorris | 41:859b5e1e3d9a | 61 | { |
thomasmorris | 42:ae1640bca2e1 | 62 | Thread::wait(Sample_Rate*1000); //Waits until a new is taken before checking again |
thomasmorris | 42:ae1640bca2e1 | 63 | if(Write_Pointer == mailsize - 1) |
thomasmorris | 42:ae1640bca2e1 | 64 | { |
thomasmorris | 42:ae1640bca2e1 | 65 | if(Log_Value==1){cout << "In SD_Card Thread" << endl;} |
thomasmorris | 41:859b5e1e3d9a | 66 | |
thomasmorris | 42:ae1640bca2e1 | 67 | //Open to WRITE |
thomasmorris | 42:ae1640bca2e1 | 68 | FILE* fp = fopen("/sd/test.csv","a"); |
thomasmorris | 42:ae1640bca2e1 | 69 | if(Log_Value==1){cout << "File Opened" << endl;} |
thomasmorris | 42:ae1640bca2e1 | 70 | |
thomasmorris | 42:ae1640bca2e1 | 71 | for(int SD_Card_Data_Pointer = 0; SD_Card_Data_Pointer != mailsize; SD_Card_Data_Pointer++) |
thomasmorris | 42:ae1640bca2e1 | 72 | { |
thomasmorris | 42:ae1640bca2e1 | 73 | time_t Time = Data_Buffer[SD_Card_Data_Pointer].get_time(); |
thomasmorris | 42:ae1640bca2e1 | 74 | tm* Time_Pointer = localtime(&Time); |
thomasmorris | 42:ae1640bca2e1 | 75 | int temp_day = Time_Pointer->tm_mday; |
thomasmorris | 42:ae1640bca2e1 | 76 | int temp_month = (Time_Pointer->tm_mon+1);//Set to current month |
thomasmorris | 42:ae1640bca2e1 | 77 | int temp_year = (Time_Pointer->tm_year+1900);//Set to current year |
thomasmorris | 42:ae1640bca2e1 | 78 | |
thomasmorris | 42:ae1640bca2e1 | 79 | int temp_hours = Time_Pointer->tm_hour; |
thomasmorris | 42:ae1640bca2e1 | 80 | int temp_minute = Time_Pointer->tm_min; |
thomasmorris | 42:ae1640bca2e1 | 81 | |
thomasmorris | 42:ae1640bca2e1 | 82 | float temp_temperature = Data_Buffer[SD_Card_Data_Pointer].get_temperature(); |
thomasmorris | 42:ae1640bca2e1 | 83 | float temp_pressure = Data_Buffer[SD_Card_Data_Pointer].get_pressure(); |
thomasmorris | 42:ae1640bca2e1 | 84 | float temp_light = Data_Buffer[SD_Card_Data_Pointer].get_light(); |
thomasmorris | 42:ae1640bca2e1 | 85 | |
thomasmorris | 42:ae1640bca2e1 | 86 | fprintf(fp, "Date: %d/%d/%d,",temp_day,temp_month,temp_year);//Date |
thomasmorris | 42:ae1640bca2e1 | 87 | fprintf(fp, "Time: %d:%d,",temp_hours,temp_minute);//Time |
thomasmorris | 42:ae1640bca2e1 | 88 | fprintf(fp, "Temperature: %1.1f,",temp_temperature);//Temperature |
thomasmorris | 42:ae1640bca2e1 | 89 | fprintf(fp, "Pressure: %1.1f,",temp_pressure);//Pressure |
thomasmorris | 42:ae1640bca2e1 | 90 | fprintf(fp, "Light: %5.3f,",temp_light);//Light |
thomasmorris | 42:ae1640bca2e1 | 91 | fprintf(fp, "End of Data \n");//End of data |
thomasmorris | 42:ae1640bca2e1 | 92 | } |
thomasmorris | 42:ae1640bca2e1 | 93 | if(Log_Value==1){cout << "Dumped data to SD Card" <<endl;} |
thomasmorris | 42:ae1640bca2e1 | 94 | //Close File |
thomasmorris | 42:ae1640bca2e1 | 95 | fclose(fp); |
thomasmorris | 42:ae1640bca2e1 | 96 | } |
thomasmorris | 41:859b5e1e3d9a | 97 | } |
thomasmorris | 38:8d86e0d8a816 | 98 | } |
thomasmorris | 35:26b0a9b55d82 | 99 | void Network() |
thomasmorris | 8:0e4481b64353 | 100 | { |
thomasmorris | 35:26b0a9b55d82 | 101 | while(1) |
thomasmorris | 35:26b0a9b55d82 | 102 | { |
thomasmorris | 41:859b5e1e3d9a | 103 | Thread::wait(NetworkWait);//Waits Network Wait amount of time |
thomasmorris | 37:7c4d7f206039 | 104 | if(Log_Value==1){cout << "In Network Thread" << endl;} //Log this |
thomasmorris | 36:a0098306fc58 | 105 | |
thomasmorris | 36:a0098306fc58 | 106 | time_t Time = Data_Active.get_time(); |
thomasmorris | 36:a0098306fc58 | 107 | tm* Time_Pointer = localtime(&Time); |
thomasmorris | 36:a0098306fc58 | 108 | int temp_day = Time_Pointer->tm_mday; |
thomasmorris | 41:859b5e1e3d9a | 109 | int temp_month = (Time_Pointer->tm_mon+1);//Set to current month |
thomasmorris | 41:859b5e1e3d9a | 110 | int temp_year = (Time_Pointer->tm_year+1900);//Set to current year |
thomasmorris | 36:a0098306fc58 | 111 | |
thomasmorris | 36:a0098306fc58 | 112 | int temp_hours = Time_Pointer->tm_hour; |
thomasmorris | 36:a0098306fc58 | 113 | int temp_minute = Time_Pointer->tm_min; |
thomasmorris | 36:a0098306fc58 | 114 | |
thomasmorris | 36:a0098306fc58 | 115 | float temp_temperature = Data_Active.get_temperature(); |
thomasmorris | 36:a0098306fc58 | 116 | float temp_pressure = Data_Active.get_pressure(); |
thomasmorris | 36:a0098306fc58 | 117 | float temp_light = Data_Active.get_light(); |
thomasmorris | 36:a0098306fc58 | 118 | |
thomasmorris | 36:a0098306fc58 | 119 | |
thomasmorris | 41:859b5e1e3d9a | 120 | Networking(temp_day,temp_month,temp_year,temp_hours,temp_minute,temp_temperature,temp_pressure,temp_light);//Pass in networking data and time values |
thomasmorris | 35:26b0a9b55d82 | 121 | } |
chills | 13:db857b3744c6 | 122 | } |
thomasmorris | 31:4a88bf97b53e | 123 | void LCD_Write_Year() |
thomasmorris | 12:536eca338ae8 | 124 | { |
thomasmorris | 41:859b5e1e3d9a | 125 | Time_Lock_Main.lock();//Appling lock for critial section |
thomasmorris | 37:7c4d7f206039 | 126 | if(Log_Value == 1){cout<<"In LCD_Write_Year Time lock taken"<<endl;} |
chills | 33:3b5096f0126a | 127 | time_t Time = time(NULL); |
chills | 33:3b5096f0126a | 128 | tm* Time_Pointer = localtime(&Time); |
chills | 33:3b5096f0126a | 129 | int Years = 1900 + Time_Pointer->tm_year; |
thomasmorris | 41:859b5e1e3d9a | 130 | Time_Lock_Main.unlock();//Releasing lock for critial section |
thomasmorris | 37:7c4d7f206039 | 131 | if(Log_Value == 1){cout<<"In LCD_Write_Year Time lock released"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 132 | stringstream ss; |
thomasmorris | 31:4a88bf97b53e | 133 | ss << Years; |
thomasmorris | 31:4a88bf97b53e | 134 | string Year_String = ss.str(); |
thomasmorris | 31:4a88bf97b53e | 135 | LCD.DDRAM_Address(0x00); |
thomasmorris | 31:4a88bf97b53e | 136 | LCD.Write_String("Set Year"); |
thomasmorris | 31:4a88bf97b53e | 137 | LCD.DDRAM_Address(0x40); |
thomasmorris | 31:4a88bf97b53e | 138 | LCD.Write_String(Year_String); |
thomasmorris | 30:4cde05cc7c4f | 139 | } |
thomasmorris | 31:4a88bf97b53e | 140 | void LCD_Write_Month() |
thomasmorris | 31:4a88bf97b53e | 141 | { |
thomasmorris | 41:859b5e1e3d9a | 142 | Time_Lock_Main.lock();//Appling lock for critial section |
thomasmorris | 37:7c4d7f206039 | 143 | if(Log_Value == 1){cout<<"In LCD_Write_Month Time lock taken"<<endl;} |
chills | 33:3b5096f0126a | 144 | time_t Time = time(NULL); |
chills | 33:3b5096f0126a | 145 | tm* Time_Pointer = localtime(&Time); |
chills | 33:3b5096f0126a | 146 | int Months = 1 + Time_Pointer->tm_mon; |
thomasmorris | 41:859b5e1e3d9a | 147 | Time_Lock_Main.unlock();//Releasing lock for critial section |
thomasmorris | 37:7c4d7f206039 | 148 | if(Log_Value == 1){cout<<"In LCD_Write_Month Time lock released"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 149 | stringstream ss; |
thomasmorris | 31:4a88bf97b53e | 150 | ss << Months; |
thomasmorris | 31:4a88bf97b53e | 151 | string Month_String = ss.str(); |
thomasmorris | 31:4a88bf97b53e | 152 | LCD.DDRAM_Address(0x00); |
thomasmorris | 31:4a88bf97b53e | 153 | LCD.Write_String("Set Month"); |
thomasmorris | 31:4a88bf97b53e | 154 | LCD.DDRAM_Address(0x40); |
thomasmorris | 31:4a88bf97b53e | 155 | LCD.Write_String(Month_String); |
thomasmorris | 31:4a88bf97b53e | 156 | } |
thomasmorris | 31:4a88bf97b53e | 157 | void LCD_Write_Day() |
thomasmorris | 31:4a88bf97b53e | 158 | { |
thomasmorris | 41:859b5e1e3d9a | 159 | Time_Lock_Main.lock();//Appling lock for critial section |
thomasmorris | 37:7c4d7f206039 | 160 | if(Log_Value == 1){cout<<"In LCD_Write_Day Time lock taken"<<endl;} |
chills | 33:3b5096f0126a | 161 | time_t Time = time(NULL); |
chills | 33:3b5096f0126a | 162 | tm* Time_Pointer = localtime(&Time); |
chills | 33:3b5096f0126a | 163 | int Days = Time_Pointer->tm_mday; |
thomasmorris | 41:859b5e1e3d9a | 164 | Time_Lock_Main.unlock();//Releasing lock for critial section |
thomasmorris | 37:7c4d7f206039 | 165 | if(Log_Value == 1){cout<<"In LCD_Write_Day Time lock released"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 166 | stringstream ss; |
thomasmorris | 31:4a88bf97b53e | 167 | ss << Days; |
thomasmorris | 31:4a88bf97b53e | 168 | string Day_String = ss.str(); |
thomasmorris | 31:4a88bf97b53e | 169 | LCD.DDRAM_Address(0x00); |
thomasmorris | 31:4a88bf97b53e | 170 | LCD.Write_String("Set Day"); |
thomasmorris | 31:4a88bf97b53e | 171 | LCD.DDRAM_Address(0x40); |
thomasmorris | 31:4a88bf97b53e | 172 | LCD.Write_String(Day_String); |
thomasmorris | 31:4a88bf97b53e | 173 | } |
thomasmorris | 31:4a88bf97b53e | 174 | void LCD_Write_Hour() |
thomasmorris | 30:4cde05cc7c4f | 175 | { |
thomasmorris | 41:859b5e1e3d9a | 176 | Time_Lock_Main.lock();//Appling lock for critial section |
thomasmorris | 37:7c4d7f206039 | 177 | if(Log_Value == 1){cout<<"In LCD_Write_Hour Time lock taken"<<endl;} |
chills | 33:3b5096f0126a | 178 | time_t Time = time(NULL); |
chills | 33:3b5096f0126a | 179 | tm* Time_Pointer = localtime(&Time); |
chills | 33:3b5096f0126a | 180 | int Hours = Time_Pointer->tm_hour; |
thomasmorris | 41:859b5e1e3d9a | 181 | Time_Lock_Main.unlock();//Releasing lock for critial section |
thomasmorris | 37:7c4d7f206039 | 182 | if(Log_Value == 1){cout<<"In LCD_Write_Hour Time lock released"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 183 | stringstream ss; |
thomasmorris | 31:4a88bf97b53e | 184 | ss << Hours; |
thomasmorris | 31:4a88bf97b53e | 185 | string Hour_String = ss.str(); |
thomasmorris | 31:4a88bf97b53e | 186 | LCD.DDRAM_Address(0x00); |
thomasmorris | 31:4a88bf97b53e | 187 | LCD.Write_String("Set Hour"); |
thomasmorris | 31:4a88bf97b53e | 188 | LCD.DDRAM_Address(0x40); |
thomasmorris | 31:4a88bf97b53e | 189 | LCD.Write_String(Hour_String); |
thomasmorris | 31:4a88bf97b53e | 190 | } |
thomasmorris | 31:4a88bf97b53e | 191 | void LCD_Write_Minute() |
thomasmorris | 31:4a88bf97b53e | 192 | { |
thomasmorris | 41:859b5e1e3d9a | 193 | Time_Lock_Main.lock();//Appling lock for critial section |
thomasmorris | 37:7c4d7f206039 | 194 | if(Log_Value == 1){cout<<"In LCD_Write_Minute Time lock taken"<<endl;} |
chills | 33:3b5096f0126a | 195 | time_t Time = time(NULL); |
chills | 33:3b5096f0126a | 196 | tm* Time_Pointer = localtime(&Time); |
chills | 33:3b5096f0126a | 197 | int Minutes = Time_Pointer->tm_min; |
thomasmorris | 41:859b5e1e3d9a | 198 | Time_Lock_Main.unlock();//Releasing lock for critial section |
thomasmorris | 37:7c4d7f206039 | 199 | if(Log_Value == 1){cout<<"In LCD_Write_Minute Time lock released"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 200 | stringstream ss; |
thomasmorris | 31:4a88bf97b53e | 201 | ss << Minutes; |
thomasmorris | 31:4a88bf97b53e | 202 | string Minute_String = ss.str(); |
thomasmorris | 31:4a88bf97b53e | 203 | LCD.DDRAM_Address(0x00); |
thomasmorris | 31:4a88bf97b53e | 204 | LCD.Write_String("Set Minute"); |
thomasmorris | 31:4a88bf97b53e | 205 | LCD.DDRAM_Address(0x40); |
thomasmorris | 31:4a88bf97b53e | 206 | LCD.Write_String(Minute_String); |
thomasmorris | 31:4a88bf97b53e | 207 | } |
thomasmorris | 31:4a88bf97b53e | 208 | void LCD_Write_Seconds() |
thomasmorris | 31:4a88bf97b53e | 209 | { |
thomasmorris | 41:859b5e1e3d9a | 210 | Time_Lock_Main.lock();//Appling lock for critial section |
thomasmorris | 37:7c4d7f206039 | 211 | if(Log_Value == 1){cout<<"In LCD_Write_Seconds Time lock taken"<<endl;} |
chills | 33:3b5096f0126a | 212 | time_t Time = time(NULL); |
chills | 33:3b5096f0126a | 213 | tm* Time_Pointer = localtime(&Time); |
chills | 33:3b5096f0126a | 214 | int Seconds = Time_Pointer->tm_sec; |
thomasmorris | 41:859b5e1e3d9a | 215 | Time_Lock_Main.unlock();//Releasing lock for critial section |
thomasmorris | 37:7c4d7f206039 | 216 | if(Log_Value == 1){cout<<"In LCD_Write_Seconds Time lock released"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 217 | stringstream ss; |
thomasmorris | 31:4a88bf97b53e | 218 | ss << Seconds; |
thomasmorris | 31:4a88bf97b53e | 219 | string Second_String = ss.str(); |
thomasmorris | 31:4a88bf97b53e | 220 | LCD.DDRAM_Address(0x00); |
thomasmorris | 31:4a88bf97b53e | 221 | LCD.Write_String("Set Second"); |
thomasmorris | 31:4a88bf97b53e | 222 | LCD.DDRAM_Address(0x40); |
thomasmorris | 31:4a88bf97b53e | 223 | LCD.Write_String(Second_String); |
thomasmorris | 7:dfe19413fdc2 | 224 | } |
thomasmorris | 30:4cde05cc7c4f | 225 | |
thomasmorris | 30:4cde05cc7c4f | 226 | void LCD_Output() |
chills | 34:c0b8705f183d | 227 | { |
thomasmorris | 30:4cde05cc7c4f | 228 | while(1) |
thomasmorris | 29:64b1f95a807c | 229 | { |
thomasmorris | 41:859b5e1e3d9a | 230 | Thread::wait(10);//Small wait |
thomasmorris | 30:4cde05cc7c4f | 231 | if(mode == 0)//Default mode |
thomasmorris | 30:4cde05cc7c4f | 232 | { |
thomasmorris | 37:7c4d7f206039 | 233 | if(Log_Value == 1){cout << "In mode 0 " << endl;}//Log this |
chills | 33:3b5096f0126a | 234 | |
thomasmorris | 41:859b5e1e3d9a | 235 | Thread::wait(Default_Mode_Toggle_Time);//Wait for this amount of time |
thomasmorris | 37:7c4d7f206039 | 236 | if(Log_Value == 1){cout<<"Writing Data to LCD"<<endl;} |
chills | 34:c0b8705f183d | 237 | 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 |
chills | 33:3b5096f0126a | 238 | |
thomasmorris | 30:4cde05cc7c4f | 239 | LCD.DDRAM_Address(0x00); |
thomasmorris | 30:4cde05cc7c4f | 240 | LCD.Write_String("Temp Pres li"); |
thomasmorris | 30:4cde05cc7c4f | 241 | LCD.DDRAM_Address(0x40); |
thomasmorris | 41:859b5e1e3d9a | 242 | LCD.Write_String(LCD_buffer);//Print out current data values to the LCD |
thomasmorris | 25:36699ed589ab | 243 | |
thomasmorris | 41:859b5e1e3d9a | 244 | Thread::wait(Default_Mode_Toggle_Time); //Wait for this amount of time |
thomasmorris | 37:7c4d7f206039 | 245 | if(Log_Value == 1){cout<<"Writing Time and Date to LCD"<<endl;} |
thomasmorris | 41:859b5e1e3d9a | 246 | Time_Lock_Main.lock();//lock Time_lock for critial section |
chills | 34:c0b8705f183d | 247 | time_t Time = Data_Active.get_time(); |
chills | 34:c0b8705f183d | 248 | tm* Time_Pointer = localtime(&Time); |
thomasmorris | 30:4cde05cc7c4f | 249 | LCD.Display_Clear(); |
chills | 33:3b5096f0126a | 250 | 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 |
thomasmorris | 41:859b5e1e3d9a | 251 | Time_Lock_Main.unlock();//unlock Time_lock for critial section |
chills | 33:3b5096f0126a | 252 | |
thomasmorris | 30:4cde05cc7c4f | 253 | LCD.DDRAM_Address(0x00); |
thomasmorris | 30:4cde05cc7c4f | 254 | LCD.Write_String("Current Time:"); |
thomasmorris | 30:4cde05cc7c4f | 255 | LCD.DDRAM_Address(0x40); |
thomasmorris | 41:859b5e1e3d9a | 256 | LCD.Write_String(LCD_buffer);//Print the current time to the LCD |
thomasmorris | 31:4a88bf97b53e | 257 | |
thomasmorris | 37:7c4d7f206039 | 258 | if(Log_Value == 1){cout<<"Checking Switches for next mode"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 259 | if(SW1.read() & SW2.read() == 1) |
thomasmorris | 31:4a88bf97b53e | 260 | { |
thomasmorris | 31:4a88bf97b53e | 261 | mode = 1; |
thomasmorris | 31:4a88bf97b53e | 262 | } |
thomasmorris | 30:4cde05cc7c4f | 263 | } |
thomasmorris | 30:4cde05cc7c4f | 264 | else if(mode == 1)//Choose either date setting or time setting |
thomasmorris | 30:4cde05cc7c4f | 265 | { |
chills | 34:c0b8705f183d | 266 | if(Log_Value == 1){cout << "In Mode 1" << endl;} |
thomasmorris | 30:4cde05cc7c4f | 267 | LCD.Display_Clear(); |
thomasmorris | 31:4a88bf97b53e | 268 | while(1) |
thomasmorris | 31:4a88bf97b53e | 269 | { |
thomasmorris | 31:4a88bf97b53e | 270 | LCD.DDRAM_Address(0x00); |
thomasmorris | 31:4a88bf97b53e | 271 | LCD.Write_String("Date Time"); |
thomasmorris | 31:4a88bf97b53e | 272 | LCD.DDRAM_Address(0x40); |
thomasmorris | 31:4a88bf97b53e | 273 | LCD.Write_String("< >"); |
thomasmorris | 31:4a88bf97b53e | 274 | Thread::wait(1000); |
thomasmorris | 37:7c4d7f206039 | 275 | if(Log_Value == 1){cout<<"Checking SW1 to go to Date setting"<<endl;} |
thomasmorris | 37:7c4d7f206039 | 276 | if(SW1.read() == 1 & SW2.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 277 | { |
thomasmorris | 37:7c4d7f206039 | 278 | mode = 2;//Date Setting |
thomasmorris | 31:4a88bf97b53e | 279 | break; |
thomasmorris | 31:4a88bf97b53e | 280 | } |
thomasmorris | 37:7c4d7f206039 | 281 | if(Log_Value == 1){cout<<"Checking SW2 to go to Time setting"<<endl;} |
thomasmorris | 37:7c4d7f206039 | 282 | if(SW2.read() == 1 & SW1.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 283 | { |
thomasmorris | 37:7c4d7f206039 | 284 | mode = 5;//Time Setting |
thomasmorris | 31:4a88bf97b53e | 285 | break; |
thomasmorris | 31:4a88bf97b53e | 286 | } |
thomasmorris | 31:4a88bf97b53e | 287 | } |
thomasmorris | 30:4cde05cc7c4f | 288 | } |
thomasmorris | 30:4cde05cc7c4f | 289 | else if(mode == 2)//Set the Year |
thomasmorris | 30:4cde05cc7c4f | 290 | { |
chills | 34:c0b8705f183d | 291 | if(Log_Value == 1){cout << "In Mode 2" << endl;} |
thomasmorris | 31:4a88bf97b53e | 292 | LCD.Display_Clear(); |
thomasmorris | 31:4a88bf97b53e | 293 | while(1) |
thomasmorris | 31:4a88bf97b53e | 294 | { |
thomasmorris | 32:8f2795716e97 | 295 | LCD_Write_Year(); |
thomasmorris | 31:4a88bf97b53e | 296 | Thread::wait(1000); |
thomasmorris | 37:7c4d7f206039 | 297 | if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go to Month setting"<<endl;} |
chills | 33:3b5096f0126a | 298 | if(SW1.read() & SW2.read() == 1) |
chills | 33:3b5096f0126a | 299 | { |
chills | 33:3b5096f0126a | 300 | mode = 3; |
chills | 33:3b5096f0126a | 301 | break; |
chills | 33:3b5096f0126a | 302 | } |
thomasmorris | 37:7c4d7f206039 | 303 | if(Log_Value == 1){cout<<"Checking SW1 to add Year"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 304 | if(SW1.read() == 1 & SW2.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 305 | { |
thomasmorris | 31:4a88bf97b53e | 306 | Add_Year(); |
thomasmorris | 31:4a88bf97b53e | 307 | } |
thomasmorris | 37:7c4d7f206039 | 308 | if(Log_Value == 1){cout<<"Checking SW2 to subtract Year"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 309 | else if(SW2.read() == 1 & SW1.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 310 | { |
thomasmorris | 31:4a88bf97b53e | 311 | Subtract_Year(); |
thomasmorris | 31:4a88bf97b53e | 312 | } |
thomasmorris | 31:4a88bf97b53e | 313 | LCD_Write_Year(); |
thomasmorris | 31:4a88bf97b53e | 314 | } |
thomasmorris | 30:4cde05cc7c4f | 315 | } |
thomasmorris | 30:4cde05cc7c4f | 316 | else if(mode == 3)//Set the Month |
thomasmorris | 30:4cde05cc7c4f | 317 | { |
chills | 34:c0b8705f183d | 318 | if(Log_Value == 1){cout << "In Mode 3" << endl;} |
thomasmorris | 31:4a88bf97b53e | 319 | LCD.Display_Clear(); |
thomasmorris | 31:4a88bf97b53e | 320 | while(1) |
thomasmorris | 32:8f2795716e97 | 321 | { |
thomasmorris | 32:8f2795716e97 | 322 | LCD_Write_Month(); |
thomasmorris | 31:4a88bf97b53e | 323 | Thread::wait(1000); |
thomasmorris | 37:7c4d7f206039 | 324 | if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go to Day setting"<<endl;} |
chills | 33:3b5096f0126a | 325 | if(SW1.read() & SW2.read() == 1) |
chills | 33:3b5096f0126a | 326 | { |
chills | 33:3b5096f0126a | 327 | mode = 4; |
chills | 33:3b5096f0126a | 328 | break; |
thomasmorris | 37:7c4d7f206039 | 329 | } |
thomasmorris | 37:7c4d7f206039 | 330 | if(Log_Value == 1){cout<<"Checking SW1 to add Month"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 331 | if(SW1.read() == 1 & SW2.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 332 | { |
thomasmorris | 31:4a88bf97b53e | 333 | Add_Month(); |
thomasmorris | 31:4a88bf97b53e | 334 | } |
thomasmorris | 37:7c4d7f206039 | 335 | if(Log_Value == 1){cout<<"Checking SW2 to subtract Month"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 336 | else if(SW2.read() == 1 & SW1.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 337 | { |
thomasmorris | 31:4a88bf97b53e | 338 | Subtract_Month(); |
thomasmorris | 32:8f2795716e97 | 339 | } |
chills | 33:3b5096f0126a | 340 | |
thomasmorris | 31:4a88bf97b53e | 341 | } |
thomasmorris | 30:4cde05cc7c4f | 342 | } |
thomasmorris | 30:4cde05cc7c4f | 343 | else if(mode == 4)//Set the Day |
thomasmorris | 30:4cde05cc7c4f | 344 | { |
chills | 34:c0b8705f183d | 345 | if(Log_Value == 1){cout << "In Mode 4" << endl;} |
thomasmorris | 31:4a88bf97b53e | 346 | LCD.Display_Clear(); |
thomasmorris | 31:4a88bf97b53e | 347 | while(1) |
thomasmorris | 32:8f2795716e97 | 348 | { |
thomasmorris | 32:8f2795716e97 | 349 | LCD_Write_Day(); |
thomasmorris | 31:4a88bf97b53e | 350 | Thread::wait(1000); |
thomasmorris | 37:7c4d7f206039 | 351 | if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Default setting"<<endl;} |
chills | 33:3b5096f0126a | 352 | if(SW1.read() & SW2.read() == 1) |
chills | 33:3b5096f0126a | 353 | { |
chills | 33:3b5096f0126a | 354 | mode = 0; |
chills | 33:3b5096f0126a | 355 | break; |
chills | 33:3b5096f0126a | 356 | } |
thomasmorris | 37:7c4d7f206039 | 357 | if(Log_Value == 1){cout<<"Checking SW1 to add Day"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 358 | if(SW1.read() == 1 & SW2.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 359 | { |
thomasmorris | 31:4a88bf97b53e | 360 | Add_Day(); |
thomasmorris | 31:4a88bf97b53e | 361 | } |
thomasmorris | 37:7c4d7f206039 | 362 | if(Log_Value == 1){cout<<"Checking SW2 to subtract Day"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 363 | else if(SW2.read() == 1 & SW1.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 364 | { |
thomasmorris | 31:4a88bf97b53e | 365 | Subtract_Day(); |
thomasmorris | 32:8f2795716e97 | 366 | } |
thomasmorris | 31:4a88bf97b53e | 367 | } |
thomasmorris | 30:4cde05cc7c4f | 368 | } |
thomasmorris | 30:4cde05cc7c4f | 369 | else if(mode == 5)//Set the Hour |
thomasmorris | 30:4cde05cc7c4f | 370 | { |
chills | 34:c0b8705f183d | 371 | if(Log_Value == 1){cout << "In Mode 5" << endl;} |
thomasmorris | 31:4a88bf97b53e | 372 | LCD.Display_Clear(); |
thomasmorris | 31:4a88bf97b53e | 373 | while(1) |
thomasmorris | 31:4a88bf97b53e | 374 | { |
thomasmorris | 32:8f2795716e97 | 375 | LCD_Write_Hour(); |
thomasmorris | 32:8f2795716e97 | 376 | Thread::wait(1000); |
thomasmorris | 37:7c4d7f206039 | 377 | if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Minute setting"<<endl;} |
chills | 33:3b5096f0126a | 378 | if(SW1.read() & SW2.read() == 1) |
chills | 33:3b5096f0126a | 379 | { |
chills | 33:3b5096f0126a | 380 | mode = 6; |
chills | 33:3b5096f0126a | 381 | break; |
thomasmorris | 37:7c4d7f206039 | 382 | } |
thomasmorris | 37:7c4d7f206039 | 383 | if(Log_Value == 1){cout<<"Checking SW1 to add Hour"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 384 | if(SW1.read() == 1 & SW2.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 385 | { |
thomasmorris | 31:4a88bf97b53e | 386 | Add_Hour(); |
thomasmorris | 31:4a88bf97b53e | 387 | } |
thomasmorris | 37:7c4d7f206039 | 388 | if(Log_Value == 1){cout<<"Checking SW2 to subtract Hour"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 389 | else if(SW2.read() == 1 & SW1.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 390 | { |
thomasmorris | 31:4a88bf97b53e | 391 | Subtract_Hour(); |
thomasmorris | 31:4a88bf97b53e | 392 | } |
thomasmorris | 31:4a88bf97b53e | 393 | } |
thomasmorris | 30:4cde05cc7c4f | 394 | } |
thomasmorris | 30:4cde05cc7c4f | 395 | else if(mode == 6)//Set the Minute |
thomasmorris | 30:4cde05cc7c4f | 396 | { |
chills | 34:c0b8705f183d | 397 | if(Log_Value == 1){cout << "In Mode 6" << endl;} |
thomasmorris | 31:4a88bf97b53e | 398 | LCD.Display_Clear(); |
thomasmorris | 31:4a88bf97b53e | 399 | while(1) |
thomasmorris | 31:4a88bf97b53e | 400 | { |
thomasmorris | 32:8f2795716e97 | 401 | LCD_Write_Minute(); |
thomasmorris | 32:8f2795716e97 | 402 | Thread::wait(1000); |
thomasmorris | 37:7c4d7f206039 | 403 | if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Seconds setting"<<endl;} |
chills | 33:3b5096f0126a | 404 | if(SW1.read() & SW2.read() == 1) |
chills | 33:3b5096f0126a | 405 | { |
chills | 33:3b5096f0126a | 406 | mode = 7; |
chills | 33:3b5096f0126a | 407 | break; |
chills | 33:3b5096f0126a | 408 | } |
thomasmorris | 37:7c4d7f206039 | 409 | if(Log_Value == 1){cout<<"Checking SW1 to add Minute"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 410 | if(SW1.read() == 1 & SW2.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 411 | { |
thomasmorris | 31:4a88bf97b53e | 412 | Add_Minute(); |
thomasmorris | 31:4a88bf97b53e | 413 | } |
thomasmorris | 37:7c4d7f206039 | 414 | if(Log_Value == 1){cout<<"Checking SW2 to subtract Minute"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 415 | else if(SW2.read() == 1 & SW1.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 416 | { |
thomasmorris | 31:4a88bf97b53e | 417 | Subtract_Minute(); |
thomasmorris | 32:8f2795716e97 | 418 | } |
thomasmorris | 31:4a88bf97b53e | 419 | } |
thomasmorris | 30:4cde05cc7c4f | 420 | } |
thomasmorris | 30:4cde05cc7c4f | 421 | else if(mode == 7)//Set the Seconds |
thomasmorris | 30:4cde05cc7c4f | 422 | { |
chills | 34:c0b8705f183d | 423 | if(Log_Value == 1){cout << "In Mode 7" << endl;} |
thomasmorris | 31:4a88bf97b53e | 424 | LCD.Display_Clear(); |
thomasmorris | 31:4a88bf97b53e | 425 | while(1) |
thomasmorris | 31:4a88bf97b53e | 426 | { |
thomasmorris | 32:8f2795716e97 | 427 | LCD_Write_Seconds(); |
thomasmorris | 32:8f2795716e97 | 428 | Thread::wait(1000); |
thomasmorris | 37:7c4d7f206039 | 429 | if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Default setting"<<endl;} |
chills | 33:3b5096f0126a | 430 | if(SW1.read() & SW2.read() == 1) |
chills | 33:3b5096f0126a | 431 | { |
chills | 33:3b5096f0126a | 432 | mode = 0; |
chills | 33:3b5096f0126a | 433 | break; |
thomasmorris | 37:7c4d7f206039 | 434 | } |
thomasmorris | 37:7c4d7f206039 | 435 | if(Log_Value == 1){cout<<"Checking SW1 to add Second"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 436 | if(SW1.read() == 1 & SW2.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 437 | { |
thomasmorris | 31:4a88bf97b53e | 438 | Add_Second(); |
thomasmorris | 31:4a88bf97b53e | 439 | } |
thomasmorris | 37:7c4d7f206039 | 440 | if(Log_Value == 1){cout<<"Checking SW1 to subtract Second"<<endl;} |
thomasmorris | 31:4a88bf97b53e | 441 | else if(SW2.read() == 1 & SW1.read() == 0) |
thomasmorris | 31:4a88bf97b53e | 442 | { |
thomasmorris | 31:4a88bf97b53e | 443 | Subtract_Second(); |
thomasmorris | 32:8f2795716e97 | 444 | } |
thomasmorris | 31:4a88bf97b53e | 445 | } |
thomasmorris | 30:4cde05cc7c4f | 446 | } |
thomasmorris | 30:4cde05cc7c4f | 447 | else |
thomasmorris | 30:4cde05cc7c4f | 448 | { |
thomasmorris | 37:7c4d7f206039 | 449 | if(Log_Value == 1){cout<<"Mode Error occured mode now set to 0"<<endl;} |
thomasmorris | 35:26b0a9b55d82 | 450 | mode = 0; |
thomasmorris | 7:dfe19413fdc2 | 451 | } |
thomasmorris | 7:dfe19413fdc2 | 452 | } |
thomasmorris | 7:dfe19413fdc2 | 453 | } |
thomasmorris | 29:64b1f95a807c | 454 | void Serial_Commands()//Used for getting input from the user to determine the opperations to perform |
thomasmorris | 22:eb4cc12087b2 | 455 | { |
thomasmorris | 38:8d86e0d8a816 | 456 | char input[100]; |
thomasmorris | 25:36699ed589ab | 457 | while(1) { |
thomasmorris | 38:8d86e0d8a816 | 458 | |
thomasmorris | 37:7c4d7f206039 | 459 | if(Log_Value == 1){cout<<"In Serial_Commands"<<endl;} |
thomasmorris | 38:8d86e0d8a816 | 460 | for (int x = 0; x < 100; x++){input[x] = ' ';}; |
thomasmorris | 38:8d86e0d8a816 | 461 | |
thomasmorris | 37:7c4d7f206039 | 462 | cout << "Please type in a command" << endl;//Log this |
chills | 34:c0b8705f183d | 463 | |
thomasmorris | 38:8d86e0d8a816 | 464 | cin.getline(input,sizeof(input),'\r'); |
thomasmorris | 38:8d86e0d8a816 | 465 | |
thomasmorris | 38:8d86e0d8a816 | 466 | //READ ALL FIX THIS |
thomasmorris | 39:5c499989d2b9 | 467 | if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ' & input[5] == 'A' & input[6] == 'L' & input[7] == 'L') |
thomasmorris | 22:eb4cc12087b2 | 468 | { |
thomasmorris | 38:8d86e0d8a816 | 469 | if(Log_Value == 1){cout<<"READ ALL Confirmed"<<endl;} |
thomasmorris | 39:5c499989d2b9 | 470 | |
thomasmorris | 41:859b5e1e3d9a | 471 | Data_Buffer_Lock.lock();//Lock data buffer due to critical section |
thomasmorris | 39:5c499989d2b9 | 472 | int Start_Address_Read_All = Write_Pointer; |
thomasmorris | 41:859b5e1e3d9a | 473 | Data_Buffer_Lock.unlock();//unLock data buffer due to critical section |
thomasmorris | 39:5c499989d2b9 | 474 | |
thomasmorris | 39:5c499989d2b9 | 475 | for(int x_Read_All = (Start_Address_Read_All - mailsize); x_Read_All != Start_Address_Read_All; x_Read_All = x_Read_All ) |
thomasmorris | 39:5c499989d2b9 | 476 | { |
thomasmorris | 39:5c499989d2b9 | 477 | if(x_Read_All < 0){x_Read_All = x_Read_All + mailsize;} |
thomasmorris | 39:5c499989d2b9 | 478 | else {x_Read_All = x_Read_All;} |
thomasmorris | 39:5c499989d2b9 | 479 | |
thomasmorris | 39:5c499989d2b9 | 480 | DATA Serial_Read_Data = Read_Data(x_Read_All); |
thomasmorris | 39:5c499989d2b9 | 481 | |
thomasmorris | 39:5c499989d2b9 | 482 | time_t Time = Serial_Read_Data.get_time(); |
thomasmorris | 39:5c499989d2b9 | 483 | tm* Time_Pointer = localtime(&Time); |
thomasmorris | 39:5c499989d2b9 | 484 | |
thomasmorris | 39:5c499989d2b9 | 485 | strftime(scom_time_buffer, 32, "%I:%M %p\t", Time_Pointer); //Format time as a string |
thomasmorris | 39:5c499989d2b9 | 486 | pc.printf("Date = %d %d %d\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900)); |
thomasmorris | 39:5c499989d2b9 | 487 | pc.printf("Time = %s", scom_time_buffer); //Print the string formatted time |
thomasmorris | 39:5c499989d2b9 | 488 | pc.printf("Temperature = %f\t", Serial_Read_Data.get_temperature()); //Print Temperature |
thomasmorris | 39:5c499989d2b9 | 489 | pc.printf("Pressure = %f\t", Serial_Read_Data.get_pressure()); //Print Pressure |
thomasmorris | 39:5c499989d2b9 | 490 | pc.printf("Light = %f\n\r", Serial_Read_Data.get_light()); //Print Light |
thomasmorris | 39:5c499989d2b9 | 491 | |
thomasmorris | 39:5c499989d2b9 | 492 | if(x_Read_All == mailsize - 1){x_Read_All = 0;} |
thomasmorris | 39:5c499989d2b9 | 493 | else {x_Read_All = x_Read_All + 1;} |
thomasmorris | 39:5c499989d2b9 | 494 | } |
thomasmorris | 38:8d86e0d8a816 | 495 | } |
thomasmorris | 38:8d86e0d8a816 | 496 | |
thomasmorris | 42:ae1640bca2e1 | 497 | //DELETE ALL FIX THIS - Might need to disable sampling during the delete |
thomasmorris | 38:8d86e0d8a816 | 498 | else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ' & input[7] == 'A' & input[8] == 'L' & input[9] == 'L' & input[10] == ' ') |
chills | 26:78f5e454e59f | 499 | { |
thomasmorris | 38:8d86e0d8a816 | 500 | if(Log_Value == 1){cout<<"DELETE ALL Confirmed"<<endl;} |
thomasmorris | 42:ae1640bca2e1 | 501 | |
thomasmorris | 42:ae1640bca2e1 | 502 | Data_Buffer_Lock.lock(); |
thomasmorris | 42:ae1640bca2e1 | 503 | int Start_Address = Write_Pointer; |
thomasmorris | 42:ae1640bca2e1 | 504 | Data_Buffer_Lock.unlock(); |
thomasmorris | 42:ae1640bca2e1 | 505 | |
thomasmorris | 42:ae1640bca2e1 | 506 | for(int x_Delete_All = (Start_Address - mailsize); x_Delete_All != Start_Address; x_Delete_All = x_Delete_All) |
thomasmorris | 42:ae1640bca2e1 | 507 | { |
thomasmorris | 42:ae1640bca2e1 | 508 | if(x_Delete_All < 0){x_Delete_All = x_Delete_All + mailsize;} |
thomasmorris | 42:ae1640bca2e1 | 509 | else {x_Delete_All = x_Delete_All;} |
thomasmorris | 42:ae1640bca2e1 | 510 | |
thomasmorris | 42:ae1640bca2e1 | 511 | Delete_Data(x_Delete_All); |
thomasmorris | 42:ae1640bca2e1 | 512 | |
thomasmorris | 42:ae1640bca2e1 | 513 | if(x_Delete_All == mailsize - 1){x_Delete_All = 0;} |
thomasmorris | 42:ae1640bca2e1 | 514 | else {x_Delete_All = x_Delete_All + 1;} |
thomasmorris | 42:ae1640bca2e1 | 515 | |
thomasmorris | 42:ae1640bca2e1 | 516 | } |
thomasmorris | 38:8d86e0d8a816 | 517 | } |
thomasmorris | 38:8d86e0d8a816 | 518 | |
thomasmorris | 41:859b5e1e3d9a | 519 | //READ |
thomasmorris | 38:8d86e0d8a816 | 520 | else if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ') |
thomasmorris | 38:8d86e0d8a816 | 521 | { |
thomasmorris | 38:8d86e0d8a816 | 522 | int NumberOfChars = 0; int ArrayAddress = 0; string ReadNumber; int NumberToRead; |
thomasmorris | 38:8d86e0d8a816 | 523 | while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} |
thomasmorris | 38:8d86e0d8a816 | 524 | for(int x=5; x < NumberOfChars; x++){ReadNumber += input[x];} |
thomasmorris | 38:8d86e0d8a816 | 525 | stringstream Number(ReadNumber); |
thomasmorris | 38:8d86e0d8a816 | 526 | Number >> NumberToRead; |
thomasmorris | 38:8d86e0d8a816 | 527 | |
thomasmorris | 39:5c499989d2b9 | 528 | |
thomasmorris | 39:5c499989d2b9 | 529 | if(Log_Value == 1){cout<<"Getting Data"<<endl;} |
thomasmorris | 39:5c499989d2b9 | 530 | |
thomasmorris | 41:859b5e1e3d9a | 531 | Data_Buffer_Lock.lock();//Lock data buffer due to critical section |
thomasmorris | 39:5c499989d2b9 | 532 | int Start_Address = Write_Pointer; |
thomasmorris | 41:859b5e1e3d9a | 533 | Data_Buffer_Lock.unlock();//unLock data buffer due to critical section |
thomasmorris | 39:5c499989d2b9 | 534 | |
thomasmorris | 39:5c499989d2b9 | 535 | for(int x = (Start_Address - NumberToRead); x != Start_Address; x = x ) |
thomasmorris | 39:5c499989d2b9 | 536 | { |
thomasmorris | 39:5c499989d2b9 | 537 | if(x < 0){x = x + mailsize;} |
thomasmorris | 39:5c499989d2b9 | 538 | else {x = x;} |
thomasmorris | 39:5c499989d2b9 | 539 | |
thomasmorris | 39:5c499989d2b9 | 540 | DATA Serial_Read_Data = Read_Data(x); |
thomasmorris | 39:5c499989d2b9 | 541 | |
thomasmorris | 39:5c499989d2b9 | 542 | time_t Time = Serial_Read_Data.get_time(); |
thomasmorris | 39:5c499989d2b9 | 543 | tm* Time_Pointer = localtime(&Time); |
thomasmorris | 39:5c499989d2b9 | 544 | |
thomasmorris | 39:5c499989d2b9 | 545 | strftime(scom_time_buffer, 32, "%I:%M %p\t", Time_Pointer); //Format time as a string |
thomasmorris | 39:5c499989d2b9 | 546 | pc.printf("Date = %d %d %d\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900)); |
thomasmorris | 39:5c499989d2b9 | 547 | pc.printf("Time = %s", scom_time_buffer); //Print the string formatted time |
thomasmorris | 39:5c499989d2b9 | 548 | pc.printf("Temperature = %f\t", Serial_Read_Data.get_temperature()); //Print Temperature |
thomasmorris | 39:5c499989d2b9 | 549 | pc.printf("Pressure = %f\t", Serial_Read_Data.get_pressure()); //Print Pressure |
thomasmorris | 39:5c499989d2b9 | 550 | pc.printf("Light = %f\n\r", Serial_Read_Data.get_light()); //Print Light |
thomasmorris | 39:5c499989d2b9 | 551 | |
thomasmorris | 39:5c499989d2b9 | 552 | if(x == mailsize - 1){x = 0;} |
thomasmorris | 39:5c499989d2b9 | 553 | else {x = x + 1;} |
thomasmorris | 39:5c499989d2b9 | 554 | |
thomasmorris | 39:5c499989d2b9 | 555 | Green_led.Toggle(); |
thomasmorris | 39:5c499989d2b9 | 556 | } |
thomasmorris | 39:5c499989d2b9 | 557 | //Serial_Comms_Data_Get(); |
thomasmorris | 38:8d86e0d8a816 | 558 | if(Log_Value == 1){cout << "Read " << NumberToRead << " samples" << endl;} |
thomasmorris | 38:8d86e0d8a816 | 559 | |
thomasmorris | 25:36699ed589ab | 560 | } |
thomasmorris | 38:8d86e0d8a816 | 561 | |
thomasmorris | 42:ae1640bca2e1 | 562 | //DELETE look into this mgiht need to turn off sampling |
thomasmorris | 38:8d86e0d8a816 | 563 | else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ') |
thomasmorris | 22:eb4cc12087b2 | 564 | { |
thomasmorris | 38:8d86e0d8a816 | 565 | int NumberOfChars = 0; int ArrayAddress = 0; string DeleteNumber; int NumberToDelete; |
thomasmorris | 38:8d86e0d8a816 | 566 | while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} |
thomasmorris | 38:8d86e0d8a816 | 567 | for(int x=7; x < NumberOfChars; x++){DeleteNumber += input[x];} |
thomasmorris | 38:8d86e0d8a816 | 568 | stringstream Number(DeleteNumber); |
thomasmorris | 38:8d86e0d8a816 | 569 | Number >> NumberToDelete; |
thomasmorris | 38:8d86e0d8a816 | 570 | |
thomasmorris | 38:8d86e0d8a816 | 571 | if(Log_Value == 1){cout << "Deleted " << NumberToDelete << " samples" << endl;} |
thomasmorris | 38:8d86e0d8a816 | 572 | |
thomasmorris | 42:ae1640bca2e1 | 573 | Data_Buffer_Lock.lock(); |
thomasmorris | 42:ae1640bca2e1 | 574 | int Start_Address = Write_Pointer; |
thomasmorris | 42:ae1640bca2e1 | 575 | Data_Buffer_Lock.unlock(); |
thomasmorris | 42:ae1640bca2e1 | 576 | |
thomasmorris | 42:ae1640bca2e1 | 577 | for(int x_Delete = (Start_Address - NumberToDelete); x_Delete != Start_Address; x_Delete = x_Delete ) |
thomasmorris | 42:ae1640bca2e1 | 578 | { |
thomasmorris | 42:ae1640bca2e1 | 579 | if(x_Delete < 0){x_Delete = x_Delete + mailsize;} |
thomasmorris | 42:ae1640bca2e1 | 580 | else {x_Delete = x_Delete;} |
thomasmorris | 42:ae1640bca2e1 | 581 | |
thomasmorris | 42:ae1640bca2e1 | 582 | Delete_Data(x_Delete); |
thomasmorris | 42:ae1640bca2e1 | 583 | |
thomasmorris | 42:ae1640bca2e1 | 584 | if(x_Delete == mailsize - 1){x_Delete = 0;} |
thomasmorris | 42:ae1640bca2e1 | 585 | else {x_Delete = x_Delete + 1;} |
thomasmorris | 42:ae1640bca2e1 | 586 | |
thomasmorris | 42:ae1640bca2e1 | 587 | } |
thomasmorris | 42:ae1640bca2e1 | 588 | //Serial_Comms_Data_Get(); |
thomasmorris | 42:ae1640bca2e1 | 589 | if(Log_Value == 1){cout << "Deleted " << NumberToDelete << " samples" << endl;} |
thomasmorris | 42:ae1640bca2e1 | 590 | |
chills | 34:c0b8705f183d | 591 | } |
thomasmorris | 38:8d86e0d8a816 | 592 | |
thomasmorris | 38:8d86e0d8a816 | 593 | //STATE |
thomasmorris | 38:8d86e0d8a816 | 594 | else if(input[0] == 'S' & input[1] == 'T' & input[2] == 'A' & input[3] == 'T' & input[4] == 'E' & input[5] == ' ') |
chills | 34:c0b8705f183d | 595 | { |
thomasmorris | 38:8d86e0d8a816 | 596 | int NumberOfChars = 0; int ArrayAddress = 0; string StateNumber; int NumberToState; |
thomasmorris | 38:8d86e0d8a816 | 597 | while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} |
thomasmorris | 38:8d86e0d8a816 | 598 | for(int x=6; x < NumberOfChars; x++){StateNumber += input[x];} |
thomasmorris | 38:8d86e0d8a816 | 599 | stringstream Number(StateNumber); |
thomasmorris | 38:8d86e0d8a816 | 600 | Number >> NumberToState; |
thomasmorris | 38:8d86e0d8a816 | 601 | |
thomasmorris | 41:859b5e1e3d9a | 602 | if(NumberToState==0){Sampling_Timer.detach(); Console_Output_Timer.detach();}//Stop Sampling |
thomasmorris | 41:859b5e1e3d9a | 603 | if(NumberToState==1){Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);}//Start Sampling |
chills | 26:78f5e454e59f | 604 | } |
thomasmorris | 38:8d86e0d8a816 | 605 | |
thomasmorris | 38:8d86e0d8a816 | 606 | //SETDATE |
thomasmorris | 38:8d86e0d8a816 | 607 | 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] == ' ') |
thomasmorris | 38:8d86e0d8a816 | 608 | { |
thomasmorris | 41:859b5e1e3d9a | 609 | //int NumberOfChars = 0; int ArrayAddress = 0; |
thomasmorris | 38:8d86e0d8a816 | 610 | string DayNumber, MonthNumber, YearNumber; |
thomasmorris | 38:8d86e0d8a816 | 611 | int NumberToDay, NumberToMonth, NumberToYear; |
thomasmorris | 38:8d86e0d8a816 | 612 | |
thomasmorris | 38:8d86e0d8a816 | 613 | for(int x=8; x < 10; x++){DayNumber += input[x];} |
thomasmorris | 38:8d86e0d8a816 | 614 | stringstream Number_1(DayNumber); |
thomasmorris | 38:8d86e0d8a816 | 615 | Number_1 >> NumberToDay; |
thomasmorris | 38:8d86e0d8a816 | 616 | |
thomasmorris | 38:8d86e0d8a816 | 617 | for(int x=11; x < 13; x++){MonthNumber += input[x];} |
thomasmorris | 38:8d86e0d8a816 | 618 | stringstream Number_2(MonthNumber); |
thomasmorris | 38:8d86e0d8a816 | 619 | Number_2 >> NumberToMonth; |
thomasmorris | 38:8d86e0d8a816 | 620 | |
thomasmorris | 38:8d86e0d8a816 | 621 | for(int x=14; x < 18; x++){YearNumber += input[x];} |
thomasmorris | 38:8d86e0d8a816 | 622 | stringstream Number_3(YearNumber); |
thomasmorris | 38:8d86e0d8a816 | 623 | Number_3 >> NumberToYear; |
thomasmorris | 38:8d86e0d8a816 | 624 | |
thomasmorris | 38:8d86e0d8a816 | 625 | set_new_date(NumberToDay,NumberToMonth,NumberToYear); |
thomasmorris | 38:8d86e0d8a816 | 626 | } |
thomasmorris | 38:8d86e0d8a816 | 627 | |
thomasmorris | 38:8d86e0d8a816 | 628 | //SETTIME |
thomasmorris | 38:8d86e0d8a816 | 629 | 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] == ' ') |
thomasmorris | 22:eb4cc12087b2 | 630 | { |
thomasmorris | 41:859b5e1e3d9a | 631 | //int NumberOfChars = 0; int ArrayAddress = 0; |
thomasmorris | 38:8d86e0d8a816 | 632 | string HourNumber, MinuteNumber, SecondNumber; |
thomasmorris | 38:8d86e0d8a816 | 633 | int NumberToHour, NumberToMinute, NumberToSecond; |
thomasmorris | 38:8d86e0d8a816 | 634 | |
thomasmorris | 38:8d86e0d8a816 | 635 | for(int x=8; x < 10; x++){HourNumber += input[x];} |
thomasmorris | 38:8d86e0d8a816 | 636 | stringstream Number_1(HourNumber); |
thomasmorris | 38:8d86e0d8a816 | 637 | Number_1 >> NumberToHour; |
thomasmorris | 38:8d86e0d8a816 | 638 | |
thomasmorris | 38:8d86e0d8a816 | 639 | for(int x=11; x < 13; x++){MinuteNumber += input[x];} |
thomasmorris | 38:8d86e0d8a816 | 640 | stringstream Number_2(MinuteNumber); |
thomasmorris | 38:8d86e0d8a816 | 641 | Number_2 >> NumberToMinute; |
thomasmorris | 38:8d86e0d8a816 | 642 | |
thomasmorris | 38:8d86e0d8a816 | 643 | for(int x=14; x < 16; x++){SecondNumber += input[x];} |
thomasmorris | 38:8d86e0d8a816 | 644 | stringstream Number_3(SecondNumber); |
thomasmorris | 38:8d86e0d8a816 | 645 | Number_3 >> NumberToSecond; |
thomasmorris | 38:8d86e0d8a816 | 646 | |
thomasmorris | 38:8d86e0d8a816 | 647 | set_new_time(NumberToHour,NumberToMinute,NumberToSecond); |
thomasmorris | 38:8d86e0d8a816 | 648 | } |
thomasmorris | 38:8d86e0d8a816 | 649 | |
thomasmorris | 38:8d86e0d8a816 | 650 | //SETT |
thomasmorris | 38:8d86e0d8a816 | 651 | else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'T' & input[4] == ' ') |
thomasmorris | 38:8d86e0d8a816 | 652 | { |
thomasmorris | 38:8d86e0d8a816 | 653 | |
thomasmorris | 38:8d86e0d8a816 | 654 | int NumberOfChars = 0; int ArrayAddress = 0; string SettNumber; int NumberToSett; |
thomasmorris | 38:8d86e0d8a816 | 655 | while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} |
thomasmorris | 38:8d86e0d8a816 | 656 | for(int x=5; x < NumberOfChars; x++){SettNumber += input[x];} |
thomasmorris | 38:8d86e0d8a816 | 657 | stringstream Number(SettNumber); |
thomasmorris | 38:8d86e0d8a816 | 658 | Number >> NumberToSett; |
thomasmorris | 38:8d86e0d8a816 | 659 | |
thomasmorris | 38:8d86e0d8a816 | 660 | if(NumberToSett >= 1){Sample_Rate = NumberToSett;} |
chills | 26:78f5e454e59f | 661 | |
chills | 26:78f5e454e59f | 662 | Sampling_Timer.detach(); |
chills | 26:78f5e454e59f | 663 | Console_Output_Timer.detach(); |
chills | 26:78f5e454e59f | 664 | Sampling_Timer.attach(&Sampling_ISR,Sample_Rate); |
thomasmorris | 38:8d86e0d8a816 | 665 | } |
thomasmorris | 38:8d86e0d8a816 | 666 | |
thomasmorris | 38:8d86e0d8a816 | 667 | //LOGGING |
thomasmorris | 38:8d86e0d8a816 | 668 | else if(input[0] == 'L' & input[1] == 'O' & input[2] == 'G' & input[3] == 'G' & input[4] == 'I' & input[5] == 'N' & input[6] == 'G' & input[7] == ' ') |
thomasmorris | 38:8d86e0d8a816 | 669 | { |
thomasmorris | 38:8d86e0d8a816 | 670 | int NumberOfChars = 0; int ArrayAddress = 0; string LoggingNumber; int NumberToLogging; |
thomasmorris | 38:8d86e0d8a816 | 671 | while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} |
thomasmorris | 38:8d86e0d8a816 | 672 | for(int x=8; x < NumberOfChars; x++){LoggingNumber += input[x];} |
thomasmorris | 38:8d86e0d8a816 | 673 | stringstream Number(LoggingNumber); |
thomasmorris | 38:8d86e0d8a816 | 674 | Number >> NumberToLogging; |
thomasmorris | 38:8d86e0d8a816 | 675 | |
thomasmorris | 38:8d86e0d8a816 | 676 | cout << "Successfully entered logging" << endl; |
thomasmorris | 38:8d86e0d8a816 | 677 | cout << NumberToLogging << endl; |
thomasmorris | 38:8d86e0d8a816 | 678 | Log_Value = NumberToLogging; |
thomasmorris | 25:36699ed589ab | 679 | } |
thomasmorris | 38:8d86e0d8a816 | 680 | |
thomasmorris | 38:8d86e0d8a816 | 681 | //HELP |
thomasmorris | 38:8d86e0d8a816 | 682 | else if(input[0] == 'H' & input[1] == 'E' & input[2] == 'L' & input[3] == 'P')// Use this to display all of the availble commands |
thomasmorris | 25:36699ed589ab | 683 | { |
thomasmorris | 41:859b5e1e3d9a | 684 | HELP();//Run HELP Command |
thomasmorris | 22:eb4cc12087b2 | 685 | } |
thomasmorris | 38:8d86e0d8a816 | 686 | |
thomasmorris | 38:8d86e0d8a816 | 687 | //STOP |
thomasmorris | 38:8d86e0d8a816 | 688 | else if(input[0] == 'S' & input[1] == 'T' & input[2] == 'O' & input[3] == 'P') |
chills | 26:78f5e454e59f | 689 | { |
thomasmorris | 28:09b5c46c8afd | 690 | Console_Output_Timer.detach(); //Stops Sampling |
chills | 26:78f5e454e59f | 691 | } |
thomasmorris | 25:36699ed589ab | 692 | else |
thomasmorris | 22:eb4cc12087b2 | 693 | { |
chills | 26:78f5e454e59f | 694 | cout << "Please enter an acceptable command" << endl; |
thomasmorris | 22:eb4cc12087b2 | 695 | } |
thomasmorris | 22:eb4cc12087b2 | 696 | } |
thomasmorris | 22:eb4cc12087b2 | 697 | } |
thomasmorris | 38:8d86e0d8a816 | 698 | void Serial_Comms_Data_Get() |
thomasmorris | 29:64b1f95a807c | 699 | { |
thomasmorris | 37:7c4d7f206039 | 700 | if(Log_Value == 1){cout<<"Getting Data"<<endl;} |
chills | 34:c0b8705f183d | 701 | time_t Time = Data_Active.get_time(); |
chills | 34:c0b8705f183d | 702 | tm* Time_Pointer = localtime(&Time); |
chills | 34:c0b8705f183d | 703 | strftime(scom_time_buffer, 32, "%I:%M %p\t", Time_Pointer); //Format time as a string |
chills | 34:c0b8705f183d | 704 | pc.printf("Date = %d %d %d\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900)); |
chills | 34:c0b8705f183d | 705 | pc.printf("Time = %s", scom_time_buffer); //Print the string formatted time |
chills | 34:c0b8705f183d | 706 | pc.printf("Temperature = %f\t", Data_Active.get_temperature()); //Print Temperature |
chills | 34:c0b8705f183d | 707 | pc.printf("Pressure = %f\t", Data_Active.get_pressure()); //Print Pressure |
chills | 34:c0b8705f183d | 708 | pc.printf("Light = %f\n\r", Data_Active.get_light()); //Print Light |
thomasmorris | 29:64b1f95a807c | 709 | |
thomasmorris | 29:64b1f95a807c | 710 | Green_led.Toggle(); |
chills | 34:c0b8705f183d | 711 | |
thomasmorris | 29:64b1f95a807c | 712 | } |
thomasmorris | 5:2594b953f111 | 713 | |
thomasmorris | 41:859b5e1e3d9a | 714 | void Sampling_ISR(){t1.signal_set(SamplingTime);} |
chills | 26:78f5e454e59f | 715 | |
chills | 26:78f5e454e59f | 716 | |
thomasmorris | 5:2594b953f111 | 717 | void Sample()//Samples the hardware and prints the result to the LCD |
thomasmorris | 5:2594b953f111 | 718 | { |
thomasmorris | 30:4cde05cc7c4f | 719 | while(1) |
thomasmorris | 30:4cde05cc7c4f | 720 | { |
chills | 10:46946784326d | 721 | Thread::signal_wait(SamplingTime); //Set the time between samples |
thomasmorris | 37:7c4d7f206039 | 722 | if(Log_Value == 1){cout<<"Sample Time"<<endl;} |
chills | 34:c0b8705f183d | 723 | Data_Active.set_temperature(sensor.getTemperature()); //Read Temperature |
chills | 34:c0b8705f183d | 724 | Data_Active.set_pressure(sensor.getPressure()); //Read Pressure |
chills | 34:c0b8705f183d | 725 | Data_Active.set_light(adcIn.read()); //Read Light |
chills | 33:3b5096f0126a | 726 | |
chills | 33:3b5096f0126a | 727 | Time_Lock_Main.lock(); |
chills | 34:c0b8705f183d | 728 | time_t buffer_time = time(NULL); |
chills | 33:3b5096f0126a | 729 | Time_Lock_Main.unlock(); |
thomasmorris | 25:36699ed589ab | 730 | |
chills | 34:c0b8705f183d | 731 | Data_Active.set_time(buffer_time); |
thomasmorris | 25:36699ed589ab | 732 | |
thomasmorris | 5:2594b953f111 | 733 | Red_led.Toggle(); |
thomasmorris | 39:5c499989d2b9 | 734 | Write_Pointer = Write_Data(Data_Active,Write_Pointer); |
thomasmorris | 5:2594b953f111 | 735 | t1.signal_set(NotSamplingTime); |
thomasmorris | 37:7c4d7f206039 | 736 | if(Log_Value == 1){cout<<"New Sample avaliable"<<endl;} |
thomasmorris | 41:859b5e1e3d9a | 737 | t2.signal_set(SD_Data_Ready); |
thomasmorris | 5:2594b953f111 | 738 | } |
thomasmorris | 25:36699ed589ab | 739 | } |
thomasmorris | 25:36699ed589ab | 740 | int main() |
thomasmorris | 25:36699ed589ab | 741 | { |
thomasmorris | 37:7c4d7f206039 | 742 | set_time(1515352584); //Set RTC time to December 10 2017 |
thomasmorris | 30:4cde05cc7c4f | 743 | |
chills | 10:46946784326d | 744 | pc.baud(9600); //Sets the Serial Comms Baud Rate |
thomasmorris | 25:36699ed589ab | 745 | |
chills | 20:cbb71f84cff9 | 746 | LCD.Initialise(); |
thomasmorris | 28:09b5c46c8afd | 747 | LCD.DDRAM_Address(0x00); |
thomasmorris | 29:64b1f95a807c | 748 | |
thomasmorris | 25:36699ed589ab | 749 | post(); //Power on Self Test |
thomasmorris | 25:36699ed589ab | 750 | |
noutram | 3:a88838ff33e7 | 751 | //Initialise the SD card (this needs to move) |
noutram | 1:e1cf7663f5ff | 752 | if ( sd.init() != 0) { |
noutram | 1:e1cf7663f5ff | 753 | printf("Init failed \n"); |
thomasmorris | 28:09b5c46c8afd | 754 | LCD.Display_Clear(); |
thomasmorris | 28:09b5c46c8afd | 755 | LCD.Write_String("CANNOT INIT SD"); //Change me |
noutram | 1:e1cf7663f5ff | 756 | errorCode(FATAL); |
thomasmorris | 25:36699ed589ab | 757 | } |
noutram | 1:e1cf7663f5ff | 758 | //Create a filing system for SD Card |
thomasmorris | 25:36699ed589ab | 759 | FATFileSystem fs("sd", &sd); |
noutram | 0:36e89e3ed7c4 | 760 | |
noutram | 1:e1cf7663f5ff | 761 | //Open to WRITE |
thomasmorris | 41:859b5e1e3d9a | 762 | FILE* fp = fopen("/sd/test.csv","a");//test.csv is created in the SD Card |
noutram | 1:e1cf7663f5ff | 763 | if (fp == NULL) { |
noutram | 1:e1cf7663f5ff | 764 | error("Could not open file for write\n"); |
thomasmorris | 28:09b5c46c8afd | 765 | LCD.Display_Clear(); |
thomasmorris | 37:7c4d7f206039 | 766 | LCD.Write_String("CANNOT OPEN FILE"); |
noutram | 1:e1cf7663f5ff | 767 | errorCode(FATAL); |
noutram | 1:e1cf7663f5ff | 768 | } |
thomasmorris | 41:859b5e1e3d9a | 769 | //Close File |
thomasmorris | 41:859b5e1e3d9a | 770 | fclose(fp); |
thomasmorris | 37:7c4d7f206039 | 771 | int network_temp; |
thomasmorris | 37:7c4d7f206039 | 772 | network_temp = Network_Init(); |
thomasmorris | 37:7c4d7f206039 | 773 | if(network_temp == 1)//Sets up the network and checks if the network cable is not pluged in |
thomasmorris | 37:7c4d7f206039 | 774 | { |
thomasmorris | 37:7c4d7f206039 | 775 | error("Could not access network"); |
thomasmorris | 37:7c4d7f206039 | 776 | LCD.Display_Clear(); |
thomasmorris | 37:7c4d7f206039 | 777 | LCD.Write_String("Could not access network"); |
thomasmorris | 41:859b5e1e3d9a | 778 | errorCode(NETWORK_FATAL); |
thomasmorris | 37:7c4d7f206039 | 779 | } |
noutram | 3:a88838ff33e7 | 780 | //Last message before sampling begins |
thomasmorris | 28:09b5c46c8afd | 781 | LCD.Display_Clear(); |
thomasmorris | 35:26b0a9b55d82 | 782 | LCD.Write_String("READY PLAYER"); |
thomasmorris | 31:4a88bf97b53e | 783 | LCD.DDRAM_Address(0x40); |
thomasmorris | 35:26b0a9b55d82 | 784 | LCD.Write_String(" One "); |
thomasmorris | 31:4a88bf97b53e | 785 | LCD.DDRAM_Address(0x00); |
thomasmorris | 25:36699ed589ab | 786 | |
thomasmorris | 25:36699ed589ab | 787 | Sample_Rate = TimerInterval; |
chills | 24:7d2da96e05ad | 788 | //Run interrupt |
chills | 26:78f5e454e59f | 789 | Sampling_Timer.attach(&Sampling_ISR,Sample_Rate); |
chills | 24:7d2da96e05ad | 790 | |
thomasmorris | 5:2594b953f111 | 791 | t1.start(Sample); |
thomasmorris | 41:859b5e1e3d9a | 792 | t2.start(SD_Card); |
thomasmorris | 30:4cde05cc7c4f | 793 | t3.start(LCD_Output); |
thomasmorris | 35:26b0a9b55d82 | 794 | t4.start(Network); |
thomasmorris | 23:3c85d7f657a2 | 795 | t5.start(Serial_Commands); |
thomasmorris | 25:36699ed589ab | 796 | |
thomasmorris | 5:2594b953f111 | 797 | //Main thread ID |
thomasmorris | 25:36699ed589ab | 798 | |
thomasmorris | 5:2594b953f111 | 799 | idMain = osThreadGetId(); //CMSIS RTOS call |
thomasmorris | 25:36699ed589ab | 800 | |
thomasmorris | 5:2594b953f111 | 801 | //Thread ID |
thomasmorris | 5:2594b953f111 | 802 | id1 = t1.gettid(); |
thomasmorris | 5:2594b953f111 | 803 | id2 = t2.gettid(); |
thomasmorris | 21:3c078c799caa | 804 | id3 = t3.gettid(); |
thomasmorris | 21:3c078c799caa | 805 | id4 = t4.gettid(); |
chills | 24:7d2da96e05ad | 806 | id5 = t5.gettid(); |
thomasmorris | 25:36699ed589ab | 807 | |
noutram | 1:e1cf7663f5ff | 808 | while(true) { |
thomasmorris | 41:859b5e1e3d9a | 809 | if(onBoardSwitch == 1) |
thomasmorris | 41:859b5e1e3d9a | 810 | { |
thomasmorris | 41:859b5e1e3d9a | 811 | fclose(fp);//Close File |
thomasmorris | 41:859b5e1e3d9a | 812 | sd.deinit();//Close down |
thomasmorris | 41:859b5e1e3d9a | 813 | LCD.Display_Clear(); |
thomasmorris | 41:859b5e1e3d9a | 814 | LCD.DDRAM_Address(0x00); |
thomasmorris | 41:859b5e1e3d9a | 815 | LCD.Write_String("SD Card"); |
thomasmorris | 41:859b5e1e3d9a | 816 | LCD.DDRAM_Address(0x40); |
thomasmorris | 41:859b5e1e3d9a | 817 | LCD.Write_String("Unmounted"); |
thomasmorris | 41:859b5e1e3d9a | 818 | LCD.DDRAM_Address(0x00); |
thomasmorris | 41:859b5e1e3d9a | 819 | cout << "SD Card Unmounted" << endl; |
thomasmorris | 41:859b5e1e3d9a | 820 | } |
noutram | 1:e1cf7663f5ff | 821 | greenLED = 1; |
noutram | 1:e1cf7663f5ff | 822 | wait(0.5); |
noutram | 1:e1cf7663f5ff | 823 | greenLED = 0; |
thomasmorris | 25:36699ed589ab | 824 | wait(0.1); |
noutram | 0:36e89e3ed7c4 | 825 | } |
thomasmorris | 41:859b5e1e3d9a | 826 | }//End of Main |