Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Main.cpp@14:0e9566546fda, 2017-12-01 (annotated)
- Committer:
- jacobgqc
- Date:
- Fri Dec 01 21:47:08 2017 +0000
- Revision:
- 14:0e9566546fda
- Parent:
- 13:e5f9b5ec30e1
- Child:
- 16:6550040fbdf4
Some new debug messages added, indentation cleaned up.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Roietronics | 11:00cb0217ce8a | 1 | /* Run Honeywell Dust Sensor in continous Sampling Mode on a mDot or |
| Roietronics | 11:00cb0217ce8a | 2 | Freedom K64 board |
| Roietronics | 11:00cb0217ce8a | 3 | Version 2.1 |
| Roietronics | 11:00cb0217ce8a | 4 | Steve Mylroie Roitronic November 29 2017 |
| Roietronics | 0:8326629a1b97 | 5 | @C Copyright Global Quality Corp |
| Roietronics | 0:8326629a1b97 | 6 | */ |
| Roietronics | 0:8326629a1b97 | 7 | |
| Roietronics | 0:8326629a1b97 | 8 | #include "mbed.h" |
| Roietronics | 0:8326629a1b97 | 9 | #include "stdlib.h" |
| Roietronics | 0:8326629a1b97 | 10 | |
| Roietronics | 11:00cb0217ce8a | 11 | #define VERSION "2.1" |
| Roietronics | 4:12dfd9a8ee76 | 12 | |
| Roietronics | 0:8326629a1b97 | 13 | #define TRACE_MODE |
| Roietronics | 0:8326629a1b97 | 14 | |
| Roietronics | 0:8326629a1b97 | 15 | //Turn on trace logging to the PC USB port |
| Roietronics | 0:8326629a1b97 | 16 | #ifdef TRACE_MODE |
| Roietronics | 11:00cb0217ce8a | 17 | //Log level need to be set one level higher than the highest level to be output |
| Roietronics | 5:7cabc36d4352 | 18 | #define LOG_LEVEL 2 |
| Roietronics | 0:8326629a1b97 | 19 | #define DEBUG |
| Roietronics | 0:8326629a1b97 | 20 | #endif |
| Roietronics | 0:8326629a1b97 | 21 | |
| Roietronics | 3:1ba96949e2fd | 22 | |
| Roietronics | 0:8326629a1b97 | 23 | const uint8_t* measureCommand = "68014057"; |
| Roietronics | 0:8326629a1b97 | 24 | const uint8_t* stopCommand = "68012077"; |
| Roietronics | 0:8326629a1b97 | 25 | |
| Roietronics | 5:7cabc36d4352 | 26 | //Sensor command response codes |
| Roietronics | 0:8326629a1b97 | 27 | #define OK 0xA5A5 |
| Roietronics | 0:8326629a1b97 | 28 | #define BAD 0x9696 |
| Roietronics | 0:8326629a1b97 | 29 | |
| Roietronics | 5:7cabc36d4352 | 30 | //Auto Mode measurement response lenght |
| Roietronics | 0:8326629a1b97 | 31 | #define MESSAGE_LEN 32 |
| Roietronics | 5:7cabc36d4352 | 32 | |
| Roietronics | 0:8326629a1b97 | 33 | //sensor measurement cycle in millseconds |
| Roietronics | 0:8326629a1b97 | 34 | #define MEASURE_DELAY 1500 |
| Roietronics | 0:8326629a1b97 | 35 | |
| Roietronics | 3:1ba96949e2fd | 36 | Serial pc(USBTX, USBRX, 115200); |
| Roietronics | 3:1ba96949e2fd | 37 | |
| Roietronics | 0:8326629a1b97 | 38 | //Use USB debug pott for the debug stream |
| Roietronics | 0:8326629a1b97 | 39 | #ifdef DEBUG |
| Roietronics | 3:1ba96949e2fd | 40 | uint32_t debugLevel = LOG_LEVEL; |
| Roietronics | 3:1ba96949e2fd | 41 | #else |
| Roietronics | 3:1ba96949e2fd | 42 | uint32_t debugLevel = 0; |
| Roietronics | 0:8326629a1b97 | 43 | #endif |
| Roietronics | 0:8326629a1b97 | 44 | |
| Roietronics | 3:1ba96949e2fd | 45 | void logInfo(char* text) { |
| Roietronics | 3:1ba96949e2fd | 46 | if ( debugLevel > 0 ) { |
| Roietronics | 3:1ba96949e2fd | 47 | pc.printf("\n%s\n", text ); |
| Roietronics | 3:1ba96949e2fd | 48 | } |
| Roietronics | 3:1ba96949e2fd | 49 | return; |
| Roietronics | 5:7cabc36d4352 | 50 | } |
| Roietronics | 3:1ba96949e2fd | 51 | |
| Roietronics | 3:1ba96949e2fd | 52 | void logTrace(uint8_t data) { |
| jacobgqc | 14:0e9566546fda | 53 | if (debugLevel > 2 ){ |
| jacobgqc | 14:0e9566546fda | 54 | pc.putc( data ); |
| jacobgqc | 14:0e9566546fda | 55 | } |
| jacobgqc | 14:0e9566546fda | 56 | return; |
| jacobgqc | 14:0e9566546fda | 57 | } |
| Roietronics | 3:1ba96949e2fd | 58 | |
| Roietronics | 5:7cabc36d4352 | 59 | //Board specfic serial port pin definitions |
| Roietronics | 3:1ba96949e2fd | 60 | |
| Roietronics | 5:7cabc36d4352 | 61 | #ifdef TARGET_Freescale //Freedom Board |
| Roietronics | 5:7cabc36d4352 | 62 | |
| Roietronics | 5:7cabc36d4352 | 63 | #define SENSOR_XMT D1 |
| Roietronics | 5:7cabc36d4352 | 64 | #define SENSOR_RCV D0 |
| Roietronics | 5:7cabc36d4352 | 65 | |
| Roietronics | 5:7cabc36d4352 | 66 | #endif |
| Roietronics | 5:7cabc36d4352 | 67 | |
| Roietronics | 5:7cabc36d4352 | 68 | #ifdef TARGET_MTS_MDOT_F411RE //Multi Tech mDot |
| Roietronics | 5:7cabc36d4352 | 69 | |
| Roietronics | 5:7cabc36d4352 | 70 | #define SENSOR_XMT PA_2 |
| Roietronics | 5:7cabc36d4352 | 71 | #define SENSOR_RCV PA_3 |
| Roietronics | 5:7cabc36d4352 | 72 | |
| Roietronics | 5:7cabc36d4352 | 73 | #endif |
| Roietronics | 5:7cabc36d4352 | 74 | |
| Roietronics | 8:494f6fcecfbc | 75 | #define RESPONSE_MSG_LEN 2 |
| Roietronics | 13:e5f9b5ec30e1 | 76 | #define AUTO_MSG_LEN 8 |
| Roietronics | 8:494f6fcecfbc | 77 | |
| Roietronics | 8:494f6fcecfbc | 78 | //Time required to receive one character at 9600 baud |
| Roietronics | 8:494f6fcecfbc | 79 | #define CHAR_TIME 1000/9600 & 10 |
| Roietronics | 8:494f6fcecfbc | 80 | |
| Roietronics | 8:494f6fcecfbc | 81 | enum MSG_TYPE{ UNKNOWN, |
| Roietronics | 8:494f6fcecfbc | 82 | ACK_MSG, |
| Roietronics | 8:494f6fcecfbc | 83 | NAK_MSG, |
| Roietronics | 8:494f6fcecfbc | 84 | AUTO_MSG, |
| Roietronics | 8:494f6fcecfbc | 85 | READ_ERROR } msgType; |
| Roietronics | 8:494f6fcecfbc | 86 | |
| Roietronics | 8:494f6fcecfbc | 87 | |
| Roietronics | 8:494f6fcecfbc | 88 | uint8_t dataBuffer[AUTO_MSG_LEN]; |
| Roietronics | 8:494f6fcecfbc | 89 | uint8_t* bufferPtr; |
| Roietronics | 8:494f6fcecfbc | 90 | uint32_t msgLen; |
| Roietronics | 8:494f6fcecfbc | 91 | |
| Roietronics | 8:494f6fcecfbc | 92 | |
| Roietronics | 5:7cabc36d4352 | 93 | //Use a boards second UART to communicate witb the Honneywell sensor |
| Roietronics | 0:8326629a1b97 | 94 | //Default UART setting are 8,N,1 |
| Roietronics | 8:494f6fcecfbc | 95 | RawSerial sensor( SENSOR_XMT, SENSOR_RCV, 9600); |
| Roietronics | 8:494f6fcecfbc | 96 | |
| Roietronics | 11:00cb0217ce8a | 97 | //Serial Received chararter interupt handler |
| Roietronics | 8:494f6fcecfbc | 98 | |
| Roietronics | 8:494f6fcecfbc | 99 | void receiveInterrupt() { |
| Roietronics | 8:494f6fcecfbc | 100 | uint8_t data; |
| Roietronics | 8:494f6fcecfbc | 101 | if(sensor.readable()) { |
| Roietronics | 8:494f6fcecfbc | 102 | data = sensor.getc(); |
| Roietronics | 11:00cb0217ce8a | 103 | if(msgType == UNKNOWN) { //Start of a new message |
| Roietronics | 11:00cb0217ce8a | 104 | bufferPtr = dataBuffer; |
| Roietronics | 8:494f6fcecfbc | 105 | switch(data) { |
| Roietronics | 8:494f6fcecfbc | 106 | case 0x42: |
| Roietronics | 8:494f6fcecfbc | 107 | msgType = AUTO_MSG; |
| Roietronics | 8:494f6fcecfbc | 108 | msgLen = AUTO_MSG_LEN; |
| Roietronics | 8:494f6fcecfbc | 109 | break; |
| Roietronics | 8:494f6fcecfbc | 110 | case 0xA5: |
| Roietronics | 8:494f6fcecfbc | 111 | msgType = ACK_MSG; |
| Roietronics | 8:494f6fcecfbc | 112 | msgLen = RESPONSE_MSG_LEN; |
| Roietronics | 8:494f6fcecfbc | 113 | break; |
| Roietronics | 8:494f6fcecfbc | 114 | case 0x69: |
| Roietronics | 8:494f6fcecfbc | 115 | msgType = NAK_MSG; |
| Roietronics | 8:494f6fcecfbc | 116 | msgLen = RESPONSE_MSG_LEN; |
| Roietronics | 8:494f6fcecfbc | 117 | break; |
| Roietronics | 8:494f6fcecfbc | 118 | } |
| jacobgqc | 14:0e9566546fda | 119 | } |
| Roietronics | 11:00cb0217ce8a | 120 | if(msgLen-- > 0 ) { //Insert Character into type ahead buffer |
| Roietronics | 8:494f6fcecfbc | 121 | *bufferPtr++ = data; |
| Roietronics | 8:494f6fcecfbc | 122 | } |
| jacobgqc | 14:0e9566546fda | 123 | } |
| Roietronics | 8:494f6fcecfbc | 124 | return; |
| jacobgqc | 14:0e9566546fda | 125 | } |
| Roietronics | 0:8326629a1b97 | 126 | |
| Roietronics | 11:00cb0217ce8a | 127 | //Read last message received from type ahead message buffer |
| Roietronics | 0:8326629a1b97 | 128 | //Mbed 5 Serial class only supports single character read and writes or |
| Roietronics | 10:43337cc2ac79 | 129 | // async buffer reads and wrutes |
| Roietronics | 8:494f6fcecfbc | 130 | MSG_TYPE readBuffer(uint8_t* buffer, uint16_t count) |
| Roietronics | 0:8326629a1b97 | 131 | { |
| Roietronics | 11:00cb0217ce8a | 132 | if(buffer == NULL || count > AUTO_MSG_LEN ) { //Calling without buffer or |
| jacobgqc | 14:0e9566546fda | 133 | return READ_ERROR; //asking for too many chars |
| jacobgqc | 14:0e9566546fda | 134 | } //is an error |
| Roietronics | 8:494f6fcecfbc | 135 | int counter = 0; |
| Roietronics | 8:494f6fcecfbc | 136 | uint8_t* inPointer; |
| Roietronics | 8:494f6fcecfbc | 137 | uint8_t* outPointer; |
| Roietronics | 8:494f6fcecfbc | 138 | int delay; |
| Roietronics | 11:00cb0217ce8a | 139 | logInfo( "Reading last message from sensor\n"); |
| Roietronics | 11:00cb0217ce8a | 140 | while(msgType == UNKNOWN ) { //No message received since last read |
| Roietronics | 11:00cb0217ce8a | 141 | Thread::wait(CHAR_TIME); //Wait for new message |
| Roietronics | 8:494f6fcecfbc | 142 | counter++; |
| Roietronics | 11:00cb0217ce8a | 143 | if(counter > 40) { //Timeout exit after 40 character 40 Charcter times |
| Roietronics | 8:494f6fcecfbc | 144 | break; |
| Roietronics | 0:8326629a1b97 | 145 | } |
| jacobgqc | 14:0e9566546fda | 146 | } |
| Roietronics | 11:00cb0217ce8a | 147 | counter = 0; |
| Roietronics | 11:00cb0217ce8a | 148 | while(msgLen > 0 ) { //Wait for complete message to arrive |
| Roietronics | 8:494f6fcecfbc | 149 | delay = CHAR_TIME * msgLen; |
| Roietronics | 11:00cb0217ce8a | 150 | Thread::wait(delay); |
| Roietronics | 8:494f6fcecfbc | 151 | counter++; |
| Roietronics | 11:00cb0217ce8a | 152 | if(counter > 40) { //Time out exit after 40 character times |
| jacobgqc | 14:0e9566546fda | 153 | pc.printf("msgLen: %d", msgLen); |
| Roietronics | 8:494f6fcecfbc | 154 | break; |
| Roietronics | 8:494f6fcecfbc | 155 | } |
| jacobgqc | 14:0e9566546fda | 156 | } |
| Roietronics | 11:00cb0217ce8a | 157 | if(counter > 40 ) { //Report timeout error |
| Roietronics | 8:494f6fcecfbc | 158 | msgType = UNKNOWN; |
| Roietronics | 8:494f6fcecfbc | 159 | return READ_ERROR; |
| jacobgqc | 14:0e9566546fda | 160 | } |
| Roietronics | 8:494f6fcecfbc | 161 | else { |
| Roietronics | 8:494f6fcecfbc | 162 | //Copy the message to the requesters buffer |
| Roietronics | 8:494f6fcecfbc | 163 | inPointer = &dataBuffer[1]; |
| Roietronics | 8:494f6fcecfbc | 164 | outPointer = buffer; |
| Roietronics | 8:494f6fcecfbc | 165 | for(int i = 0; i < count; i++) { |
| Roietronics | 8:494f6fcecfbc | 166 | *outPointer++ = *inPointer++; |
| Roietronics | 8:494f6fcecfbc | 167 | } |
| jacobgqc | 14:0e9566546fda | 168 | } |
| Roietronics | 8:494f6fcecfbc | 169 | MSG_TYPE temp = msgType; |
| Roietronics | 8:494f6fcecfbc | 170 | //Start Search for the next message |
| Roietronics | 8:494f6fcecfbc | 171 | msgType = UNKNOWN; |
| Roietronics | 8:494f6fcecfbc | 172 | return temp; |
| jacobgqc | 14:0e9566546fda | 173 | } |
| Roietronics | 0:8326629a1b97 | 174 | |
| Roietronics | 8:494f6fcecfbc | 175 | |
| Roietronics | 0:8326629a1b97 | 176 | void writeBuffer(const uint8_t* buffer, uint16_t count) |
| Roietronics | 0:8326629a1b97 | 177 | { |
| Roietronics | 11:00cb0217ce8a | 178 | logInfo( "Sending Command to sensor\n"); |
| Roietronics | 0:8326629a1b97 | 179 | uint8_t* pointer = (uint8_t*)buffer; |
| Roietronics | 0:8326629a1b97 | 180 | uint16_t counter = count; |
| Roietronics | 0:8326629a1b97 | 181 | while(1) |
| Roietronics | 0:8326629a1b97 | 182 | { |
| Roietronics | 0:8326629a1b97 | 183 | if(sensor.writeable()) |
| Roietronics | 0:8326629a1b97 | 184 | { |
| Roietronics | 3:1ba96949e2fd | 185 | logTrace(*pointer); |
| Roietronics | 0:8326629a1b97 | 186 | sensor.putc(*pointer++); |
| Roietronics | 0:8326629a1b97 | 187 | counter--; |
| jacobgqc | 14:0e9566546fda | 188 | } |
| Roietronics | 4:12dfd9a8ee76 | 189 | if(counter == 0) { |
| Roietronics | 0:8326629a1b97 | 190 | return; |
| Roietronics | 0:8326629a1b97 | 191 | } |
| jacobgqc | 14:0e9566546fda | 192 | } |
| jacobgqc | 14:0e9566546fda | 193 | } |
| Roietronics | 0:8326629a1b97 | 194 | |
| Roietronics | 4:12dfd9a8ee76 | 195 | //Validate the received mesurements checksum |
| Roietronics | 0:8326629a1b97 | 196 | |
| Roietronics | 0:8326629a1b97 | 197 | bool checkValue(uint8_t *thebuf, uint8_t leng) |
| Roietronics | 0:8326629a1b97 | 198 | { |
| jacobgqc | 14:0e9566546fda | 199 | bool receiveflag = false; |
| jacobgqc | 14:0e9566546fda | 200 | uint16_t receiveSum = 0; |
| jacobgqc | 14:0e9566546fda | 201 | |
| jacobgqc | 14:0e9566546fda | 202 | //Don't include the checksum bytes in the sum |
| jacobgqc | 14:0e9566546fda | 203 | for(int i=0; i<(leng-3); i++){ |
| jacobgqc | 14:0e9566546fda | 204 | r eceiveSum=receiveSum+thebuf[i]; |
| jacobgqc | 14:0e9566546fda | 205 | } |
| jacobgqc | 14:0e9566546fda | 206 | receiveSum=receiveSum + 0x42; |
| jacobgqc | 14:0e9566546fda | 207 | |
| jacobgqc | 14:0e9566546fda | 208 | if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the debug data |
| jacobgqc | 14:0e9566546fda | 209 | { |
| jacobgqc | 14:0e9566546fda | 210 | receiveSum = 0; |
| jacobgqc | 14:0e9566546fda | 211 | receiveflag = true; |
| jacobgqc | 14:0e9566546fda | 212 | } |
| jacobgqc | 14:0e9566546fda | 213 | return receiveflag; |
| Roietronics | 0:8326629a1b97 | 214 | } |
| Roietronics | 0:8326629a1b97 | 215 | |
| Roietronics | 0:8326629a1b97 | 216 | //Extract the 1 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 217 | uint16_t transmitPM01(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 218 | { |
| jacobgqc | 14:0e9566546fda | 219 | uint16_t PM01Val; |
| jacobgqc | 14:0e9566546fda | 220 | PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module |
| jacobgqc | 14:0e9566546fda | 221 | return PM01Val; |
| Roietronics | 0:8326629a1b97 | 222 | } |
| Roietronics | 0:8326629a1b97 | 223 | |
| Roietronics | 0:8326629a1b97 | 224 | //Extract the 2.5 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 225 | uint16_t transmitPM2_5(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 226 | { |
| jacobgqc | 14:0e9566546fda | 227 | uint16_t PM2_5Val; |
| jacobgqc | 14:0e9566546fda | 228 | PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module |
| jacobgqc | 14:0e9566546fda | 229 | return PM2_5Val; |
| jacobgqc | 14:0e9566546fda | 230 | } |
| Roietronics | 0:8326629a1b97 | 231 | |
| Roietronics | 0:8326629a1b97 | 232 | //Extract the 10 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 233 | uint16_t transmitPM10(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 234 | { |
| jacobgqc | 14:0e9566546fda | 235 | uint16_t PM10Val; |
| jacobgqc | 14:0e9566546fda | 236 | PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module |
| jacobgqc | 14:0e9566546fda | 237 | return PM10Val; |
| Roietronics | 0:8326629a1b97 | 238 | } |
| Roietronics | 0:8326629a1b97 | 239 | |
| Roietronics | 0:8326629a1b97 | 240 | int main() |
| Roietronics | 0:8326629a1b97 | 241 | { |
| Roietronics | 5:7cabc36d4352 | 242 | uint8_t dataBuffer[MESSAGE_LEN]; |
| Roietronics | 8:494f6fcecfbc | 243 | MSG_TYPE mType; |
| Roietronics | 0:8326629a1b97 | 244 | uint16_t PM01Value=0; //define PM1.0 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 245 | uint16_t PM2_5Value=0; //define PM2.5 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 246 | uint16_t PM10Value=0; //define PM10 value of the air detector module |
| Roietronics | 4:12dfd9a8ee76 | 247 | |
| Roietronics | 7:5f612ed18852 | 248 | pc.printf("Starting Honeywell Dust Sesor App version %\n", VERSION); |
| Roietronics | 8:494f6fcecfbc | 249 | //Attach a receive interrupt handler |
| Roietronics | 8:494f6fcecfbc | 250 | sensor.attach(receiveInterrupt, Serial::RxIrq); |
| Roietronics | 8:494f6fcecfbc | 251 | msgType = UNKNOWN; |
| Roietronics | 0:8326629a1b97 | 252 | //Send start command to the sensor |
| Roietronics | 0:8326629a1b97 | 253 | writeBuffer(measureCommand, 4); |
| Roietronics | 5:7cabc36d4352 | 254 | /* |
| Roietronics | 5:7cabc36d4352 | 255 | Not geting response from sensor - first characters received are measurement data |
| Roietronics | 0:8326629a1b97 | 256 | //Wait for sensors response |
| Roietronics | 0:8326629a1b97 | 257 | //while(!sensor.readable()); |
| Roietronics | 0:8326629a1b97 | 258 | readBuffer(dataBuffer, 2); |
| Roietronics | 0:8326629a1b97 | 259 | response = dataBuffer[0] << 8 || dataBuffer[1]; |
| Roietronics | 0:8326629a1b97 | 260 | |
| Roietronics | 0:8326629a1b97 | 261 | switch(response) { |
| Roietronics | 0:8326629a1b97 | 262 | case OK: |
| Roietronics | 0:8326629a1b97 | 263 | logInfo("Sensor Auto Measurement Started"); |
| Roietronics | 0:8326629a1b97 | 264 | break; |
| Roietronics | 0:8326629a1b97 | 265 | case BAD: |
| Roietronics | 0:8326629a1b97 | 266 | logInfo("Sensor rejected Start Measurement Commmand"); |
| Roietronics | 0:8326629a1b97 | 267 | return -1; |
| Roietronics | 0:8326629a1b97 | 268 | default: |
| Roietronics | 0:8326629a1b97 | 269 | logInfo("Communication Error: Invalid Sensor Response"); |
| Roietronics | 0:8326629a1b97 | 270 | return -2; |
| Roietronics | 0:8326629a1b97 | 271 | } |
| Roietronics | 5:7cabc36d4352 | 272 | */ |
| Roietronics | 4:12dfd9a8ee76 | 273 | |
| Roietronics | 0:8326629a1b97 | 274 | //Start continous loop |
| Roietronics | 8:494f6fcecfbc | 275 | while(1) { |
| jacobgqc | 14:0e9566546fda | 276 | if((mType = readBuffer(dataBuffer, MESSAGE_LEN -1)) == AUTO_MSG) { |
| Roietronics | 0:8326629a1b97 | 277 | if(dataBuffer[0] == 0x4d){ |
| jacobgqc | 14:0e9566546fda | 278 | if(checkValue(dataBuffer, MESSAGE_LEN-1)){ |
| jacobgqc | 14:0e9566546fda | 279 | PM01Value = transmitPM01(dataBuffer); //count PM1.0 value of the air detector module |
| jacobgqc | 14:0e9566546fda | 280 | PM2_5Value = transmitPM2_5(dataBuffer);//count PM2.5 value of the air detector module |
| jacobgqc | 14:0e9566546fda | 281 | PM10Value = transmitPM10(dataBuffer); //count PM10 value of the air detector module |
| Roietronics | 8:494f6fcecfbc | 282 | } |
| jacobgqc | 14:0e9566546fda | 283 | else { |
| jacobgqc | 14:0e9566546fda | 284 | pc.puts("Message checksum error\n"); |
| Roietronics | 8:494f6fcecfbc | 285 | } |
| jacobgqc | 14:0e9566546fda | 286 | } |
| Roietronics | 7:5f612ed18852 | 287 | else { |
| jacobgqc | 14:0e9566546fda | 288 | pc.printf("Second Character was %x\n", dataBuffer[0]); |
| jacobgqc | 14:0e9566546fda | 289 | } |
| Roietronics | 7:5f612ed18852 | 290 | //Check for exit request |
| jacobgqc | 14:0e9566546fda | 291 | if(pc.readable()) { // Returning false here |
| Roietronics | 7:5f612ed18852 | 292 | char input = pc.getc(); |
| Roietronics | 7:5f612ed18852 | 293 | if(input == 'Q' || input == 'q') |
| Roietronics | 7:5f612ed18852 | 294 | { |
| Roietronics | 8:494f6fcecfbc | 295 | //Shutdown the sensor |
| Roietronics | 7:5f612ed18852 | 296 | writeBuffer(stopCommand, 4); |
| Roietronics | 8:494f6fcecfbc | 297 | //Unlink the receive interrupt handler |
| Roietronics | 8:494f6fcecfbc | 298 | sensor.attach(0, Serial::RxIrq); |
| Roietronics | 8:494f6fcecfbc | 299 | pc.puts("Exit request received\n"); |
| Roietronics | 7:5f612ed18852 | 300 | return 0; |
| Roietronics | 7:5f612ed18852 | 301 | } |
| jacobgqc | 14:0e9566546fda | 302 | } |
| jacobgqc | 14:0e9566546fda | 303 | // Use MBed wait function instead of Arduino delay loop |
| Roietronics | 8:494f6fcecfbc | 304 | wait_ms(1000); |
| Roietronics | 8:494f6fcecfbc | 305 | |
| Roietronics | 8:494f6fcecfbc | 306 | pc.printf("PM1.0: %d ug/m3\n", PM01Value); |
| Roietronics | 8:494f6fcecfbc | 307 | |
| Roietronics | 8:494f6fcecfbc | 308 | pc.printf("PM2.5: %d ug/m3\n", PM2_5Value); |
| Roietronics | 8:494f6fcecfbc | 309 | |
| Roietronics | 8:494f6fcecfbc | 310 | pc.printf("PM10: %d ug/m3\n", PM10Value); |
| Roietronics | 8:494f6fcecfbc | 311 | |
| Roietronics | 8:494f6fcecfbc | 312 | pc.printf("\n"); |
| jacobgqc | 14:0e9566546fda | 313 | } |
| jacobgqc | 14:0e9566546fda | 314 | else { |
| jacobgqc | 14:0e9566546fda | 315 | switch(mType) { |
| jacobgqc | 14:0e9566546fda | 316 | case ACK_MSG: |
| Roietronics | 8:494f6fcecfbc | 317 | if(dataBuffer[0] == 0xA5) { |
| Roietronics | 8:494f6fcecfbc | 318 | pc.puts("Recived ACK response'\n"); |
| jacobgqc | 14:0e9566546fda | 319 | } |
| Roietronics | 8:494f6fcecfbc | 320 | else { |
| Roietronics | 8:494f6fcecfbc | 321 | pc.puts("Received corrupt ACK Response\n"); |
| jacobgqc | 14:0e9566546fda | 322 | } |
| Roietronics | 8:494f6fcecfbc | 323 | break; |
| jacobgqc | 14:0e9566546fda | 324 | case NAK_MSG: |
| Roietronics | 8:494f6fcecfbc | 325 | if(dataBuffer[0] == 0x69) { |
| Roietronics | 8:494f6fcecfbc | 326 | pc.puts("Recived NAK response'\n"); |
| jacobgqc | 14:0e9566546fda | 327 | } |
| Roietronics | 8:494f6fcecfbc | 328 | else { |
| Roietronics | 8:494f6fcecfbc | 329 | pc.puts("Received corrupt NAK Response\n"); |
| jacobgqc | 14:0e9566546fda | 330 | } |
| Roietronics | 8:494f6fcecfbc | 331 | break; |
| jacobgqc | 14:0e9566546fda | 332 | case READ_ERROR: |
| jacobgqc | 14:0e9566546fda | 333 | // pc.printf("%s", dataBuffer); |
| jacobgqc | 14:0e9566546fda | 334 | pc.printf("%d", msgLen); |
| jacobgqc | 14:0e9566546fda | 335 | pc.printf("\nMESSAGE_LEN: %d ", MESSAGE_LEN); |
| jacobgqc | 14:0e9566546fda | 336 | pc.puts("Data Reading Error\n"); |
| Roietronics | 8:494f6fcecfbc | 337 | break; |
| Roietronics | 8:494f6fcecfbc | 338 | } |
| jacobgqc | 14:0e9566546fda | 339 | } |
| Roietronics | 8:494f6fcecfbc | 340 | } |
| Roietronics | 8:494f6fcecfbc | 341 | } |