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

LaserMon-ScanInput.cpp

Committer:
Damotclese
Date:
2019-06-10
Revision:
0:1ebe7d222470
Child:
1:b9d4b9b8884c

File content as of revision 0:1ebe7d222470:


// ----------------------------------------------------------------------
// LaserMon-ScanInput.cpp
//
// Fredric L. Rice, June 2019
//
// ----------------------------------------------------------------------

#include "mbed.h"                   // The mbed operating system
#include "LCD_DISCO_F429ZI.h"       // For controlling the LCD
#include "TS_DISCO_F429ZI.h"        // For controlling the touch screen
#include "LaserMon-Main.h"          // For data exported to us
#include "LaserMon-TestOutput.h"    // For test signal

// ----------------------------------------------------------------------
// Options, defined constants, and MACROs
//
// ----------------------------------------------------------------------

#define WANT_TEST_SIGNAL        1

// ----------------------------------------------------------------------
// Local data storage
//
// ----------------------------------------------------------------------

    // We create an analog input
    static AnalogIn st_scanInput(LASER_SCAN_IN);
    
    // We will be creating a thread
    static Thread st_scanInputThread;
    
    // For diagnostic purposes to show that the Laser Scan is operating
    static DigitalOut st_scanInputLED(LED2);
    
    // There are LCD_HEIGHT possible scan lines with a height of
    // LCD_WIDTH -- yes, the defined constant names are swapped
    // because we plot sideways.
    //
    // This value contains the next scan line number to plot
    static uint16_t u16_nextScanLine;
    
    static uint16_t u16_lastHeight = 0;
    
    bool b_done = false;
    
// ----------------------------------------------------------------------
// ScanInputPlotThisValue()
//
// What percentage of LCD_HEIGHT is the voltage? We need to scale 0 to 
// 3.3 volts across LCD_HEIGHT, with 0 being zero, and 3.3 volts being 
// LCD_HEIGHT.
//
// We compute the percentage of 3.3 volts that the input signal is 
// currently showing, then we compute that percentage of LCD_HEIGHT to
// determine how many pixels on a scan line that percentage is. That
// gives us the height of the plot line while u16_nextScanLine gives 
// us the line to plot it on.
//
// This is easy because the value passed to this function is a value
// from 0.0 to 1.0 which is a percentage of 3.3 volts, so we use the
// value against LCD_HEIGHT to yield the percentage of height.
//
// ----------------------------------------------------------------------
static void ScanInputPlotThisValue(float f_analogValue)
{
    // Compute the percentage of LCD height
    uint16_t u16_pixelHeight = ((f_analogValue / 3.8f) * LCD_HEIGHT);
    
    u16_pixelHeight /= 2;
    u16_pixelHeight += 10;
    
    // Plot the pixel
    if (u16_nextScanLine > 0)
    {
        st_lcd.DrawLine(u16_pixelHeight, u16_nextScanLine, u16_lastHeight, u16_nextScanLine - 1);
    }
    else
    {
        st_lcd.DrawPixel(u16_pixelHeight, u16_nextScanLine, LCD_COLOR_BLACK);
    }
    
    u16_lastHeight = u16_pixelHeight;
    
    // Increment the scan line and see if it wrapped
    if (++u16_nextScanLine >= LCD_WIDTH)
    {
        u16_nextScanLine = 40;
        
        b_done = true;
    }
}

// ----------------------------------------------------------------------
// ScanInputGetNextValue()
//
//
// ----------------------------------------------------------------------
static void ScanInputGetNextValue(void)
{
    // Get the current analog input value and convert to volts
    float f_analogValue = st_scanInput.read() * 3.3f;

#if WANT_TEST_SIGNAL
    // Use the test output signal voltage instead
    f_analogValue = f_rampVoltage;
#endif

    // Call the function which plots this value
    ScanInputPlotThisValue(f_analogValue);
}
    
// ----------------------------------------------------------------------
// 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.
//
// ----------------------------------------------------------------------
static void ScanInputThread(void)
{
    // This thread goes in to a forever loop waking up once a millisecond
    while(true)
    {
        if (false == b_done)
        {
            // Drive the input scanner
            ScanInputGetNextValue();
        }
        
        // Wait for 1 millisecond
        accurate_wait_ms(1);
    }
}

// ----------------------------------------------------------------------
// ScanInputInit()
//
// ----------------------------------------------------------------------
void ScanInputInit(void)
{
    // Start out with the LED turned ON
    st_scanInputLED = 1;
    
    // Initialize locally-held variables
    u16_nextScanLine = 40;
    
    // Launch the thread
    st_scanInputThread.start(ScanInputThread);
}

// End of file