![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
READDELETEALL
Fork of SOFT253_Assignment_V2 by
main.cpp@41:0f4a18bb1f83, 2017-05-13 (annotated)
- Committer:
- Mpoumtsakalaka
- Date:
- Sat May 13 20:28:50 2017 +0000
- Revision:
- 41:0f4a18bb1f83
- Parent:
- 40:4ea91e49d60c
ee
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" |
akovaci | 37:b62b3aa19645 | 5 | |
akovaci | 36:7426f37f0c96 | 6 | Serial pc(USBTX, USBRX); |
martinsimpson | 32:260a288be58f | 7 | |
akovaci | 37:b62b3aa19645 | 8 | #define NUMTHRDS 2 |
akovaci | 36:7426f37f0c96 | 9 | #define N 10 |
akovaci | 36:7426f37f0c96 | 10 | DigitalOut myled(D7); |
akovaci | 36:7426f37f0c96 | 11 | Ticker t; |
martinsimpson | 32:260a288be58f | 12 | I2C i2c2(I2C_SDA, I2C_SCL); |
noutram | 35:af125862c33e | 13 | |
noutram | 35:af125862c33e | 14 | float tempCelsius = 25.50; |
noutram | 35:af125862c33e | 15 | float humi = 55; |
akovaci | 38:8b6a6927b4f2 | 16 | int humiMax = 100; |
akovaci | 38:8b6a6927b4f2 | 17 | char cmd[20]; |
akovaci | 38:8b6a6927b4f2 | 18 | char cmd2[20]; |
Mpoumtsakalaka | 40:4ea91e49d60c | 19 | char input1[20]; |
Mpoumtsakalaka | 40:4ea91e49d60c | 20 | char input11[20]; |
Mpoumtsakalaka | 39:f43b008adc9d | 21 | char stringToCompare2[20]; |
Mpoumtsakalaka | 39:f43b008adc9d | 22 | char stringToCompare22[20]; |
akovaci | 36:7426f37f0c96 | 23 | float tempArray[N]; |
akovaci | 36:7426f37f0c96 | 24 | float humArray[N]; |
akovaci | 36:7426f37f0c96 | 25 | float pressArray[N]; |
noutram | 35:af125862c33e | 26 | uint32_t seconds = 0, minutes=0, hours=0; |
akovaci | 37:b62b3aa19645 | 27 | |
akovaci | 36:7426f37f0c96 | 28 | |
akovaci | 37:b62b3aa19645 | 29 | |
martinsimpson | 32:260a288be58f | 30 | |
noutram | 35:af125862c33e | 31 | LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
noutram | 35:af125862c33e | 32 | HTS221 humidity(I2C_SDA, I2C_SCL); |
noutram | 35:af125862c33e | 33 | |
noutram | 35:af125862c33e | 34 | |
akovaci | 36:7426f37f0c96 | 35 | void adcISR(); |
akovaci | 37:b62b3aa19645 | 36 | void thread1 (void const *args ); |
akovaci | 37:b62b3aa19645 | 37 | void threadcomun (void const *args); |
akovaci | 37:b62b3aa19645 | 38 | |
akovaci | 37:b62b3aa19645 | 39 | Thread* t1; |
akovaci | 37:b62b3aa19645 | 40 | Thread* t2; |
akovaci | 37:b62b3aa19645 | 41 | |
akovaci | 37:b62b3aa19645 | 42 | |
akovaci | 36:7426f37f0c96 | 43 | |
akovaci | 36:7426f37f0c96 | 44 | |
akovaci | 36:7426f37f0c96 | 45 | |
akovaci | 36:7426f37f0c96 | 46 | typedef struct { |
akovaci | 36:7426f37f0c96 | 47 | |
akovaci | 36:7426f37f0c96 | 48 | float tempVal ; |
akovaci | 36:7426f37f0c96 | 49 | float humVal; |
akovaci | 36:7426f37f0c96 | 50 | float pressVal; |
akovaci | 36:7426f37f0c96 | 51 | }message_t; |
martinsimpson | 32:260a288be58f | 52 | |
akovaci | 36:7426f37f0c96 | 53 | Mail<message_t, 10> mail_box; |
akovaci | 36:7426f37f0c96 | 54 | |
akovaci | 36:7426f37f0c96 | 55 | void adcISR() |
akovaci | 36:7426f37f0c96 | 56 | { |
akovaci | 36:7426f37f0c96 | 57 | message_t *message = mail_box.alloc(); |
akovaci | 36:7426f37f0c96 | 58 | message->tempVal = tempCelsius; |
akovaci | 36:7426f37f0c96 | 59 | message->humVal = humi; |
akovaci | 36:7426f37f0c96 | 60 | message->pressVal = barometer.pressure(); |
akovaci | 36:7426f37f0c96 | 61 | myled=1; |
akovaci | 36:7426f37f0c96 | 62 | mail_box.put(message); |
akovaci | 36:7426f37f0c96 | 63 | //Thread::wait(1000); |
akovaci | 36:7426f37f0c96 | 64 | |
akovaci | 36:7426f37f0c96 | 65 | } |
akovaci | 37:b62b3aa19645 | 66 | void thread1 (void const *args ) |
akovaci | 36:7426f37f0c96 | 67 | { |
akovaci | 36:7426f37f0c96 | 68 | |
akovaci | 36:7426f37f0c96 | 69 | pc.baud(115200); |
akovaci | 37:b62b3aa19645 | 70 | //pc.printf("Temperature,Humidity,Pressure\n\n"); |
akovaci | 36:7426f37f0c96 | 71 | while(1) |
akovaci | 36:7426f37f0c96 | 72 | { |
akovaci | 38:8b6a6927b4f2 | 73 | |
akovaci | 36:7426f37f0c96 | 74 | osEvent evt = mail_box.get(); |
akovaci | 36:7426f37f0c96 | 75 | if (evt.status == osEventMail) |
akovaci | 36:7426f37f0c96 | 76 | { |
akovaci | 36:7426f37f0c96 | 77 | for (unsigned int n=(N-1); n>0; n--) |
akovaci | 36:7426f37f0c96 | 78 | { |
akovaci | 36:7426f37f0c96 | 79 | tempArray[n]= tempArray[n-1]; |
akovaci | 36:7426f37f0c96 | 80 | humArray[n]= humArray[n-1]; |
akovaci | 36:7426f37f0c96 | 81 | pressArray[n]= pressArray[n-1]; |
akovaci | 36:7426f37f0c96 | 82 | } |
akovaci | 36:7426f37f0c96 | 83 | message_t *message = (message_t*)evt.value.p; |
akovaci | 36:7426f37f0c96 | 84 | tempArray[0]= message->tempVal; |
akovaci | 36:7426f37f0c96 | 85 | humArray[0] = message->humVal; |
akovaci | 36:7426f37f0c96 | 86 | pressArray[0]= message->pressVal; |
akovaci | 37:b62b3aa19645 | 87 | |
akovaci | 36:7426f37f0c96 | 88 | |
akovaci | 36:7426f37f0c96 | 89 | // pc.printf("%4.2f,%3.1f,%6.1f\n\r", tempArray[N/2], humArray[N/2], pressArray[N/2]); |
akovaci | 36:7426f37f0c96 | 90 | |
akovaci | 36:7426f37f0c96 | 91 | mail_box.free(message); |
akovaci | 36:7426f37f0c96 | 92 | } |
akovaci | 36:7426f37f0c96 | 93 | } |
akovaci | 36:7426f37f0c96 | 94 | } |
akovaci | 37:b62b3aa19645 | 95 | |
akovaci | 37:b62b3aa19645 | 96 | void threadcomun (void const *args) |
akovaci | 37:b62b3aa19645 | 97 | { |
Mpoumtsakalaka | 39:f43b008adc9d | 98 | int res; |
Mpoumtsakalaka | 39:f43b008adc9d | 99 | int res2; |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 100 | |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 101 | |
akovaci | 38:8b6a6927b4f2 | 102 | |
akovaci | 37:b62b3aa19645 | 103 | pc.baud(115200); |
akovaci | 37:b62b3aa19645 | 104 | pc.printf("Temperature,Humidity,Pressure\n\n"); |
akovaci | 38:8b6a6927b4f2 | 105 | //while(cmd==NULL){cmd=getchar();} |
Mpoumtsakalaka | 39:f43b008adc9d | 106 | while(1) |
Mpoumtsakalaka | 39:f43b008adc9d | 107 | { |
Mpoumtsakalaka | 39:f43b008adc9d | 108 | scanf("%s%s",&cmd,&cmd2); |
akovaci | 38:8b6a6927b4f2 | 109 | |
Mpoumtsakalaka | 39:f43b008adc9d | 110 | //READ ALL------------------------------------------ |
Mpoumtsakalaka | 39:f43b008adc9d | 111 | //pc.printf("YOU ENTERED: %s \n",cmd); |
Mpoumtsakalaka | 39:f43b008adc9d | 112 | |
Mpoumtsakalaka | 39:f43b008adc9d | 113 | |
Mpoumtsakalaka | 40:4ea91e49d60c | 114 | strcpy (input1,cmd); //INPUT 1 |
Mpoumtsakalaka | 40:4ea91e49d60c | 115 | strcpy (input11,cmd2); //INPUT 2 |
Mpoumtsakalaka | 39:f43b008adc9d | 116 | strcpy (stringToCompare2,"READ"); //STRING TO COMPARE 1 |
Mpoumtsakalaka | 39:f43b008adc9d | 117 | strcpy (stringToCompare22,"ALL"); //STRING TO COMPARE 2 |
Mpoumtsakalaka | 40:4ea91e49d60c | 118 | res = strncmp(input1,stringToCompare2,20); |
Mpoumtsakalaka | 40:4ea91e49d60c | 119 | res2 = strncmp(input11,stringToCompare22,20); |
akovaci | 38:8b6a6927b4f2 | 120 | |
Mpoumtsakalaka | 39:f43b008adc9d | 121 | if (res==0 && res2==0) |
Mpoumtsakalaka | 39:f43b008adc9d | 122 | { |
akovaci | 37:b62b3aa19645 | 123 | for (unsigned int n=0; n<N; n++) |
akovaci | 37:b62b3aa19645 | 124 | { |
akovaci | 37:b62b3aa19645 | 125 | pc.printf("the element of %d is %4.2f\n\r ",n,tempArray[n]); |
Mpoumtsakalaka | 39:f43b008adc9d | 126 | // wait(1.0); |
Mpoumtsakalaka | 39:f43b008adc9d | 127 | } |
akovaci | 38:8b6a6927b4f2 | 128 | } |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 129 | else //1 |
Mpoumtsakalaka | 39:f43b008adc9d | 130 | { |
Mpoumtsakalaka | 39:f43b008adc9d | 131 | strcpy (stringToCompare2,"DELETE"); |
Mpoumtsakalaka | 39:f43b008adc9d | 132 | strcpy (stringToCompare22,"ALL"); |
Mpoumtsakalaka | 40:4ea91e49d60c | 133 | res = strncmp(input1,stringToCompare2,20); |
Mpoumtsakalaka | 40:4ea91e49d60c | 134 | res2 = strncmp(input11,stringToCompare22,20); |
Mpoumtsakalaka | 39:f43b008adc9d | 135 | |
Mpoumtsakalaka | 39:f43b008adc9d | 136 | if(res==0 && res2==0) |
Mpoumtsakalaka | 39:f43b008adc9d | 137 | { |
Mpoumtsakalaka | 39:f43b008adc9d | 138 | //Delete every element. |
Mpoumtsakalaka | 39:f43b008adc9d | 139 | memset(tempArray, 0, sizeof tempArray); |
Mpoumtsakalaka | 39:f43b008adc9d | 140 | pc.printf("%d Elements deleted\n",N); |
Mpoumtsakalaka | 39:f43b008adc9d | 141 | } |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 142 | else //2 |
Mpoumtsakalaka | 39:f43b008adc9d | 143 | { |
Mpoumtsakalaka | 39:f43b008adc9d | 144 | strcpy (stringToCompare2,"READ"); |
Mpoumtsakalaka | 39:f43b008adc9d | 145 | strcpy (stringToCompare22,"Kapoion arithmo"); |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 146 | res = strncmp(input1,stringToCompare2,20); |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 147 | res2 = strncmp(input11,stringToCompare22,20); |
Mpoumtsakalaka | 39:f43b008adc9d | 148 | |
Mpoumtsakalaka | 39:f43b008adc9d | 149 | if(res==0 && res2==0) |
Mpoumtsakalaka | 39:f43b008adc9d | 150 | { |
Mpoumtsakalaka | 39:f43b008adc9d | 151 | |
Mpoumtsakalaka | 39:f43b008adc9d | 152 | } |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 153 | else //3 |
Mpoumtsakalaka | 40:4ea91e49d60c | 154 | { |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 155 | strcpy (stringToCompare2,"DELETE"); |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 156 | // strcpy (stringToCompare22,"Kapoion arithmo"); |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 157 | // res = strncmp(input1,stringToCompare2,20); |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 158 | // res2 = strncmp(input11,stringToCompare22,20); |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 159 | |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 160 | // int numberOfElements; //To input sto DELETE <n> |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 161 | |
Mpoumtsakalaka | 40:4ea91e49d60c | 162 | |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 163 | // if(res==0 && res2==0) |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 164 | // { |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 165 | // memset(tempArray, 0, numberOfElements); |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 166 | // } |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 167 | // else //4 |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 168 | // { |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 169 | //EDW tha paei to setDate |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 170 | |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 171 | // }//End of else 4 |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 172 | }//End of else 3 |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 173 | }//End of else 2 |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 174 | }//End of else 1 |
Mpoumtsakalaka | 39:f43b008adc9d | 175 | |
Mpoumtsakalaka | 41:0f4a18bb1f83 | 176 | |
Mpoumtsakalaka | 39:f43b008adc9d | 177 | |
Mpoumtsakalaka | 39:f43b008adc9d | 178 | }//End of while |
akovaci | 37:b62b3aa19645 | 179 | |
akovaci | 37:b62b3aa19645 | 180 | } |
akovaci | 36:7426f37f0c96 | 181 | char answer; |
akovaci | 36:7426f37f0c96 | 182 | int main(void) |
akovaci | 36:7426f37f0c96 | 183 | { |
akovaci | 36:7426f37f0c96 | 184 | |
akovaci | 36:7426f37f0c96 | 185 | puts("Loading... \n\n"); |
akovaci | 37:b62b3aa19645 | 186 | t1 = new Thread(thread1); |
akovaci | 37:b62b3aa19645 | 187 | t2 = new Thread(threadcomun); |
akovaci | 37:b62b3aa19645 | 188 | |
akovaci | 37:b62b3aa19645 | 189 | |
akovaci | 37:b62b3aa19645 | 190 | // Thread thread(thread1); |
akovaci | 37:b62b3aa19645 | 191 | //threadcomun(); |
akovaci | 37:b62b3aa19645 | 192 | |
akovaci | 37:b62b3aa19645 | 193 | //Thread thread(threadcomun); |
akovaci | 38:8b6a6927b4f2 | 194 | t.attach(&adcISR,5); // timer of measurements |
akovaci | 36:7426f37f0c96 | 195 | |
akovaci | 36:7426f37f0c96 | 196 | while(1) |
noutram | 35:af125862c33e | 197 | { |
akovaci | 36:7426f37f0c96 | 198 | humidity.init(); |
akovaci | 36:7426f37f0c96 | 199 | humidity.calib(); |
akovaci | 36:7426f37f0c96 | 200 | humidity.ReadTempHumi(&tempCelsius, &humi); |
akovaci | 36:7426f37f0c96 | 201 | barometer.get(); |
akovaci | 36:7426f37f0c96 | 202 | barometer.pressure(); |
akovaci | 36:7426f37f0c96 | 203 | barometer.temperature(); |
akovaci | 36:7426f37f0c96 | 204 | sleep(); |
akovaci | 36:7426f37f0c96 | 205 | Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! |
akovaci | 36:7426f37f0c96 | 206 | myled = 0; // LED is OFF |
akovaci | 36:7426f37f0c96 | 207 | Thread::wait(100); // 100 ms |
FairyMental | 34:62aae7d507e2 | 208 | } |
akovaci | 36:7426f37f0c96 | 209 | |
noutram | 35:af125862c33e | 210 | } |
noutram | 35:af125862c33e | 211 | |
noutram | 35:af125862c33e | 212 | |
noutram | 35:af125862c33e | 213 | |
akovaci | 36:7426f37f0c96 | 214 | |
noutram | 35:af125862c33e | 215 | |
akovaci | 36:7426f37f0c96 | 216 | |
noutram | 35:af125862c33e | 217 | |
akovaci | 36:7426f37f0c96 | 218 | |
akovaci | 36:7426f37f0c96 | 219 | |
akovaci | 36:7426f37f0c96 | 220 | |
akovaci | 36:7426f37f0c96 | 221 |