![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
soft253
Fork of SOFT253_Assignment by
main.cpp@38:8b6a6927b4f2, 2017-05-13 (annotated)
- Committer:
- akovaci
- Date:
- Sat May 13 00:48:10 2017 +0000
- Revision:
- 38:8b6a6927b4f2
- Parent:
- 37:b62b3aa19645
PANOS
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]; |
akovaci | 38:8b6a6927b4f2 | 19 | char s1[20]; |
akovaci | 38:8b6a6927b4f2 | 20 | char s11[20]; |
akovaci | 38:8b6a6927b4f2 | 21 | char s2[20]; |
akovaci | 38:8b6a6927b4f2 | 22 | char s22[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 | { |
akovaci | 38:8b6a6927b4f2 | 98 | int ret; |
akovaci | 38:8b6a6927b4f2 | 99 | int ret2; |
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();} |
akovaci | 38:8b6a6927b4f2 | 104 | while(1){ |
akovaci | 38:8b6a6927b4f2 | 105 | scanf("%s%s",&cmd,&cmd2); |
akovaci | 38:8b6a6927b4f2 | 106 | |
akovaci | 38:8b6a6927b4f2 | 107 | //pc.printf("YOU ENTERED: %s \n",cmd); |
akovaci | 38:8b6a6927b4f2 | 108 | strcpy (s1,cmd); |
akovaci | 38:8b6a6927b4f2 | 109 | strcpy (s11,cmd2); |
akovaci | 38:8b6a6927b4f2 | 110 | strcpy (s2,"READ"); |
akovaci | 38:8b6a6927b4f2 | 111 | strcpy (s22,"ALL"); |
akovaci | 38:8b6a6927b4f2 | 112 | ret = strncmp(s1,s2,20); |
akovaci | 38:8b6a6927b4f2 | 113 | ret = strncmp(s11,s22,20); |
akovaci | 38:8b6a6927b4f2 | 114 | |
akovaci | 38:8b6a6927b4f2 | 115 | if (ret==0 && ret2==0) |
akovaci | 38:8b6a6927b4f2 | 116 | { |
akovaci | 38:8b6a6927b4f2 | 117 | |
akovaci | 37:b62b3aa19645 | 118 | for (unsigned int n=0; n<N; n++) |
akovaci | 37:b62b3aa19645 | 119 | { |
akovaci | 37:b62b3aa19645 | 120 | pc.printf("the element of %d is %4.2f\n\r ",n,tempArray[n]); |
akovaci | 38:8b6a6927b4f2 | 121 | |
akovaci | 38:8b6a6927b4f2 | 122 | // wait(1.0); |
akovaci | 38:8b6a6927b4f2 | 123 | } |
akovaci | 38:8b6a6927b4f2 | 124 | } |
akovaci | 38:8b6a6927b4f2 | 125 | else |
akovaci | 38:8b6a6927b4f2 | 126 | { |
akovaci | 38:8b6a6927b4f2 | 127 | pc.printf("error re mlk: %s\n",cmd); |
akovaci | 38:8b6a6927b4f2 | 128 | } |
akovaci | 38:8b6a6927b4f2 | 129 | } |
akovaci | 37:b62b3aa19645 | 130 | |
akovaci | 37:b62b3aa19645 | 131 | } |
akovaci | 36:7426f37f0c96 | 132 | char answer; |
akovaci | 36:7426f37f0c96 | 133 | int main(void) |
akovaci | 36:7426f37f0c96 | 134 | { |
akovaci | 36:7426f37f0c96 | 135 | |
akovaci | 36:7426f37f0c96 | 136 | puts("Loading... \n\n"); |
akovaci | 37:b62b3aa19645 | 137 | t1 = new Thread(thread1); |
akovaci | 37:b62b3aa19645 | 138 | t2 = new Thread(threadcomun); |
akovaci | 37:b62b3aa19645 | 139 | |
akovaci | 37:b62b3aa19645 | 140 | |
akovaci | 37:b62b3aa19645 | 141 | // Thread thread(thread1); |
akovaci | 37:b62b3aa19645 | 142 | //threadcomun(); |
akovaci | 37:b62b3aa19645 | 143 | |
akovaci | 37:b62b3aa19645 | 144 | //Thread thread(threadcomun); |
akovaci | 38:8b6a6927b4f2 | 145 | t.attach(&adcISR,5); // timer of measurements |
akovaci | 36:7426f37f0c96 | 146 | |
akovaci | 36:7426f37f0c96 | 147 | while(1) |
noutram | 35:af125862c33e | 148 | { |
akovaci | 36:7426f37f0c96 | 149 | humidity.init(); |
akovaci | 36:7426f37f0c96 | 150 | humidity.calib(); |
akovaci | 36:7426f37f0c96 | 151 | humidity.ReadTempHumi(&tempCelsius, &humi); |
akovaci | 36:7426f37f0c96 | 152 | barometer.get(); |
akovaci | 36:7426f37f0c96 | 153 | barometer.pressure(); |
akovaci | 36:7426f37f0c96 | 154 | barometer.temperature(); |
akovaci | 36:7426f37f0c96 | 155 | sleep(); |
akovaci | 36:7426f37f0c96 | 156 | Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! |
akovaci | 36:7426f37f0c96 | 157 | myled = 0; // LED is OFF |
akovaci | 36:7426f37f0c96 | 158 | Thread::wait(100); // 100 ms |
FairyMental | 34:62aae7d507e2 | 159 | } |
akovaci | 36:7426f37f0c96 | 160 | |
noutram | 35:af125862c33e | 161 | } |
noutram | 35:af125862c33e | 162 | |
noutram | 35:af125862c33e | 163 | |
noutram | 35:af125862c33e | 164 | |
akovaci | 36:7426f37f0c96 | 165 | |
noutram | 35:af125862c33e | 166 | |
akovaci | 36:7426f37f0c96 | 167 | |
noutram | 35:af125862c33e | 168 | |
akovaci | 36:7426f37f0c96 | 169 | |
akovaci | 36:7426f37f0c96 | 170 | |
akovaci | 36:7426f37f0c96 | 171 | |
akovaci | 36:7426f37f0c96 | 172 |