lucas kovaci
/
SOFT253_Assignment_Group_K
SOFT253 ASSIGNMENT
Fork of SOFT253_Assignment_V4 by
main.cpp
- Committer:
- akovaci
- Date:
- 2017-05-15
- Revision:
- 44:5bb7e0a2387a
- Parent:
- 43:d7cb544ad771
- Child:
- 45:4ed55fd0f06f
File content as of revision 44:5bb7e0a2387a:
#include "mbed.h" #include "rtos.h" #include "hts221.h" #include "LPS25H.h" Serial pc(USBTX, USBRX); #define N 10 DigitalOut myled(D7); Ticker t; I2C i2c2(I2C_SDA, I2C_SCL); float tempCelsius = 25.50; float humi = 55; int humiMax = 100; char cmd[20]; char cmd2[20]; char cmdSet[20]; char cmdSet2[20]; char input1[20]; char input2[20]; char inputSet1[20]; char inputSet2[20]; char stringToCompare1[20]; char stringToCompare2[20]; char stringToCompareSet1[20]; char stringToCompareSet2[20]; float tempArray[N]; float humArray[N]; float pressArray[N]; int sampleSpeed = 15; uint32_t seconds = 0, minutes=0, hours=0; int counters = 0; int res; int res2; LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); HTS221 humidity(I2C_SDA, I2C_SCL); void adcISR(); void thread1 (void const *args ); void threadcomun (void const *args); Thread* t1; Thread* t2; Thread* t3; Thread* t4; typedef struct { float tempVal ; float humVal; float pressVal; }message_t; Mail<message_t, 16> mail_box; void adcISR() { message_t *message = mail_box.alloc(); message->tempVal = tempCelsius; message->humVal = humi; message->pressVal = barometer.pressure(); myled=1; if (counters < 10) { counters= counters + 1; } else { counters =10; } mail_box.put(message); //Thread::wait(1000); } void thread1 (void const *args ) { pc.baud(115200); //pc.printf("Temperature,Humidity,Pressure\n\n"); while(1) { osEvent evt = mail_box.get(); if (evt.status == osEventMail) { for (unsigned int n=(N-1); n>0; n--) { tempArray[n]= tempArray[n-1]; humArray[n]= humArray[n-1]; pressArray[n]= pressArray[n-1]; } message_t *message = (message_t*)evt.value.p; tempArray[0]= message->tempVal; humArray[0] = message->humVal; pressArray[0]= message->pressVal; // pc.printf("%4.2f,%3.1f,%6.1f\n\r", tempArray[N/2], humArray[N/2], pressArray[N/2]); mail_box.free(message); } } } void threadReadComun (void const *args) { pc.baud(115200); pc.printf("Temperature,Humidity,Pressure\n"); //while(cmd==NULL){cmd=getchar();} while(1) { scanf("%s%s",cmd,cmd2); //READ ALL------------------------------------------ //INPUT 2 strcpy (input1,cmd); //INPUT 1 pc.printf("you entered: %s\n\r",input1); strcpy (input2,cmd2); strcpy (stringToCompare1,"READ"); //STRING TO COMPARE 1 strcpy (stringToCompare2,input2); //STRING TO COMPARE 2 res = strncmp(input1,stringToCompare1,20); res2 = strncmp(input2,stringToCompare2,20); int val = atoi(input2); if (val <=10 && val >=1) { if (res==0 && res2==0) { for (unsigned int n=0; n<val; n++) { pc.printf("the element of %d is %4.2f\n\r",n,tempArray[n]); } } } else //2 { strcpy (stringToCompare1,"READ"); //STRING TO COMPARE 1 strcpy (stringToCompare2,"ALL"); //STRING TO COMPARE 2 res = strncmp(input1,stringToCompare1,20); res2 = strncmp(input2,stringToCompare2,20); if (res==0 && res2==0) { for (unsigned int n=0; n<N; n++) pc.printf("the element of %d is %4.2f\n\r",n,tempArray[n]); pc.printf("the Counter: %d\n",counters); } } }//while } char answer; void threadDeleteComun (void const *args) { pc.baud(115200); while(1) { scanf("%s%s",cmd,cmd2); //READ ALL------------------------------------------ //INPUT 2 strcpy (input1,cmd); //INPUT 1 pc.printf("you entered: %s\n\r",input1); strcpy (input2,cmd2); strcpy (stringToCompare1,"DELETE"); //STRING TO COMPARE 1 strcpy (stringToCompare2,input2); //STRING TO COMPARE 2 res = strncmp(input1,stringToCompare1,20); res2 = strncmp(input2,stringToCompare2,20); int val = atoi(input2); int startDel=(counters - val); if (val <=10 && val >=1) { if(res==0 && res2==0) { for (unsigned int n=startDel; n<counters; n++) { tempArray[n]=0.00; } pc.printf("Deleted %d records\n",val); counters=counters - val; } } else { strcpy (stringToCompare1,"DELETE"); strcpy (stringToCompare2,"ALL"); res = strncmp(input1,stringToCompare1,20); res2 = strncmp(input2,stringToCompare2,20); if(res==0 && res2==0) { //Delete every element. memset(tempArray, 0, sizeof tempArray); pc.printf("%d Elements deleted\n",N); counters=0; } }//else 2 }//while } void threadSetTComun (void const *args) { pc.baud(115200); //while(cmd==NULL){cmd=getchar();} while(1) { scanf("%s%s",cmdSet,cmdSet2); //READ ALL------------------------------------------ //INPUT 2 strcpy (inputSet1,cmdSet); //INPUT 1 pc.printf("you entered: %s\n\r",inputSet1); strcpy (inputSet2,cmdSet2); strcpy (stringToCompareSet1,"SETT"); //STRING TO COMPARE 1 strcpy (stringToCompareSet2,inputSet2); //STRING TO COMPARE 2 res = strncmp(inputSet1,stringToCompareSet1,20); res2 = strncmp(inputSet2,stringToCompareSet2,20); int inputSpeed = atoi(inputSet2); if (inputSpeed <=60 && inputSpeed >=0.1) { if (res==0 && res2==0) { pc.printf("CURRENT SPEED %d \n",inputSpeed); sampleSpeed = inputSpeed; pc.printf("T UPDATED TO %d %d\n",sampleSpeed,inputSpeed); } } } } int main(void) { puts("Loading... \n\n"); t1 = new Thread(thread1); t2 = new Thread(threadReadComun); t3 = new Thread( threadDeleteComun); t4 = new Thread( threadSetTComun); // Thread thread(thread1); //threadcomun(); //Thread thread(threadcomun); t.attach(&adcISR,sampleSpeed); // timer of measurements while(1) { humidity.init(); humidity.calib(); humidity.ReadTempHumi(&tempCelsius, &humi); barometer.get(); barometer.pressure(); barometer.temperature(); sleep(); Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! myled = 0; // LED is OFF Thread::wait(100); // 100 ms } }