![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
my coursework
Dependencies: CheckRTC LPS25H hts221
Fork of ELEC350-extended-referral1 by
main.cpp@43:a9682b81632b, 2017-11-22 (annotated)
- Committer:
- spanesar
- Date:
- Wed Nov 22 12:48:08 2017 +0000
- Revision:
- 43:a9682b81632b
- Parent:
- 42:4bf55ab88fbc
- Child:
- 44:ca34a376a17c
put all serial commands in a single thread
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 | 43:a9682b81632b | 9 | #define READ_OFF 7 |
spanesar | 43:a9682b81632b | 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 | 43:a9682b81632b | 39 | |
spanesar | 39:504f5dc30a33 | 40 | |
spanesar | 40:426cd176ce80 | 41 | Thread t1; |
spanesar | 43:a9682b81632b | 42 | |
spanesar | 39:504f5dc30a33 | 43 | |
spanesar | 40:426cd176ce80 | 44 | osThreadId idMain; |
spanesar | 40:426cd176ce80 | 45 | osThreadId id1; |
spanesar | 41:ce3d6fb38d3b | 46 | osThreadId id2; |
spanesar | 41:ce3d6fb38d3b | 47 | osThreadId id3; |
spanesar | 41:ce3d6fb38d3b | 48 | osThreadId id4; |
spanesar | 41:ce3d6fb38d3b | 49 | |
spanesar | 43:a9682b81632b | 50 | |
noutram | 35:af125862c33e | 51 | |
spanesar | 40:426cd176ce80 | 52 | void function1() |
spanesar | 43:a9682b81632b | 53 | {//begin function |
spanesar | 43:a9682b81632b | 54 | while (true) {//begin while true |
spanesar | 43:a9682b81632b | 55 | char buffer [128]; |
spanesar | 43:a9682b81632b | 56 | scanf("%127s", buffer); |
spanesar | 43:a9682b81632b | 57 | string buffer_str(buffer); |
spanesar | 43:a9682b81632b | 58 | char date [32]; |
spanesar | 43:a9682b81632b | 59 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 43:a9682b81632b | 60 | char time [32]; |
spanesar | 43:a9682b81632b | 61 | strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); |
spanesar | 43:a9682b81632b | 62 | |
spanesar | 43:a9682b81632b | 63 | if(buffer_str=="READALL") {//begin if buffer readall |
spanesar | 43:a9682b81632b | 64 | humidity.ReadTempHumi(&tempCelsius, &humi); // reads temperature and humity levels |
spanesar | 43:a9682b81632b | 65 | barometer.get(); // gets barometer readings |
spanesar | 43:a9682b81632b | 66 | // displays all the variables of time, date, temperature, humidity and all the barometric readings to the serial |
spanesar | 43:a9682b81632b | 67 | printf("%s, %s, %4.2fC , %3.1f%%, %6.1f, %4.1f\n\r", time, date, tempCelsius, humi, barometer.pressure(), barometer.temperature()); |
spanesar | 43:a9682b81632b | 68 | myled = 1; // LED is ON |
spanesar | 43:a9682b81632b | 69 | Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! |
spanesar | 43:a9682b81632b | 70 | myled = 0; // LED is OFF |
spanesar | 43:a9682b81632b | 71 | t1.signal_set(READALL_OFF); |
spanesar | 43:a9682b81632b | 72 | }//end if buffer readall |
spanesar | 41:ce3d6fb38d3b | 73 | Thread::wait(200); |
spanesar | 43:a9682b81632b | 74 | |
spanesar | 43:a9682b81632b | 75 | |
spanesar | 43:a9682b81632b | 76 | Thread::signal_wait(READALL_OFF); |
spanesar | 43:a9682b81632b | 77 | scanf("%127s", buffer); |
spanesar | 43:a9682b81632b | 78 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 43:a9682b81632b | 79 | strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); |
spanesar | 43:a9682b81632b | 80 | if(buffer_str=="SETTIME") {// begin if buffer settime |
spanesar | 43:a9682b81632b | 81 | set_time(1508268288); |
spanesar | 43:a9682b81632b | 82 | printf("time and date updated to %s %s\n\r", time, date); |
spanesar | 43:a9682b81632b | 83 | myled = 1; |
spanesar | 43:a9682b81632b | 84 | Thread::wait(200); |
spanesar | 43:a9682b81632b | 85 | myled = 0; |
spanesar | 43:a9682b81632b | 86 | t1.signal_set(SETTIME_OFF); |
spanesar | 43:a9682b81632b | 87 | }//end if buffer settime |
spanesar | 43:a9682b81632b | 88 | Thread::wait(200); |
spanesar | 43:a9682b81632b | 89 | |
spanesar | 43:a9682b81632b | 90 | |
spanesar | 43:a9682b81632b | 91 | Thread::signal_wait(SETTIME_OFF); |
spanesar | 43:a9682b81632b | 92 | scanf("%127s", buffer); |
spanesar | 43:a9682b81632b | 93 | strftime(date, 32, "%Y/%m/%d %a", localtime(&seconds)); |
spanesar | 43:a9682b81632b | 94 | strftime(time, 32, "%H:%M:%S %p", localtime(&seconds)); |
spanesar | 43:a9682b81632b | 95 | if(buffer_str=="SETT") // begin if buffer sett |
spanesar | 43:a9682b81632b | 96 | for (count = 0; count < 5; count++) { |
spanesar | 43:a9682b81632b | 97 | 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 | 43:a9682b81632b | 98 | buf = (char *)malloc(sz + 1); |
spanesar | 43:a9682b81632b | 99 | 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 | 43:a9682b81632b | 100 | printf("%s\n\r",buf); |
spanesar | 43:a9682b81632b | 101 | printf("written to memory\n\r"); |
spanesar | 43:a9682b81632b | 102 | myled = 1; |
spanesar | 43:a9682b81632b | 103 | Thread::wait(200); |
spanesar | 43:a9682b81632b | 104 | myled = 0; |
spanesar | 43:a9682b81632b | 105 | t1.signal_set(SETT_OFF); |
spanesar | 43:a9682b81632b | 106 | } //end if buffer sett |
spanesar | 43:a9682b81632b | 107 | Thread::wait(200); |
spanesar | 43:a9682b81632b | 108 | |
spanesar | 42:4bf55ab88fbc | 109 | |
spanesar | 43:a9682b81632b | 110 | |
spanesar | 43:a9682b81632b | 111 | Thread::signal_wait(SETT_OFF); |
spanesar | 43:a9682b81632b | 112 | scanf("%127s", buffer); |
spanesar | 43:a9682b81632b | 113 | if(buffer_str=="READ") { //begin if buffer read |
spanesar | 43:a9682b81632b | 114 | printf("%s\n\r", buf); |
spanesar | 43:a9682b81632b | 115 | myled = 1; |
spanesar | 43:a9682b81632b | 116 | Thread::wait(200); |
spanesar | 43:a9682b81632b | 117 | myled = 0; |
spanesar | 43:a9682b81632b | 118 | osSignalSet(idMain, ALL_OFF); |
spanesar | 43:a9682b81632b | 119 | }//end if buffer read |
spanesar | 43:a9682b81632b | 120 | Thread::wait(200); |
spanesar | 43:a9682b81632b | 121 | }//end while true |
spanesar | 43:a9682b81632b | 122 | }//end function |
spanesar | 43:a9682b81632b | 123 | |
spanesar | 43:a9682b81632b | 124 | |
spanesar | 43:a9682b81632b | 125 | int main() |
spanesar | 43:a9682b81632b | 126 | { |
spanesar | 43:a9682b81632b | 127 | pc.baud(9600); //sets baud rate to 9600 |
spanesar | 43:a9682b81632b | 128 | time_t seconds = time(NULL); |
spanesar | 43:a9682b81632b | 129 | idMain = osThreadGetId(); |
spanesar | 43:a9682b81632b | 130 | t1.start(function1); |
spanesar | 43:a9682b81632b | 131 | id1 = t1.gettid(); |
spanesar | 43:a9682b81632b | 132 | humidity.init(); |
spanesar | 43:a9682b81632b | 133 | humidity.calib(); |
spanesar | 43:a9682b81632b | 134 | printf("Type SETTIME to set time and date [enter] \n\r"); |
spanesar | 43:a9682b81632b | 135 | printf("Type SETT to start sampling [ENTER]\n\r"); |
spanesar | 43:a9682b81632b | 136 | printf("Type DELETEALL to delete sensor data [enter]\n\r"); |
spanesar | 43:a9682b81632b | 137 | printf("Type READALL [enter]\n\r"); |
spanesar | 43:a9682b81632b | 138 | printf("Type READ to read sensor data in memory [ENTER]\n\r"); |
spanesar | 43:a9682b81632b | 139 | |
spanesar | 43:a9682b81632b | 140 | |
spanesar | 43:a9682b81632b | 141 | while(1) { |
spanesar | 43:a9682b81632b | 142 | char buffer [128]; |
spanesar | 43:a9682b81632b | 143 | scanf("%127s", buffer); |
spanesar | 43:a9682b81632b | 144 | string buffer_str(buffer); |
spanesar | 43:a9682b81632b | 145 | osSignalWait(ALL_OFF,osWaitForever); |
spanesar | 43:a9682b81632b | 146 | |
spanesar | 41:ce3d6fb38d3b | 147 | } |
spanesar | 41:ce3d6fb38d3b | 148 | } |
spanesar | 41:ce3d6fb38d3b | 149 |