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@0:1ebe7d222470, 2019-06-10 (annotated)
- Committer:
- Damotclese
- Date:
- Mon Jun 10 17:10:01 2019 +0000
- Revision:
- 0:1ebe7d222470
- Child:
- 1:b9d4b9b8884c
Checking in the project for safe keeping;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Damotclese | 0:1ebe7d222470 | 1 | |
Damotclese | 0:1ebe7d222470 | 2 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 3 | // LaserMon-ScanInput.cpp |
Damotclese | 0:1ebe7d222470 | 4 | // |
Damotclese | 0:1ebe7d222470 | 5 | // Fredric L. Rice, June 2019 |
Damotclese | 0:1ebe7d222470 | 6 | // |
Damotclese | 0:1ebe7d222470 | 7 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 8 | |
Damotclese | 0:1ebe7d222470 | 9 | #include "mbed.h" // The mbed operating system |
Damotclese | 0:1ebe7d222470 | 10 | #include "LCD_DISCO_F429ZI.h" // For controlling the LCD |
Damotclese | 0:1ebe7d222470 | 11 | #include "TS_DISCO_F429ZI.h" // For controlling the touch screen |
Damotclese | 0:1ebe7d222470 | 12 | #include "LaserMon-Main.h" // For data exported to us |
Damotclese | 0:1ebe7d222470 | 13 | #include "LaserMon-TestOutput.h" // For test signal |
Damotclese | 0:1ebe7d222470 | 14 | |
Damotclese | 0:1ebe7d222470 | 15 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 16 | // Options, defined constants, and MACROs |
Damotclese | 0:1ebe7d222470 | 17 | // |
Damotclese | 0:1ebe7d222470 | 18 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 19 | |
Damotclese | 0:1ebe7d222470 | 20 | #define WANT_TEST_SIGNAL 1 |
Damotclese | 0:1ebe7d222470 | 21 | |
Damotclese | 0:1ebe7d222470 | 22 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 23 | // Local data storage |
Damotclese | 0:1ebe7d222470 | 24 | // |
Damotclese | 0:1ebe7d222470 | 25 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 26 | |
Damotclese | 0:1ebe7d222470 | 27 | // We create an analog input |
Damotclese | 0:1ebe7d222470 | 28 | static AnalogIn st_scanInput(LASER_SCAN_IN); |
Damotclese | 0:1ebe7d222470 | 29 | |
Damotclese | 0:1ebe7d222470 | 30 | // We will be creating a thread |
Damotclese | 0:1ebe7d222470 | 31 | static Thread st_scanInputThread; |
Damotclese | 0:1ebe7d222470 | 32 | |
Damotclese | 0:1ebe7d222470 | 33 | // For diagnostic purposes to show that the Laser Scan is operating |
Damotclese | 0:1ebe7d222470 | 34 | static DigitalOut st_scanInputLED(LED2); |
Damotclese | 0:1ebe7d222470 | 35 | |
Damotclese | 0:1ebe7d222470 | 36 | // There are LCD_HEIGHT possible scan lines with a height of |
Damotclese | 0:1ebe7d222470 | 37 | // LCD_WIDTH -- yes, the defined constant names are swapped |
Damotclese | 0:1ebe7d222470 | 38 | // because we plot sideways. |
Damotclese | 0:1ebe7d222470 | 39 | // |
Damotclese | 0:1ebe7d222470 | 40 | // This value contains the next scan line number to plot |
Damotclese | 0:1ebe7d222470 | 41 | static uint16_t u16_nextScanLine; |
Damotclese | 0:1ebe7d222470 | 42 | |
Damotclese | 0:1ebe7d222470 | 43 | static uint16_t u16_lastHeight = 0; |
Damotclese | 0:1ebe7d222470 | 44 | |
Damotclese | 0:1ebe7d222470 | 45 | bool b_done = false; |
Damotclese | 0:1ebe7d222470 | 46 | |
Damotclese | 0:1ebe7d222470 | 47 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 48 | // ScanInputPlotThisValue() |
Damotclese | 0:1ebe7d222470 | 49 | // |
Damotclese | 0:1ebe7d222470 | 50 | // What percentage of LCD_HEIGHT is the voltage? We need to scale 0 to |
Damotclese | 0:1ebe7d222470 | 51 | // 3.3 volts across LCD_HEIGHT, with 0 being zero, and 3.3 volts being |
Damotclese | 0:1ebe7d222470 | 52 | // LCD_HEIGHT. |
Damotclese | 0:1ebe7d222470 | 53 | // |
Damotclese | 0:1ebe7d222470 | 54 | // We compute the percentage of 3.3 volts that the input signal is |
Damotclese | 0:1ebe7d222470 | 55 | // currently showing, then we compute that percentage of LCD_HEIGHT to |
Damotclese | 0:1ebe7d222470 | 56 | // determine how many pixels on a scan line that percentage is. That |
Damotclese | 0:1ebe7d222470 | 57 | // gives us the height of the plot line while u16_nextScanLine gives |
Damotclese | 0:1ebe7d222470 | 58 | // us the line to plot it on. |
Damotclese | 0:1ebe7d222470 | 59 | // |
Damotclese | 0:1ebe7d222470 | 60 | // This is easy because the value passed to this function is a value |
Damotclese | 0:1ebe7d222470 | 61 | // from 0.0 to 1.0 which is a percentage of 3.3 volts, so we use the |
Damotclese | 0:1ebe7d222470 | 62 | // value against LCD_HEIGHT to yield the percentage of height. |
Damotclese | 0:1ebe7d222470 | 63 | // |
Damotclese | 0:1ebe7d222470 | 64 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 65 | static void ScanInputPlotThisValue(float f_analogValue) |
Damotclese | 0:1ebe7d222470 | 66 | { |
Damotclese | 0:1ebe7d222470 | 67 | // Compute the percentage of LCD height |
Damotclese | 0:1ebe7d222470 | 68 | uint16_t u16_pixelHeight = ((f_analogValue / 3.8f) * LCD_HEIGHT); |
Damotclese | 0:1ebe7d222470 | 69 | |
Damotclese | 0:1ebe7d222470 | 70 | u16_pixelHeight /= 2; |
Damotclese | 0:1ebe7d222470 | 71 | u16_pixelHeight += 10; |
Damotclese | 0:1ebe7d222470 | 72 | |
Damotclese | 0:1ebe7d222470 | 73 | // Plot the pixel |
Damotclese | 0:1ebe7d222470 | 74 | if (u16_nextScanLine > 0) |
Damotclese | 0:1ebe7d222470 | 75 | { |
Damotclese | 0:1ebe7d222470 | 76 | st_lcd.DrawLine(u16_pixelHeight, u16_nextScanLine, u16_lastHeight, u16_nextScanLine - 1); |
Damotclese | 0:1ebe7d222470 | 77 | } |
Damotclese | 0:1ebe7d222470 | 78 | else |
Damotclese | 0:1ebe7d222470 | 79 | { |
Damotclese | 0:1ebe7d222470 | 80 | st_lcd.DrawPixel(u16_pixelHeight, u16_nextScanLine, LCD_COLOR_BLACK); |
Damotclese | 0:1ebe7d222470 | 81 | } |
Damotclese | 0:1ebe7d222470 | 82 | |
Damotclese | 0:1ebe7d222470 | 83 | u16_lastHeight = u16_pixelHeight; |
Damotclese | 0:1ebe7d222470 | 84 | |
Damotclese | 0:1ebe7d222470 | 85 | // Increment the scan line and see if it wrapped |
Damotclese | 0:1ebe7d222470 | 86 | if (++u16_nextScanLine >= LCD_WIDTH) |
Damotclese | 0:1ebe7d222470 | 87 | { |
Damotclese | 0:1ebe7d222470 | 88 | u16_nextScanLine = 40; |
Damotclese | 0:1ebe7d222470 | 89 | |
Damotclese | 0:1ebe7d222470 | 90 | b_done = true; |
Damotclese | 0:1ebe7d222470 | 91 | } |
Damotclese | 0:1ebe7d222470 | 92 | } |
Damotclese | 0:1ebe7d222470 | 93 | |
Damotclese | 0:1ebe7d222470 | 94 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 95 | // ScanInputGetNextValue() |
Damotclese | 0:1ebe7d222470 | 96 | // |
Damotclese | 0:1ebe7d222470 | 97 | // |
Damotclese | 0:1ebe7d222470 | 98 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 99 | static void ScanInputGetNextValue(void) |
Damotclese | 0:1ebe7d222470 | 100 | { |
Damotclese | 0:1ebe7d222470 | 101 | // Get the current analog input value and convert to volts |
Damotclese | 0:1ebe7d222470 | 102 | float f_analogValue = st_scanInput.read() * 3.3f; |
Damotclese | 0:1ebe7d222470 | 103 | |
Damotclese | 0:1ebe7d222470 | 104 | #if WANT_TEST_SIGNAL |
Damotclese | 0:1ebe7d222470 | 105 | // Use the test output signal voltage instead |
Damotclese | 0:1ebe7d222470 | 106 | f_analogValue = f_rampVoltage; |
Damotclese | 0:1ebe7d222470 | 107 | #endif |
Damotclese | 0:1ebe7d222470 | 108 | |
Damotclese | 0:1ebe7d222470 | 109 | // Call the function which plots this value |
Damotclese | 0:1ebe7d222470 | 110 | ScanInputPlotThisValue(f_analogValue); |
Damotclese | 0:1ebe7d222470 | 111 | } |
Damotclese | 0:1ebe7d222470 | 112 | |
Damotclese | 0:1ebe7d222470 | 113 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 114 | // ScanInputThread() |
Damotclese | 0:1ebe7d222470 | 115 | // |
Damotclese | 0:1ebe7d222470 | 116 | // This thread wakes up every 1 millisecond to drive the input of the |
Damotclese | 0:1ebe7d222470 | 117 | // signal coming in on the analog input -- or if it's enabled, to drive |
Damotclese | 0:1ebe7d222470 | 118 | // the input based on the test signal for diagnostic and software |
Damotclese | 0:1ebe7d222470 | 119 | // development use. |
Damotclese | 0:1ebe7d222470 | 120 | // |
Damotclese | 0:1ebe7d222470 | 121 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 122 | static void ScanInputThread(void) |
Damotclese | 0:1ebe7d222470 | 123 | { |
Damotclese | 0:1ebe7d222470 | 124 | // This thread goes in to a forever loop waking up once a millisecond |
Damotclese | 0:1ebe7d222470 | 125 | while(true) |
Damotclese | 0:1ebe7d222470 | 126 | { |
Damotclese | 0:1ebe7d222470 | 127 | if (false == b_done) |
Damotclese | 0:1ebe7d222470 | 128 | { |
Damotclese | 0:1ebe7d222470 | 129 | // Drive the input scanner |
Damotclese | 0:1ebe7d222470 | 130 | ScanInputGetNextValue(); |
Damotclese | 0:1ebe7d222470 | 131 | } |
Damotclese | 0:1ebe7d222470 | 132 | |
Damotclese | 0:1ebe7d222470 | 133 | // Wait for 1 millisecond |
Damotclese | 0:1ebe7d222470 | 134 | accurate_wait_ms(1); |
Damotclese | 0:1ebe7d222470 | 135 | } |
Damotclese | 0:1ebe7d222470 | 136 | } |
Damotclese | 0:1ebe7d222470 | 137 | |
Damotclese | 0:1ebe7d222470 | 138 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 139 | // ScanInputInit() |
Damotclese | 0:1ebe7d222470 | 140 | // |
Damotclese | 0:1ebe7d222470 | 141 | // ---------------------------------------------------------------------- |
Damotclese | 0:1ebe7d222470 | 142 | void ScanInputInit(void) |
Damotclese | 0:1ebe7d222470 | 143 | { |
Damotclese | 0:1ebe7d222470 | 144 | // Start out with the LED turned ON |
Damotclese | 0:1ebe7d222470 | 145 | st_scanInputLED = 1; |
Damotclese | 0:1ebe7d222470 | 146 | |
Damotclese | 0:1ebe7d222470 | 147 | // Initialize locally-held variables |
Damotclese | 0:1ebe7d222470 | 148 | u16_nextScanLine = 40; |
Damotclese | 0:1ebe7d222470 | 149 | |
Damotclese | 0:1ebe7d222470 | 150 | // Launch the thread |
Damotclese | 0:1ebe7d222470 | 151 | st_scanInputThread.start(ScanInputThread); |
Damotclese | 0:1ebe7d222470 | 152 | } |
Damotclese | 0:1ebe7d222470 | 153 | |
Damotclese | 0:1ebe7d222470 | 154 | // End of file |
Damotclese | 0:1ebe7d222470 | 155 |