![](/media/cache/profiles/acab0116c354964a558e65bdd07ff047.jpg.50x50_q85.jpg)
test
Dependencies: CheckRTC LPS25H hts221
Fork of ELEC350-extended-referral2 by
main.cpp@42:4bf55ab88fbc, 2017-11-21 (annotated)
- Committer:
- spanesar
- Date:
- Tue Nov 21 17:13:45 2017 +0000
- Revision:
- 42:4bf55ab88fbc
- Parent:
- 41:ce3d6fb38d3b
- Child:
- 43:a9682b81632b
Tried to thread the serial functions but it 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 | 42:4bf55ab88fbc | 65 | char time [32]; |
spanesar | 42:4bf55ab88fbc | 66 | strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); |
spanesar | 40:426cd176ce80 | 67 | |
spanesar | 40:426cd176ce80 | 68 | if(buffer_str=="READALL") { |
spanesar | 40:426cd176ce80 | 69 | humidity.ReadTempHumi(&tempCelsius, &humi); // reads temperature and humity levels |
spanesar | 40:426cd176ce80 | 70 | barometer.get(); // gets barometer readings |
spanesar | 40:426cd176ce80 | 71 | // displays all the variables of time, date, temperature, humidity and all the barometric readings to the serial |
spanesar | 40:426cd176ce80 | 72 | printf("%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, barometer.pressure(), barometer.temperature()); |
spanesar | 40:426cd176ce80 | 73 | myled = 1; // LED is ON |
spanesar | 42:4bf55ab88fbc | 74 | Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! |
spanesar | 42:4bf55ab88fbc | 75 | myled = 0; // LED is OFF |
spanesar | 41:ce3d6fb38d3b | 76 | t2.signal_set(READALL_OFF); |
spanesar | 41:ce3d6fb38d3b | 77 | } |
spanesar | 41:ce3d6fb38d3b | 78 | Thread::wait(200); |
spanesar | 42:4bf55ab88fbc | 79 | |
spanesar | 42:4bf55ab88fbc | 80 | |
spanesar | 41:ce3d6fb38d3b | 81 | } |
spanesar | 41:ce3d6fb38d3b | 82 | } |
spanesar | 41:ce3d6fb38d3b | 83 | void function2() |
spanesar | 41:ce3d6fb38d3b | 84 | { |
spanesar | 41:ce3d6fb38d3b | 85 | while (true){ |
spanesar | 41:ce3d6fb38d3b | 86 | Thread::signal_wait(READALL_OFF); |
spanesar | 41:ce3d6fb38d3b | 87 | char buffer [128]; |
spanesar | 41:ce3d6fb38d3b | 88 | scanf("%127s", buffer); |
spanesar | 41:ce3d6fb38d3b | 89 | string buffer_str(buffer); |
spanesar | 41:ce3d6fb38d3b | 90 | char date [32]; |
spanesar | 41:ce3d6fb38d3b | 91 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 42:4bf55ab88fbc | 92 | char time [32]; |
spanesar | 42:4bf55ab88fbc | 93 | strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); |
spanesar | 40:426cd176ce80 | 94 | if(buffer_str=="SETTIME") { |
spanesar | 40:426cd176ce80 | 95 | set_time(1508268288); |
spanesar | 40:426cd176ce80 | 96 | printf("time and date updated to %s %s\n\r", time, date); |
spanesar | 40:426cd176ce80 | 97 | myled = 1; |
spanesar | 41:ce3d6fb38d3b | 98 | |
spanesar | 41:ce3d6fb38d3b | 99 | t3.signal_set(SETTIME_OFF); |
spanesar | 41:ce3d6fb38d3b | 100 | } |
spanesar | 41:ce3d6fb38d3b | 101 | //Thread::wait(200); |
spanesar | 41:ce3d6fb38d3b | 102 | //Thread::wait(200); |
spanesar | 41:ce3d6fb38d3b | 103 | //myled = 0; |
spanesar | 41:ce3d6fb38d3b | 104 | //Thread::wait(100); |
spanesar | 41:ce3d6fb38d3b | 105 | } |
spanesar | 41:ce3d6fb38d3b | 106 | } |
spanesar | 41:ce3d6fb38d3b | 107 | |
spanesar | 41:ce3d6fb38d3b | 108 | void function3() |
spanesar | 41:ce3d6fb38d3b | 109 | { |
spanesar | 41:ce3d6fb38d3b | 110 | while (true){ |
spanesar | 41:ce3d6fb38d3b | 111 | Thread::signal_wait(SETTIME_OFF); |
spanesar | 41:ce3d6fb38d3b | 112 | char buffer [128]; |
spanesar | 41:ce3d6fb38d3b | 113 | scanf("%127s", buffer); |
spanesar | 41:ce3d6fb38d3b | 114 | string buffer_str(buffer); |
spanesar | 41:ce3d6fb38d3b | 115 | char date [32]; |
spanesar | 41:ce3d6fb38d3b | 116 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 42:4bf55ab88fbc | 117 | char time [32]; |
spanesar | 42:4bf55ab88fbc | 118 | strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); |
spanesar | 40:426cd176ce80 | 119 | if(buffer_str=="SETT") { |
spanesar | 40:426cd176ce80 | 120 | for (count = 0; count < 5; count++) { |
spanesar | 39:504f5dc30a33 | 121 | 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 | 122 | buf = (char *)malloc(sz + 1); |
spanesar | 39:504f5dc30a33 | 123 | 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 | 124 | printf("%s\n\r",buf); |
spanesar | 39:504f5dc30a33 | 125 | printf("written to memory\n\r"); |
spanesar | 39:504f5dc30a33 | 126 | myled = 1; |
spanesar | 41:ce3d6fb38d3b | 127 | t4.signal_set(SETT_OFF); |
spanesar | 41:ce3d6fb38d3b | 128 | } |
spanesar | 41:ce3d6fb38d3b | 129 | //Thread::wait(200); |
spanesar | 41:ce3d6fb38d3b | 130 | //myled = 0; |
spanesar | 41:ce3d6fb38d3b | 131 | //Thread::wait(100); |
spanesar | 41:ce3d6fb38d3b | 132 | //wait(1); |
spanesar | 41:ce3d6fb38d3b | 133 | } |
spanesar | 41:ce3d6fb38d3b | 134 | } |
spanesar | 41:ce3d6fb38d3b | 135 | } |
spanesar | 41:ce3d6fb38d3b | 136 | void function4() |
spanesar | 41:ce3d6fb38d3b | 137 | { |
spanesar | 41:ce3d6fb38d3b | 138 | while (true){ |
spanesar | 41:ce3d6fb38d3b | 139 | Thread::signal_wait(SETT_OFF); |
spanesar | 41:ce3d6fb38d3b | 140 | char buffer [128]; |
spanesar | 41:ce3d6fb38d3b | 141 | scanf("%127s", buffer); |
spanesar | 41:ce3d6fb38d3b | 142 | string buffer_str(buffer); |
spanesar | 41:ce3d6fb38d3b | 143 | char date [32]; |
spanesar | 41:ce3d6fb38d3b | 144 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 40:426cd176ce80 | 145 | if(buffer_str=="READ") { |
spanesar | 39:504f5dc30a33 | 146 | printf("%s\n\r", buf); |
spanesar | 39:504f5dc30a33 | 147 | myled = 1; |
spanesar | 42:4bf55ab88fbc | 148 | osSignalSet(idMain, ALL_OFF); |
spanesar | 41:ce3d6fb38d3b | 149 | } |
spanesar | 41:ce3d6fb38d3b | 150 | //Thread::wait(200); |
spanesar | 41:ce3d6fb38d3b | 151 | //myled = 0; |
spanesar | 41:ce3d6fb38d3b | 152 | //Thread::wait (100); |
spanesar | 39:504f5dc30a33 | 153 | } |
noutram | 35:af125862c33e | 154 | } |
spanesar | 40:426cd176ce80 | 155 | |
spanesar | 40:426cd176ce80 | 156 | int main() |
spanesar | 40:426cd176ce80 | 157 | { |
spanesar | 40:426cd176ce80 | 158 | pc.baud(9600); //sets baud rate to 9600 |
spanesar | 40:426cd176ce80 | 159 | idMain = osThreadGetId(); |
spanesar | 40:426cd176ce80 | 160 | t1.start(function1); |
spanesar | 41:ce3d6fb38d3b | 161 | t2.start(function2); |
spanesar | 41:ce3d6fb38d3b | 162 | t3.start(function3); |
spanesar | 41:ce3d6fb38d3b | 163 | t4.start(function4); |
spanesar | 40:426cd176ce80 | 164 | id1 = t1.gettid(); |
spanesar | 41:ce3d6fb38d3b | 165 | id2 = t2.gettid(); |
spanesar | 41:ce3d6fb38d3b | 166 | id3 = t3.gettid(); |
spanesar | 41:ce3d6fb38d3b | 167 | id4 = t4.gettid(); |
spanesar | 40:426cd176ce80 | 168 | time_t seconds = time(NULL); |
spanesar | 42:4bf55ab88fbc | 169 | |
spanesar | 40:426cd176ce80 | 170 | humidity.init(); |
spanesar | 40:426cd176ce80 | 171 | humidity.calib(); |
spanesar | 40:426cd176ce80 | 172 | printf("Type SETTIME to set time and date [enter] \n\r"); |
spanesar | 40:426cd176ce80 | 173 | printf("Type SETT to start sampling [ENTER]\n\r"); |
spanesar | 40:426cd176ce80 | 174 | printf("Type DELETEALL to delete sensor data [enter]\n\r"); |
spanesar | 40:426cd176ce80 | 175 | printf("Type READALL [enter]\n\r"); |
spanesar | 40:426cd176ce80 | 176 | printf("Type READ to read sensor data in memory [ENTER]\n\r"); |
spanesar | 40:426cd176ce80 | 177 | |
spanesar | 40:426cd176ce80 | 178 | |
spanesar | 40:426cd176ce80 | 179 | |
spanesar | 40:426cd176ce80 | 180 | |
spanesar | 40:426cd176ce80 | 181 | |
spanesar | 40:426cd176ce80 | 182 | while(1) { |
spanesar | 40:426cd176ce80 | 183 | char buffer [128]; |
spanesar | 40:426cd176ce80 | 184 | scanf("%127s", buffer); |
spanesar | 40:426cd176ce80 | 185 | string buffer_str(buffer); |
spanesar | 41:ce3d6fb38d3b | 186 | osSignalWait(ALL_OFF,osWaitForever); |
spanesar | 40:426cd176ce80 | 187 | |
spanesar | 40:426cd176ce80 | 188 | |
spanesar | 40:426cd176ce80 | 189 | if(buffer_str=="?") { |
spanesar | 40:426cd176ce80 | 190 | printf("SOFT253 simple Temperature Humidity and Pressure Sensor Monitor\n\r"); |
spanesar | 40:426cd176ce80 | 191 | printf("Using the X-NUCLEO-IKS01A1 shield and MBED Libraries\n\r"); |
spanesar | 40:426cd176ce80 | 192 | } |
spanesar | 40:426cd176ce80 | 193 | |
spanesar | 40:426cd176ce80 | 194 | |
spanesar | 40:426cd176ce80 | 195 | } |
spanesar | 40:426cd176ce80 | 196 | |
spanesar | 39:504f5dc30a33 | 197 | |
FairyMental | 34:62aae7d507e2 | 198 | } |