mDot / Mbed OS Honneywell_Dust_Simple
Revision:
5:7cabc36d4352
Parent:
4:12dfd9a8ee76
Child:
6:6aa9d16e27bc
--- a/Main.cpp	Mon Aug 07 23:36:40 2017 +0000
+++ b/Main.cpp	Wed Aug 09 07:29:26 2017 +0000
@@ -9,23 +9,12 @@
 
 #define VERSION  "1.2"
 
-
-#ifdef MDOT
-#define
-#define
-#define
-#define
-#endif
-
-#ifdef FREEDOM
-#endif
-
 #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
-#define LOG_LEVEL 4
+#define LOG_LEVEL 2
 #define DEBUG
 #endif
 
@@ -33,10 +22,13 @@
 const uint8_t* measureCommand = "68014057";
 const uint8_t* stopCommand    = "68012077";
 
+//Sensor command response codes
 #define OK   0xA5A5
 #define BAD  0x9696
 
+//Auto Mode measurement response lenght
 #define MESSAGE_LEN 32
+
 //sensor measurement cycle in millseconds
 #define MEASURE_DELAY 1500 
 
@@ -54,7 +46,7 @@
           pc.printf("\n%s\n", text );
      }
      return;
-     }
+ }
  
 void logTrace(uint8_t data) {
      if (debugLevel > 2 ){ pc.putc( data );
@@ -62,10 +54,25 @@
       return;
   } 
 
+//Board specfic serial port pin definitions
 
-//Use second UART   to communicate witb the Honneywell sensor
+#ifdef TARGET_Freescale  //Freedom Board
+
+#define SENSOR_XMT D1
+#define SENSOR_RCV D0
+
+#endif
+
+#ifdef TARGET_MTS_MDOT_F411RE  //Multi Tech mDot
+
+#define SENSOR_XMT PA_2
+#define SENSOR_RCV PA_3
+
+#endif
+
+//Use a boards second UART   to communicate witb the Honneywell sensor
 //Default UART setting are 8,N,1
-Serial sensor(D1, D0, 9600);
+Serial sensor( SENSOR_XMT, SENSOR_RCV, 9600);
 
 Timer t;
 
@@ -75,7 +82,7 @@
 // async buffer reads abd wrutes
 void readBuffer(uint8_t* buffer, uint16_t count)
 {
-    logInfo( "Receiving Data from sensor");
+    logInfo( "Receiving Data from sensor\n");
     uint8_t* pointer = buffer;
     uint16_t counter = count;
     while(1)
@@ -94,7 +101,7 @@
     
 void writeBuffer(const uint8_t* buffer, uint16_t count)
 {
-    logInfo( "Sending Data to sensor");
+    logInfo( "Sending Data to sensor\n");
     uint8_t* pointer = (uint8_t*)buffer;
     uint16_t counter = count;
     while(1)
@@ -118,7 +125,7 @@
   bool receiveflag = false;
   uint16_t receiveSum=0;
 
-  for(int i=0; i<(leng-2); i++){
+  for(int i=0; i<(leng-1); i++){
   receiveSum=receiveSum+thebuf[i];
   }
   receiveSum=receiveSum + 0x42;
@@ -157,16 +164,18 @@
 
 int main()
 {
-    uint8_t dataBuffer[32];
+    uint8_t dataBuffer[MESSAGE_LEN];
     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
     
-    pc.printf("Starting Honeywell Dust Sesor App version %s", VERSION);
+    pc.printf("Starting Honeywell Dust Sesor App version %s/n", VERSION);
 
     //Send start command to the sensor
     writeBuffer(measureCommand, 4);
+ /*
+ Not geting response from sensor - first characters received are measurement data   
     //Wait for sensors response
     //while(!sensor.readable());
     readBuffer(dataBuffer, 2);
@@ -183,11 +192,11 @@
         logInfo("Communication Error: Invalid Sensor Response");
         return -2;
     }
+*/    
     t.start();
 
     //Start continous loop 
-    uint32_t count = 0;
-    while(count++ < 4)
+    while(1)
     {
        if(sensor.getc() == 0x42){    //start to read when detect 0x42
             readBuffer(dataBuffer,MESSAGE_LEN -1);
@@ -200,34 +209,38 @@
               }
           }           
         }
-        
-        char input = pc.getc();
-        if(input == 'Q' || input == 'q')
-        { 
-            t.stop();
-            writeBuffer(stopCommand, 4);
-            logInfo("Exit request received");
-            return 0;
+        //Check for exit request
+        if(pc.readable()) {
+            char input = pc.getc();
+            if(input == 'Q' || input == 'q')
+            { 
+                t.stop();
+                writeBuffer(stopCommand, 4);
+                pc.printf("Exit request received\n");
+                return 0;
+            }
         }
+/* Use MBed wait function instead of Arduino delay loop
         static unsigned long OledTimer= t.read_ms();  
         if (t.read_ms() - OledTimer >=1000) 
         {
           OledTimer= t.read_ms(); 
+*/
+      wait_ms(1000);          
           
-          pc.printf("PM1.0: %d ug/m3", PM01Value);  
+      pc.printf("PM1.0: %d ug/m3", PM01Value);  
 //          debug.print(PM01Value);
 //          debug.println("  ug/m3");            
         
-          pc.printf("PM2.5: %d ug/m3", PM2_5Value);  
+      pc.printf("PM2.5: %d ug/m3", PM2_5Value);  
 //          debug.print(PM2_5Value);
 //          debug.println("  ug/m3");     
           
-          pc.printf("PM10: %d ug/m3", PM10Value);  
+    pc.printf("PM10: %d ug/m3", PM10Value);  
 //          debug.print(PM10Value);
 //          debug.println("  ug/m3");   
-          pc.printf("\n");
+      pc.printf("\n");
             
-        }
+ //       }
     }
-    pc.printf("Honeywell Dust Sensor exiting");
  }
\ No newline at end of file