mDot / Mbed OS Honneywell_Dust_Simple
Revision:
14:0e9566546fda
Parent:
13:e5f9b5ec30e1
Child:
16:6550040fbdf4
--- a/Main.cpp	Wed Nov 29 22:55:34 2017 +0000
+++ b/Main.cpp	Fri Dec 01 21:47:08 2017 +0000
@@ -50,10 +50,11 @@
  }
  
 void logTrace(uint8_t data) {
-     if (debugLevel > 2 ){ pc.putc( data );
-      }
-      return;
-  } 
+    if (debugLevel > 2 ){ 
+        pc.putc( data );
+    }
+    return;
+} 
 
 //Board specfic serial port pin definitions
 
@@ -114,14 +115,14 @@
                     msgType = NAK_MSG;
                     msgLen = RESPONSE_MSG_LEN;
                     break;
-                }
             }
+        }
         if(msgLen--  > 0 ) {  //Insert Character into type ahead buffer
             *bufferPtr++ = data;
-            }
         }
+    }
     return;    
-    }    
+}
 
 //Read last message received from type ahead message buffer
 //Mbed 5 Serial class only supports single character read and writes or
@@ -129,8 +130,8 @@
 MSG_TYPE readBuffer(uint8_t* buffer, uint16_t count)
 {
     if(buffer == NULL || count > AUTO_MSG_LEN ) { //Calling without buffer or
-            return READ_ERROR;                    //asking for too many chars
-        }                                         //is an error
+        return READ_ERROR;                    //asking for too many chars
+    }                                         //is an error
     int counter = 0;
     uint8_t* inPointer;
     uint8_t* outPointer;
@@ -141,34 +142,35 @@
         counter++;
         if(counter > 40) { //Timeout exit after 40 character 40 Charcter times
             break;
-            }
         }
+    }
     counter = 0;    
     while(msgLen > 0 ) { //Wait for complete message to arrive
         delay = CHAR_TIME * msgLen;
         Thread::wait(delay);
         counter++;
         if(counter > 40) { //Time out exit after 40 character times
+            pc.printf("msgLen: %d", msgLen);
             break;
-            }
         }
+    }
     if(counter > 40 ) { //Report timeout error
        msgType = UNKNOWN;
        return READ_ERROR;
-         }
+     }
     else {
         //Copy the message to the requesters buffer 
         inPointer = &dataBuffer[1];
         outPointer = buffer;
         for(int i = 0; i < count; i++) {
             *outPointer++ = *inPointer++;
-            }
         }
+    }
     MSG_TYPE temp = msgType;
     //Start Search for the next message
     msgType = UNKNOWN;     
     return temp;
-    }
+}
     
             
 void writeBuffer(const uint8_t* buffer, uint16_t count)
@@ -183,56 +185,56 @@
             logTrace(*pointer);
             sensor.putc(*pointer++);
             counter--;
-            }
+        }
         if(counter == 0) {
             return;
-            }
         }
-    } 
+    }
+} 
 
 //Validate the received mesurements checksum
 
 bool checkValue(uint8_t *thebuf, uint8_t leng)
 {  
-  bool receiveflag = false;
-  uint16_t receiveSum = 0;
-
-  //Don't include the checksum bytes in the sum
-  for(int i=0; i<(leng-3); i++){
-  receiveSum=receiveSum+thebuf[i];
-  }
-  receiveSum=receiveSum + 0x42;
- 
-  if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1]))  //check the debug data 
-  {
-    receiveSum = 0;
-    receiveflag = true;
-  }
-  return receiveflag;
+    bool receiveflag = false;
+    uint16_t receiveSum = 0;
+    
+    //Don't include the checksum bytes in the sum
+    for(int i=0; i<(leng-3); i++){
+    r   eceiveSum=receiveSum+thebuf[i];
+    }
+    receiveSum=receiveSum + 0x42;
+    
+    if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1]))  //check the debug data 
+    {
+        receiveSum = 0;
+        receiveflag = true;
+    }
+    return receiveflag;
 }
 
 //Extract the 1 micron particle count from the messaage
 uint16_t transmitPM01(uint8_t *thebuf)
 {
-  uint16_t PM01Val;
-  PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
-  return PM01Val;
+    uint16_t PM01Val;
+    PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
+    return PM01Val;
 }
 
 //Extract the 2.5 micron particle count from the messaage
 uint16_t transmitPM2_5(uint8_t *thebuf)
 {
-  uint16_t PM2_5Val;
-  PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
-  return PM2_5Val;
-  }
+    uint16_t PM2_5Val;
+    PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
+    return PM2_5Val;
+}
 
 //Extract the 10 micron particle count from the messaage
 uint16_t transmitPM10(uint8_t *thebuf)
 {
-  uint16_t PM10Val;
-  PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module  
-  return PM10Val;
+    uint16_t PM10Val;
+    PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module  
+    return PM10Val;
 }
 
 int main()
@@ -271,22 +273,22 @@
 
     //Start continous loop 
     while(1) {
-        if((mType = readBuffer(dataBuffer,MESSAGE_LEN -1)) == AUTO_MSG) {
+        if((mType = readBuffer(dataBuffer, MESSAGE_LEN -1)) == AUTO_MSG) {
             if(dataBuffer[0] == 0x4d){
-              if(checkValue(dataBuffer, MESSAGE_LEN-1)){
-                PM01Value = transmitPM01(dataBuffer); //count PM1.0 value of the air detector module
-                PM2_5Value = transmitPM2_5(dataBuffer);//count PM2.5 value of the air detector module
-                PM10Value = transmitPM10(dataBuffer); //count PM10 value of the air detector module 
+                if(checkValue(dataBuffer, MESSAGE_LEN-1)){
+                    PM01Value = transmitPM01(dataBuffer); //count PM1.0 value of the air detector module
+                    PM2_5Value = transmitPM2_5(dataBuffer);//count PM2.5 value of the air detector module
+                    PM10Value = transmitPM10(dataBuffer); //count PM10 value of the air detector module 
                 }
-              else {
-                pc.puts("Message checksum error\n");
+                else {
+                    pc.puts("Message checksum error\n");
                 }
-              }
+            }
             else {
-              pc.printf("Second Character was %x\n", dataBuffer[0]);
-              }
+                pc.printf("Second Character was %x\n", dataBuffer[0]);
+            }
              //Check for exit request
-            if(pc.readable()) {
+            if(pc.readable()) { // Returning false here
                 char input = pc.getc();
                 if(input == 'Q' || input == 'q')
                 { 
@@ -296,9 +298,9 @@
                     sensor.attach(0, Serial::RxIrq);
                     pc.puts("Exit request received\n");
                     return 0;
-                    }
                 }
-              // Use MBed wait function instead of Arduino delay loop
+            }
+            // Use MBed wait function instead of Arduino delay loop
             wait_ms(1000);          
                   
             pc.printf("PM1.0: %d ug/m3\n", PM01Value);  
@@ -308,29 +310,32 @@
             pc.printf("PM10: %d ug/m3\n", PM10Value);  
         
             pc.printf("\n");
-            }
-            else {
-                switch(mType) {
-                    case ACK_MSG:
+        }
+        else {
+            switch(mType) {
+                case ACK_MSG:
                     if(dataBuffer[0] == 0xA5) {
                         pc.puts("Recived ACK response'\n");
-                        }
+                    }
                     else {
                         pc.puts("Received corrupt ACK Response\n");
-                        }
+                    }
                     break;
-                    case NAK_MSG:
+                case NAK_MSG:
                     if(dataBuffer[0] == 0x69) {
                         pc.puts("Recived NAK response'\n");
-                        }
+                    }
                     else {
                         pc.puts("Received corrupt NAK Response\n");
-                        }
+                    }
                     break;
-                    case READ_ERROR:
-                        pc.puts("Data Reading Error");
+                case READ_ERROR:
+//                    pc.printf("%s", dataBuffer);
+                    pc.printf("%d", msgLen);
+                    pc.printf("\nMESSAGE_LEN: %d    ", MESSAGE_LEN);
+                    pc.puts("Data Reading Error\n");
                     break;
-                    }
                 }
+            }
         }
     }
\ No newline at end of file