Nicholas Outram / Mbed OS sarbir

Dependencies:   CheckRTC LPS25H hts221

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