![](/media/cache/profiles/ba5be68d3d541f2ca043d77fc54fe702.jpg.50x50_q85.jpg)
3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
Diff: main.cpp
- Revision:
- 81:996c0a3319b4
- Parent:
- 80:959151952153
- Child:
- 82:668b51a39148
--- a/main.cpp Wed May 10 10:02:22 2017 +0000 +++ b/main.cpp Thu May 11 14:34:53 2017 +0000 @@ -4,9 +4,9 @@ #include "hts221.h" #include "LPS25H.h" +#include "MessageLogger.h" #include "CircularArray.h" #include "FakeSensor.h" -#include "MessageLogger.h" #include <string.h> #include <stdio.h> @@ -16,6 +16,7 @@ #define SIGNAL_doMeasure 1 #define SIGNAL_printMessage 2 +#define SIGNAL_terminate 3 #define SWITCH1_RELEASE 90 #define BUFFER_SIZE 120 @@ -49,17 +50,16 @@ // GLOBAL VARIABLES // Mail<Measure, 16> mail_box; -CircularArray buffer(BUFFER_SIZE); LocalDate *localDate; //Mail<> bool logging = true; -float sampleRate = 1; -char* temp; +double sampleRate = 15; +char temp[256]; // Logging objects -std::ostringstream oss; MessageLogger logger; +CircularArray buffer(BUFFER_SIZE, &logger); // // Called by a TICKER // Adds 1 second every second to the clock @@ -82,7 +82,7 @@ // Reads values from sensor board, sends over through mail queue void MeasureThread() { - float temperature , humidity,pressure; + double temperature , humidity, pressure; while(true) { @@ -95,7 +95,7 @@ Measure *measure = mail_box.alloc(); if (measure == NULL) { - printf("Out of memory\n\r"); + logger.SendError("Out of memory\n\r"); return; } @@ -113,7 +113,8 @@ //Check if succesful if (stat == osErrorResource) { - printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); + snprintf(temp, 256, "queue->put() Error code: %4Xh, Resource not available\r\n", stat); + logger.SendError(temp); mail_box.free(measure); return; } @@ -140,9 +141,9 @@ buffer.pushValue(msr); mail_box.free(measure); } else { - printf("ERROR: %x\n\r", evt.status); - } - + snprintf(temp, 256, "ERROR: %x\n\r", evt.status); + logger.SendError(temp); + } } } int i; @@ -153,14 +154,16 @@ // Param3. Size of the smallest char arrays (between param1 and param2) // Return: "-1" IF NOT EQUAL // "1 " IF EQUAL -int CompareCommands(char command[],char targetcommand[], int size) +bool CompareCommands(char command[],char targetcommand[], int size) { for(i = 0; i < size; i ++) { if(command[i] != targetcommand[i]) - return -1; + { + return false; + } } - return 1; + return true; } // @@ -174,32 +177,23 @@ char command[40]; //Current Command Size int crtChar = 0; - logger.SendMessage("\r\nAwaiting command: \n\r"); - //printf("\r\nAwaiting command: \n\r"); + logger.SendMessage("\r\nAwaiting Command: \r\n"); while(1) { charCmd = NULL; charCmd = getchar(); - //uint32_t i = (*consumeThread).used_stack(); - //printf("%d" , i); if(charCmd != NULL) { //If BACKSPACE is pressed, Print "DEL" so it deletes last character typed. if (charCmd == 127 && crtChar > 0 ) { command[--crtChar] = '\0'; - printf("%c",charCmd); } //If NOT enter AND NOT Backspace is pressed, SAVE the char else if(charCmd != 13 && charCmd != 127) { command[crtChar++] = charCmd; - //char *temp; - //int i = 1337; - //sprintf(temp, "DIE YOU %d", i); - //logger.SendMessage(temp); - printf("%c",charCmd); } //If ENTER is pressed, PROCESS it else if(charCmd == 13) // If Enter is pressed @@ -209,105 +203,98 @@ charPos = strtok(command," -,"); //Check if it's a "LIST" command - if(CompareCommands(charPos, "read",4) == 1) + if(CompareCommands(charPos, "read",4)) { charPos = strtok(NULL," -,"); //Check if it's a "LIST ALL" command - if(CompareCommands(charPos, "all",3) == 1) + if(CompareCommands(charPos, "all",3)) { - logger.SendMessage("\r\nPrinting all measures performed so far: \r\n"); - //printf("\r\nPrinting all measures performed so far: \r\n"); + logger.SendMessage("Printing all measures performed so far: \r\n"); // Changed to use circular buffer rather than list buffer buffer.readAll(); - //logger.SendMessage("\r\nD O N E ! \r\n"); - printf("\r\nD O N E ! \r\n"); + logger.SendMessage("D O N E ! \r\n"); } //Check if it's a "LIST X" command else if(strtol(charPos,NULL,10) != 0) { + int num = atoi(charPos); + //char *temp1; + //strncpy(temp1, "\r\nPrinting ", 31); + //strcat(temp1, charPos); + //strcat(temp1, " measures: \r\n"); - // PROBLEMATIC (HAVING 2 SEND MESSAGE IN 1 BLOCK) - int num = atoi(charPos); - sprintf(temp, "\r\nPrinting %i measures: \r\n",num); + snprintf(temp, 256, "Printing %i measures: \r\n", num); logger.SendMessage(temp); - //printf(temp); // Changed to use circular buffer rather than list buffer buffer.readX(num); - logger.SendMessage("\r\nD O N E ! \r\n"); + logger.SendMessage("D O N E ! \r\n"); } else { - logger.SendMessage("\n\rExpected parameters: \"all\" | \"n\", where n is a number."); - //printf("\n\rExpected parameters: \"all\" | \"n\", where n is a number."); + logger.SendMessage("Expected parameters: \"all\" | \"n\", where n is a number.\r\n"); } } //Check if it's a "DELETE" command - else if (CompareCommands(charPos,"delete",6) == 1) + else if (CompareCommands(charPos,"delete",6)) { charPos = strtok(NULL," -,"); //Check if it's a "DELETE ALL" command - if(CompareCommands(charPos,"all",3) == 1) + if(CompareCommands(charPos,"all",3)) { - //logger.SendMessage("\r\nDeleting all measures performed so far: \r\n"); - printf("\r\nDeleting all measures performed so far: \r\n"); + logger.SendMessage("Deleting all measures performed so far: \r\n"); // Changed to use circular buffer rather than list buffer buffer.deleteAll(); - //logger.SendMessage("\r\nElements deleted!\r\n"); - printf("\r\nElements deleted!\r\n"); + logger.SendMessage("Elements deleted!\r\n"); } //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)); - //logger.SendMessage("\r\nElements deleted!\r\n"); - printf("\r\nElements deleted!\r\n"); + logger.SendMessage("Elements deleted!\r\n"); } else { - //logger.SendMessage("\n\rExpected parameters: \"all\" | \"n\", where n is a number."); - printf(temp); - } - + logger.SendMessage("Expected parameters: \"all\" | \"n\", where n is a number."); + } } //Check if it's a "STATUS" command - else if (CompareCommands(charPos,"status",6) == 1) + else if (CompareCommands(charPos,"status",6)) { char *ptr = localDate->ToString(); // Changed to use circular buffer rather than list buffer if(logging == true) { - sprintf(temp, "\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); + snprintf(temp, 256, "STATUS: \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); } else { - sprintf(temp, "\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); + snprintf(temp, 256, "STATUS: \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); } - //logger.SendMessage(temp); - printf(temp); + logger.SendMessage(temp); } //Check if it's a "SETTIME" command - else if (CompareCommands(charPos,"settime",7) == 1) + else if (CompareCommands(charPos,"settime",7)) { int h,m,s; //Fetch 1st Param - charPos = strtok(NULL," ,"); + charPos = strtok(NULL," -,"); if(strtol(charPos,NULL,10) != 0) { h = atoi(charPos); } //Fech 2nd Param - charPos = strtok(NULL," ,"); + charPos = strtok(NULL," -,"); if(strtol(charPos,NULL,10) != 0) { m = atoi(charPos); } //Fetch 3rd Param - charPos = strtok(NULL," ,"); + charPos = strtok(NULL," -,"); if(strtol(charPos,NULL,10) != 0) { s = atoi(charPos); @@ -319,35 +306,33 @@ localDate->min = m; localDate->sec = s; char *ptr = localDate->ToString(); - sprintf(temp, "\r\nUpdated Date to: %s \r\n", ptr); - //logger.SendMessage(temp); - printf(temp); + snprintf(temp, 256, "Updated Date to: %s \r\n", ptr); + logger.SendMessage(temp); } //If not valid, prompt user else { - //logger.SendMessage("\r\nWrong format! please use HH-MM-SS separated by spaces. \r\n"); - printf("\r\nWrong format! please use HH-MM-SS separated by spaces. \r\n"); + logger.SendMessage("\r\nWrong format! please use HH-MM-SS. \r\n"); } } //Check if it's a "SETDATE" command - else if (CompareCommands(charPos,"setdate",7) == 1) + else if (CompareCommands(charPos,"setdate",7)) { int d,m,y; //Fetch 1st Parameter - charPos = strtok(NULL," ,"); + charPos = strtok(NULL," ,-"); if(strtol(charPos,NULL,10) != 0) { d = atoi(charPos); } //Fetch 2nd Parameter - charPos = strtok(NULL," ,"); + charPos = strtok(NULL," ,-"); if(strtol(charPos,NULL,10) != 0) { m = atoi(charPos); } //Fetch 3rd Parameter - charPos = strtok(NULL," ,"); + charPos = strtok(NULL," ,-"); if(strtol(charPos,NULL,10) != 0) { y = atoi(charPos); @@ -359,96 +344,75 @@ localDate->month = m; localDate->year = y; char *ptr = localDate->ToString(); - sprintf(temp, "\r\nUpdated Date to: %s \r\n", ptr); - //logger.SendMessage(temp); - printf(temp); + snprintf(temp, 256, "Updated Date to: %s \r\n", ptr); + logger.SendMessage(temp); } // Prompt user if they are not. else { - //logger.SendMessage("\r\nWrong format! please use DD-MM-YYYY separated by spaces. \r\n"); - printf("\r\nWrong format! please use DD-MM-YYYY separated by spaces. \r\n"); + logger.SendMessage("Wrong format! please use DD-MM-YYYY. \r\n"); } } // Check if it's a "LOGGING" command - else if(CompareCommands(charPos,"state",5) == 1) + else if(CompareCommands(charPos,"state",5)) { charPos = strtok(NULL," ,"); //Check if it should be turned ON / OFF - if(CompareCommands(charPos,"on",2) == 1) + if(CompareCommands(charPos,"on",2)) { logging = true; - //logger.SendMessage("\r\nSampling turned ON!\r\n"); - printf("\r\nSampling turned ON!\r\n"); + logger.SendMessage("Sampling turned ON!\r\n"); } - else if (CompareCommands(charPos,"off",3) == 1) + else if (CompareCommands(charPos,"off",3)) { logging = false; - //logger.SendMessage("\r\nSampling turned OFF!\r\n"); - printf("\r\nSampling turned OFF!\r\n"); + logger.SendMessage("Sampling turned OFF!\r\n"); } else { - printf("\n\rExpected parameters: \"on\" | \"off\""); + logger.SendMessage("Expected parameters: \"on\" | \"off\"\r\n"); } } // Check if it's a "SETT" command - else if(CompareCommands(charPos,"sett",4) == 1) + else if(CompareCommands(charPos,"sett",4)) { charPos = strtok(NULL," ,"); - float auxRate = atof(charPos); + double auxRate = atof(charPos); // Validate rate - if(auxRate >= 0.1 && - auxRate <= 60 ) + if(auxRate >= 0.1 && + auxRate <= 60) { sampleRate = auxRate; timer.detach(); timer.attach(&SendSignalDoMeasure, sampleRate); - sprintf(temp, "\r\nSuccessfully updated sample rate to: %2.2f .\r\n",sampleRate); - //logger.SendMessage(temp); - printf(temp); + snprintf(temp, 256, "Successfully updated sample rate to: %2.2f .\r\n",sampleRate); + logger.SendMessage(temp); } // if rate is not valid, prompt: else { - //logger.SendMessage("\r\n Sample rate must be between 0.1 and 60. \r\n"); - printf("\r\n Sample rate must be between 0.1 and 60. \r\n"); + logger.SendMessage("Sample rate must be between 0.1 and 60.\r\n"); } } // Check if it's a "HELP" command - else if (CompareCommands(charPos,"help",4) == 1 || CompareCommands(charPos,"?",1) == 1) - { - //logger.SendMessage("\r\nAvailable Commands:\r\n read <ALL|N> - Read ALL or N first measures.\r\n delete <ALL|N> - Delete ALL or N first measures.\r\n setdate <DD> <MM> <YYYY> Set current date.\r\n settime <HH> <MM> <SS> Set current time.\r\n sett <T> Set sample rate (in seconds).\r\n status - Status report of device.\r\n state - <ON|OFF> - Turn sampling on or OFF.\r\n logging <ON|OFF> - Turn logging on or OFF.\r\n"); - printf("\r\nAvailable Commands:\r\n read <ALL|N> - Read ALL or N first measures.\r\n delete <ALL|N> - Delete ALL or N first measures.\r\n setdate <DD> <MM> <YYYY> Set current date.\r\n settime <HH> <MM> <SS> Set current time.\r\n sett <T> Set sample rate (in seconds).\r\n status - Status report of device.\r\n state - <ON|OFF> - Turn sampling on or OFF.\r\n logging <ON|OFF> - Turn logging on or OFF.\r\n"); - } - else if(CompareCommands(charPos, "test", 4) == 1) + else if (CompareCommands(charPos,"help",4) || CompareCommands(charPos,"?",1)) { - charPos = strtok(NULL," ,"); - //Check if it should be turned ON / OFF - if(CompareCommands(charPos,"1",1) == 1) - { - int i = 1337; - sprintf(temp, "DIE YOU %d", i); - //logger.SendMessage(temp); - printf(temp); - } - else - { - int i = 504; - sprintf(temp, "DIE YOU %d", i); - //logger.SendError(temp); - printf(temp); - } + logger.SendMessage("Available Commands:\r\n"); + logger.SendMessage(" read <ALL|N> - Read ALL or N first measures.\r\n"); + logger.SendMessage(" delete <ALL|N> - Delete ALL or N first measures.\r\n"); + logger.SendMessage(" setdate <DD> <MM> <YYYY> Set current date.\r\n"); + logger.SendMessage(" settime <HH> <MM> <SS> Set current time.\r\n"); + logger.SendMessage(" sett <T> Set sample rate (in seconds).\r\n"); + logger.SendMessage(" status - Status report of device.\r\n"); + logger.SendMessage(" state - <ON|OFF> - Turn sampling on or OFF.\r\n"); + logger.SendMessage(" logging <ON|OFF> - Turn logging on or OFF.\r\n"); } // If command not recognized else { - sprintf(temp, "\r\n Command not recognized. Type \"help\" for more info.\r\n"); - //logger.SendMessage(temp); - printf(temp); + logger.SendMessage("Command not recognized. Type \"help\" for more info.\r\n"); } - logger.SendMessage("Awaiting command: \n\r"); - //printf("Awaiting command: \n\r"); + logger.SendMessage("\r\nAwaiting Command:\n\r"); //Clear command! //* NOTE * Setting first char in array to '\0' WILL NOT RESET IT...for some reason. int i = 0; @@ -476,10 +440,8 @@ while(true) { - Thread::signal_wait(SIGNAL_printMessage); - //uint32_t i = (*loggingThread).used_stack(); - //printf("Logging Stack: %d" , i); - + Thread::signal_wait(SIGNAL_printMessage); + if(logger.GetError()) { // Kill EVERYTHING @@ -502,8 +464,7 @@ measurer.calib(); localDate = new LocalDate(); - //Start message - printf("\r\n--- W E L C O M E --\r\n"); + //Start message //Hook up timer interrupt timer.attach(&SendSignalDoMeasure, sampleRate); @@ -522,11 +483,11 @@ consumeThread = new Thread(); consumeThread->start(ConsumeThread); + logger.SendMessage("\r\n--- W E L C O M E --\r\n"); - 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); + Thread::signal_wait(SIGNAL_terminate); } } \ No newline at end of file