Group PAG / Mbed OS PAG-CourseWork-NicksEdits

Dependencies:   LPS25H hts221

Fork of Coursework by Group PAG

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "hts221.h"
00004 #include "LPS25H.h"
00005 //use for cin
00006 #include <iostream>
00007 //Clock class is a class for time and date
00008 #include "Clock.h"
00009 //Reading class is for each reading, date and time (Clock), temperature, pressure and humidity 
00010 #include "Reading.h"
00011 //#include "string.h" //do we need this to use a string? where is it in classes?
00012 
00013 DigitalOut myled(LED1);
00014 I2C i2c2(I2C_SDA, I2C_SCL);
00015 
00016 float tempCelsius = 25.50;
00017 float humi = 55;
00018 int humiMax = 100; 
00019 char cmd=0;
00020 uint32_t seconds = 0, minutes=0, hours=0; 
00021 Ticker timer;
00022 Clock *clockDT = new Clock(); //DT stands for Date Time, this stores the 'current' time of the system. operated by ticker. Note: install ticker
00023 
00024 
00025 LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR);
00026 HTS221 humidity(I2C_SDA, I2C_SCL);
00027 
00028 
00029 //void thread1( const void* );
00030 Thread *t1;
00031 Thread *t2;
00032 Thread *t3;
00033 
00034     
00035 Mail<Reading,16> mail_box;
00036 
00037 //Queue<Reading,16> queue;
00038 void pingMeasure(){
00039     //ping the measure()
00040     t2->signal_set(8);
00041     }    
00042 void measure() {
00043     
00044     //Thread waits for the ping from pingMeasure
00045     Thread::signal_wait(8);
00046     
00047     Reading *reading = mail_box.alloc();
00048     if (reading == NULL) {
00049         printf("reading=null");
00050         return;
00051         }
00052     
00053      reading->clock.setClock(clockDT);
00054      humidity.ReadTempHumi(&tempCelsius, &humi);
00055      reading->temp = tempCelsius;
00056      reading->humidity = humi;
00057      barometer.get();
00058      reading->pressure = barometer.pressure();
00059      //printf("winner winner chicken dinner");
00060     
00061     osStatus stat = mail_box.put(reading);
00062     
00063     if (stat == osErrorResource) { 
00064         printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat);   
00065         mail_box.free(reading);
00066         return;
00067     }
00068 }
00069 void commands(){
00070     float f1 =0;
00071     int i1 =0;
00072     int i2 =0;
00073     int i3 =0;
00074     char s1 [10];
00075     char s2 [10];
00076     char s3 [10];
00077     char s4 [10];
00078     char line [150];
00079     
00080     while(1) {
00081       cin.getline( line, 100, '\n' );
00082       scanf("%s %s %s %s",line);
00083       //i1 = std::stoi( s2);
00084       printf("s1 is %s0   s2 is %s    s3 is %s \n",s1,s2,s3);
00085 
00086 
00087         //USE STRNCMP INSTEAD : (strncmp (str[n],"R2xx",2) == 0) WHERE str[n] and "R2xx" are strings to compare and 2 is an int of characters to check
00088       if(s1=="?"){
00089         printf("SOFT253 simple Temperature Humidity and Pressure Sensor Monitor\n\r");
00090         printf("Using the X-NUCLEO-IKS01A1 shield and MBED Libraries\n\r");
00091       }
00092       if(s1=="A"){//Gives a reading on command
00093       printf("A was entered");
00094       
00095         //humidity.ReadTempHumi(&tempCelsius, &humi);
00096         //printf("%4.2fC %3.1f%%", tempCelsius, humi);
00097         //barometer.get();
00098         //printf(" %6.1f %4.1f\r\n", barometer.pressure(), barometer.temperature());
00099         //myled = 1; // LED is ON
00100         //Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! 
00101         //myled = 0; // LED is OFF
00102         //Thread::wait(100); // 100 ms
00103       }
00104       //readers
00105       if(s1 =="READ"){//reads all records Sends a comma separated list of all measurements. Each record shall be separated with a \n newline. Each record shall be comma separated as follows: date, time, temperature, pressure, humidity
00106           if (s2 == "ALL"){
00107               //READ ALL 
00108           }
00109           else if (i1>0){
00110               //READ <n> n is i1
00111           }
00112           else{
00113               printf("Are you trying to read something? try READ ALL or READ <n>");
00114           }
00115       }
00116       //deleters
00117       else if(s1=="DELETE"){//deletes all records  The F401 will return a string �DELETED <N> RECORDS� N = no. deleted obviously
00118         if (s2 =="ALL"){
00119             //delete all
00120         }
00121         else if (i1>0){
00122             //delete <n> //Delete the oldest <n> records.return �DELETED <m> RECORDS� m is how many were deleted obviously
00123         }
00124         else{
00125             printf("Are you trying to delete something? Try DELETE ALL or DELETE <n>");
00126         }
00127       }
00128         
00129      
00130       
00131       if(s1=="SETDATE"){//  no points for guessing what this one does. return a string �DATE UPDATED TO <dd> <dd> <yyyy>�
00132       
00133       }
00134       if(s1=="SETTIME"){//  nil point. return �TIME UPDATED TO <hh> <mm> <ss>�
00135        
00136       }
00137       if(s1 == "SETT"){// SETT <t> set sampling rate to 0.1=T=60.0  return a string �T UPDATED TO <T>� otherwise return an �OUT OF RANGE� error.
00138           //f1 =  std::stof( s2 );
00139 
00140       }
00141       if(s1=="STATE"){//STATE <x> set sampling ON or OFF
00142         if(s2=="ON"){
00143             //turn sampling on. somehting like timer.attach
00144         }
00145         else if(s2=="OFF"){
00146             //turn sampling off. somehting like timer.detach
00147         }
00148       
00149       }
00150       if(s1=="LOGGING"){// turns on/off diagnostic logging to the serial interface. The F401 will return a string �LOGGING <x>�
00151         if(s2=="ON"){
00152             //turn logging on. 
00153         }
00154         else if(s2=="OFF"){
00155             //turn logging off. 
00156         }
00157 
00158       
00159       }
00160         
00161     }
00162     }
00163 void thread1()
00164 {
00165         while (true) {
00166         osEvent evt = mail_box.get();
00167         //Check status
00168         if (evt.status == osEventMessage) {
00169             Reading *pReading = (Reading*)evt.value.p;  //This is the pointer (address)
00170             //Make a copy
00171             Reading msg(pReading->clock ,pReading->temp, pReading->humidity, pReading->pressure);//CHECKVARIABLE NAMES
00172             //We are done with this, so give back the memory to the pool
00173             mail_box.free(pReading);
00174             
00175            
00176         }//add it to the queue!!!!
00177     }
00178 }
00179 
00180 // main() runs in its own thread in the OS
00181 int main() {
00182         //show a sign of life
00183         //myled = 1;
00184         humidity.init();
00185         humidity.calib();
00186         printf("SOFT253 simple Temperature Humidity and Pressure Sensor Monitor\n\r");
00187         //printf("Using the X-NUCLEO-IKS01A1 shield and MBED Libraries\n\r");
00188         //tickevey 15seconds to measure environment
00189         
00190         timer.detach();
00191         timer.attach( &pingMeasure, 15);
00192         printf("well something should be happening right baout now");
00193         t1 = new Thread();
00194         t1->start(thread1);
00195         t2 = new Thread();
00196         t2->start(measure);
00197         t3 = new Thread();
00198         t3->start(commands);
00199         
00200         printf("Threads Started\n\r");
00201         
00202         while (true){
00203         Thread:: wait(5000);
00204         }
00205 }