Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI mbed-os BSP_DISCO_F429ZI
NextGen-Exerciser-Main.cpp
00001 00002 // ---------------------------------------------------------------------- 00003 // NextGen-Exerciser-Main.cpp 00004 // 00005 // Fredric L. Rice, May 2019 00006 // 00007 // o The LCD is cleared and configured 00008 // o A canned AC and DC 2f water-in-ambient-air spectrum is plotted 00009 // on the LCD after the spectrum is "normalized" by making each of 00010 // the sample values positive and then compressed so that it fits 00011 // in the display 00012 // o A thread is created to perform Modbus against a CPUM device 00013 // o A thread is created to perform Modbus Tunneling with foreign 00014 // frames against an ISEM device 00015 // o The main() thread wakes up once a second and performs whatever 00016 // other functionality might be desired 00017 // 00018 // ---------------------------------------------------------------------- 00019 00020 #include "mbed.h" // The mbed operating system 00021 #include "NextGen-Exerciser-Modbus.h" // For the CPUM Modbus thread 00022 #include "NextGen-Exerciser-Tunnel.h" // For the Modbus Tunnel thread 00023 #include "NextGen-Exerciser-Defines.h" // For defined constants and MACROs 00024 #include "LCD_DISCO_F429ZI.h" // For controlling the LCD 00025 00026 // ---------------------------------------------------------------------- 00027 // Defined constants that we will use here 00028 // 00029 // ---------------------------------------------------------------------- 00030 00031 #define LCD_WIDTH 240 00032 #define LCD_HEIGHT 320 00033 00034 // ---------------------------------------------------------------------- 00035 // Allocate local data storage 00036 // 00037 // ---------------------------------------------------------------------- 00038 00039 // Describe a canned 2f AC spectrum consisting of 512 floating point samples 00040 // Note that the values after the decimal point are not really needed, they 00041 // get discarded when the spectrum values are plotted as integer values. 00042 // These values were extracted after Spectra Processor decimated a spectrum 00043 // of 65536 samples. We "normalize" the values to make them all positive and 00044 // later we take the 512 samples and further "decimate" by averaging every 00045 // two samples, resuling in 256 final values. The 256 values get plotted on 00046 // to the LCD along the "width" of the display which is 320 pixels, leaving 00047 // 64 pixels to the right of the plot free to be used for other things. 00048 static float f_decimatedACSpectrum[] = 00049 { 00050 -1.502448670,9.237028121,18.872425000,27.111267000,33.651569300,38.191074300,40.437301633,40.118068700, 00051 36.992050100,30.858972544,21.569023133,9.031014442,-6.781046860,-25.823335644,-47.979820200,-73.062263400, 00052 -100.812744110,-130.908676110,-162.970184330,-196.569778440,-231.243743000,-266.505157440,-301.857879000,-336.811126000, 00053 -370.893982000,-403.669342000,-434.746948220,-463.794647220,-490.547790000,-514.816223110,-536.488708440,-555.534729000, 00054 -572.003357000,-586.019348110,-597.776916000,-607.531311000,-615.588317000,-622.292602000,-628.014892000,-633.139160110, 00055 -638.049499000,-643.118225000,-648.694641110,-655.095459000,-662.597473110,-671.431945000,-681.781494110,-693.779174000, 00056 -707.509704000,-723.013183000,-740.289856000,-759.307129000,-780.007019000,-802.314636220,-826.146362330,-851.418640110, 00057 -878.054870000,-905.992431000,-935.187500000,-965.618835440,-997.289367000,-1030.226684400,-1064.481080000,-1100.122430000, 00058 -1137.235840000,-1175.915650000,-1216.259270000,-1258.360100000,-1302.301140000,-1348.147820000,-1395.942993300,-1445.701293300, 00059 -1497.406372200,-1551.007934400,-1606.421874400,-1663.530761100,-1722.186401100,-1782.213134400,-1843.413452200,-1905.572753300, 00060 -1968.465942200,-2031.863770000,-2095.539062200,-2159.272210000,-2222.855460000,-2286.097900000,-2348.825683300,-2410.885253300, 00061 -2472.140130000,-2532.470214400,-2591.767090000,-2649.928460000,-2706.852294400,-2762.428950000,-2816.533440000,-2869.017333300, 00062 -2919.700190000,-2968.362304400,-3014.738520000,-3058.511710000,-3099.308830000,-3136.698974400,-3170.191162200,-3199.236572200, 00063 -3223.229492200,-3241.513671100,-3253.386710000,-3258.107421100,-3254.905273300,-3242.988030000,-3221.553710000,-3189.798340000, 00064 -3146.928950000,-3092.171142200,-3024.780761100,-2944.051020000,-2849.322510000,-2739.990234400,-2615.509270000,-2475.402343300, 00065 -2319.263183300,-2146.761962200,-1957.648560000,-1751.756340000,-1529.004510000,-1289.401733300,-1033.048700000,-760.140625000, 00066 -470.970947220,-165.934036220,154.471374000,489.638946000,838.851257330,1201.276733300,1575.967041100,1961.854370000, 00067 2357.750480000,2762.345940000,3174.210693300,3591.797120000,4013.442382200,4437.375480000,4861.724610000,5284.524414400, 00068 5703.730950000,6117.232910000,6522.867180000,6918.436030000,7301.727050000,7670.530761100,8022.663080000,8355.986320000, 00069 8668.431640000,8958.021484400,9222.888671100,9461.302734400,9671.681640000,9852.618164400,10002.893500000,10121.489200000, 00070 10207.603511000,10260.660100000,10280.314400000,10266.458900000,10219.221600000,10138.971600000,10026.307611000,9882.054680000, 00071 9707.252930000,9503.147460000,9271.170890000,9012.928710000,8730.179680000,8424.816400000,8098.844230000,7754.357421100, 00072 7393.519531100,7018.538080000,6631.642570000,6235.063470000,5831.010742200,5421.653800000,5009.104003300,4595.396972200, 00073 4182.478510000,3772.192871100,3366.271484400,2966.325440000,2573.839350000,2190.168450000,1816.535644400,1454.033320000, 00074 1103.623901100,766.144043000,442.308807330,132.717117330,-162.142257000,-441.884216330,-706.220947220,-954.955566440, 00075 -1187.975830000,-1405.248901100,-1606.816040000,-1792.787720000,-1963.340570000,-2118.713134400,-2259.203370000,-2385.165283300, 00076 -2497.007812200,-2595.190430000,-2680.222412200,-2752.658203300,-2813.093994400,-2862.164550000,-2900.537590000,-2928.907950000, 00077 -2947.991700000,-2958.517822200,-2961.222650000,-2956.839350000,-2946.091552200,-2929.683350000,-2908.292230000,-2882.560540000, 00078 -2853.090080000,-2820.434082200,-2785.094482200,-2747.517333300,-2708.092041100,-2667.150140000,-2624.968500000,-2581.769770000, 00079 -2537.729730000,-2492.980224400,-2447.618164400,-2401.711420000,-2355.307120000,-2308.439690000,-2261.137940000,-2213.432861100, 00080 -2165.363520000,-2116.981440000,-2068.355224400,-2019.572380000,-1970.740350000,-1921.984980000,-1873.449340000,-1825.289550000, 00081 -1777.670043300,-1730.759150000,-1684.721923300,-1639.715080000,-1595.880493300,-1553.340080000,-1512.190790000,-1472.501342200, 00082 -1434.309570000,-1397.621210000,-1362.410760000,-1328.622680000,-1296.175292200,-1264.964600000,-1234.869873300,-1205.759880000, 00083 -1177.499140000,-1149.954711100,-1123.002800000,-1096.534050000,-1070.459472200,-1044.713134400,-1019.255790000,-994.075744000, 00084 -969.188293440,-944.634338330,-920.477295000,-896.798462000,-873.692077000,-851.259216330,-829.601562440,-808.814880330, 00085 -788.983154220,-770.172973000,-752.429016110,-735.770691000,-720.190063440,-705.651245110,-692.091308000,-679.422546330, 00086 -667.536193000,-656.307312000,-645.600402000,-635.275817000,-625.196350000,-615.233886000,-605.275512000,-595.229004000, 00087 -585.027282000,-574.631591000,-564.033447220,-553.254821000,-542.346923000,-531.387756330,-520.477966330,-509.735595000, 00088 -499.290527330,-489.277618440,-479.830261220,-471.073608330,-463.118743000,-456.057190000,-449.956909110,-444.859253000, 00089 -440.777649000,-437.697479220,-435.577880000,-434.354522000,-433.944000220,-434.248993000,-435.164337110,-436.583465000, 00090 -438.404754000,-440.537933330,-442.909454330,-445.467254000,-448.184082000,-451.059661000,-454.121124220,-457.422180110, 00091 -461.040466330,-465.073761000,-469.634948000,-474.846222000,-480.832397440,-487.714386000,-495.602508000,-504.590667000, 00092 -514.750915000,-526.129760000,-538.745178220,-552.586059000,-567.612426000,-583.757812440,-600.933227000,-619.031982440, 00093 -637.936096110,-657.523071220,-677.673339000,-698.277160000,-719.241394000,-740.495056110,-761.993713330,-783.721740000, 00094 -805.693237330,-827.950134220,-850.558349000,-873.601989000,-897.174682000,-921.369873000,-946.269043000,-971.929199220, 00095 -998.369567000,-1025.558350000,-1053.400024400,-1081.723632200,-1110.273071100,-1138.698974400,-1166.554560000,-1193.292602200, 00096 -1218.267211100,-1240.738770000,-1259.881470000,-1274.795280000,-1284.520751100,-1288.056152200,-1284.377800000,-1272.461420000, 00097 -1251.305900000,-1219.956910000,-1177.530883300,-1123.238160000,-1056.406250000,-976.499206000,-883.136718000,-776.109497000, 00098 -655.391845000,-521.151001000,-373.752929000,-213.764404220,-41.951789800,140.723938000,333.115356440,533.900024440, 00099 741.594665000,954.571960440,1171.079711100,1389.261840000,1607.181152200,1822.842651100,2034.219230000,2239.276360000, 00100 2435.997802200,2622.412110000,2796.617180000,2956.805420000,3101.288080000,3228.517822200,3337.110590000,3425.865234400, 00101 3493.781000000,3540.073974400,3564.189453300,3565.812744400,3544.875244400,3501.559570000,3436.298820000,3349.772700000, 00102 3242.901850000,3116.834472200,2972.933350000,2812.756340000,2638.033691100,2450.644531100,2252.587640000,2045.952140000, 00103 1832.885253300,1615.559814400,1396.141350000,1176.755120000,959.454528000,746.191284110,538.787902000,338.913055440, 00104 148.060974110,-32.465728700,-201.568542440,-358.361267000,-502.173584000,-632.550109000,-749.244995110,-852.212768000, 00105 -941.595398000,-1017.705932200,-1081.010010000,-1132.105340000,-1171.699584400,-1200.587760000,-1219.629394400,-1229.727172200, 00106 -1231.805170000,-1226.789910000,-1215.592890000,-1199.095330000,-1178.135864400,-1153.499633300,-1125.911621100,-1096.030510000, 00107 -1064.447021100,-1031.682000000,-998.188537000,-964.353820000,-930.503479000,-896.906066000,-863.778503440,-831.291504000, 00108 -799.575500440,-768.725891110,-738.808532000,-709.864502000,-681.914245000,-654.961486000,-628.996582000,-603.999267000, 00109 -579.941101000,-556.787353000,-534.498901330,-513.033691440,-492.348114000,-472.398071220,-453.140289330,-434.533050000, 00110 -416.537292440,-399.117187440,-382.240600000,-365.879577000,-350.010192000,-334.612487000,-319.669769220,-305.167755110, 00111 -291.093566000,-277.434234000,-264.175262440,-251.299041000,-238.783386220,-226.600113000,-214.714141000,-203.082885000, 00112 -191.656234000,-180.377243000,-169.183487000,-158.009185000,-146.788055440,-135.456863440,-123.959488000,-112.251495330, 00113 -100.304824000,-88.112571711,-75.693458500,-63.095802300,-50.400745400,-37.724391933,-25.218734744,-13.071174622 00114 } ; 00115 00116 static float f_decimatedDCSpectrum[] = 00117 { 00118 164622.859, 156185.187, 130679.648, 94457.766, 60629.086, 35816.664, 20260.646, 12169.420, 00119 9521.371, 9680.042, 10389.553, 14924.355, 29797.771, 54048.340, 79677.930, 100584.007, 00120 114916.859, 123130.789, 126640.937, 127108.539, 125978.539, 124264.656, 122565.765, 121160.593, 00121 120120.234, 119405.492, 118934.531, 118622.812, 118402.031, 118224.390, 118060.507, 117896.296, 00122 117727.086, 117552.828, 117375.304, 117196.453, 117017.570, 116837.726, 116656.039, 116473.492, 00123 116289.906, 116104.265, 115915.937, 115723.570, 115526.664, 115325.656, 115120.421, 114911.429, 00124 114697.828, 114476.804, 114247.992, 114013.179, 113771.867, 113522.140, 113264.578, 113002.109, 00125 112735.640, 112462.671, 112182.187, 111892.171, 111590.554, 111280.000, 110962.125, 110639.429, 00126 110312.679, 109975.781, 109623.398, 109255.132, 108878.789, 108498.742, 108109.039, 107706.664, 00127 107291.461, 106863.039, 106420.086, 105960.101, 105483.578, 104994.242, 104495.437, 103984.203, 00128 103457.976, 102917.992, 102363.515, 101794.343, 101214.031, 100629.211, 100040.492, 99439.859, 00129 98819.461, 98173.063, 97503.469, 96825.117, 96144.188, 95452.000, 94738.039, 93996.977, 00130 93228.984, 92435.734, 91625.875, 90815.219, 90004.086, 89177.555, 88331.219, 87471.523, 00131 86607.555, 85742.336, 84864.008, 83967.336, 83058.867, 82140.016, 81212.930, 80285.523, 00132 79358.758, 78424.992, 77494.000, 76579.289, 75662.469, 74731.563, 73808.039, 72908.133, 00133 72025.875, 71144.391, 70256.500, 69372.094, 68501.109, 67648.109, 66813.898, 65996.867, 00134 65195.641, 64414.758, 63665.418, 62947.723, 62251.930, 61577.820, 60930.938, 60310.703, 00135 59717.070, 59153.746, 58622.043, 58124.535, 57664.281, 57239.590, 56849.543, 56495.852, 00136 56177.289, 55892.000, 55643.016, 55431.777, 55257.840, 55124.254, 55029.434, 54971.840, 00137 54955.445, 54979.289, 55040.957, 55139.879, 55278.230, 55457.840, 55673.457, 55925.805, 00138 56220.500, 56554.961, 56925.184, 57330.996, 57773.441, 58253.074, 58770.402, 59326.258, 00139 59917.891, 60538.234, 61187.504, 61875.184, 62605.102, 63370.285, 64161.137, 64974.477, 00140 65816.297, 66691.320, 67592.148, 68503.984, 69422.414, 70364.266, 71343.883, 72351.906, 00141 73372.016, 74390.320, 75401.859, 76421.227, 77464.242, 78523.742, 79582.586, 80642.227, 00142 81714.758, 82804.367, 83906.422, 85006.211, 86092.281, 87169.992, 88245.125, 89310.359, 00143 90360.758, 91407.516, 92457.242, 93497.219, 94524.414, 95552.211, 96577.430, 97588.609, 00144 98588.539, 99575.063, 100543.234, 101498.179, 102434.992, 103344.476, 104234.867, 105114.281, 00145 105982.656, 106839.539, 107682.593, 108518.468, 109349.765, 110166.625, 110967.500, 111753.843, 00146 112524.437, 113279.453, 114016.820, 114732.929, 115433.171, 116129.640, 116820.101, 117493.351, 00147 118144.578, 118774.367, 119390.500, 119997.554, 120589.562, 121168.031, 121741.507, 122308.328, 00148 122861.468, 123400.164, 123925.187, 124437.125, 124944.039, 125448.539, 125940.484, 126416.187, 00149 126881.367, 127340.125, 127790.164, 128227.226, 128651.578, 129064.890, 129469.812, 129867.937, 00150 130259.703, 130648.023, 131031.523, 131406.046, 131770.140, 132124.406, 132471.937, 132814.875, 00151 133153.359, 133487.703, 133816.296, 134134.171, 134439.421, 134738.953, 135040.187, 135338.218, 00152 135628.468, 135915.687, 136198.750, 136474.656, 136746.062, 137011.234, 137270.937, 137531.343, 00153 137787.781, 138033.875, 138274.046, 138511.078, 138741.453, 138967.078, 139194.343, 139418.796, 00154 139635.859, 139849.718, 140059.359, 140263.546, 140464.187, 140661.343, 140856.937, 141051.328, 00155 141241.859, 141427.875, 141610.156, 141789.250, 141965.359, 142139.234, 142310.125, 142476.343, 00156 142639.187, 142799.046, 142954.359, 143106.234, 143255.687, 143402.343, 143547.250, 143690.609, 00157 143830.875, 143967.234, 144099.421, 144228.046, 144353.625, 144475.734, 144594.656, 144710.671, 00158 144823.093, 144931.390, 145036.046, 145138.921, 145240.593, 145339.421, 145434.562, 145525.906, 00159 145612.765, 145694.562, 145771.609, 145845.453, 145916.843, 145984.953, 146049.296, 146109.531, 00160 146165.484, 146216.843, 146262.718, 146304.031, 146341.515, 146373.906, 146400.953, 146423.250, 00161 146440.265, 146451.703, 146457.453, 146456.968, 146450.687, 146439.015, 146420.750, 146395.453, 00162 146364.703, 146329.000, 146286.328, 146235.890, 146179.187, 146116.390, 146046.562, 145970.250, 00163 145888.875, 145802.343, 145709.578, 145611.015, 145507.875, 145400.656, 145289.718, 145175.437, 00164 145058.906, 144941.578, 144823.593, 144704.640, 144585.046, 144464.984, 144345.546, 144229.687, 00165 144119.281, 144013.609, 143912.671, 143818.609, 143732.531, 143654.062, 143584.265, 143524.062, 00166 143472.843, 143430.656, 143398.421, 143376.796, 143365.031, 143361.171, 143364.421, 143375.312, 00167 143393.546, 143417.750, 143448.921, 143488.828, 143537.078, 143593.468, 143657.265, 143727.843, 00168 143806.734, 143893.125, 143984.609, 144081.359, 144184.625, 144293.546, 144406.218, 144524.500, 00169 144650.468, 144782.187, 144918.953, 145061.843, 145211.578, 145369.562, 145536.781, 145712.218, 00170 145895.156, 146085.937, 146284.156, 146491.125, 146708.437, 146934.328, 147168.796, 147413.734, 00171 147668.906, 147933.296, 148206.078, 148487.953, 148777.734, 149071.968, 149373.265, 149686.093, 00172 150007.109, 150328.703, 150648.953, 150969.625, 151287.671, 151603.171, 151921.078, 152239.421, 00173 152552.781, 152860.218, 153163.453, 153463.234, 153758.718, 154050.921, 154341.515, 154628.250, 00174 154907.796, 155179.656, 155444.531, 155703.500, 155957.656, 156205.265, 156446.359, 156685.718, 00175 156922.781, 157152.437, 157374.281, 157590.484, 157802.796, 158010.562, 158211.375, 158407.156, 00176 158600.015, 158787.093, 158967.703, 159144.312, 159317.187, 159486.218, 159652.437, 159816.218, 00177 159976.906, 160135.031, 160291.250, 160443.359, 160590.359, 160733.843, 160875.171, 161015.390, 00178 161154.515, 161291.843, 161427.000, 161559.953, 161689.953, 161815.781, 161939.203, 162063.375, 00179 162187.312, 162307.875, 162424.328, 162538.328, 162651.750, 162765.296, 162878.078, 162987.562, 00180 163093.531, 163199.171, 163305.312, 163410.593, 163514.609, 163617.453, 163720.437, 163824.093, 00181 163926.906, 164028.125, 164126.515, 164224.531, 164334.281, 164304.281, 163889.750, 164209.968 00182 } ; 00183 00184 // We will be using the LCD so instantiate an object locally 00185 static LCD_DISCO_F429ZI st_lcd; 00186 00187 // ---------------------------------------------------------------------- 00188 // MainProcessDecimatedSpectrum() 00189 // 00190 // This function will: 00191 // o Search through the AC spectra and the DC spectra looking for 00192 // the smallest and the highest numeric values in both 00193 // o Convert the smallest number in to an absolute value by removing 00194 // the sign bit using the ANSI absolute function 00195 // o Add that now-positive smallest number to every sample in the 00196 // AC and DC spectrums so that each value in the spectrums are 00197 // positive values 00198 // o Add that smallest value to the known largest value so that it 00199 // agrees with the rest of the spectrum again 00200 // o Compute a ratio derived by dividing the ;argest known value 00201 // by the "width" of the screen 00202 // o Goes through all 512 samples and averages them two by two, 00203 // compressing the spectrum in place (in the same array of values) 00204 // so that we end up with 256 samples which can fit on the LCD 00205 // o Divides each of the 256 samples by the radio so that each of 00206 // the samples will fit on the LCD 00207 // 00208 // ---------------------------------------------------------------------- 00209 static void MainProcessDecimatedSpectrum(void) 00210 { 00211 uint16_t u16_searchLoop = 0; 00212 uint32_t u32_sampleACRatio = 0; 00213 uint32_t u32_sampleDCRatio = 0; 00214 uint16_t u16_resultIndex = 0; 00215 float f_smallACNumber = 10000000.0f; 00216 float f_largeACNumber = 0.0f; 00217 float f_smallDCNumber = 10000000.0f; 00218 float f_largeDCNumber = 0.0f; 00219 00220 // Search through the AC spectrum for the smallest and the largest values 00221 for (u16_searchLoop = 0; u16_searchLoop < 512; u16_searchLoop++) 00222 { 00223 // First do the AC SPectrum 00224 if (f_decimatedACSpectrum[u16_searchLoop] < f_smallACNumber) 00225 { 00226 // This value is the new lowest number 00227 f_smallACNumber = f_decimatedACSpectrum[u16_searchLoop]; 00228 } 00229 00230 if (f_decimatedACSpectrum[u16_searchLoop] > f_largeACNumber) 00231 { 00232 // This value is the new highest number 00233 f_largeACNumber = f_decimatedACSpectrum[u16_searchLoop]; 00234 } 00235 00236 // Now do the DC spectrum 00237 if (f_decimatedDCSpectrum[u16_searchLoop] < f_smallDCNumber) 00238 { 00239 // This value is the new lowest number 00240 f_smallDCNumber = f_decimatedDCSpectrum[u16_searchLoop]; 00241 } 00242 00243 if (f_decimatedDCSpectrum[u16_searchLoop] > f_largeDCNumber) 00244 { 00245 // This value is the new highest number 00246 f_largeDCNumber = f_decimatedDCSpectrum[u16_searchLoop]; 00247 } 00248 } 00249 00250 // Is the smallest AC number negative? We know that it is but we check 00251 if (f_smallACNumber < 0.0f) 00252 { 00253 // Make the value absolute so that it is positive 00254 f_smallACNumber = abs(f_smallACNumber); 00255 } 00256 00257 // Is the smallest DC number negative? 00258 if (f_smallDCNumber < 0.0f) 00259 { 00260 // Make the value absolute so that it is positive 00261 f_smallDCNumber = abs(f_smallDCNumber); 00262 } 00263 00264 // To make all values positive, add the newly-derived positive value. 00265 // This makes all numbers positive and ensures that the smallest 00266 // value in the spectra is zero. 00267 for (u16_searchLoop = 0; u16_searchLoop < 512; u16_searchLoop++) 00268 { 00269 // Do the AC spectrum 00270 f_decimatedACSpectrum[u16_searchLoop] += f_smallACNumber; 00271 00272 // Do the DC spectrum 00273 f_decimatedDCSpectrum[u16_searchLoop] += f_smallDCNumber; 00274 } 00275 00276 // Also add the newly-positive number to our largest known value 00277 f_largeACNumber += f_smallACNumber; 00278 f_largeDCNumber += f_smallDCNumber; 00279 00280 // Determine the ratio to use, the LCD screen is 240 by 320 so we 00281 // take the highest number value in the spectra and divide by 240 00282 // for the verticle plot, and we have 512 samples which will be 00283 // plotted across the 320 by averaging every 2 samples to yield 00284 // 256 horizontal positions. We plot with the screen laying on 00285 // its side 00286 u32_sampleACRatio = (f_largeACNumber / LCD_WIDTH); 00287 u32_sampleDCRatio = (f_largeDCNumber / LCD_WIDTH); 00288 00289 // Go through each pairs of samples and average them and then 00290 // divide the result by the ratio. Step through in twos 00291 for (u16_searchLoop = 0; u16_searchLoop < 512; u16_searchLoop += 2, u16_resultIndex++) 00292 { 00293 // Take the next two AC samples and average them, putting the result 00294 // in to the next result index of the same buffer we're processing 00295 f_decimatedACSpectrum[u16_resultIndex] = 00296 (f_decimatedACSpectrum[u16_searchLoop] + f_decimatedACSpectrum[u16_searchLoop]) / 2; 00297 00298 // Divide the sample value by the ratio 00299 f_decimatedACSpectrum[u16_resultIndex] /= u32_sampleACRatio; 00300 00301 // Take the next two DC samples and average them, putting the result 00302 // in to the next result index of the same buffer we're processing 00303 f_decimatedDCSpectrum[u16_resultIndex] = 00304 (f_decimatedDCSpectrum[u16_searchLoop] + f_decimatedDCSpectrum[u16_searchLoop]) / 2; 00305 00306 // Divide the sample value by the ratio 00307 f_decimatedDCSpectrum[u16_resultIndex] /= u32_sampleDCRatio; 00308 } 00309 } 00310 00311 // ---------------------------------------------------------------------- 00312 // MainPlotFinalSpectrum() 00313 // 00314 // This function will step through the "normalized" and compressed AC 00315 // and DC spectrums consisting of 256 samples and will plot a line from 00316 // one sample to the next on the LCD 00317 // 00318 // ---------------------------------------------------------------------- 00319 static void MainPlotFinalSpectrum(void) 00320 { 00321 uint16_t u16_searchLoop = 0; 00322 00323 // Plot the spectrum values, plotting a line from point to point 00324 for (u16_searchLoop = 0; u16_searchLoop < ((512 / 2) - 1); u16_searchLoop++) 00325 { 00326 // Ensure that we actually fit. The AC sample should 00327 if (f_decimatedACSpectrum[u16_searchLoop] <= LCD_WIDTH) 00328 { 00329 st_lcd.DrawLine(f_decimatedACSpectrum[u16_searchLoop], u16_searchLoop, 00330 f_decimatedACSpectrum[u16_searchLoop + 1], u16_searchLoop + 1); 00331 } 00332 else 00333 { 00334 // Mathematically we topped-out past the maximum some how 00335 st_lcd.DrawPixel(0, u16_searchLoop, LCD_COLOR_WHITE); 00336 } 00337 00338 // Ensure that we actually fit. The DC sample should 00339 if (f_decimatedDCSpectrum[u16_searchLoop] <= LCD_WIDTH) 00340 { 00341 st_lcd.DrawLine(f_decimatedDCSpectrum[u16_searchLoop], u16_searchLoop, 00342 f_decimatedDCSpectrum[u16_searchLoop + 1], u16_searchLoop + 1); 00343 } 00344 else 00345 { 00346 // Mathematically we topped-out past the maximum some how 00347 st_lcd.DrawPixel(0, u16_searchLoop, LCD_COLOR_WHITE); 00348 } 00349 } 00350 } 00351 00352 // ---------------------------------------------------------------------- 00353 // MainInit() 00354 // 00355 // This function will: 00356 // o Clear and initialize the LCD 00357 // o Process the scanned decimated 2f AC and DC spectrums 00358 // o Plot the processed spectrums 00359 // 00360 // ---------------------------------------------------------------------- 00361 static void MainInit(void) 00362 { 00363 // Bring the LCD up and set its various colors 00364 st_lcd.Clear(LCD_COLOR_BLACK); 00365 st_lcd.SetBackColor(LCD_COLOR_BLACK); 00366 st_lcd.SetTextColor(LCD_COLOR_WHITE); 00367 00368 // Process the decimated spectrum and then plot it 00369 MainProcessDecimatedSpectrum(); 00370 MainPlotFinalSpectrum(); 00371 } 00372 00373 // ---------------------------------------------------------------------- 00374 // MainThread() 00375 // 00376 // Called once a second. 00377 // 00378 // ---------------------------------------------------------------------- 00379 static void MainThread(void) 00380 { 00381 } 00382 00383 // ---------------------------------------------------------------------- 00384 // main() 00385 // 00386 // This is the main entry point which the operating system will load 00387 // and execute on power-up after the OS completes its own start-up 00388 // functionality. 00389 // 00390 // The function will: 00391 // o Initialize this module's functionality 00392 // o Launch the MODBUS CPUM thread 00393 // o Launch the Modbus Tunnel thread 00394 // o Enter in to a forever loop which should never terminate, 00395 // waking up once a second to perform any additional desired 00396 // functionality 00397 // 00398 // ---------------------------------------------------------------------- 00399 int main(void) 00400 { 00401 // Perform local module initialization, if any 00402 MainInit(); 00403 00404 // Initialize and launch the MODBUS CPUM thread 00405 ModbusInit(); 00406 00407 // Initialize and launch the MODBUS Tunnel thread 00408 TunnelInit(); 00409 00410 // Enter in to a forever loop which wakes up once a second 00411 while (true) 00412 { 00413 // Sleep for one second 00414 wait(1.0); 00415 00416 MainThread(); 00417 } 00418 } 00419 00420 // End of file 00421
Generated on Tue Jul 19 2022 01:20:56 by
1.7.2