![](/media/cache/profiles/acab0116c354964a558e65bdd07ff047.jpg.50x50_q85.jpg)
test
Dependencies: CheckRTC LPS25H hts221
Fork of ELEC350-extended-referral2 by
main.cpp@41:ce3d6fb38d3b, 2017-11-17 (annotated)
- Committer:
- spanesar
- Date:
- Fri Nov 17 14:40:45 2017 +0000
- Revision:
- 41:ce3d6fb38d3b
- Parent:
- 40:426cd176ce80
- Child:
- 42:4bf55ab88fbc
tried doing the threading for the serial commands but only one thread is working but also locks;
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" |
spanesar | 40:426cd176ce80 | 5 | #include <sstream> |
spanesar | 41:ce3d6fb38d3b | 6 | #define READALL_OFF 1 |
spanesar | 41:ce3d6fb38d3b | 7 | #define SETTIME_OFF 2 |
spanesar | 41:ce3d6fb38d3b | 8 | #define SETT_OFF 4 |
spanesar | 41:ce3d6fb38d3b | 9 | #define READ_OFF 7 |
spanesar | 41:ce3d6fb38d3b | 10 | #define ALL_OFF 9 |
spanesar | 41:ce3d6fb38d3b | 11 | |
spanesar | 39:504f5dc30a33 | 12 | |
martinsimpson | 32:260a288be58f | 13 | |
martinsimpson | 32:260a288be58f | 14 | DigitalOut myled(LED1); |
martinsimpson | 32:260a288be58f | 15 | I2C i2c2(I2C_SDA, I2C_SCL); |
spanesar | 39:504f5dc30a33 | 16 | Serial pc(USBTX, USBRX); |
noutram | 35:af125862c33e | 17 | float tempCelsius = 25.50; |
noutram | 35:af125862c33e | 18 | float humi = 55; |
spanesar | 39:504f5dc30a33 | 19 | int humiMax = 100; |
spanesar | 40:426cd176ce80 | 20 | char buffer_str=0; |
spanesar | 39:504f5dc30a33 | 21 | uint32_t seconds = 0, minutes=0, hours=0; |
spanesar | 39:504f5dc30a33 | 22 | Ticker t; |
spanesar | 39:504f5dc30a33 | 23 | volatile static unsigned short sample16 = 0; |
spanesar | 39:504f5dc30a33 | 24 | void doSample1Hz(); |
spanesar | 39:504f5dc30a33 | 25 | int count = 0; |
spanesar | 39:504f5dc30a33 | 26 | char *buf; |
spanesar | 39:504f5dc30a33 | 27 | size_t sz; |
spanesar | 40:426cd176ce80 | 28 | typedef struct { |
spanesar | 40:426cd176ce80 | 29 | float temp; |
spanesar | 40:426cd176ce80 | 30 | float press; |
spanesar | 40:426cd176ce80 | 31 | float humid; |
spanesar | 40:426cd176ce80 | 32 | } measurement; |
spanesar | 40:426cd176ce80 | 33 | measurement buffer [120]; |
martinsimpson | 32:260a288be58f | 34 | |
noutram | 35:af125862c33e | 35 | LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
noutram | 35:af125862c33e | 36 | HTS221 humidity(I2C_SDA, I2C_SCL); |
noutram | 35:af125862c33e | 37 | |
spanesar | 40:426cd176ce80 | 38 | void function1(); |
spanesar | 40:426cd176ce80 | 39 | void function2(); |
spanesar | 40:426cd176ce80 | 40 | void function3(); |
spanesar | 41:ce3d6fb38d3b | 41 | void function4(); |
spanesar | 39:504f5dc30a33 | 42 | |
spanesar | 40:426cd176ce80 | 43 | Thread t1; |
spanesar | 40:426cd176ce80 | 44 | Thread t2; |
spanesar | 40:426cd176ce80 | 45 | Thread t3; |
spanesar | 40:426cd176ce80 | 46 | Thread t4; |
spanesar | 41:ce3d6fb38d3b | 47 | Thread t5; |
spanesar | 39:504f5dc30a33 | 48 | |
spanesar | 40:426cd176ce80 | 49 | osThreadId idMain; |
spanesar | 40:426cd176ce80 | 50 | osThreadId id1; |
spanesar | 41:ce3d6fb38d3b | 51 | osThreadId id2; |
spanesar | 41:ce3d6fb38d3b | 52 | osThreadId id3; |
spanesar | 41:ce3d6fb38d3b | 53 | osThreadId id4; |
spanesar | 41:ce3d6fb38d3b | 54 | |
spanesar | 41:ce3d6fb38d3b | 55 | |
noutram | 35:af125862c33e | 56 | |
spanesar | 40:426cd176ce80 | 57 | void function1() |
spanesar | 40:426cd176ce80 | 58 | { |
spanesar | 41:ce3d6fb38d3b | 59 | while (true){ |
spanesar | 40:426cd176ce80 | 60 | char buffer [128]; |
spanesar | 40:426cd176ce80 | 61 | scanf("%127s", buffer); |
spanesar | 40:426cd176ce80 | 62 | string buffer_str(buffer); |
spanesar | 41:ce3d6fb38d3b | 63 | char date [32]; |
spanesar | 41:ce3d6fb38d3b | 64 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 40:426cd176ce80 | 65 | |
spanesar | 40:426cd176ce80 | 66 | if(buffer_str=="READALL") { |
spanesar | 40:426cd176ce80 | 67 | humidity.ReadTempHumi(&tempCelsius, &humi); // reads temperature and humity levels |
spanesar | 40:426cd176ce80 | 68 | barometer.get(); // gets barometer readings |
spanesar | 40:426cd176ce80 | 69 | // displays all the variables of time, date, temperature, humidity and all the barometric readings to the serial |
spanesar | 40:426cd176ce80 | 70 | printf("%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, barometer.pressure(), barometer.temperature()); |
spanesar | 40:426cd176ce80 | 71 | myled = 1; // LED is ON |
spanesar | 41:ce3d6fb38d3b | 72 | t2.signal_set(READALL_OFF); |
spanesar | 41:ce3d6fb38d3b | 73 | } |
spanesar | 41:ce3d6fb38d3b | 74 | Thread::wait(200); |
spanesar | 41:ce3d6fb38d3b | 75 | //Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! |
spanesar | 41:ce3d6fb38d3b | 76 | //myled = 0; // LED is OFF |
spanesar | 41:ce3d6fb38d3b | 77 | //Thread::wait(100); // 100 ms |
spanesar | 41:ce3d6fb38d3b | 78 | } |
spanesar | 41:ce3d6fb38d3b | 79 | } |
spanesar | 41:ce3d6fb38d3b | 80 | void function2() |
spanesar | 41:ce3d6fb38d3b | 81 | { |
spanesar | 41:ce3d6fb38d3b | 82 | while (true){ |
spanesar | 41:ce3d6fb38d3b | 83 | Thread::signal_wait(READALL_OFF); |
spanesar | 41:ce3d6fb38d3b | 84 | char buffer [128]; |
spanesar | 41:ce3d6fb38d3b | 85 | scanf("%127s", buffer); |
spanesar | 41:ce3d6fb38d3b | 86 | string buffer_str(buffer); |
spanesar | 41:ce3d6fb38d3b | 87 | char date [32]; |
spanesar | 41:ce3d6fb38d3b | 88 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 40:426cd176ce80 | 89 | if(buffer_str=="SETTIME") { |
spanesar | 40:426cd176ce80 | 90 | set_time(1508268288); |
spanesar | 40:426cd176ce80 | 91 | printf("time and date updated to %s %s\n\r", time, date); |
spanesar | 40:426cd176ce80 | 92 | myled = 1; |
spanesar | 41:ce3d6fb38d3b | 93 | |
spanesar | 41:ce3d6fb38d3b | 94 | t3.signal_set(SETTIME_OFF); |
spanesar | 41:ce3d6fb38d3b | 95 | } |
spanesar | 41:ce3d6fb38d3b | 96 | //Thread::wait(200); |
spanesar | 41:ce3d6fb38d3b | 97 | //Thread::wait(200); |
spanesar | 41:ce3d6fb38d3b | 98 | //myled = 0; |
spanesar | 41:ce3d6fb38d3b | 99 | //Thread::wait(100); |
spanesar | 41:ce3d6fb38d3b | 100 | } |
spanesar | 41:ce3d6fb38d3b | 101 | } |
spanesar | 41:ce3d6fb38d3b | 102 | |
spanesar | 41:ce3d6fb38d3b | 103 | void function3() |
spanesar | 41:ce3d6fb38d3b | 104 | { |
spanesar | 41:ce3d6fb38d3b | 105 | while (true){ |
spanesar | 41:ce3d6fb38d3b | 106 | Thread::signal_wait(SETTIME_OFF); |
spanesar | 41:ce3d6fb38d3b | 107 | char buffer [128]; |
spanesar | 41:ce3d6fb38d3b | 108 | scanf("%127s", buffer); |
spanesar | 41:ce3d6fb38d3b | 109 | string buffer_str(buffer); |
spanesar | 41:ce3d6fb38d3b | 110 | char date [32]; |
spanesar | 41:ce3d6fb38d3b | 111 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 40:426cd176ce80 | 112 | if(buffer_str=="SETT") { |
spanesar | 40:426cd176ce80 | 113 | for (count = 0; count < 5; count++) { |
spanesar | 39:504f5dc30a33 | 114 | sz = snprintf(NULL, 0,"%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, barometer.pressure(), barometer.temperature()); |
spanesar | 39:504f5dc30a33 | 115 | buf = (char *)malloc(sz + 1); |
spanesar | 39:504f5dc30a33 | 116 | snprintf(buf, sz + 1, "%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, barometer.pressure(), barometer.temperature()); |
spanesar | 39:504f5dc30a33 | 117 | printf("%s\n\r",buf); |
spanesar | 39:504f5dc30a33 | 118 | printf("written to memory\n\r"); |
spanesar | 39:504f5dc30a33 | 119 | myled = 1; |
spanesar | 41:ce3d6fb38d3b | 120 | t4.signal_set(SETT_OFF); |
spanesar | 41:ce3d6fb38d3b | 121 | } |
spanesar | 41:ce3d6fb38d3b | 122 | //Thread::wait(200); |
spanesar | 41:ce3d6fb38d3b | 123 | //myled = 0; |
spanesar | 41:ce3d6fb38d3b | 124 | //Thread::wait(100); |
spanesar | 41:ce3d6fb38d3b | 125 | //wait(1); |
spanesar | 41:ce3d6fb38d3b | 126 | } |
spanesar | 41:ce3d6fb38d3b | 127 | } |
spanesar | 41:ce3d6fb38d3b | 128 | } |
spanesar | 41:ce3d6fb38d3b | 129 | void function4() |
spanesar | 41:ce3d6fb38d3b | 130 | { |
spanesar | 41:ce3d6fb38d3b | 131 | while (true){ |
spanesar | 41:ce3d6fb38d3b | 132 | Thread::signal_wait(SETT_OFF); |
spanesar | 41:ce3d6fb38d3b | 133 | char buffer [128]; |
spanesar | 41:ce3d6fb38d3b | 134 | scanf("%127s", buffer); |
spanesar | 41:ce3d6fb38d3b | 135 | string buffer_str(buffer); |
spanesar | 41:ce3d6fb38d3b | 136 | char date [32]; |
spanesar | 41:ce3d6fb38d3b | 137 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 40:426cd176ce80 | 138 | if(buffer_str=="READ") { |
spanesar | 39:504f5dc30a33 | 139 | printf("%s\n\r", buf); |
spanesar | 39:504f5dc30a33 | 140 | myled = 1; |
spanesar | 41:ce3d6fb38d3b | 141 | t5.signal_set(READ_OFF); |
spanesar | 41:ce3d6fb38d3b | 142 | } |
spanesar | 41:ce3d6fb38d3b | 143 | //Thread::wait(200); |
spanesar | 41:ce3d6fb38d3b | 144 | //myled = 0; |
spanesar | 41:ce3d6fb38d3b | 145 | //Thread::wait (100); |
spanesar | 39:504f5dc30a33 | 146 | } |
noutram | 35:af125862c33e | 147 | } |
spanesar | 40:426cd176ce80 | 148 | |
spanesar | 40:426cd176ce80 | 149 | int main() |
spanesar | 40:426cd176ce80 | 150 | { |
spanesar | 40:426cd176ce80 | 151 | pc.baud(9600); //sets baud rate to 9600 |
spanesar | 40:426cd176ce80 | 152 | idMain = osThreadGetId(); |
spanesar | 40:426cd176ce80 | 153 | t1.start(function1); |
spanesar | 41:ce3d6fb38d3b | 154 | t2.start(function2); |
spanesar | 41:ce3d6fb38d3b | 155 | t3.start(function3); |
spanesar | 41:ce3d6fb38d3b | 156 | t4.start(function4); |
spanesar | 40:426cd176ce80 | 157 | id1 = t1.gettid(); |
spanesar | 41:ce3d6fb38d3b | 158 | id2 = t2.gettid(); |
spanesar | 41:ce3d6fb38d3b | 159 | id3 = t3.gettid(); |
spanesar | 41:ce3d6fb38d3b | 160 | id4 = t4.gettid(); |
spanesar | 40:426cd176ce80 | 161 | time_t seconds = time(NULL); |
spanesar | 40:426cd176ce80 | 162 | char time [32]; |
spanesar | 40:426cd176ce80 | 163 | strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); |
spanesar | 40:426cd176ce80 | 164 | humidity.init(); |
spanesar | 40:426cd176ce80 | 165 | humidity.calib(); |
spanesar | 40:426cd176ce80 | 166 | printf("Type SETTIME to set time and date [enter] \n\r"); |
spanesar | 40:426cd176ce80 | 167 | printf("Type SETT to start sampling [ENTER]\n\r"); |
spanesar | 40:426cd176ce80 | 168 | printf("Type DELETEALL to delete sensor data [enter]\n\r"); |
spanesar | 40:426cd176ce80 | 169 | printf("Type READALL [enter]\n\r"); |
spanesar | 40:426cd176ce80 | 170 | printf("Type READ to read sensor data in memory [ENTER]\n\r"); |
spanesar | 40:426cd176ce80 | 171 | |
spanesar | 40:426cd176ce80 | 172 | |
spanesar | 40:426cd176ce80 | 173 | |
spanesar | 40:426cd176ce80 | 174 | |
spanesar | 40:426cd176ce80 | 175 | |
spanesar | 40:426cd176ce80 | 176 | while(1) { |
spanesar | 40:426cd176ce80 | 177 | char buffer [128]; |
spanesar | 40:426cd176ce80 | 178 | scanf("%127s", buffer); |
spanesar | 40:426cd176ce80 | 179 | string buffer_str(buffer); |
spanesar | 41:ce3d6fb38d3b | 180 | osSignalWait(ALL_OFF,osWaitForever); |
spanesar | 40:426cd176ce80 | 181 | |
spanesar | 40:426cd176ce80 | 182 | |
spanesar | 40:426cd176ce80 | 183 | if(buffer_str=="?") { |
spanesar | 40:426cd176ce80 | 184 | printf("SOFT253 simple Temperature Humidity and Pressure Sensor Monitor\n\r"); |
spanesar | 40:426cd176ce80 | 185 | printf("Using the X-NUCLEO-IKS01A1 shield and MBED Libraries\n\r"); |
spanesar | 40:426cd176ce80 | 186 | } |
spanesar | 40:426cd176ce80 | 187 | |
spanesar | 40:426cd176ce80 | 188 | |
spanesar | 40:426cd176ce80 | 189 | } |
spanesar | 40:426cd176ce80 | 190 | |
spanesar | 39:504f5dc30a33 | 191 | |
FairyMental | 34:62aae7d507e2 | 192 | } |