3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
main.cpp
- Committer:
- FairyMental
- Date:
- 2017-04-06
- Revision:
- 49:83bea7fb2728
- Parent:
- 48:a8219954b3f2
- Child:
- 50:c07e968b9582
File content as of revision 49:83bea7fb2728:
#include "mbed.h" #include "rtos.h" #include <string.h> #include <stdio.h> #include <ctype.h> #include "hts221.h" #include "LPS25H.h" #include "LinkedList.h" #include <iostream> #define SIGNAL_doMeasure 1 #define SWITCH1_RELEASE 90 DigitalOut myled(LED1); I2C i2c2(I2C_SDA, I2C_SCL); LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); HTS221 measurer(I2C_SDA, I2C_SCL); DigitalIn onBoardSwitch(USER_BUTTON); //Threads Thread *produceThread; Thread *measureThread; Thread *consumeThread; int count= 0; //Mail queue Mail<Measure, 16> mail_box; LinkedList *listBuffer; LocalDate *localDate; bool logging = true; void RealTimeDate() { localDate->TickSecond(); } void MeasureThread() { while(true) { Thread::signal_wait(SIGNAL_doMeasure); //Read sample - make a copy float temperature = 0 , humidity = 0,pressure = 0; //Allocate a block from the memory pool Measure *measure = mail_box.alloc(); if (measure == NULL) { //Out of memory printf("Out of memory\n\r"); return; } //Fill in the data measurer.ReadTempHumi(&temperature,&humidity); barometer.get(); pressure = barometer.pressure(); measure->temperature = temperature; measure->humidity = humidity; measure->pressure = pressure; measure->date = new LocalDate(localDate); //Write to queue osStatus stat = mail_box.put(measure); //Note we are sending the "pointer" //Check if succesful if (stat == osErrorResource) { printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); mail_box.free(measure); return; } } } //Normal priority thread (consumer) 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; //This is the pointer (address) //Make a copy // printf("Consumer: %fC | %f% % | %f \r\n", measure->temperature, measure->humidity,measure->pressure); Measure msr(measure->date,measure->temperature, measure->humidity,measure->pressure); listBuffer->addValueEnd(msr); //We are done with this, so give back the memory to the pool mail_box.free(measure); //Echo to the terminal } else { printf("ERROR: %x\n\r", evt.status); } } //end while } 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; } void ConsumeThread() { char charCmd; char command[40]; int crtChar = 0; printf("\r\nAwaiting command:\r\n"); while(1) { charCmd = NULL; charCmd = getchar(); if(charCmd != NULL) { if (charCmd == 127 && crtChar > 0 ) // If Backspace is pressed { printf("%c",charCmd); command[--crtChar] = '\0'; } else if(charCmd != 13 && charCmd != 127) // If NOT enter AND NOT Backspace is pressed { command[crtChar++] = charCmd; printf("%c",charCmd); } else if(charCmd == 13) // If Enter is pressed { // this thing that follows splits a string into multiple strings, just like String.Split(char[] delimiters) // here we will check for commands and parameters :) char *charPos; charPos = strtok(command," -,"); if(CompareCommands(charPos, "list",4) == 1) { charPos = strtok(NULL," -,"); if(CompareCommands(charPos, "all",3) == 1) { printf("\r\n Printing all measures performed so far: \r\n"); listBuffer->ListAll(); printf("\r\n D O N E ! \r\n"); } else if(strtol(charPos,NULL,10) != 0) { listBuffer->ListX(atoi(charPos)); printf("\r\n D O N E ! \r\n"); } } else if (CompareCommands(charPos,"delete",6) == 1) { charPos = strtok(NULL," -,"); if(CompareCommands(charPos,"all",3) == 1) { printf("\r\n Deleting all measures performed so far: \r\n"); listBuffer->DeleteAll(); printf("\r\n D O N E ! \r\n"); } else if (strtol(charPos,NULL,10) != 0) { listBuffer->DeleteX(atoi(charPos)); printf("\r\nD O N E ! \r\n"); } } else if (CompareCommands(charPos,"status",6) == 1) { char *ptr = localDate->ToString(); if(logging == true) printf("\r\n STATUS: \r\n # of measures: %i \r\n SAMPLING: ON \r\n Current Date: %s \r\n", listBuffer->GetSize(),ptr); else printf("\r\n STATUS: \r\n # of measures: %i \r\n SAMPLING: OFF \r\n Current Date: %s \r\n", listBuffer->GetSize(),ptr); } else if (CompareCommands(charPos,"settime",7) == 1) { int h,m,s; charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { h = atoi(charPos); } charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { m = atoi(charPos); } charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { s = atoi(charPos); } if((h>=0 && h < 24) && (m>=0 && m<60) && (s>=0 && s<60)) { localDate->hour = h; localDate->min = m; localDate->sec = s; printf("\r\n D O N E ! \r\n"); } else { printf("\r\nWrong format! \r\n"); } } else if (CompareCommands(charPos,"setdate",7) == 1) { int d,m,y; charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { d = atoi(charPos); } charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { m = atoi(charPos); } charPos = strtok(NULL," ,"); if(strtol(charPos,NULL,10) != 0) { y = atoi(charPos); } if((d>=0 && d < 31) && (m>=0 && m<13)) { localDate->day = d; localDate->month = m; localDate->year = y; printf("\r\n D O N E ! \r\n"); } else { printf("\r\nWrong format! \r\n"); } } else if(CompareCommands(charPos,"logging",7) == 1) { charPos = strtok(NULL," ,"); if(CompareCommands(charPos,"on",2) == 1) { logging = true; printf("\r\n Logging turned ON!\r\n"); } else if (CompareCommands(charPos,"off",3) == 1) { logging = false; printf("\r\n Logging turned OFF!\r\n"); } } // printf("%s \r\n", charPos); // charPos = strtok(NULL," -,"); printf("Awaiting command: \r\n"); int i = 0; for(i =0 ; i < crtChar; i++) command[i] = ' '; command[0] = 0; crtChar = 0; } } } } void SendSignalDoMeasure() { if(logging == true) measureThread->signal_set(SIGNAL_doMeasure); } // Main thread int main() { //Initialize stuff measurer.init(); measurer.calib(); listBuffer = new LinkedList(); localDate = new LocalDate(); //Start message printf("Welcome\r\n"); //Hook up timer interrupt Ticker timer; timer.attach(&SendSignalDoMeasure, 2.0); Ticker realTimeDate; realTimeDate.attach(&RealTimeDate,1.0); //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()); } }