3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
Diff: main.cpp
- Revision:
- 78:6c2b8ade8414
- Parent:
- 77:db3384071634
--- a/main.cpp Wed Apr 26 21:58:08 2017 +0000 +++ b/main.cpp Thu Apr 27 17:09:50 2017 +0000 @@ -16,6 +16,7 @@ #define SIGNAL_doMeasure 1 #define SIGNAL_printMessage 2 +#define SIGNAL_terminate 3 #define SWITCH1_RELEASE 90 #define BUFFER_SIZE 120 @@ -39,6 +40,7 @@ // // THREADS DECLARATION // +osThreadId mainThreadID; Thread *produceThread; Thread *measureThread; Thread *consumeThread; @@ -91,7 +93,7 @@ Measure *measure = mail_box.alloc(); if (measure == NULL) { - printf("Out of memory\n\r"); + logger.SendError("Out of memory"); return; } @@ -108,8 +110,12 @@ osStatus stat = mail_box.put(measure); //Check if succesful - if (stat == osErrorResource) { - printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); + if (stat == osErrorResource) + { + char* c; + sprintf(c, "%4X - Resource not available\n\r", stat); + string s(c); + logger.SendError(s); mail_box.free(measure); return; } @@ -135,8 +141,13 @@ // Changed to use circlar buffer rather than list buffer buffer.pushValue(msr); mail_box.free(measure); - } else { - printf("ERROR: %x\n\r", evt.status); + } + else + { + char* c; + sprintf(c, "%4X - Failed to allocate memory\n\r", evt.status); + string s(c); + logger.SendError(s); } } @@ -170,7 +181,8 @@ char command[40]; //Current Command Size int crtChar = 0; - printf("\r\nAwaiting command:\r\n"); + logger.SendMessage("\n\rAwaiting command:\n\r"); + while(1) { charCmd = NULL; @@ -180,14 +192,20 @@ //If BACKSPACE is pressed, Print "DEL" so it deletes last character typed. if (charCmd == 127 && crtChar > 0 ) { - printf("%c",charCmd); + char* c; + sprintf(c, "%c", charCmd); + string s(c); + logger.SendMessage(s); command[--crtChar] = '\0'; } //If NOT enter AND NOT Backspace is pressed, SAVE the char else if(charCmd != 13 && charCmd != 127) { command[crtChar++] = charCmd; - printf("%c",charCmd); + char* c; + sprintf(c, "%c", charCmd); + string s(c); + logger.SendMessage(s); } //If ENTER is pressed, PROCESS it else if(charCmd == 13) // If Enter is pressed @@ -203,25 +221,28 @@ //Check if it's a "LIST ALL" command if(CompareCommands(charPos, "all",3) == 1) { - printf("\r\nPrinting all measures performed so far: \r\n"); + logger.SendMessage("\n\rPrinting all measures performed so far: \n\r"); // Changed to use circular buffer rather than list buffer buffer.readAll(); - printf("\r\nD O N E ! \r\n"); + logger.SendMessage("\n\rD O N E ! \n\r"); } //Check if it's a "LIST X" command else if(strtol(charPos,NULL,10) != 0) { int num = atoi(charPos); - printf("\r\nPrinting %i measures: \r\n",num); + char* c; + sprintf(c, "\n\rPrinting %i measures: \n\r", num); + string s(c); + logger.SendMessage(s); // Changed to use circular buffer rather than list buffer buffer.readX(num); - printf("\r\nD O N E ! \r\n"); + logger.SendMessage("\n\rD O N E ! \n\r"); } else { - printf("\n\rExpected parameters: \"all\" | \"n\", where n is a number."); + logger.SendMessage("\n\rExpected parameters: \"all\" | \"n\", where n is a number."); } } //Check if it's a "DELETE" command @@ -231,22 +252,22 @@ //Check if it's a "DELETE ALL" command if(CompareCommands(charPos,"all",3) == 1) { - printf("\r\nDeleting all measures performed so far: \r\n"); + logger.SendMessage("\n\rDeleting all measures performed so far: \n\r"); // Changed to use circular buffer rather than list buffer buffer.deleteAll(); - printf("\r\nElements deleted!\r\n"); + logger.SendMessage("\n\rElements deleted!\n\r"); } //Check if it's a "DELETE X" command else if (strtol(charPos,NULL,10) != 0) { // Changed to use circular buffer rather than list buffer buffer.deleteX(atoi(charPos)); - printf("\r\nElements deleted!\r\n"); + logger.SendMessage("\n\rElements deleted!\n\r"); } else { - printf("\n\rExpected parameters: \"all\" | \"n\", where n is a number."); + logger.SendMessage("\n\rExpected parameters: \"all\" | \"n\", where n is a number."); } } @@ -257,9 +278,19 @@ // Changed to use circular buffer rather than list buffer if(logging == true) - printf("\r\nSTATUS: \r\n # of measures: %i \r\n SAMPLING: ON \r\n Current Date: %s \r\n Sample Rate(s): %2.2f \r\n", buffer.getSize(), ptr,sampleRate); + { + char* c; + sprintf(c, "\n\rSTATUS: \n\r # of measures: %i \n\r SAMPLING: ON \n\r Current Date: %s \n\r Sample Rate(s): %2.2f \n\r", buffer.getSize(), ptr,sampleRate); + string s(c); + logger.SendMessage(s); + } else - printf("\r\nSTATUS: \r\n # of measures: %i \r\n SAMPLING: OFF \r\n Current Date: %s \r\n Sample Rate(s): %2.2f \r\n", buffer.getSize(), ptr,sampleRate); + { + char* c; + sprintf(c, "\n\rSTATUS: \n\r # of measures: %i \n\r SAMPLING: OFF \n\r Current Date: %s \n\r Sample Rate(s): %2.2f \n\r", buffer.getSize(), ptr,sampleRate); + string s(c); + logger.SendMessage(s); + } } //Check if it's a "SETTIME" command else if (CompareCommands(charPos,"settime",7) == 1) @@ -290,12 +321,15 @@ localDate->min = m; localDate->sec = s; char *ptr = localDate->ToString(); - printf("\r\nUpdated Date to: %s \r\n", ptr); + char* c; + sprintf(c, "\n\rUpdated Date to: %s \n\r", ptr); + string s(c); + logger.SendMessage(s); } //If not valid, prompt user else { - printf("\r\nWrong format! please use HH-MM-SS separated by spaces. \r\n"); + printf("\n\rWrong format! please use HH-MM-SS separated by spaces. \n\r"); } } //Check if it's a "SETDATE" command @@ -327,12 +361,12 @@ localDate->month = m; localDate->year = y; char *ptr = localDate->ToString(); - printf("\r\nUpdated Date to: %s \r\n", ptr); + printf("\n\rUpdated Date to: %s \n\r", ptr); } // Prompt user if they are not. else { - printf("\r\nWrong format! please use DD-MM-YYYY separated by spaces. \r\n"); + printf("\n\rWrong format! please use DD-MM-YYYY separated by spaces. \n\r"); } } // Check if it's a "LOGGING" command @@ -343,12 +377,12 @@ if(CompareCommands(charPos,"on",2) == 1) { logging = true; - printf("\r\nSampling turned ON!\r\n"); + printf("\n\rSampling turned ON!\n\r"); } else if (CompareCommands(charPos,"off",3) == 1) { logging = false; - printf("\r\nSampling turned OFF!\r\n"); + printf("\n\rSampling turned OFF!\n\r"); } else { @@ -367,26 +401,26 @@ sampleRate = auxRate; timer.detach(); timer.attach(&SendSignalDoMeasure, sampleRate); - printf("\r\nSuccessfully updated sample rate to: %2.2f .\r\n",sampleRate); + printf("\n\rSuccessfully updated sample rate to: %2.2f .\n\r",sampleRate); } // if rate is not valid, prompt: else { - printf("\r\n Sample rate must be between 0.1 and 60. \r\n"); + printf("\n\r Sample rate must be between 0.1 and 60. \n\r"); } } // Check if it's a "HELP" command else if (CompareCommands(charPos,"help",4) == 1 || CompareCommands(charPos,"?",1) == 1) { - printf("\r\nAvailable Commands:\r\n"); - printf(" read <ALL|N> - Read ALL or N first measures.\r\n"); - printf(" delete <ALL|N> - Delete ALL or N first measures.\r\n"); - printf(" setdate <DD> <MM> <YYYY> Set current date.\r\n"); - printf(" settime <HH> <MM> <SS> Set current time.\r\n"); - printf(" sett <T> Set sample rate (in seconds).\r\n"); - printf(" status - Status report of device.\r\n"); - printf(" state - <ON|OFF> - Turn sampling on or OFF.\r\n"); - printf(" logging <ON|OFF> - Turn logging on or OFF.\r\n"); + printf("\n\rAvailable Commands:\n\r"); + printf(" read <ALL|N> - Read ALL or N first measures.\n\r"); + printf(" delete <ALL|N> - Delete ALL or N first measures.\n\r"); + printf(" setdate <DD> <MM> <YYYY> Set current date.\n\r"); + printf(" settime <HH> <MM> <SS> Set current time.\n\r"); + printf(" sett <T> Set sample rate (in seconds).\n\r"); + printf(" status - Status report of device.\n\r"); + printf(" state - <ON|OFF> - Turn sampling on or OFF.\n\r"); + printf(" logging <ON|OFF> - Turn logging on or OFF.\n\r"); } else if(CompareCommands(charPos, "test", 4) == 1) { @@ -411,9 +445,9 @@ // If command not recognized else { - printf("\r\n Command not recognized. Type \"help\" for more info.\r\n"); + printf("\n\r Command not recognized. Type \"help\" for more info.\n\r"); } - printf("\r\nAwaiting command: \r\n"); + printf("\n\rAwaiting command: \n\r"); //Clear command! //* NOTE * Setting first char in array to '\0' WILL NOT RESET IT...for some reason. int i = 0; @@ -442,30 +476,33 @@ while(true) { Thread::signal_wait(SIGNAL_printMessage); + while(true) + + { if(logger.GetError()) { - // Kill EVERYTHING - //mbed_reset(); - //NVIC_SystemReset(); + osSignalSet(mainThreadID, SIGNAL_terminate); } else if(!logger.GetMessage()) { - + break; } - + } } } // Main thread -int main() { - +int main() +{ + mainThreadID = osThreadGetId(); + //Initialize all stuff you need here: measurer.init(); measurer.calib(); localDate = new LocalDate(); //Start message - printf("\r\n--- W E L C O M E --\r\n"); + printf("\n\r\n\r--- W E L C O M E --\n\r"); //Hook up timer interrupt timer.attach(&SendSignalDoMeasure, sampleRate); @@ -483,10 +520,18 @@ logger.SetThread(loggingThread); - printf("Main Thread\n"); while(true) { - // Is there a sleep method that could be used instead or waiting and awaking every 3 seconds? - Thread::wait(300); + osSignalWait(SIGNAL_terminate, osWaitForever); + + produceThread->terminate(); + measureThread->terminate(); + consumeThread->terminate(); + loggingThread->terminate(); + + printf("Press any key to restart... "); + char c = getchar(); + + NVIC_SystemReset(); } } \ No newline at end of file