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@16:6550040fbdf4, 2017-12-03 (annotated)
- Committer:
- Roietronics
- Date:
- Sun Dec 03 00:24:46 2017 +0000
- Revision:
- 16:6550040fbdf4
- Parent:
- 15:5ba877be4304
- Parent:
- 14:0e9566546fda
Merge Jacob's changes
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 | 15:5ba877be4304 | 3 | Version 2.2 |
| Roietronics | 15:5ba877be4304 | 4 | Steve Mylroie Roitronic December 2 2017 |
| Roietronics | 15:5ba877be4304 | 5 | @C Copyright 2017 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 | 15:5ba877be4304 | 11 | #define VERSION "2.2" |
| 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 | 15:5ba877be4304 | 30 | //Maximum response message 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 | 15:5ba877be4304 | 75 | //Number of character following the |
| Roietronics | 8:494f6fcecfbc | 76 | #define RESPONSE_MSG_LEN 2 |
| Roietronics | 15:5ba877be4304 | 77 | #define AUTO_MSG_LEN 32 |
| Roietronics | 8:494f6fcecfbc | 78 | |
| Roietronics | 8:494f6fcecfbc | 79 | //Time required to receive one character at 9600 baud |
| Roietronics | 15:5ba877be4304 | 80 | #define CHAR_TIME (1000 * 10)/9600 |
| Roietronics | 8:494f6fcecfbc | 81 | |
| Roietronics | 8:494f6fcecfbc | 82 | enum MSG_TYPE{ UNKNOWN, |
| Roietronics | 8:494f6fcecfbc | 83 | ACK_MSG, |
| Roietronics | 8:494f6fcecfbc | 84 | NAK_MSG, |
| Roietronics | 8:494f6fcecfbc | 85 | AUTO_MSG, |
| Roietronics | 8:494f6fcecfbc | 86 | READ_ERROR } msgType; |
| Roietronics | 8:494f6fcecfbc | 87 | |
| Roietronics | 8:494f6fcecfbc | 88 | |
| Roietronics | 8:494f6fcecfbc | 89 | uint8_t dataBuffer[AUTO_MSG_LEN]; |
| Roietronics | 8:494f6fcecfbc | 90 | uint8_t* bufferPtr; |
| Roietronics | 8:494f6fcecfbc | 91 | uint32_t msgLen; |
| Roietronics | 8:494f6fcecfbc | 92 | |
| Roietronics | 8:494f6fcecfbc | 93 | |
| Roietronics | 5:7cabc36d4352 | 94 | //Use a boards second UART to communicate witb the Honneywell sensor |
| Roietronics | 0:8326629a1b97 | 95 | //Default UART setting are 8,N,1 |
| Roietronics | 8:494f6fcecfbc | 96 | RawSerial sensor( SENSOR_XMT, SENSOR_RCV, 9600); |
| Roietronics | 8:494f6fcecfbc | 97 | |
| Roietronics | 11:00cb0217ce8a | 98 | //Serial Received chararter interupt handler |
| Roietronics | 8:494f6fcecfbc | 99 | |
| Roietronics | 8:494f6fcecfbc | 100 | void receiveInterrupt() { |
| Roietronics | 8:494f6fcecfbc | 101 | uint8_t data; |
| Roietronics | 8:494f6fcecfbc | 102 | if(sensor.readable()) { |
| Roietronics | 8:494f6fcecfbc | 103 | data = sensor.getc(); |
| Roietronics | 11:00cb0217ce8a | 104 | if(msgType == UNKNOWN) { //Start of a new message |
| Roietronics | 11:00cb0217ce8a | 105 | bufferPtr = dataBuffer; |
| Roietronics | 15:5ba877be4304 | 106 | switch(data) { //Switch on message type character |
| Roietronics | 8:494f6fcecfbc | 107 | case 0x42: |
| Roietronics | 8:494f6fcecfbc | 108 | msgType = AUTO_MSG; |
| Roietronics | 8:494f6fcecfbc | 109 | msgLen = AUTO_MSG_LEN; |
| Roietronics | 8:494f6fcecfbc | 110 | break; |
| Roietronics | 8:494f6fcecfbc | 111 | case 0xA5: |
| Roietronics | 8:494f6fcecfbc | 112 | msgType = ACK_MSG; |
| Roietronics | 8:494f6fcecfbc | 113 | msgLen = RESPONSE_MSG_LEN; |
| Roietronics | 8:494f6fcecfbc | 114 | break; |
| Roietronics | 8:494f6fcecfbc | 115 | case 0x69: |
| Roietronics | 8:494f6fcecfbc | 116 | msgType = NAK_MSG; |
| Roietronics | 8:494f6fcecfbc | 117 | msgLen = RESPONSE_MSG_LEN; |
| Roietronics | 8:494f6fcecfbc | 118 | break; |
| Roietronics | 8:494f6fcecfbc | 119 | } |
| jacobgqc | 14:0e9566546fda | 120 | } |
| Roietronics | 11:00cb0217ce8a | 121 | if(msgLen-- > 0 ) { //Insert Character into type ahead buffer |
| Roietronics | 8:494f6fcecfbc | 122 | *bufferPtr++ = data; |
| Roietronics | 8:494f6fcecfbc | 123 | } |
| jacobgqc | 14:0e9566546fda | 124 | } |
| Roietronics | 8:494f6fcecfbc | 125 | return; |
| jacobgqc | 14:0e9566546fda | 126 | } |
| Roietronics | 0:8326629a1b97 | 127 | |
| Roietronics | 11:00cb0217ce8a | 128 | //Read last message received from type ahead message buffer |
| Roietronics | 0:8326629a1b97 | 129 | //Mbed 5 Serial class only supports single character read and writes or |
| Roietronics | 10:43337cc2ac79 | 130 | // async buffer reads and wrutes |
| Roietronics | 8:494f6fcecfbc | 131 | MSG_TYPE readBuffer(uint8_t* buffer, uint16_t count) |
| Roietronics | 0:8326629a1b97 | 132 | { |
| Roietronics | 11:00cb0217ce8a | 133 | if(buffer == NULL || count > AUTO_MSG_LEN ) { //Calling without buffer or |
| jacobgqc | 14:0e9566546fda | 134 | return READ_ERROR; //asking for too many chars |
| jacobgqc | 14:0e9566546fda | 135 | } //is an error |
| Roietronics | 8:494f6fcecfbc | 136 | int counter = 0; |
| Roietronics | 8:494f6fcecfbc | 137 | uint8_t* inPointer; |
| Roietronics | 8:494f6fcecfbc | 138 | uint8_t* outPointer; |
| Roietronics | 8:494f6fcecfbc | 139 | int delay; |
| Roietronics | 11:00cb0217ce8a | 140 | logInfo( "Reading last message from sensor\n"); |
| Roietronics | 11:00cb0217ce8a | 141 | while(msgType == UNKNOWN ) { //No message received since last read |
| Roietronics | 11:00cb0217ce8a | 142 | Thread::wait(CHAR_TIME); //Wait for new message |
| Roietronics | 8:494f6fcecfbc | 143 | counter++; |
| Roietronics | 11:00cb0217ce8a | 144 | if(counter > 40) { //Timeout exit after 40 character 40 Charcter times |
| Roietronics | 8:494f6fcecfbc | 145 | break; |
| Roietronics | 0:8326629a1b97 | 146 | } |
| jacobgqc | 14:0e9566546fda | 147 | } |
| Roietronics | 11:00cb0217ce8a | 148 | counter = 0; |
| Roietronics | 11:00cb0217ce8a | 149 | while(msgLen > 0 ) { //Wait for complete message to arrive |
| Roietronics | 8:494f6fcecfbc | 150 | delay = CHAR_TIME * msgLen; |
| Roietronics | 11:00cb0217ce8a | 151 | Thread::wait(delay); |
| Roietronics | 8:494f6fcecfbc | 152 | counter++; |
| Roietronics | 11:00cb0217ce8a | 153 | if(counter > 40) { //Time out exit after 40 character times |
| jacobgqc | 14:0e9566546fda | 154 | pc.printf("msgLen: %d", msgLen); |
| Roietronics | 8:494f6fcecfbc | 155 | break; |
| Roietronics | 8:494f6fcecfbc | 156 | } |
| jacobgqc | 14:0e9566546fda | 157 | } |
| Roietronics | 11:00cb0217ce8a | 158 | if(counter > 40 ) { //Report timeout error |
| Roietronics | 8:494f6fcecfbc | 159 | msgType = UNKNOWN; |
| Roietronics | 8:494f6fcecfbc | 160 | return READ_ERROR; |
| jacobgqc | 14:0e9566546fda | 161 | } |
| Roietronics | 8:494f6fcecfbc | 162 | else { |
| Roietronics | 15:5ba877be4304 | 163 | //Copy the message minus type flag to the requesters buffer |
| Roietronics | 8:494f6fcecfbc | 164 | inPointer = &dataBuffer[1]; |
| Roietronics | 8:494f6fcecfbc | 165 | outPointer = buffer; |
| Roietronics | 8:494f6fcecfbc | 166 | for(int i = 0; i < count; i++) { |
| Roietronics | 8:494f6fcecfbc | 167 | *outPointer++ = *inPointer++; |
| Roietronics | 8:494f6fcecfbc | 168 | } |
| jacobgqc | 14:0e9566546fda | 169 | } |
| Roietronics | 8:494f6fcecfbc | 170 | MSG_TYPE temp = msgType; |
| Roietronics | 8:494f6fcecfbc | 171 | //Start Search for the next message |
| Roietronics | 8:494f6fcecfbc | 172 | msgType = UNKNOWN; |
| Roietronics | 8:494f6fcecfbc | 173 | return temp; |
| jacobgqc | 14:0e9566546fda | 174 | } |
| Roietronics | 0:8326629a1b97 | 175 | |
| Roietronics | 8:494f6fcecfbc | 176 | |
| Roietronics | 0:8326629a1b97 | 177 | void writeBuffer(const uint8_t* buffer, uint16_t count) |
| Roietronics | 0:8326629a1b97 | 178 | { |
| Roietronics | 11:00cb0217ce8a | 179 | logInfo( "Sending Command to sensor\n"); |
| Roietronics | 0:8326629a1b97 | 180 | uint8_t* pointer = (uint8_t*)buffer; |
| Roietronics | 0:8326629a1b97 | 181 | uint16_t counter = count; |
| Roietronics | 0:8326629a1b97 | 182 | while(1) |
| Roietronics | 0:8326629a1b97 | 183 | { |
| Roietronics | 0:8326629a1b97 | 184 | if(sensor.writeable()) |
| Roietronics | 0:8326629a1b97 | 185 | { |
| Roietronics | 3:1ba96949e2fd | 186 | logTrace(*pointer); |
| Roietronics | 0:8326629a1b97 | 187 | sensor.putc(*pointer++); |
| Roietronics | 0:8326629a1b97 | 188 | counter--; |
| jacobgqc | 14:0e9566546fda | 189 | } |
| Roietronics | 4:12dfd9a8ee76 | 190 | if(counter == 0) { |
| Roietronics | 0:8326629a1b97 | 191 | return; |
| Roietronics | 0:8326629a1b97 | 192 | } |
| jacobgqc | 14:0e9566546fda | 193 | } |
| jacobgqc | 14:0e9566546fda | 194 | } |
| Roietronics | 0:8326629a1b97 | 195 | |
| Roietronics | 4:12dfd9a8ee76 | 196 | //Validate the received mesurements checksum |
| Roietronics | 0:8326629a1b97 | 197 | |
| Roietronics | 0:8326629a1b97 | 198 | bool checkValue(uint8_t *thebuf, uint8_t leng) |
| Roietronics | 0:8326629a1b97 | 199 | { |
| jacobgqc | 14:0e9566546fda | 200 | bool receiveflag = false; |
| jacobgqc | 14:0e9566546fda | 201 | uint16_t receiveSum = 0; |
| jacobgqc | 14:0e9566546fda | 202 | |
| jacobgqc | 14:0e9566546fda | 203 | //Don't include the checksum bytes in the sum |
| jacobgqc | 14:0e9566546fda | 204 | for(int i=0; i<(leng-3); i++){ |
| Roietronics | 16:6550040fbdf4 | 205 | receiveSum=receiveSum+thebuf[i]; |
| jacobgqc | 14:0e9566546fda | 206 | } |
| jacobgqc | 14:0e9566546fda | 207 | receiveSum=receiveSum + 0x42; |
| jacobgqc | 14:0e9566546fda | 208 | |
| jacobgqc | 14:0e9566546fda | 209 | if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the debug data |
| jacobgqc | 14:0e9566546fda | 210 | { |
| jacobgqc | 14:0e9566546fda | 211 | receiveSum = 0; |
| jacobgqc | 14:0e9566546fda | 212 | receiveflag = true; |
| jacobgqc | 14:0e9566546fda | 213 | } |
| jacobgqc | 14:0e9566546fda | 214 | return receiveflag; |
| Roietronics | 0:8326629a1b97 | 215 | } |
| Roietronics | 0:8326629a1b97 | 216 | |
| Roietronics | 0:8326629a1b97 | 217 | //Extract the 1 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 218 | uint16_t transmitPM01(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 219 | { |
| jacobgqc | 14:0e9566546fda | 220 | uint16_t PM01Val; |
| jacobgqc | 14:0e9566546fda | 221 | PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module |
| jacobgqc | 14:0e9566546fda | 222 | return PM01Val; |
| Roietronics | 0:8326629a1b97 | 223 | } |
| Roietronics | 0:8326629a1b97 | 224 | |
| Roietronics | 0:8326629a1b97 | 225 | //Extract the 2.5 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 226 | uint16_t transmitPM2_5(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 227 | { |
| jacobgqc | 14:0e9566546fda | 228 | uint16_t PM2_5Val; |
| jacobgqc | 14:0e9566546fda | 229 | PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module |
| jacobgqc | 14:0e9566546fda | 230 | return PM2_5Val; |
| jacobgqc | 14:0e9566546fda | 231 | } |
| Roietronics | 0:8326629a1b97 | 232 | |
| Roietronics | 0:8326629a1b97 | 233 | //Extract the 10 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 234 | uint16_t transmitPM10(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 235 | { |
| jacobgqc | 14:0e9566546fda | 236 | uint16_t PM10Val; |
| jacobgqc | 14:0e9566546fda | 237 | PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module |
| jacobgqc | 14:0e9566546fda | 238 | return PM10Val; |
| Roietronics | 0:8326629a1b97 | 239 | } |
| Roietronics | 0:8326629a1b97 | 240 | |
| Roietronics | 0:8326629a1b97 | 241 | int main() |
| Roietronics | 0:8326629a1b97 | 242 | { |
| Roietronics | 5:7cabc36d4352 | 243 | uint8_t dataBuffer[MESSAGE_LEN]; |
| Roietronics | 8:494f6fcecfbc | 244 | MSG_TYPE mType; |
| Roietronics | 0:8326629a1b97 | 245 | uint16_t PM01Value=0; //define PM1.0 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 246 | uint16_t PM2_5Value=0; //define PM2.5 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 247 | uint16_t PM10Value=0; //define PM10 value of the air detector module |
| Roietronics | 4:12dfd9a8ee76 | 248 | |
| Roietronics | 7:5f612ed18852 | 249 | pc.printf("Starting Honeywell Dust Sesor App version %\n", VERSION); |
| Roietronics | 15:5ba877be4304 | 250 | |
| Roietronics | 15:5ba877be4304 | 251 | #ifdef DEBIG |
| Roietronics | 15:5ba877be4304 | 252 | pc.printf("Character wait time is %d millseconds", CHAR_TIME); |
| Roietronics | 15:5ba877be4304 | 253 | #endif |
| Roietronics | 8:494f6fcecfbc | 254 | //Attach a receive interrupt handler |
| Roietronics | 8:494f6fcecfbc | 255 | sensor.attach(receiveInterrupt, Serial::RxIrq); |
| Roietronics | 8:494f6fcecfbc | 256 | msgType = UNKNOWN; |
| Roietronics | 0:8326629a1b97 | 257 | //Send start command to the sensor |
| Roietronics | 0:8326629a1b97 | 258 | writeBuffer(measureCommand, 4); |
| Roietronics | 5:7cabc36d4352 | 259 | /* |
| Roietronics | 5:7cabc36d4352 | 260 | Not geting response from sensor - first characters received are measurement data |
| Roietronics | 0:8326629a1b97 | 261 | //Wait for sensors response |
| Roietronics | 0:8326629a1b97 | 262 | //while(!sensor.readable()); |
| Roietronics | 0:8326629a1b97 | 263 | readBuffer(dataBuffer, 2); |
| Roietronics | 0:8326629a1b97 | 264 | response = dataBuffer[0] << 8 || dataBuffer[1]; |
| Roietronics | 0:8326629a1b97 | 265 | |
| Roietronics | 0:8326629a1b97 | 266 | switch(response) { |
| Roietronics | 0:8326629a1b97 | 267 | case OK: |
| Roietronics | 0:8326629a1b97 | 268 | logInfo("Sensor Auto Measurement Started"); |
| Roietronics | 0:8326629a1b97 | 269 | break; |
| Roietronics | 0:8326629a1b97 | 270 | case BAD: |
| Roietronics | 0:8326629a1b97 | 271 | logInfo("Sensor rejected Start Measurement Commmand"); |
| Roietronics | 0:8326629a1b97 | 272 | return -1; |
| Roietronics | 0:8326629a1b97 | 273 | default: |
| Roietronics | 0:8326629a1b97 | 274 | logInfo("Communication Error: Invalid Sensor Response"); |
| Roietronics | 0:8326629a1b97 | 275 | return -2; |
| Roietronics | 0:8326629a1b97 | 276 | } |
| Roietronics | 5:7cabc36d4352 | 277 | */ |
| Roietronics | 4:12dfd9a8ee76 | 278 | |
| Roietronics | 0:8326629a1b97 | 279 | //Start continous loop |
| Roietronics | 8:494f6fcecfbc | 280 | while(1) { |
| jacobgqc | 14:0e9566546fda | 281 | if((mType = readBuffer(dataBuffer, MESSAGE_LEN -1)) == AUTO_MSG) { |
| Roietronics | 0:8326629a1b97 | 282 | if(dataBuffer[0] == 0x4d){ |
| jacobgqc | 14:0e9566546fda | 283 | if(checkValue(dataBuffer, MESSAGE_LEN-1)){ |
| jacobgqc | 14:0e9566546fda | 284 | PM01Value = transmitPM01(dataBuffer); //count PM1.0 value of the air detector module |
| jacobgqc | 14:0e9566546fda | 285 | PM2_5Value = transmitPM2_5(dataBuffer);//count PM2.5 value of the air detector module |
| jacobgqc | 14:0e9566546fda | 286 | PM10Value = transmitPM10(dataBuffer); //count PM10 value of the air detector module |
| Roietronics | 8:494f6fcecfbc | 287 | } |
| jacobgqc | 14:0e9566546fda | 288 | else { |
| jacobgqc | 14:0e9566546fda | 289 | pc.puts("Message checksum error\n"); |
| Roietronics | 8:494f6fcecfbc | 290 | } |
| jacobgqc | 14:0e9566546fda | 291 | } |
| Roietronics | 7:5f612ed18852 | 292 | else { |
| Roietronics | 15:5ba877be4304 | 293 | pc.printf("Error Second Character not 0x4d but #x\n", dataBuffer[0]); |
| Roietronics | 7:5f612ed18852 | 294 | } |
| Roietronics | 7:5f612ed18852 | 295 | //Check for exit request |
| jacobgqc | 14:0e9566546fda | 296 | if(pc.readable()) { // Returning false here |
| Roietronics | 7:5f612ed18852 | 297 | char input = pc.getc(); |
| Roietronics | 7:5f612ed18852 | 298 | if(input == 'Q' || input == 'q') |
| Roietronics | 7:5f612ed18852 | 299 | { |
| Roietronics | 8:494f6fcecfbc | 300 | //Shutdown the sensor |
| Roietronics | 7:5f612ed18852 | 301 | writeBuffer(stopCommand, 4); |
| Roietronics | 8:494f6fcecfbc | 302 | //Unlink the receive interrupt handler |
| Roietronics | 8:494f6fcecfbc | 303 | sensor.attach(0, Serial::RxIrq); |
| Roietronics | 8:494f6fcecfbc | 304 | pc.puts("Exit request received\n"); |
| Roietronics | 7:5f612ed18852 | 305 | return 0; |
| Roietronics | 7:5f612ed18852 | 306 | } |
| jacobgqc | 14:0e9566546fda | 307 | } |
| jacobgqc | 14:0e9566546fda | 308 | // Use MBed wait function instead of Arduino delay loop |
| Roietronics | 8:494f6fcecfbc | 309 | wait_ms(1000); |
| Roietronics | 8:494f6fcecfbc | 310 | |
| Roietronics | 8:494f6fcecfbc | 311 | pc.printf("PM1.0: %d ug/m3\n", PM01Value); |
| Roietronics | 8:494f6fcecfbc | 312 | |
| Roietronics | 8:494f6fcecfbc | 313 | pc.printf("PM2.5: %d ug/m3\n", PM2_5Value); |
| Roietronics | 8:494f6fcecfbc | 314 | |
| Roietronics | 8:494f6fcecfbc | 315 | pc.printf("PM10: %d ug/m3\n", PM10Value); |
| Roietronics | 8:494f6fcecfbc | 316 | |
| Roietronics | 8:494f6fcecfbc | 317 | pc.printf("\n"); |
| jacobgqc | 14:0e9566546fda | 318 | } |
| jacobgqc | 14:0e9566546fda | 319 | else { |
| jacobgqc | 14:0e9566546fda | 320 | switch(mType) { |
| jacobgqc | 14:0e9566546fda | 321 | case ACK_MSG: |
| Roietronics | 8:494f6fcecfbc | 322 | if(dataBuffer[0] == 0xA5) { |
| Roietronics | 8:494f6fcecfbc | 323 | pc.puts("Recived ACK response'\n"); |
| jacobgqc | 14:0e9566546fda | 324 | } |
| Roietronics | 8:494f6fcecfbc | 325 | else { |
| Roietronics | 8:494f6fcecfbc | 326 | pc.puts("Received corrupt ACK Response\n"); |
| jacobgqc | 14:0e9566546fda | 327 | } |
| Roietronics | 8:494f6fcecfbc | 328 | break; |
| jacobgqc | 14:0e9566546fda | 329 | case NAK_MSG: |
| Roietronics | 8:494f6fcecfbc | 330 | if(dataBuffer[0] == 0x69) { |
| Roietronics | 8:494f6fcecfbc | 331 | pc.puts("Recived NAK response'\n"); |
| jacobgqc | 14:0e9566546fda | 332 | } |
| Roietronics | 8:494f6fcecfbc | 333 | else { |
| Roietronics | 8:494f6fcecfbc | 334 | pc.puts("Received corrupt NAK Response\n"); |
| jacobgqc | 14:0e9566546fda | 335 | } |
| Roietronics | 8:494f6fcecfbc | 336 | break; |
| jacobgqc | 14:0e9566546fda | 337 | case READ_ERROR: |
| jacobgqc | 14:0e9566546fda | 338 | pc.puts("Data Reading Error\n"); |
| Roietronics | 16:6550040fbdf4 | 339 | #ifdef DEBUG |
| Roietronics | 16:6550040fbdf4 | 340 | pc.printf("\nMESSAGE_LEN: %d\n",msgLen); |
| Roietronics | 16:6550040fbdf4 | 341 | for (int index = 0; index < MESSAGE_LEN - msgLen; index++) { |
| Roietronics | 16:6550040fbdf4 | 342 | pc.putc(dataBuffer[index]); |
| Roietronics | 16:6550040fbdf4 | 343 | } |
| Roietronics | 16:6550040fbdf4 | 344 | pc.putc('\n'); |
| Roietronics | 16:6550040fbdf4 | 345 | #endif |
| Roietronics | 8:494f6fcecfbc | 346 | break; |
| Roietronics | 8:494f6fcecfbc | 347 | } |
| jacobgqc | 14:0e9566546fda | 348 | } |
| Roietronics | 8:494f6fcecfbc | 349 | } |
| Roietronics | 8:494f6fcecfbc | 350 | } |