Generates a test signal on an AnalogOut and monitors a signal on an AnalogIn, plotting the test signal or the actual signal depending on a conditional compile. The wait() and wait_ms() library calls for this board are highly inaccurate so a new function is provided to wait for X number of milliseconds -- which is not very accurate.

Dependencies:   LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI mbed-os BSP_DISCO_F429ZI

Revision:
2:cbcf2695a4a1
Parent:
1:b9d4b9b8884c
--- a/LaserMon-ScanInput.cpp	Fri Jun 14 21:11:31 2019 +0000
+++ b/LaserMon-ScanInput.cpp	Mon Jun 17 17:11:07 2019 +0000
@@ -17,7 +17,7 @@
 //
 // ----------------------------------------------------------------------
 
-#define WANT_TEST_SIGNAL        1
+#define WANT_TEST_SIGNAL        0
 
 // ----------------------------------------------------------------------
 // Local data storage
@@ -43,10 +43,23 @@
     // Flag indicates whether we believe we are receiving porch or not
     static bool b_inPorch;
     
+    // We keep track of how many times we have had to search for porch.
+    // Typically we could expect not to have to search for it when the
+    // plot goes beyond the end of the screen, however we may need to
+    // search on power-up and when the scan frequency changes
     static uint16_t u16_resyncCount;
+    
+    // For no reason at all we keep track of how many scans we detect
     static uint32_t u32_scanCount;
     
+    // When we are searching for porch, we use this flag to indicate that
     static bool b_waitingForPorch;
+    
+    // We keep track of how many milliseconds there are between porch
+    // detections. This indicates a fairly close timing of the scan
+    // frequency.
+    static uint16_t u16_scanFrequency;
+    static uint16_t u16_msFromPorchToPorch;
    
 // ----------------------------------------------------------------------
 // ScanInputPlotThisValue()
@@ -115,6 +128,15 @@
     return f_analogValue;
 }
 
+static void ScanInputUpdateScanFrequency(void)
+{
+    // Store the last porch to porch counter
+    u16_scanFrequency = u16_msFromPorchToPorch;
+    
+    // Restart the porch to porch counter
+    u16_msFromPorchToPorch = 0;
+}
+
 // ----------------------------------------------------------------------
 // ScanInputWaitForPorch()
 //
@@ -134,12 +156,19 @@
     
     b_waitingForPorch = false;
     
+    // Keep track of our scan frequency
+    ScanInputUpdateScanFrequency();
+    
     // Since we detected porch, start the plot over from the beginning
     u16_nextScanLine = 50;
     
     // The last height we consider to be 1 pixel on porch
     u16_lastHeight = 1;
     
+    // Keep track of how many times we had to wait for locating porch
+    // again after loosing it. Note that on power-up, if we are using
+    // the test signal output, it is driven to porch co-incident with
+    // the input test, so we start up with porch as the first sample
     ++u16_resyncCount;
 }
 
@@ -148,7 +177,7 @@
 //
 //
 // ----------------------------------------------------------------------
-static void ScanInputGetNextValue(void)
+static void ScanInputGetNextValue(bool b_allowPlotting)
 {
     float f_analogValue = ScanInputGetInputVoltage();
 
@@ -158,7 +187,7 @@
         // Did we previously know that we were in porch?
         if (false == b_inPorch)
         {
-            char pch_testMessage[21] = { 0 };
+            float f_plusFourPercent = 0.0f;
             
             // Flag the fact that we're in porch now
             b_inPorch = true;
@@ -169,21 +198,36 @@
             // The last height we consider to be 1 pixel on porch
             u16_lastHeight = 1;
             
-            (void)sprintf(pch_testMessage, "Resync %u, Scans %u", u16_resyncCount, ++u32_scanCount);
-            st_lcd.DisplayStringAt(1, LINE(0), (uint8_t *)pch_testMessage, LEFT_MODE);
+            // Update our scan frequency
+            ScanInputUpdateScanFrequency();
+            
+            // Since our timing in this device is off, we add a correction
+            f_plusFourPercent = (((float)u16_scanFrequency / 100.0f) * 4.3f);
+            
+            // Let the main module know what the scan rate is for display
+            // and for testing and evaluating
+            LaserMonMainInformScanInformation((uint16_t)((float)u16_scanFrequency + f_plusFourPercent), ++u32_scanCount);
         }
     }
     else
     {
         // We are in the ramp so flag the fact even if we already know
         b_inPorch = false;
+        
+        // Keep track of the scan frequency
+        u16_msFromPorchToPorch++;
     }
 
     // Indicate the next line to plot this reading on to
     u16_nextScanLine++;
     
-    // Call the function which plots this value
-    (void)ScanInputPlotThisValue(f_analogValue);
+    // The display may be in use for other functionality so we
+    // check to ensure that we are permitted to plot the input
+    if (true == b_allowPlotting)
+    {
+        // Call the function which plots this value
+        (void)ScanInputPlotThisValue(f_analogValue);
+    }
     
     // Are we about to exceed the display?
     if (u16_nextScanLine >= (LCD_HEIGHT - 10))
@@ -199,41 +243,43 @@
 // ----------------------------------------------------------------------
 // ScanInputThread()
 //
-// This thread wakes up every 1 millisecond to drive the input of the
-// signal coming in on the analog input -- or if it's enabled, to drive
-// the input based on the test signal for diagnostic and software
-// development use.
+// This is called once a millisecond however it is not a thread, the
+// thread class on this board ended up with timing that could not be
+// controlled so the main() loop calls us once a millisecond.
 //
 // ----------------------------------------------------------------------
-void ScanInputThread(void)
+void ScanInputThread(bool b_allowPlotting)
 {
+    // Are we waiting for porch synchronization?
     if (true == b_waitingForPorch)
     {
         // Start out searching for porch
         ScanInputWaitForPorch();
     }
 
-    ScanInputGetNextValue();
+    // Scan the signal coming in and optionally plot it
+    ScanInputGetNextValue(b_allowPlotting);
 }
 
 // ----------------------------------------------------------------------
 // ScanInputInit()
 //
+// Initialize this module's locally-held data
+//
 // ----------------------------------------------------------------------
 void ScanInputInit(void)
 {
     // Start out with the LED turned ON
     st_scanInputLED = 1;
     
-    // Initialize locally-held variables
-    u16_nextScanLine = 50;
-    
-    // Flag the fact that we don't believe that we are seeing porch
-    b_inPorch         = false;
-    b_waitingForPorch = true;
-    
-    u16_resyncCount = 0;
-    u32_scanCount   = 0;
+    // Initialize loc ally-held variables
+    u16_nextScanLine       = 50;
+    b_inPorch              = false;
+    b_waitingForPorch      = true;
+    u16_scanFrequency      = 0;    
+    u16_resyncCount        = 0;
+    u32_scanCount          = 0;
+    u16_msFromPorchToPorch = 0;
 }
 
 // End of file