![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Minor fixes
Fork of Coursework by
main.cpp@43:4ddc392dd0cc, 2017-05-04 (annotated)
- Committer:
- pburtenshaw
- Date:
- Thu May 04 12:55:16 2017 +0000
- Revision:
- 43:4ddc392dd0cc
- Parent:
- 42:4e0a96b52e65
queue updatd but many errors - suspected missing colon or bracket?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
0:2757d7abb7d9 | 1 | #include "mbed.h" |
martinsimpson | 32:260a288be58f | 2 | #include "rtos.h" |
noutram | 35:af125862c33e | 3 | #include "hts221.h" |
martinsimpson | 32:260a288be58f | 4 | #include "LPS25H.h" |
RossalaTTM | 36:bbc4b371cb75 | 5 | //use for cin |
RossalaTTM | 36:bbc4b371cb75 | 6 | #include <iostream> |
RossalaTTM | 36:bbc4b371cb75 | 7 | //Clock class is a class for time and date |
RossalaTTM | 36:bbc4b371cb75 | 8 | #include "Clock.h" |
RossalaTTM | 36:bbc4b371cb75 | 9 | //Reading class is for each reading, date and time (Clock), temperature, pressure and humidity |
RossalaTTM | 36:bbc4b371cb75 | 10 | #include "Reading.h" |
RossalaTTM | 36:bbc4b371cb75 | 11 | //#include "string.h" //do we need this to use a string? where is it in classes? |
noutram | 35:af125862c33e | 12 | |
martinsimpson | 32:260a288be58f | 13 | DigitalOut myled(LED1); |
martinsimpson | 32:260a288be58f | 14 | I2C i2c2(I2C_SDA, I2C_SCL); |
noutram | 35:af125862c33e | 15 | |
noutram | 35:af125862c33e | 16 | float tempCelsius = 25.50; |
noutram | 35:af125862c33e | 17 | float humi = 55; |
noutram | 35:af125862c33e | 18 | int humiMax = 100; |
noutram | 35:af125862c33e | 19 | char cmd=0; |
noutram | 35:af125862c33e | 20 | uint32_t seconds = 0, minutes=0, hours=0; |
RossalaTTM | 36:bbc4b371cb75 | 21 | Ticker timer; |
RossalaTTM | 36:bbc4b371cb75 | 22 | Clock *clockDT = new Clock(); //DT stands for Date Time, this stores the 'current' time of the system. operated by ticker. Note: install ticker |
RossalaTTM | 36:bbc4b371cb75 | 23 | |
martinsimpson | 32:260a288be58f | 24 | |
noutram | 35:af125862c33e | 25 | LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
noutram | 35:af125862c33e | 26 | HTS221 humidity(I2C_SDA, I2C_SCL); |
noutram | 35:af125862c33e | 27 | |
noutram | 35:af125862c33e | 28 | |
RossalaTTM | 36:bbc4b371cb75 | 29 | //void thread1( const void* ); |
RossalaTTM | 36:bbc4b371cb75 | 30 | Thread *t1; |
RossalaTTM | 36:bbc4b371cb75 | 31 | Thread *t2; |
RossalaTTM | 36:bbc4b371cb75 | 32 | Thread *t3; |
RossalaTTM | 36:bbc4b371cb75 | 33 | |
RossalaTTM | 36:bbc4b371cb75 | 34 | |
RossalaTTM | 36:bbc4b371cb75 | 35 | Mail<Reading,16> mail_box; |
RossalaTTM | 36:bbc4b371cb75 | 36 | |
RossalaTTM | 36:bbc4b371cb75 | 37 | //Queue<Reading,16> queue; |
RossalaTTM | 36:bbc4b371cb75 | 38 | void pingMeasure(){ |
RossalaTTM | 36:bbc4b371cb75 | 39 | //ping the measure() |
RossalaTTM | 36:bbc4b371cb75 | 40 | t2->signal_set(8); |
RossalaTTM | 36:bbc4b371cb75 | 41 | } |
RossalaTTM | 36:bbc4b371cb75 | 42 | void measure() { |
RossalaTTM | 36:bbc4b371cb75 | 43 | |
RossalaTTM | 36:bbc4b371cb75 | 44 | //Thread waits for the ping from pingMeasure |
RossalaTTM | 36:bbc4b371cb75 | 45 | Thread::signal_wait(8); |
RossalaTTM | 36:bbc4b371cb75 | 46 | |
RossalaTTM | 36:bbc4b371cb75 | 47 | Reading *reading = mail_box.alloc(); |
RossalaTTM | 36:bbc4b371cb75 | 48 | if (reading == NULL) { |
RossalaTTM | 36:bbc4b371cb75 | 49 | printf("reading=null"); |
RossalaTTM | 36:bbc4b371cb75 | 50 | return; |
RossalaTTM | 36:bbc4b371cb75 | 51 | } |
martinsimpson | 32:260a288be58f | 52 | |
RossalaTTM | 36:bbc4b371cb75 | 53 | reading->clock.setClock(clockDT); |
RossalaTTM | 36:bbc4b371cb75 | 54 | humidity.ReadTempHumi(&tempCelsius, &humi); |
RossalaTTM | 36:bbc4b371cb75 | 55 | reading->temp = tempCelsius; |
RossalaTTM | 36:bbc4b371cb75 | 56 | reading->humidity = humi; |
RossalaTTM | 36:bbc4b371cb75 | 57 | barometer.get(); |
RossalaTTM | 36:bbc4b371cb75 | 58 | reading->pressure = barometer.pressure(); |
RossalaTTM | 36:bbc4b371cb75 | 59 | //printf("winner winner chicken dinner"); |
RossalaTTM | 36:bbc4b371cb75 | 60 | |
RossalaTTM | 36:bbc4b371cb75 | 61 | osStatus stat = mail_box.put(reading); |
RossalaTTM | 36:bbc4b371cb75 | 62 | |
RossalaTTM | 36:bbc4b371cb75 | 63 | if (stat == osErrorResource) { |
RossalaTTM | 36:bbc4b371cb75 | 64 | printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); |
RossalaTTM | 36:bbc4b371cb75 | 65 | mail_box.free(reading); |
RossalaTTM | 36:bbc4b371cb75 | 66 | return; |
RossalaTTM | 36:bbc4b371cb75 | 67 | } |
RossalaTTM | 36:bbc4b371cb75 | 68 | } |
RossalaTTM | 36:bbc4b371cb75 | 69 | void commands(){ |
RossalaTTM | 36:bbc4b371cb75 | 70 | float f1 =0; |
RossalaTTM | 36:bbc4b371cb75 | 71 | int i1 =0; |
RossalaTTM | 36:bbc4b371cb75 | 72 | int i2 =0; |
RossalaTTM | 36:bbc4b371cb75 | 73 | int i3 =0; |
RossalaTTM | 36:bbc4b371cb75 | 74 | char s1 [10]; |
RossalaTTM | 36:bbc4b371cb75 | 75 | char s2 [10]; |
RossalaTTM | 36:bbc4b371cb75 | 76 | char s3 [10]; |
RossalaTTM | 36:bbc4b371cb75 | 77 | char s4 [10]; |
RossalaTTM | 36:bbc4b371cb75 | 78 | char line [150]; |
RossalaTTM | 36:bbc4b371cb75 | 79 | |
RossalaTTM | 36:bbc4b371cb75 | 80 | while(1) { |
RossalaTTM | 36:bbc4b371cb75 | 81 | cin.getline( line, 100, '\n' ); |
RossalaTTM | 36:bbc4b371cb75 | 82 | scanf("%s %s %s %s",line); |
RossalaTTM | 36:bbc4b371cb75 | 83 | //i1 = std::stoi( s2); |
RossalaTTM | 36:bbc4b371cb75 | 84 | printf("s1 is %s0 s2 is %s s3 is %s \n",s1,s2,s3); |
RossalaTTM | 36:bbc4b371cb75 | 85 | |
RossalaTTM | 36:bbc4b371cb75 | 86 | |
RossalaTTM | 36:bbc4b371cb75 | 87 | //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 |
RossalaTTM | 36:bbc4b371cb75 | 88 | if(s1=="?"){ |
noutram | 35:af125862c33e | 89 | printf("SOFT253 simple Temperature Humidity and Pressure Sensor Monitor\n\r"); |
noutram | 35:af125862c33e | 90 | printf("Using the X-NUCLEO-IKS01A1 shield and MBED Libraries\n\r"); |
noutram | 35:af125862c33e | 91 | } |
RossalaTTM | 36:bbc4b371cb75 | 92 | if(s1=="A"){//Gives a reading on command |
RossalaTTM | 36:bbc4b371cb75 | 93 | printf("A was entered"); |
RossalaTTM | 36:bbc4b371cb75 | 94 | |
RossalaTTM | 36:bbc4b371cb75 | 95 | //humidity.ReadTempHumi(&tempCelsius, &humi); |
RossalaTTM | 36:bbc4b371cb75 | 96 | //printf("%4.2fC %3.1f%%", tempCelsius, humi); |
RossalaTTM | 36:bbc4b371cb75 | 97 | //barometer.get(); |
RossalaTTM | 36:bbc4b371cb75 | 98 | //printf(" %6.1f %4.1f\r\n", barometer.pressure(), barometer.temperature()); |
RossalaTTM | 36:bbc4b371cb75 | 99 | //myled = 1; // LED is ON |
RossalaTTM | 36:bbc4b371cb75 | 100 | //Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! |
RossalaTTM | 36:bbc4b371cb75 | 101 | //myled = 0; // LED is OFF |
RossalaTTM | 36:bbc4b371cb75 | 102 | //Thread::wait(100); // 100 ms |
RossalaTTM | 36:bbc4b371cb75 | 103 | } |
RossalaTTM | 36:bbc4b371cb75 | 104 | //readers |
RossalaTTM | 36:bbc4b371cb75 | 105 | 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 |
pburtenshaw | 43:4ddc392dd0cc | 106 | if (s2 == "ALL"){ |
RossalaTTM | 36:bbc4b371cb75 | 107 | //READ ALL |
RossalaTTM | 36:bbc4b371cb75 | 108 | } |
pburtenshaw | 43:4ddc392dd0cc | 109 | else if (i1>0){ |
RossalaTTM | 36:bbc4b371cb75 | 110 | //READ <n> n is i1 |
RossalaTTM | 36:bbc4b371cb75 | 111 | } |
pburtenshaw | 43:4ddc392dd0cc | 112 | else{ |
RossalaTTM | 36:bbc4b371cb75 | 113 | printf("Are you trying to read something? try READ ALL or READ <n>"); |
RossalaTTM | 36:bbc4b371cb75 | 114 | } |
RossalaTTM | 36:bbc4b371cb75 | 115 | } |
RossalaTTM | 36:bbc4b371cb75 | 116 | //deleters |
RossalaTTM | 36:bbc4b371cb75 | 117 | else if(s1=="DELETE"){//deletes all records The F401 will return a string �DELETED <N> RECORDS� N = no. deleted obviously |
RossalaTTM | 36:bbc4b371cb75 | 118 | if (s2 =="ALL"){ |
RossalaTTM | 36:bbc4b371cb75 | 119 | //delete all |
RossalaTTM | 36:bbc4b371cb75 | 120 | } |
RossalaTTM | 36:bbc4b371cb75 | 121 | else if (i1>0){ |
RossalaTTM | 36:bbc4b371cb75 | 122 | //delete <n> //Delete the oldest <n> records.return �DELETED <m> RECORDS� m is how many were deleted obviously |
RossalaTTM | 36:bbc4b371cb75 | 123 | } |
RossalaTTM | 36:bbc4b371cb75 | 124 | else{ |
RossalaTTM | 36:bbc4b371cb75 | 125 | printf("Are you trying to delete something? Try DELETE ALL or DELETE <n>"); |
RossalaTTM | 36:bbc4b371cb75 | 126 | } |
RossalaTTM | 36:bbc4b371cb75 | 127 | } |
RossalaTTM | 36:bbc4b371cb75 | 128 | |
RossalaTTM | 36:bbc4b371cb75 | 129 | |
RossalaTTM | 36:bbc4b371cb75 | 130 | |
RossalaTTM | 36:bbc4b371cb75 | 131 | if(s1=="SETDATE"){// no points for guessing what this one does. return a string �DATE UPDATED TO <dd> <dd> <yyyy>� |
RossalaTTM | 36:bbc4b371cb75 | 132 | |
noutram | 35:af125862c33e | 133 | } |
RossalaTTM | 36:bbc4b371cb75 | 134 | if(s1=="SETTIME"){// nil point. return �TIME UPDATED TO <hh> <mm> <ss>� |
pburtenshaw | 42:4e0a96b52e65 | 135 | |
RossalaTTM | 36:bbc4b371cb75 | 136 | } |
RossalaTTM | 36:bbc4b371cb75 | 137 | 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. |
RossalaTTM | 36:bbc4b371cb75 | 138 | //f1 = std::stof( s2 ); |
noutram | 35:af125862c33e | 139 | |
RossalaTTM | 36:bbc4b371cb75 | 140 | } |
RossalaTTM | 36:bbc4b371cb75 | 141 | if(s1=="STATE"){//STATE <x> set sampling ON or OFF |
RossalaTTM | 36:bbc4b371cb75 | 142 | if(s2=="ON"){ |
RossalaTTM | 36:bbc4b371cb75 | 143 | //turn sampling on. somehting like timer.attach |
RossalaTTM | 36:bbc4b371cb75 | 144 | } |
RossalaTTM | 36:bbc4b371cb75 | 145 | else if(s2=="OFF"){ |
RossalaTTM | 36:bbc4b371cb75 | 146 | //turn sampling off. somehting like timer.detach |
RossalaTTM | 36:bbc4b371cb75 | 147 | } |
RossalaTTM | 36:bbc4b371cb75 | 148 | |
RossalaTTM | 36:bbc4b371cb75 | 149 | } |
RossalaTTM | 36:bbc4b371cb75 | 150 | if(s1=="LOGGING"){// turns on/off diagnostic logging to the serial interface. The F401 will return a string �LOGGING <x>� |
RossalaTTM | 36:bbc4b371cb75 | 151 | if(s2=="ON"){ |
RossalaTTM | 36:bbc4b371cb75 | 152 | //turn logging on. |
RossalaTTM | 36:bbc4b371cb75 | 153 | } |
RossalaTTM | 36:bbc4b371cb75 | 154 | else if(s2=="OFF"){ |
RossalaTTM | 36:bbc4b371cb75 | 155 | //turn logging off. |
RossalaTTM | 36:bbc4b371cb75 | 156 | } |
noutram | 35:af125862c33e | 157 | |
RossalaTTM | 36:bbc4b371cb75 | 158 | |
RossalaTTM | 36:bbc4b371cb75 | 159 | } |
RossalaTTM | 36:bbc4b371cb75 | 160 | |
RossalaTTM | 36:bbc4b371cb75 | 161 | } |
RossalaTTM | 36:bbc4b371cb75 | 162 | } |
RossalaTTM | 36:bbc4b371cb75 | 163 | void thread1() |
RossalaTTM | 36:bbc4b371cb75 | 164 | { |
RossalaTTM | 36:bbc4b371cb75 | 165 | while (true) { |
RossalaTTM | 36:bbc4b371cb75 | 166 | osEvent evt = mail_box.get(); |
RossalaTTM | 36:bbc4b371cb75 | 167 | //Check status |
RossalaTTM | 36:bbc4b371cb75 | 168 | if (evt.status == osEventMessage) { |
RossalaTTM | 36:bbc4b371cb75 | 169 | Reading *pReading = (Reading*)evt.value.p; //This is the pointer (address) |
RossalaTTM | 36:bbc4b371cb75 | 170 | //Make a copy |
RossalaTTM | 36:bbc4b371cb75 | 171 | Reading msg(pReading->clock ,pReading->temp, pReading->humidity, pReading->pressure);//CHECKVARIABLE NAMES |
RossalaTTM | 36:bbc4b371cb75 | 172 | //We are done with this, so give back the memory to the pool |
RossalaTTM | 36:bbc4b371cb75 | 173 | mail_box.free(pReading); |
RossalaTTM | 36:bbc4b371cb75 | 174 | |
RossalaTTM | 36:bbc4b371cb75 | 175 | |
RossalaTTM | 36:bbc4b371cb75 | 176 | }//add it to the queue!!!! |
RossalaTTM | 36:bbc4b371cb75 | 177 | } |
RossalaTTM | 36:bbc4b371cb75 | 178 | } |
noutram | 35:af125862c33e | 179 | |
noutram | 35:af125862c33e | 180 | // main() runs in its own thread in the OS |
noutram | 35:af125862c33e | 181 | int main() { |
RossalaTTM | 36:bbc4b371cb75 | 182 | //show a sign of life |
RossalaTTM | 36:bbc4b371cb75 | 183 | //myled = 1; |
RossalaTTM | 36:bbc4b371cb75 | 184 | humidity.init(); |
RossalaTTM | 36:bbc4b371cb75 | 185 | humidity.calib(); |
RossalaTTM | 36:bbc4b371cb75 | 186 | printf("SOFT253 simple Temperature Humidity and Pressure Sensor Monitor\n\r"); |
RossalaTTM | 36:bbc4b371cb75 | 187 | //printf("Using the X-NUCLEO-IKS01A1 shield and MBED Libraries\n\r"); |
RossalaTTM | 36:bbc4b371cb75 | 188 | //tickevey 15seconds to measure environment |
RossalaTTM | 36:bbc4b371cb75 | 189 | |
RossalaTTM | 36:bbc4b371cb75 | 190 | timer.detach(); |
RossalaTTM | 36:bbc4b371cb75 | 191 | timer.attach( &pingMeasure, 15); |
RossalaTTM | 36:bbc4b371cb75 | 192 | printf("well something should be happening right baout now"); |
RossalaTTM | 36:bbc4b371cb75 | 193 | t1 = new Thread(); |
RossalaTTM | 36:bbc4b371cb75 | 194 | t1->start(thread1); |
RossalaTTM | 36:bbc4b371cb75 | 195 | t2 = new Thread(); |
RossalaTTM | 36:bbc4b371cb75 | 196 | t2->start(measure); |
RossalaTTM | 36:bbc4b371cb75 | 197 | t3 = new Thread(); |
RossalaTTM | 36:bbc4b371cb75 | 198 | t3->start(commands); |
RossalaTTM | 36:bbc4b371cb75 | 199 | |
RossalaTTM | 36:bbc4b371cb75 | 200 | printf("Threads Started\n\r"); |
RossalaTTM | 36:bbc4b371cb75 | 201 | |
RossalaTTM | 36:bbc4b371cb75 | 202 | while (true){ |
RossalaTTM | 36:bbc4b371cb75 | 203 | Thread:: wait(5000); |
RossalaTTM | 36:bbc4b371cb75 | 204 | } |
RossalaTTM | 36:bbc4b371cb75 | 205 | } |