3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
main.cpp
- Committer:
- Netaphous
- Date:
- 2017-04-09
- Branch:
- feature/listOptimisation
- Revision:
- 65:3723d2729b68
- Parent:
- 60:db8c5b7fc548
- Child:
- 67:8d0e88172e2a
File content as of revision 65:3723d2729b68:
#include "mbed.h" #include "rtos.h" #include <string.h> #include <stdio.h> #include <ctype.h> #include "hts221.h" #include "LPS25H.h" #include "CircularArray.h" #include "FakeSensor.h" #include <iostream> #define SIGNAL_doMeasure 1 #define SWITCH1_RELEASE 90 #define BUFFER_SIZE 120 // // MBED DECLARATIONS // DigitalOut myled(LED1); DigitalIn onBoardSwitch(USER_BUTTON); I2C i2c2(I2C_SDA, I2C_SCL); // // SENSOR DECLARATIONS // MAKE SURE ONE OF THESE IS COMMENTED OUT // Real sensor LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); HTS221 measurer(I2C_SDA, I2C_SCL); // Fake sensor //FakeBarometer barometer(1029.0, 1031.0); //FakeMeasurer measurer(20.0, 25.0, 30.0, 50.0); // // THREADS DECLARATION // Thread *produceThread; Thread *measureThread; Thread *consumeThread; Ticker timer; Ticker realTimeDate; // // GLOBAL VARIABLES // Mail<Measure, 16> mail_box; CircularArray buffer(BUFFER_SIZE); LocalDate *localDate; bool logging = true; float sampleRate = 1; // // Called by a TICKER // Adds 1 second every second to the clock void RealTimeDate() { localDate->TickSecond(); } // // Ticker that signals the measureThread to do a measure // void SendSignalDoMeasure() { if(logging == true) measureThread->signal_set(SIGNAL_doMeasure); } // // SIGNALED BY Ticker at a frequency of <T> Hz // Reads values from sensor board, sends over through mail queue void MeasureThread() { while(true) { //Await signal from ticker Thread::signal_wait(SIGNAL_doMeasure); float temperature = 0 , humidity = 0,pressure = 0; Measure *measure = mail_box.alloc(); if (measure == NULL) { printf("Out of memory\n\r"); return; } //Read and fill in data measurer.ReadTempHumi(&temperature,&humidity); barometer.get(); pressure = barometer.pressure(); measure->temperature = temperature; measure->humidity = humidity; measure->pressure = pressure; measure->date = new LocalDate(localDate); osStatus stat = mail_box.put(measure); //Check if succesful if (stat == osErrorResource) { printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); mail_box.free(measure); return; } } } // // Receives data through mail queue, then adds it to the global declared list // A.K.A. Producer Thread void ProducerThread() { while (true) { //Block on the queue osEvent evt = mail_box.get(); //Check status if (evt.status == osEventMail) { Measure *measure = (Measure*)evt.value.p; Measure msr(measure->date,measure->temperature, measure->humidity,measure->pressure); // Changed to use circlar buffer rather than list buffer buffer.pushValue(msr); mail_box.free(measure); } else { printf("ERROR: %x\n\r", evt.status); } } } // // Compares two char arrays and returns result // Param1: First char Array / pointer // Param2: Second char Array / pointer // 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) { int i; for(i = 0; i < size; i ++) { if(command[i] != targetcommand[i]) return -1; } return 1; } // // Reads commands through PUTTY and 'consumes the data' accordingly // A.K.A. Consumer Thread void ConsumeThread() { //Last character pressed read (last key input) char charCmd; //Char array that stores the command after user presses ENTER char command[40]; //Current Command Size int crtChar = 0; printf("\r\nAwaiting command:\r\n"); while(1) { charCmd = NULL; charCmd = getchar(); if(charCmd != NULL) { //If BACKSPACE is pressed, Print "DEL" so it deletes last character typed. if (charCmd == 127 && crtChar > 0 ) { printf("%c",charCmd); 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); } //If ENTER is pressed, PROCESS it else if(charCmd == 13) // If Enter is pressed { //Get first word of command: char *charPos; charPos = strtok(command," -,"); //Check if it's a "LIST" command if(CompareCommands(charPos, "read",4) == 1) { charPos = strtok(NULL," -,"); //Check if it's a "LIST ALL" command if(CompareCommands(charPos, "all",3) == 1) { printf("\r\n Printing all measures performed so far: \r\n"); // Changed to use circular buffer rather than list buffer buffer.readAll(); printf("\r\nD O N E ! \r\n"); } //Check if it's a "LIST X" command else if(strtol(charPos,NULL,10) != 0) { printf("\r\n Printing %i measures: \r\n",atoi(charPos)); // Changed to use circular buffer rather than list buffer buffer.readX(atoi(charPos)); printf("\r\nD O N E ! \r\n"); } else { printf("Expected parameters: \"all\" | \"n\", where n is a number."); } } //Check if it's a "DELETE" command else if (CompareCommands(charPos,"delete",6) == 1) { charPos = strtok(NULL," -,"); //Check if it's a "DELETE ALL" command if(CompareCommands(charPos,"all",3) == 1) { printf("\r\n Deleting all measures performed so far: \r\n"); // Changed to use circular buffer rather than list buffer buffer.deleteAll(); printf("\r\nElements 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)); printf("\r\nElements deleted!\r\n"); } else { printf("Expected parameters: \"all\" | \"n\", where n is a number."); } } //Check if it's a "STATUS" command else if (CompareCommands(charPos,"status",6) == 1) { char *ptr = localDate->ToString(); // 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); 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); } //Check if it's a "SETTIME" command else if (CompareCommands(charPos,"settime",7) == 1) { int h,m,s; //Fetch 1st Param charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { h = atoi(charPos); } //Fech 2nd Param charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { m = atoi(charPos); } //Fetch 3rd Param charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { s = atoi(charPos); } //Check if parameters are valid if((h>=0 && h < 24) && (m>=0 && m<60) && (s>=0 && s<60)) { localDate->hour = h; localDate->min = m; localDate->sec = s; char *ptr = localDate->ToString(); printf("\r\nUpdated Date to: %s \r\n", ptr); } //If not valid, prompt user else { printf("\r\nWrong format! please use HH-MM-SS separated by spaces. \r\n"); } } //Check if it's a "SETDATE" command else if (CompareCommands(charPos,"setdate",7) == 1) { int d,m,y; //Fetch 1st Parameter charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { d = atoi(charPos); } //Fetch 2nd Parameter charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { m = atoi(charPos); } //Fetch 3rd Parameter charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { y = atoi(charPos); } //Check if parameters are valid if((d>=0 && d < 31) && (m>=0 && m<13)) { localDate->day = d; localDate->month = m; localDate->year = y; char *ptr = localDate->ToString(); printf("\r\nUpdated Date to: %s \r\n", ptr); } // Prompt user if they are not. else { printf("\r\nWrong format! please use DD-MM-YYYY separated by spaces. \r\n"); } } // Check if it's a "LOGGING" command else if(CompareCommands(charPos,"state",5) == 1) { charPos = strtok(NULL," ,"); //Check if it should be turned ON / OFF if(CompareCommands(charPos,"on",2) == 1) { logging = true; printf("\r\nSampling turned ON!\r\n"); } else if (CompareCommands(charPos,"off",3) == 1) { logging = false; printf("\r\nSampling turned OFF!\r\n"); } else { printf("Expected parameters: \"on\" | \"off\""); } } else if(CompareCommands(charPos,"sett",4) == 1) { charPos = strtok(NULL," ,"); float auxRate = atof(charPos); if(auxRate != 0 && auxRate > 0.09 && auxRate <= 60 ) { sampleRate = auxRate; timer.detach(); timer.attach(&SendSignalDoMeasure, sampleRate); printf("\r\nSuccessfully updated sample rate to: %2.2f .\r\n",sampleRate); } else { printf("\r\n Sample rate must be between greater than 0.1 or less equal than 60. \r\n"); } } 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"); } else { printf("\r\n Command not recognized. Type \"help\" for more info.\r\n"); } printf("\r\nAwaiting command: \r\n"); //Clear command! //* NOTE * Setting first char in array to '\0' WILL NOT RESET IT...for some reason. int i = 0; for(i =0 ; i < crtChar; i++) command[i] = ' '; command[0] = 0; crtChar = 0; } } } } // Main thread int main() { //Initialize all stuff you need here: measurer.init(); measurer.calib(); localDate = new LocalDate(); //Start message printf("Welcome\r\n"); //Hook up timer interrupt timer.attach(&SendSignalDoMeasure, sampleRate); realTimeDate.attach(&RealTimeDate,1.0); //Run Threads produceThread = new Thread(); produceThread->start(ProducerThread); measureThread = new Thread(); measureThread->start(MeasureThread); consumeThread = new Thread(); consumeThread->start(ConsumeThread); printf("Main Thread\n"); while(1) { Thread::wait(3000); // float temp,humi; // measurer.ReadTempHumi(&temp, &humi); // barometer.get(); // t2->signal_set(SIGNAL_doMeasure); // printf("Main Thread Measures: %fC %f %f \r\n", temp, humi,barometer.pressure()); } }