![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
READDELETEALL
Fork of SOFT253_Assignment_V2 by
main.cpp@40:4ea91e49d60c, 2017-05-13 (annotated)
- Committer:
- Mpoumtsakalaka
- Date:
- Sat May 13 19:55:22 2017 +0000
- Revision:
- 40:4ea91e49d60c
- Parent:
- 39:f43b008adc9d
- Child:
- 41:0f4a18bb1f83
READ ALL DELETE ALL
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; |
akovaci | 38:8b6a6927b4f2 | 100 | |
akovaci | 37:b62b3aa19645 | 101 | pc.baud(115200); |
akovaci | 37:b62b3aa19645 | 102 | pc.printf("Temperature,Humidity,Pressure\n\n"); |
akovaci | 38:8b6a6927b4f2 | 103 | //while(cmd==NULL){cmd=getchar();} |
Mpoumtsakalaka | 39:f43b008adc9d | 104 | while(1) |
Mpoumtsakalaka | 39:f43b008adc9d | 105 | { |
Mpoumtsakalaka | 39:f43b008adc9d | 106 | scanf("%s%s",&cmd,&cmd2); |
akovaci | 38:8b6a6927b4f2 | 107 | |
Mpoumtsakalaka | 39:f43b008adc9d | 108 | //READ ALL------------------------------------------ |
Mpoumtsakalaka | 39:f43b008adc9d | 109 | //pc.printf("YOU ENTERED: %s \n",cmd); |
Mpoumtsakalaka | 39:f43b008adc9d | 110 | |
Mpoumtsakalaka | 39:f43b008adc9d | 111 | |
Mpoumtsakalaka | 40:4ea91e49d60c | 112 | strcpy (input1,cmd); //INPUT 1 |
Mpoumtsakalaka | 40:4ea91e49d60c | 113 | strcpy (input11,cmd2); //INPUT 2 |
Mpoumtsakalaka | 39:f43b008adc9d | 114 | strcpy (stringToCompare2,"READ"); //STRING TO COMPARE 1 |
Mpoumtsakalaka | 39:f43b008adc9d | 115 | strcpy (stringToCompare22,"ALL"); //STRING TO COMPARE 2 |
Mpoumtsakalaka | 40:4ea91e49d60c | 116 | res = strncmp(input1,stringToCompare2,20); |
Mpoumtsakalaka | 40:4ea91e49d60c | 117 | res2 = strncmp(input11,stringToCompare22,20); |
akovaci | 38:8b6a6927b4f2 | 118 | |
Mpoumtsakalaka | 39:f43b008adc9d | 119 | if (res==0 && res2==0) |
Mpoumtsakalaka | 39:f43b008adc9d | 120 | { |
akovaci | 37:b62b3aa19645 | 121 | for (unsigned int n=0; n<N; n++) |
akovaci | 37:b62b3aa19645 | 122 | { |
akovaci | 37:b62b3aa19645 | 123 | pc.printf("the element of %d is %4.2f\n\r ",n,tempArray[n]); |
Mpoumtsakalaka | 39:f43b008adc9d | 124 | // wait(1.0); |
Mpoumtsakalaka | 39:f43b008adc9d | 125 | } |
akovaci | 38:8b6a6927b4f2 | 126 | } |
akovaci | 38:8b6a6927b4f2 | 127 | else |
Mpoumtsakalaka | 39:f43b008adc9d | 128 | { |
Mpoumtsakalaka | 39:f43b008adc9d | 129 | strcpy (stringToCompare2,"DELETE"); |
Mpoumtsakalaka | 39:f43b008adc9d | 130 | strcpy (stringToCompare22,"ALL"); |
Mpoumtsakalaka | 40:4ea91e49d60c | 131 | res = strncmp(input1,stringToCompare2,20); |
Mpoumtsakalaka | 40:4ea91e49d60c | 132 | res2 = strncmp(input11,stringToCompare22,20); |
Mpoumtsakalaka | 39:f43b008adc9d | 133 | |
Mpoumtsakalaka | 39:f43b008adc9d | 134 | if(res==0 && res2==0) |
Mpoumtsakalaka | 39:f43b008adc9d | 135 | { |
Mpoumtsakalaka | 39:f43b008adc9d | 136 | //Delete every element. |
Mpoumtsakalaka | 39:f43b008adc9d | 137 | memset(tempArray, 0, sizeof tempArray); |
Mpoumtsakalaka | 39:f43b008adc9d | 138 | pc.printf("%d Elements deleted\n",N); |
Mpoumtsakalaka | 39:f43b008adc9d | 139 | } |
Mpoumtsakalaka | 39:f43b008adc9d | 140 | else |
Mpoumtsakalaka | 39:f43b008adc9d | 141 | { |
Mpoumtsakalaka | 39:f43b008adc9d | 142 | strcpy (stringToCompare2,"READ"); |
Mpoumtsakalaka | 39:f43b008adc9d | 143 | strcpy (stringToCompare22,"Kapoion arithmo"); |
Mpoumtsakalaka | 39:f43b008adc9d | 144 | res = strncmp(readAll1,stringToCompare2,20); |
Mpoumtsakalaka | 39:f43b008adc9d | 145 | res2 = strncmp(readAll11,stringToCompare22,20); |
Mpoumtsakalaka | 39:f43b008adc9d | 146 | |
Mpoumtsakalaka | 39:f43b008adc9d | 147 | if(res==0 && res2==0) |
Mpoumtsakalaka | 39:f43b008adc9d | 148 | { |
Mpoumtsakalaka | 39:f43b008adc9d | 149 | |
Mpoumtsakalaka | 39:f43b008adc9d | 150 | } |
Mpoumtsakalaka | 40:4ea91e49d60c | 151 | else |
Mpoumtsakalaka | 40:4ea91e49d60c | 152 | { |
Mpoumtsakalaka | 40:4ea91e49d60c | 153 | |
Mpoumtsakalaka | 40:4ea91e49d60c | 154 | } |
Mpoumtsakalaka | 39:f43b008adc9d | 155 | } |
Mpoumtsakalaka | 39:f43b008adc9d | 156 | } |
Mpoumtsakalaka | 39:f43b008adc9d | 157 | |
Mpoumtsakalaka | 39:f43b008adc9d | 158 | |
Mpoumtsakalaka | 39:f43b008adc9d | 159 | |
Mpoumtsakalaka | 39:f43b008adc9d | 160 | |
Mpoumtsakalaka | 39:f43b008adc9d | 161 | |
Mpoumtsakalaka | 39:f43b008adc9d | 162 | //switch(cmd) |
Mpoumtsakalaka | 39:f43b008adc9d | 163 | //{ |
Mpoumtsakalaka | 39:f43b008adc9d | 164 | // case 'f': pc.printf("Douleuei"); |
Mpoumtsakalaka | 39:f43b008adc9d | 165 | //} |
Mpoumtsakalaka | 39:f43b008adc9d | 166 | |
Mpoumtsakalaka | 39:f43b008adc9d | 167 | |
Mpoumtsakalaka | 39:f43b008adc9d | 168 | |
Mpoumtsakalaka | 39:f43b008adc9d | 169 | }//End of while |
akovaci | 37:b62b3aa19645 | 170 | |
akovaci | 37:b62b3aa19645 | 171 | } |
akovaci | 36:7426f37f0c96 | 172 | char answer; |
akovaci | 36:7426f37f0c96 | 173 | int main(void) |
akovaci | 36:7426f37f0c96 | 174 | { |
akovaci | 36:7426f37f0c96 | 175 | |
akovaci | 36:7426f37f0c96 | 176 | puts("Loading... \n\n"); |
akovaci | 37:b62b3aa19645 | 177 | t1 = new Thread(thread1); |
akovaci | 37:b62b3aa19645 | 178 | t2 = new Thread(threadcomun); |
akovaci | 37:b62b3aa19645 | 179 | |
akovaci | 37:b62b3aa19645 | 180 | |
akovaci | 37:b62b3aa19645 | 181 | // Thread thread(thread1); |
akovaci | 37:b62b3aa19645 | 182 | //threadcomun(); |
akovaci | 37:b62b3aa19645 | 183 | |
akovaci | 37:b62b3aa19645 | 184 | //Thread thread(threadcomun); |
akovaci | 38:8b6a6927b4f2 | 185 | t.attach(&adcISR,5); // timer of measurements |
akovaci | 36:7426f37f0c96 | 186 | |
akovaci | 36:7426f37f0c96 | 187 | while(1) |
noutram | 35:af125862c33e | 188 | { |
akovaci | 36:7426f37f0c96 | 189 | humidity.init(); |
akovaci | 36:7426f37f0c96 | 190 | humidity.calib(); |
akovaci | 36:7426f37f0c96 | 191 | humidity.ReadTempHumi(&tempCelsius, &humi); |
akovaci | 36:7426f37f0c96 | 192 | barometer.get(); |
akovaci | 36:7426f37f0c96 | 193 | barometer.pressure(); |
akovaci | 36:7426f37f0c96 | 194 | barometer.temperature(); |
akovaci | 36:7426f37f0c96 | 195 | sleep(); |
akovaci | 36:7426f37f0c96 | 196 | Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! |
akovaci | 36:7426f37f0c96 | 197 | myled = 0; // LED is OFF |
akovaci | 36:7426f37f0c96 | 198 | Thread::wait(100); // 100 ms |
FairyMental | 34:62aae7d507e2 | 199 | } |
akovaci | 36:7426f37f0c96 | 200 | |
noutram | 35:af125862c33e | 201 | } |
noutram | 35:af125862c33e | 202 | |
noutram | 35:af125862c33e | 203 | |
noutram | 35:af125862c33e | 204 | |
akovaci | 36:7426f37f0c96 | 205 | |
noutram | 35:af125862c33e | 206 | |
akovaci | 36:7426f37f0c96 | 207 | |
noutram | 35:af125862c33e | 208 | |
akovaci | 36:7426f37f0c96 | 209 | |
akovaci | 36:7426f37f0c96 | 210 | |
akovaci | 36:7426f37f0c96 | 211 | |
akovaci | 36:7426f37f0c96 | 212 |