mDot / Mbed OS Honneywell_Dust_Simple
Revision:
11:00cb0217ce8a
Parent:
10:43337cc2ac79
Child:
12:d5f15e5bd256
--- a/Main.cpp	Wed Nov 29 06:01:32 2017 +0000
+++ b/Main.cpp	Wed Nov 29 21:06:43 2017 +0000
@@ -1,19 +1,20 @@
-/* Run Honeywell Dust Sensor in continous Sampling Mode on a mDot
-   Version 1.1
-   Steve Mylroie Roitronic August 1st 2017
+/* Run Honeywell Dust Sensor in continous Sampling Mode on a mDot or 
+   Freedom K64 board
+   Version 2.1
+   Steve Mylroie Roitronic November 29 2017
    @C Copyright Global Quality Corp
 */
 
 #include "mbed.h"
 #include "stdlib.h"
 
-#define VERSION  "2.0"
+#define VERSION  "2.1"
 
 #define TRACE_MODE
 
 //Turn on trace logging to the PC USB port
 #ifdef TRACE_MODE
-//Log level need to be set one level higher than the high level to be output
+//Log level need to be set one level higher than the highest level to be output
 #define LOG_LEVEL 2
 #define DEBUG
 #endif
@@ -92,68 +93,66 @@
 //Default UART setting are 8,N,1
 RawSerial sensor( SENSOR_XMT, SENSOR_RCV, 9600);
 
-//Received chararter interupt handler
+//Serial Received chararter interupt handler
 
 void receiveInterrupt() {
     uint8_t data;
     if(sensor.readable()) {
         data = sensor.getc();
-        if(msgType == UNKNOWN) {
+        if(msgType == UNKNOWN) {  //Start of a new message
+            bufferPtr = dataBuffer;
             switch(data) {
                 case 0x42:
                     msgType = AUTO_MSG;
                     msgLen = AUTO_MSG_LEN;
-                    bufferPtr = dataBuffer;
                     break;
                 case 0xA5:
                     msgType = ACK_MSG;
                     msgLen = RESPONSE_MSG_LEN;
-                    bufferPtr = dataBuffer;
                     break;
                 case 0x69:
                     msgType = NAK_MSG;
                     msgLen = RESPONSE_MSG_LEN;
-                    bufferPtr = dataBuffer;
                     break;
                 }
             }
-        if(msgLen--  > 0 ) {
+        if(msgLen--  > 0 ) {  //Insert Character into type ahead buffer
             *bufferPtr++ = data;
             }
         }
     return;    
     }    
 
-//synchrnous serial read and writes
+//Read last message received from type ahead message buffer
 //Mbed 5 Serial class only supports single character read and writes or
 // async buffer reads and wrutes
 MSG_TYPE readBuffer(uint8_t* buffer, uint16_t count)
 {
-    if(buffer == NULL || count > AUTO_MSG_LEN ) {
-        return READ_ERROR;
-        }
+    if(buffer == NULL || count > AUTO_MSG_LEN ) { //Calling without buffer or
+            return READ_ERROR;                    //asking for too many chars
+        }                                         //is an error
     int counter = 0;
     uint8_t* inPointer;
     uint8_t* outPointer;
     int delay;
-    logInfo( "Receiving Data from sensor\n");
-    while(msgType == UNKNOWN ) {
-        wait(CHAR_TIME);
+    logInfo( "Reading last message from sensor\n");
+    while(msgType == UNKNOWN ) { //No message received since last read
+        Thread::wait(CHAR_TIME); //Wait for new message
         counter++;
-        if(counter > 40) {
+        if(counter > 40) { //Timeout exit after 40 character 40 Charcter times
             break;
             }
         }
-    counter = 9;    
-    while(msgLen > 0 ) {
+    counter = 0;    
+    while(msgLen > 0 ) { //Wait for complete message to arrive
         delay = CHAR_TIME * msgLen;
-        wait(delay);
+        Thread::wait(delay);
         counter++;
-        if(counter > 40) {
+        if(counter > 40) { //Time out exit after 40 character times
             break;
             }
         }
-    if(counter > 40 ) {
+    if(counter > 40 ) { //Report timeout error
        msgType = UNKNOWN;
        return READ_ERROR;
          }
@@ -174,7 +173,7 @@
             
 void writeBuffer(const uint8_t* buffer, uint16_t count)
 {
-    logInfo( "Sending Data to sensor\n");
+    logInfo( "Sending Command to sensor\n");
     uint8_t* pointer = (uint8_t*)buffer;
     uint16_t counter = count;
     while(1)
@@ -240,7 +239,6 @@
 {
     uint8_t dataBuffer[MESSAGE_LEN];
     MSG_TYPE mType;
-    uint16_t response;
     uint16_t PM01Value=0;     //define PM1.0 value of the air detector module
     uint16_t PM2_5Value=0;        //define PM2.5 value of the air detector module
     uint16_t PM10Value=0;         //define PM10 value of the air detector module