my coursework

Dependencies:   CheckRTC LPS25H hts221

Fork of ELEC350-extended-referral1 by satbir panesar

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 #include <sstream>
00006 #define READALL_OFF 1
00007 #define SETTIME_OFF 2
00008 #define SETT_OFF    4
00009 #define READ_OFF    7
00010 #define ALL_OFF     9
00011 
00012 
00013 
00014 DigitalOut myled(LED1);
00015 I2C i2c2(I2C_SDA, I2C_SCL);
00016 Serial pc(USBTX, USBRX);
00017 float tempCelsius = 25.50;
00018 float humi = 55;
00019 int humiMax = 100;
00020 char buffer_str=0;
00021 uint32_t seconds = 0, minutes=0, hours=0;
00022 Ticker t;
00023 volatile static unsigned short sample16 = 0;
00024 void doSample1Hz();
00025 int count = 0;
00026 char *buf;
00027 size_t sz;
00028 int oldest;
00029 
00030 typedef struct {
00031     float temp;
00032     float press;
00033     float humid;
00034 } measurement;
00035 
00036 measurement buffer[120];
00037 
00038 LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR);
00039 HTS221 humidity(I2C_SDA, I2C_SCL);
00040 
00041 void function1();
00042 
00043 
00044 Thread t1;
00045 Thread tSample;
00046 
00047 osThreadId idMain;
00048 osThreadId id1;
00049 osThreadId id2;
00050 osThreadId id3;
00051 osThreadId id4;
00052 
00053 Ticker ticker;
00054 
00055 void function1()
00056 {
00057     //begin function
00058     while (true) {//begin while true
00059         char strbuffer [128];
00060         scanf("%127s", strbuffer);
00061         string buffer_str(strbuffer);
00062         char date [32];
00063         strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds));
00064         char time [32];
00065         strftime(time, 32, "%H:%M:%S %p", localtime(&seconds));
00066 
00067         if(buffer_str=="READALL") {//begin if buffer readall
00068 
00069             // displays all the variables of time, date, temperature, humidity and all the barometric readings to the serial
00070             for (int i = 0; i <120; i++) {
00071                 //printf("%s, %s, %4.2fC , %3.1f%%,  %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, buffer[i].humid, buffer[i].press, buffer[i].temp, barometer.temperature());
00072                 //measurement test=buffer[i];
00073                 printf("%f %f %f \n\r",buffer[i].humid, buffer[i].press, buffer[i].temp);
00074             }
00075             myled = 1; // LED is ON
00076             Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds!
00077             myled = 0; // LED is OFF
00078 
00079         }//end if buffer readall
00080 
00081 
00082 
00083 
00084         if(buffer_str=="SETTIME") {// begin if buffer settime
00085             set_time(1508268288);   //sets time in UTC
00086             printf("time and date updated to %s %s\n\r", time, date); //prints date and time
00087             myled = 1;  //led on
00088             Thread::wait(200); // wait 200ms
00089             myled = 0; //led off
00090 
00091         }//end if buffer settime
00092 
00093 
00094 
00095         if(buffer_str=="SETT") { // begin if buffer sett
00096             for (count = 0; count < 5; count++) {
00097                 float temp = barometer.temperature();
00098                 float pressure = barometer.pressure();
00099 
00100                 sz = snprintf(NULL, 0,"%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, pressure, temp);
00101                 buf = (char *)malloc(sz + 1);
00102                 snprintf(buf, sz + 1, "%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, pressure, temp);
00103                 printf("%s\n\r",buf);
00104                 free(buf);
00105                 printf("written to memory\n\r");
00106                 myled = 1;
00107                 Thread::wait(200);
00108                 myled = 0;
00109 
00110             } //end if buffer sett
00111 
00112         }
00113 
00114 
00115 
00116 
00117         if(buffer_str=="READ") { //begin if buffer read
00118             printf("%s\n\r", buf);
00119             myled = 1;
00120             Thread::wait(200);
00121             myled = 0;
00122 
00123         }//end if buffer read
00124 
00125     }//end while true
00126 }//end function
00127 
00128 void getSampleThread()
00129 {
00130     while (true) {
00131         Thread::signal_wait(99);
00132         //Get samples
00133         measurement m;
00134         
00135         float pressure = barometer.pressure();
00136         m.press = pressure;
00137 
00138         float temp;
00139         float humidityval;
00140         humidity.ReadTempHumi(&temp, &humidityval); // reads temperature and humity levels
00141         m.humid = humidityval;
00142         m.temp = temp;
00143 
00144         buffer[oldest] = m;
00145 
00146         //update oldest
00147         oldest++;
00148         //wrap back to zero if at the end
00149         if (oldest>120) {
00150             oldest = 0;
00151         }
00152     }
00153 }
00154 
00155 void getSample()
00156 {
00157     tSample.signal_set(99);
00158 }
00159 
00160 int main()
00161 {
00162     time_t seconds = time(NULL);
00163     pc.baud(9600); //sets baud rate to 9600
00164     ticker.attach(getSample, 15);
00165     tSample.start(getSampleThread);
00166     idMain = osThreadGetId();
00167     t1.start(function1);
00168     id1 = t1.gettid();
00169     humidity.init();
00170     humidity.calib();
00171     printf("Type SETTIME to set time and date [enter] \n\r");
00172     printf("Type SETT to start sampling [ENTER]\n\r");
00173     printf("Type DELETEALL to delete sensor data [enter]\n\r");
00174     printf("Type READALL [enter]\n\r");
00175     printf("Type READ to read sensor data in memory [ENTER]\n\r");
00176 
00177 
00178     while(1) {
00179         char buffer [128];
00180         scanf("%127s", buffer);
00181         string buffer_str(buffer);
00182         osSignalWait(ALL_OFF,osWaitForever);
00183 
00184     }
00185 }
00186