MLX90640_API.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /**
  2. * @copyright (C) 2017 Melexis N.V.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include "MLX90640_I2C_Driver.h"
  18. #include "MLX90640_API.h"
  19. #include <math.h>
  20. void ExtractVDDParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  21. void ExtractPTATParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  22. void ExtractGainParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  23. void ExtractTgcParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  24. void ExtractResolutionParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  25. void ExtractKsTaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  26. void ExtractKsToParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  27. void ExtractAlphaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  28. void ExtractOffsetParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  29. void ExtractKtaPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  30. void ExtractKvPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  31. void ExtractCPParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  32. void ExtractCILCParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
  33. int ExtractDeviatingPixels(uint16_t *eeData, paramsMLX90640 *mlx90640);
  34. int CheckAdjacentPixels(uint16_t pix1, uint16_t pix2);
  35. float GetMedian(float *values, int n);
  36. int IsPixelBad(uint16_t pixel,paramsMLX90640 *params);
  37. int MLX90640_DumpEE(uint8_t slaveAddr, uint16_t *eeData)
  38. {
  39. return MLX90640_I2CRead(slaveAddr, 0x2400, 832, eeData);
  40. }
  41. int MLX90640_GetFrameData(uint8_t slaveAddr, uint16_t *frameData)
  42. {
  43. uint16_t dataReady = 1;
  44. uint16_t controlRegister1;
  45. uint16_t statusRegister;
  46. int error = 1;
  47. uint8_t cnt = 0;
  48. dataReady = 0;
  49. while(dataReady == 0)
  50. {
  51. error = MLX90640_I2CRead(slaveAddr, 0x8000, 1, &statusRegister);
  52. if(error != 0)
  53. {
  54. return error;
  55. }
  56. dataReady = statusRegister & 0x0008;
  57. }
  58. while(dataReady != 0 && cnt < 5)
  59. {
  60. error = MLX90640_I2CWrite(slaveAddr, 0x8000, 0x0030);
  61. if(error == -1)
  62. {
  63. return error;
  64. }
  65. error = MLX90640_I2CRead(slaveAddr, 0x0400, 832, frameData);
  66. if(error != 0)
  67. {
  68. return error;
  69. }
  70. error = MLX90640_I2CRead(slaveAddr, 0x8000, 1, &statusRegister);
  71. if(error != 0)
  72. {
  73. return error;
  74. }
  75. dataReady = statusRegister & 0x0008;
  76. cnt = cnt + 1;
  77. }
  78. if(cnt > 4)
  79. {
  80. return -8;
  81. }
  82. error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1);
  83. frameData[832] = controlRegister1;
  84. frameData[833] = statusRegister & 0x0001;
  85. if(error != 0)
  86. {
  87. return error;
  88. }
  89. return frameData[833];
  90. }
  91. int MLX90640_ExtractParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  92. {
  93. int error = 0;
  94. ExtractVDDParameters(eeData, mlx90640);
  95. ExtractPTATParameters(eeData, mlx90640);
  96. ExtractGainParameters(eeData, mlx90640);
  97. ExtractTgcParameters(eeData, mlx90640);
  98. ExtractResolutionParameters(eeData, mlx90640);
  99. ExtractKsTaParameters(eeData, mlx90640);
  100. ExtractKsToParameters(eeData, mlx90640);
  101. ExtractCPParameters(eeData, mlx90640);
  102. ExtractAlphaParameters(eeData, mlx90640);
  103. ExtractOffsetParameters(eeData, mlx90640);
  104. ExtractKtaPixelParameters(eeData, mlx90640);
  105. ExtractKvPixelParameters(eeData, mlx90640);
  106. ExtractCILCParameters(eeData, mlx90640);
  107. error = ExtractDeviatingPixels(eeData, mlx90640);
  108. return error;
  109. }
  110. //------------------------------------------------------------------------------
  111. int MLX90640_SetResolution(uint8_t slaveAddr, uint8_t resolution)
  112. {
  113. uint16_t controlRegister1;
  114. int value;
  115. int error;
  116. value = (resolution & 0x03) << 10;
  117. error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1);
  118. if(error == 0)
  119. {
  120. value = (controlRegister1 & 0xF3FF) | value;
  121. error = MLX90640_I2CWrite(slaveAddr, 0x800D, value);
  122. }
  123. return error;
  124. }
  125. //------------------------------------------------------------------------------
  126. int MLX90640_GetCurResolution(uint8_t slaveAddr)
  127. {
  128. uint16_t controlRegister1;
  129. int resolutionRAM;
  130. int error;
  131. error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1);
  132. if(error != 0)
  133. {
  134. return error;
  135. }
  136. resolutionRAM = (controlRegister1 & 0x0C00) >> 10;
  137. return resolutionRAM;
  138. }
  139. //------------------------------------------------------------------------------
  140. int MLX90640_SetRefreshRate(uint8_t slaveAddr, uint8_t refreshRate)
  141. {
  142. uint16_t controlRegister1;
  143. int value;
  144. int error;
  145. value = (refreshRate & 0x07)<<7;
  146. error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1);
  147. if(error == 0)
  148. {
  149. value = (controlRegister1 & 0xFC7F) | value;
  150. error = MLX90640_I2CWrite(slaveAddr, 0x800D, value);
  151. }
  152. return error;
  153. }
  154. //------------------------------------------------------------------------------
  155. int MLX90640_GetRefreshRate(uint8_t slaveAddr)
  156. {
  157. uint16_t controlRegister1;
  158. int refreshRate;
  159. int error;
  160. error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1);
  161. if(error != 0)
  162. {
  163. return error;
  164. }
  165. refreshRate = (controlRegister1 & 0x0380) >> 7;
  166. return refreshRate;
  167. }
  168. //------------------------------------------------------------------------------
  169. int MLX90640_SetInterleavedMode(uint8_t slaveAddr)
  170. {
  171. uint16_t controlRegister1;
  172. int value;
  173. int error;
  174. error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1);
  175. if(error == 0)
  176. {
  177. value = (controlRegister1 & 0xEFFF);
  178. error = MLX90640_I2CWrite(slaveAddr, 0x800D, value);
  179. }
  180. return error;
  181. }
  182. //------------------------------------------------------------------------------
  183. int MLX90640_SetChessMode(uint8_t slaveAddr)
  184. {
  185. uint16_t controlRegister1;
  186. int value;
  187. int error;
  188. error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1);
  189. if(error == 0)
  190. {
  191. value = (controlRegister1 | 0x1000);
  192. error = MLX90640_I2CWrite(slaveAddr, 0x800D, value);
  193. }
  194. return error;
  195. }
  196. //------------------------------------------------------------------------------
  197. int MLX90640_GetCurMode(uint8_t slaveAddr)
  198. {
  199. uint16_t controlRegister1;
  200. int modeRAM;
  201. int error;
  202. error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1);
  203. if(error != 0)
  204. {
  205. return error;
  206. }
  207. modeRAM = (controlRegister1 & 0x1000) >> 12;
  208. return modeRAM;
  209. }
  210. //------------------------------------------------------------------------------
  211. void MLX90640_CalculateTo(uint16_t *frameData, const paramsMLX90640 *params, float emissivity, float tr, float *result)
  212. {
  213. float vdd;
  214. float ta;
  215. float ta4;
  216. float tr4;
  217. float taTr;
  218. float gain;
  219. float irDataCP[2];
  220. float irData;
  221. float alphaCompensated;
  222. uint8_t mode;
  223. int8_t ilPattern;
  224. int8_t chessPattern;
  225. int8_t pattern;
  226. int8_t conversionPattern;
  227. float Sx;
  228. float To;
  229. float alphaCorrR[4];
  230. int8_t range;
  231. uint16_t subPage;
  232. float ktaScale;
  233. float kvScale;
  234. float alphaScale;
  235. float kta;
  236. float kv;
  237. subPage = frameData[833];
  238. vdd = MLX90640_GetVdd(frameData, params);
  239. ta = MLX90640_GetTa(frameData, params);
  240. ta4 = (ta + 273.15);
  241. ta4 = ta4 * ta4;
  242. ta4 = ta4 * ta4;
  243. tr4 = (tr + 273.15);
  244. tr4 = tr4 * tr4;
  245. tr4 = tr4 * tr4;
  246. taTr = tr4 - (tr4-ta4)/emissivity;
  247. ktaScale = pow(2,(double)params->ktaScale);
  248. kvScale = pow(2,(double)params->kvScale);
  249. alphaScale = pow(2,(double)params->alphaScale);
  250. alphaCorrR[0] = 1 / (1 + params->ksTo[0] * 40);
  251. alphaCorrR[1] = 1 ;
  252. alphaCorrR[2] = (1 + params->ksTo[1] * params->ct[2]);
  253. alphaCorrR[3] = alphaCorrR[2] * (1 + params->ksTo[2] * (params->ct[3] - params->ct[2]));
  254. //------------------------- Gain calculation -----------------------------------
  255. gain = frameData[778];
  256. if(gain > 32767)
  257. {
  258. gain = gain - 65536;
  259. }
  260. gain = params->gainEE / gain;
  261. //------------------------- To calculation -------------------------------------
  262. mode = (frameData[832] & 0x1000) >> 5;
  263. irDataCP[0] = frameData[776];
  264. irDataCP[1] = frameData[808];
  265. for( int i = 0; i < 2; i++)
  266. {
  267. if(irDataCP[i] > 32767)
  268. {
  269. irDataCP[i] = irDataCP[i] - 65536;
  270. }
  271. irDataCP[i] = irDataCP[i] * gain;
  272. }
  273. irDataCP[0] = irDataCP[0] - params->cpOffset[0] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3));
  274. if( mode == params->calibrationModeEE)
  275. {
  276. irDataCP[1] = irDataCP[1] - params->cpOffset[1] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3));
  277. }
  278. else
  279. {
  280. irDataCP[1] = irDataCP[1] - (params->cpOffset[1] + params->ilChessC[0]) * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3));
  281. }
  282. for( int pixelNumber = 0; pixelNumber < 768; pixelNumber++)
  283. {
  284. ilPattern = pixelNumber / 32 - (pixelNumber / 64) * 2;
  285. chessPattern = ilPattern ^ (pixelNumber - (pixelNumber/2)*2);
  286. conversionPattern = ((pixelNumber + 2) / 4 - (pixelNumber + 3) / 4 + (pixelNumber + 1) / 4 - pixelNumber / 4) * (1 - 2 * ilPattern);
  287. if(mode == 0)
  288. {
  289. pattern = ilPattern;
  290. }
  291. else
  292. {
  293. pattern = chessPattern;
  294. }
  295. if(pattern == frameData[833])
  296. {
  297. irData = frameData[pixelNumber];
  298. if(irData > 32767)
  299. {
  300. irData = irData - 65536;
  301. }
  302. irData = irData * gain;
  303. kta = params->kta[pixelNumber]/ktaScale;
  304. kv = params->kv[pixelNumber]/kvScale;
  305. irData = irData - params->offset[pixelNumber]*(1 + kta*(ta - 25))*(1 + kv*(vdd - 3.3));
  306. if(mode != params->calibrationModeEE)
  307. {
  308. irData = irData + params->ilChessC[2] * (2 * ilPattern - 1) - params->ilChessC[1] * conversionPattern;
  309. }
  310. irData = irData - params->tgc * irDataCP[subPage];
  311. irData = irData / emissivity;
  312. alphaCompensated = SCALEALPHA*alphaScale/params->alpha[pixelNumber];
  313. alphaCompensated = alphaCompensated*(1 + params->KsTa * (ta - 25));
  314. Sx = alphaCompensated * alphaCompensated * alphaCompensated * (irData + alphaCompensated * taTr);
  315. Sx = sqrt(sqrt(Sx)) * params->ksTo[1];
  316. To = sqrt(sqrt(irData/(alphaCompensated * (1 - params->ksTo[1] * 273.15) + Sx) + taTr)) - 273.15;
  317. if(To < params->ct[1])
  318. {
  319. range = 0;
  320. }
  321. else if(To < params->ct[2])
  322. {
  323. range = 1;
  324. }
  325. else if(To < params->ct[3])
  326. {
  327. range = 2;
  328. }
  329. else
  330. {
  331. range = 3;
  332. }
  333. To = sqrt(sqrt(irData / (alphaCompensated * alphaCorrR[range] * (1 + params->ksTo[range] * (To - params->ct[range]))) + taTr)) - 273.15;
  334. result[pixelNumber] = To;
  335. }
  336. }
  337. }
  338. //------------------------------------------------------------------------------
  339. void MLX90640_GetImage(uint16_t *frameData, const paramsMLX90640 *params, float *result)
  340. {
  341. float vdd;
  342. float ta;
  343. float gain;
  344. float irDataCP[2];
  345. float irData;
  346. float alphaCompensated;
  347. uint8_t mode;
  348. int8_t ilPattern;
  349. int8_t chessPattern;
  350. int8_t pattern;
  351. int8_t conversionPattern;
  352. float image;
  353. uint16_t subPage;
  354. float ktaScale;
  355. float kvScale;
  356. float kta;
  357. float kv;
  358. subPage = frameData[833];
  359. vdd = MLX90640_GetVdd(frameData, params);
  360. ta = MLX90640_GetTa(frameData, params);
  361. ktaScale = pow(2,(double)params->ktaScale);
  362. kvScale = pow(2,(double)params->kvScale);
  363. //------------------------- Gain calculation -----------------------------------
  364. gain = frameData[778];
  365. if(gain > 32767)
  366. {
  367. gain = gain - 65536;
  368. }
  369. gain = params->gainEE / gain;
  370. //------------------------- Image calculation -------------------------------------
  371. mode = (frameData[832] & 0x1000) >> 5;
  372. irDataCP[0] = frameData[776];
  373. irDataCP[1] = frameData[808];
  374. for( int i = 0; i < 2; i++)
  375. {
  376. if(irDataCP[i] > 32767)
  377. {
  378. irDataCP[i] = irDataCP[i] - 65536;
  379. }
  380. irDataCP[i] = irDataCP[i] * gain;
  381. }
  382. irDataCP[0] = irDataCP[0] - params->cpOffset[0] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3));
  383. if( mode == params->calibrationModeEE)
  384. {
  385. irDataCP[1] = irDataCP[1] - params->cpOffset[1] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3));
  386. }
  387. else
  388. {
  389. irDataCP[1] = irDataCP[1] - (params->cpOffset[1] + params->ilChessC[0]) * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3));
  390. }
  391. for( int pixelNumber = 0; pixelNumber < 768; pixelNumber++)
  392. {
  393. ilPattern = pixelNumber / 32 - (pixelNumber / 64) * 2;
  394. chessPattern = ilPattern ^ (pixelNumber - (pixelNumber/2)*2);
  395. conversionPattern = ((pixelNumber + 2) / 4 - (pixelNumber + 3) / 4 + (pixelNumber + 1) / 4 - pixelNumber / 4) * (1 - 2 * ilPattern);
  396. if(mode == 0)
  397. {
  398. pattern = ilPattern;
  399. }
  400. else
  401. {
  402. pattern = chessPattern;
  403. }
  404. if(pattern == frameData[833])
  405. {
  406. irData = frameData[pixelNumber];
  407. if(irData > 32767)
  408. {
  409. irData = irData - 65536;
  410. }
  411. irData = irData * gain;
  412. kta = params->kta[pixelNumber]/ktaScale;
  413. kv = params->kv[pixelNumber]/kvScale;
  414. irData = irData - params->offset[pixelNumber]*(1 + kta*(ta - 25))*(1 + kv*(vdd - 3.3));
  415. if(mode != params->calibrationModeEE)
  416. {
  417. irData = irData + params->ilChessC[2] * (2 * ilPattern - 1) - params->ilChessC[1] * conversionPattern;
  418. }
  419. irData = irData - params->tgc * irDataCP[subPage];
  420. alphaCompensated = params->alpha[pixelNumber];
  421. image = irData*alphaCompensated;
  422. result[pixelNumber] = image;
  423. }
  424. }
  425. }
  426. //------------------------------------------------------------------------------
  427. float MLX90640_GetVdd(uint16_t *frameData, const paramsMLX90640 *params)
  428. {
  429. float vdd;
  430. float resolutionCorrection;
  431. int resolutionRAM;
  432. vdd = frameData[810];
  433. if(vdd > 32767)
  434. {
  435. vdd = vdd - 65536;
  436. }
  437. resolutionRAM = (frameData[832] & 0x0C00) >> 10;
  438. resolutionCorrection = pow(2, (double)params->resolutionEE) / pow(2, (double)resolutionRAM);
  439. vdd = (resolutionCorrection * vdd - params->vdd25) / params->kVdd + 3.3;
  440. return vdd;
  441. }
  442. //------------------------------------------------------------------------------
  443. float MLX90640_GetTa(uint16_t *frameData, const paramsMLX90640 *params)
  444. {
  445. float ptat;
  446. float ptatArt;
  447. float vdd;
  448. float ta;
  449. vdd = MLX90640_GetVdd(frameData, params);
  450. ptat = frameData[800];
  451. if(ptat > 32767)
  452. {
  453. ptat = ptat - 65536;
  454. }
  455. ptatArt = frameData[768];
  456. if(ptatArt > 32767)
  457. {
  458. ptatArt = ptatArt - 65536;
  459. }
  460. ptatArt = (ptat / (ptat * params->alphaPTAT + ptatArt)) * pow(2, (double)18);
  461. ta = (ptatArt / (1 + params->KvPTAT * (vdd - 3.3)) - params->vPTAT25);
  462. ta = ta / params->KtPTAT + 25;
  463. return ta;
  464. }
  465. //------------------------------------------------------------------------------
  466. int MLX90640_GetSubPageNumber(uint16_t *frameData)
  467. {
  468. return frameData[833];
  469. }
  470. //------------------------------------------------------------------------------
  471. void MLX90640_BadPixelsCorrection(uint16_t *pixels, float *to, int mode, paramsMLX90640 *params)
  472. {
  473. float ap[4];
  474. uint8_t pix;
  475. uint8_t line;
  476. uint8_t column;
  477. pix = 0;
  478. while(pixels[pix] != 0xFFFF)
  479. {
  480. line = pixels[pix]>>5;
  481. column = pixels[pix] - (line<<5);
  482. if(mode == 1)
  483. {
  484. if(line == 0)
  485. {
  486. if(column == 0)
  487. {
  488. to[pixels[pix]] = to[33];
  489. }
  490. else if(column == 31)
  491. {
  492. to[pixels[pix]] = to[62];
  493. }
  494. else
  495. {
  496. to[pixels[pix]] = (to[pixels[pix]+31] + to[pixels[pix]+33])/2.0;
  497. }
  498. }
  499. else if(line == 23)
  500. {
  501. if(column == 0)
  502. {
  503. to[pixels[pix]] = to[705];
  504. }
  505. else if(column == 31)
  506. {
  507. to[pixels[pix]] = to[734];
  508. }
  509. else
  510. {
  511. to[pixels[pix]] = (to[pixels[pix]-33] + to[pixels[pix]-31])/2.0;
  512. }
  513. }
  514. else if(column == 0)
  515. {
  516. to[pixels[pix]] = (to[pixels[pix]-31] + to[pixels[pix]+33])/2.0;
  517. }
  518. else if(column == 31)
  519. {
  520. to[pixels[pix]] = (to[pixels[pix]-33] + to[pixels[pix]+31])/2.0;
  521. }
  522. else
  523. {
  524. ap[0] = to[pixels[pix]-33];
  525. ap[1] = to[pixels[pix]-31];
  526. ap[2] = to[pixels[pix]+31];
  527. ap[3] = to[pixels[pix]+33];
  528. to[pixels[pix]] = GetMedian(ap,4);
  529. }
  530. }
  531. else
  532. {
  533. if(column == 0)
  534. {
  535. to[pixels[pix]] = to[pixels[pix]+1];
  536. }
  537. else if(column == 1 || column == 30)
  538. {
  539. to[pixels[pix]] = (to[pixels[pix]-1]+to[pixels[pix]+1])/2.0;
  540. }
  541. else if(column == 31)
  542. {
  543. to[pixels[pix]] = to[pixels[pix]-1];
  544. }
  545. else
  546. {
  547. if(IsPixelBad(pixels[pix]-2,params) == 0 && IsPixelBad(pixels[pix]+2,params) == 0)
  548. {
  549. ap[0] = to[pixels[pix]+1] - to[pixels[pix]+2];
  550. ap[1] = to[pixels[pix]-1] - to[pixels[pix]-2];
  551. if(fabs(ap[0]) > fabs(ap[1]))
  552. {
  553. to[pixels[pix]] = to[pixels[pix]-1] + ap[1];
  554. }
  555. else
  556. {
  557. to[pixels[pix]] = to[pixels[pix]+1] + ap[0];
  558. }
  559. }
  560. else
  561. {
  562. to[pixels[pix]] = (to[pixels[pix]-1]+to[pixels[pix]+1])/2.0;
  563. }
  564. }
  565. }
  566. pix = pix + 1;
  567. }
  568. }
  569. //------------------------------------------------------------------------------
  570. void ExtractVDDParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  571. {
  572. int16_t kVdd;
  573. int16_t vdd25;
  574. kVdd = eeData[51];
  575. kVdd = (eeData[51] & 0xFF00) >> 8;
  576. if(kVdd > 127)
  577. {
  578. kVdd = kVdd - 256;
  579. }
  580. kVdd = 32 * kVdd;
  581. vdd25 = eeData[51] & 0x00FF;
  582. vdd25 = ((vdd25 - 256) << 5) - 8192;
  583. mlx90640->kVdd = kVdd;
  584. mlx90640->vdd25 = vdd25;
  585. }
  586. //------------------------------------------------------------------------------
  587. void ExtractPTATParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  588. {
  589. float KvPTAT;
  590. float KtPTAT;
  591. int16_t vPTAT25;
  592. float alphaPTAT;
  593. KvPTAT = (eeData[50] & 0xFC00) >> 10;
  594. if(KvPTAT > 31)
  595. {
  596. KvPTAT = KvPTAT - 64;
  597. }
  598. KvPTAT = KvPTAT/4096;
  599. KtPTAT = eeData[50] & 0x03FF;
  600. if(KtPTAT > 511)
  601. {
  602. KtPTAT = KtPTAT - 1024;
  603. }
  604. KtPTAT = KtPTAT/8;
  605. vPTAT25 = eeData[49];
  606. alphaPTAT = (eeData[16] & 0xF000) / pow(2, (double)14) + 8.0f;
  607. mlx90640->KvPTAT = KvPTAT;
  608. mlx90640->KtPTAT = KtPTAT;
  609. mlx90640->vPTAT25 = vPTAT25;
  610. mlx90640->alphaPTAT = alphaPTAT;
  611. }
  612. //------------------------------------------------------------------------------
  613. void ExtractGainParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  614. {
  615. int16_t gainEE;
  616. gainEE = eeData[48];
  617. if(gainEE > 32767)
  618. {
  619. gainEE = gainEE -65536;
  620. }
  621. mlx90640->gainEE = gainEE;
  622. }
  623. //------------------------------------------------------------------------------
  624. void ExtractTgcParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  625. {
  626. float tgc;
  627. tgc = eeData[60] & 0x00FF;
  628. if(tgc > 127)
  629. {
  630. tgc = tgc - 256;
  631. }
  632. tgc = tgc / 32.0f;
  633. mlx90640->tgc = tgc;
  634. }
  635. //------------------------------------------------------------------------------
  636. void ExtractResolutionParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  637. {
  638. uint8_t resolutionEE;
  639. resolutionEE = (eeData[56] & 0x3000) >> 12;
  640. mlx90640->resolutionEE = resolutionEE;
  641. }
  642. //------------------------------------------------------------------------------
  643. void ExtractKsTaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  644. {
  645. float KsTa;
  646. KsTa = (eeData[60] & 0xFF00) >> 8;
  647. if(KsTa > 127)
  648. {
  649. KsTa = KsTa -256;
  650. }
  651. KsTa = KsTa / 8192.0f;
  652. mlx90640->KsTa = KsTa;
  653. }
  654. //------------------------------------------------------------------------------
  655. void ExtractKsToParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  656. {
  657. int KsToScale;
  658. int8_t step;
  659. step = ((eeData[63] & 0x3000) >> 12) * 10;
  660. mlx90640->ct[0] = -40;
  661. mlx90640->ct[1] = 0;
  662. mlx90640->ct[2] = (eeData[63] & 0x00F0) >> 4;
  663. mlx90640->ct[3] = (eeData[63] & 0x0F00) >> 8;
  664. mlx90640->ct[2] = mlx90640->ct[2]*step;
  665. mlx90640->ct[3] = mlx90640->ct[2] + mlx90640->ct[3]*step;
  666. mlx90640->ct[4] = 400;
  667. KsToScale = (eeData[63] & 0x000F) + 8;
  668. KsToScale = 1 << KsToScale;
  669. mlx90640->ksTo[0] = eeData[61] & 0x00FF;
  670. mlx90640->ksTo[1] = (eeData[61] & 0xFF00) >> 8;
  671. mlx90640->ksTo[2] = eeData[62] & 0x00FF;
  672. mlx90640->ksTo[3] = (eeData[62] & 0xFF00) >> 8;
  673. for(int i = 0; i < 4; i++)
  674. {
  675. if(mlx90640->ksTo[i] > 127)
  676. {
  677. mlx90640->ksTo[i] = mlx90640->ksTo[i] - 256;
  678. }
  679. mlx90640->ksTo[i] = mlx90640->ksTo[i] / KsToScale;
  680. }
  681. mlx90640->ksTo[4] = -0.0002;
  682. }
  683. //------------------------------------------------------------------------------
  684. void ExtractAlphaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  685. {
  686. int accRow[24];
  687. int accColumn[32];
  688. int p = 0;
  689. int alphaRef;
  690. uint8_t alphaScale;
  691. uint8_t accRowScale;
  692. uint8_t accColumnScale;
  693. uint8_t accRemScale;
  694. float alphaTemp[768];
  695. float temp;
  696. accRemScale = eeData[32] & 0x000F;
  697. accColumnScale = (eeData[32] & 0x00F0) >> 4;
  698. accRowScale = (eeData[32] & 0x0F00) >> 8;
  699. alphaScale = ((eeData[32] & 0xF000) >> 12) + 30;
  700. alphaRef = eeData[33];
  701. for(int i = 0; i < 6; i++)
  702. {
  703. p = i * 4;
  704. accRow[p + 0] = (eeData[34 + i] & 0x000F);
  705. accRow[p + 1] = (eeData[34 + i] & 0x00F0) >> 4;
  706. accRow[p + 2] = (eeData[34 + i] & 0x0F00) >> 8;
  707. accRow[p + 3] = (eeData[34 + i] & 0xF000) >> 12;
  708. }
  709. for(int i = 0; i < 24; i++)
  710. {
  711. if (accRow[i] > 7)
  712. {
  713. accRow[i] = accRow[i] - 16;
  714. }
  715. }
  716. for(int i = 0; i < 8; i++)
  717. {
  718. p = i * 4;
  719. accColumn[p + 0] = (eeData[40 + i] & 0x000F);
  720. accColumn[p + 1] = (eeData[40 + i] & 0x00F0) >> 4;
  721. accColumn[p + 2] = (eeData[40 + i] & 0x0F00) >> 8;
  722. accColumn[p + 3] = (eeData[40 + i] & 0xF000) >> 12;
  723. }
  724. for(int i = 0; i < 32; i ++)
  725. {
  726. if (accColumn[i] > 7)
  727. {
  728. accColumn[i] = accColumn[i] - 16;
  729. }
  730. }
  731. for(int i = 0; i < 24; i++)
  732. {
  733. for(int j = 0; j < 32; j ++)
  734. {
  735. p = 32 * i +j;
  736. alphaTemp[p] = (eeData[64 + p] & 0x03F0) >> 4;
  737. if (alphaTemp[p] > 31)
  738. {
  739. alphaTemp[p] = alphaTemp[p] - 64;
  740. }
  741. alphaTemp[p] = alphaTemp[p]*(1 << accRemScale);
  742. alphaTemp[p] = (alphaRef + (accRow[i] << accRowScale) + (accColumn[j] << accColumnScale) + alphaTemp[p]);
  743. alphaTemp[p] = alphaTemp[p] / pow(2,(double)alphaScale);
  744. alphaTemp[p] = alphaTemp[p] - mlx90640->tgc * (mlx90640->cpAlpha[0] + mlx90640->cpAlpha[1])/2;
  745. alphaTemp[p] = SCALEALPHA/alphaTemp[p];
  746. }
  747. }
  748. temp = alphaTemp[0];
  749. for(int i = 1; i < 768; i++)
  750. {
  751. if (alphaTemp[i] > temp)
  752. {
  753. temp = alphaTemp[i];
  754. }
  755. }
  756. alphaScale = 0;
  757. while(temp < 32768)
  758. {
  759. temp = temp*2;
  760. alphaScale = alphaScale + 1;
  761. }
  762. for(int i = 0; i < 768; i++)
  763. {
  764. temp = alphaTemp[i] * pow(2,(double)alphaScale);
  765. mlx90640->alpha[i] = (temp + 0.5);
  766. }
  767. mlx90640->alphaScale = alphaScale;
  768. }
  769. //------------------------------------------------------------------------------
  770. void ExtractOffsetParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  771. {
  772. int occRow[24];
  773. int occColumn[32];
  774. int p = 0;
  775. int16_t offsetRef;
  776. uint8_t occRowScale;
  777. uint8_t occColumnScale;
  778. uint8_t occRemScale;
  779. occRemScale = (eeData[16] & 0x000F);
  780. occColumnScale = (eeData[16] & 0x00F0) >> 4;
  781. occRowScale = (eeData[16] & 0x0F00) >> 8;
  782. offsetRef = eeData[17];
  783. if (offsetRef > 32767)
  784. {
  785. offsetRef = offsetRef - 65536;
  786. }
  787. for(int i = 0; i < 6; i++)
  788. {
  789. p = i * 4;
  790. occRow[p + 0] = (eeData[18 + i] & 0x000F);
  791. occRow[p + 1] = (eeData[18 + i] & 0x00F0) >> 4;
  792. occRow[p + 2] = (eeData[18 + i] & 0x0F00) >> 8;
  793. occRow[p + 3] = (eeData[18 + i] & 0xF000) >> 12;
  794. }
  795. for(int i = 0; i < 24; i++)
  796. {
  797. if (occRow[i] > 7)
  798. {
  799. occRow[i] = occRow[i] - 16;
  800. }
  801. }
  802. for(int i = 0; i < 8; i++)
  803. {
  804. p = i * 4;
  805. occColumn[p + 0] = (eeData[24 + i] & 0x000F);
  806. occColumn[p + 1] = (eeData[24 + i] & 0x00F0) >> 4;
  807. occColumn[p + 2] = (eeData[24 + i] & 0x0F00) >> 8;
  808. occColumn[p + 3] = (eeData[24 + i] & 0xF000) >> 12;
  809. }
  810. for(int i = 0; i < 32; i ++)
  811. {
  812. if (occColumn[i] > 7)
  813. {
  814. occColumn[i] = occColumn[i] - 16;
  815. }
  816. }
  817. for(int i = 0; i < 24; i++)
  818. {
  819. for(int j = 0; j < 32; j ++)
  820. {
  821. p = 32 * i +j;
  822. mlx90640->offset[p] = (eeData[64 + p] & 0xFC00) >> 10;
  823. if (mlx90640->offset[p] > 31)
  824. {
  825. mlx90640->offset[p] = mlx90640->offset[p] - 64;
  826. }
  827. mlx90640->offset[p] = mlx90640->offset[p]*(1 << occRemScale);
  828. mlx90640->offset[p] = (offsetRef + (occRow[i] << occRowScale) + (occColumn[j] << occColumnScale) + mlx90640->offset[p]);
  829. }
  830. }
  831. }
  832. //------------------------------------------------------------------------------
  833. void ExtractKtaPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  834. {
  835. int p = 0;
  836. int8_t KtaRC[4];
  837. int8_t KtaRoCo;
  838. int8_t KtaRoCe;
  839. int8_t KtaReCo;
  840. int8_t KtaReCe;
  841. uint8_t ktaScale1;
  842. uint8_t ktaScale2;
  843. uint8_t split;
  844. float ktaTemp[768];
  845. float temp;
  846. KtaRoCo = (eeData[54] & 0xFF00) >> 8;
  847. if (KtaRoCo > 127)
  848. {
  849. KtaRoCo = KtaRoCo - 256;
  850. }
  851. KtaRC[0] = KtaRoCo;
  852. KtaReCo = (eeData[54] & 0x00FF);
  853. if (KtaReCo > 127)
  854. {
  855. KtaReCo = KtaReCo - 256;
  856. }
  857. KtaRC[2] = KtaReCo;
  858. KtaRoCe = (eeData[55] & 0xFF00) >> 8;
  859. if (KtaRoCe > 127)
  860. {
  861. KtaRoCe = KtaRoCe - 256;
  862. }
  863. KtaRC[1] = KtaRoCe;
  864. KtaReCe = (eeData[55] & 0x00FF);
  865. if (KtaReCe > 127)
  866. {
  867. KtaReCe = KtaReCe - 256;
  868. }
  869. KtaRC[3] = KtaReCe;
  870. ktaScale1 = ((eeData[56] & 0x00F0) >> 4) + 8;
  871. ktaScale2 = (eeData[56] & 0x000F);
  872. for(int i = 0; i < 24; i++)
  873. {
  874. for(int j = 0; j < 32; j ++)
  875. {
  876. p = 32 * i +j;
  877. split = 2*(p/32 - (p/64)*2) + p%2;
  878. ktaTemp[p] = (eeData[64 + p] & 0x000E) >> 1;
  879. if (ktaTemp[p] > 3)
  880. {
  881. ktaTemp[p] = ktaTemp[p] - 8;
  882. }
  883. ktaTemp[p] = ktaTemp[p] * (1 << ktaScale2);
  884. ktaTemp[p] = KtaRC[split] + ktaTemp[p];
  885. ktaTemp[p] = ktaTemp[p] / pow(2,(double)ktaScale1);
  886. //ktaTemp[p] = ktaTemp[p] * mlx90640->offset[p];
  887. }
  888. }
  889. temp = fabs(ktaTemp[0]);
  890. for(int i = 1; i < 768; i++)
  891. {
  892. if (fabs(ktaTemp[i]) > temp)
  893. {
  894. temp = fabs(ktaTemp[i]);
  895. }
  896. }
  897. ktaScale1 = 0;
  898. while(temp < 64)
  899. {
  900. temp = temp*2;
  901. ktaScale1 = ktaScale1 + 1;
  902. }
  903. for(int i = 0; i < 768; i++)
  904. {
  905. temp = ktaTemp[i] * pow(2,(double)ktaScale1);
  906. if (temp < 0)
  907. {
  908. mlx90640->kta[i] = (temp - 0.5);
  909. }
  910. else
  911. {
  912. mlx90640->kta[i] = (temp + 0.5);
  913. }
  914. }
  915. mlx90640->ktaScale = ktaScale1;
  916. }
  917. //------------------------------------------------------------------------------
  918. void ExtractKvPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  919. {
  920. int p = 0;
  921. int8_t KvT[4];
  922. int8_t KvRoCo;
  923. int8_t KvRoCe;
  924. int8_t KvReCo;
  925. int8_t KvReCe;
  926. uint8_t kvScale;
  927. uint8_t split;
  928. float kvTemp[768];
  929. float temp;
  930. KvRoCo = (eeData[52] & 0xF000) >> 12;
  931. if (KvRoCo > 7)
  932. {
  933. KvRoCo = KvRoCo - 16;
  934. }
  935. KvT[0] = KvRoCo;
  936. KvReCo = (eeData[52] & 0x0F00) >> 8;
  937. if (KvReCo > 7)
  938. {
  939. KvReCo = KvReCo - 16;
  940. }
  941. KvT[2] = KvReCo;
  942. KvRoCe = (eeData[52] & 0x00F0) >> 4;
  943. if (KvRoCe > 7)
  944. {
  945. KvRoCe = KvRoCe - 16;
  946. }
  947. KvT[1] = KvRoCe;
  948. KvReCe = (eeData[52] & 0x000F);
  949. if (KvReCe > 7)
  950. {
  951. KvReCe = KvReCe - 16;
  952. }
  953. KvT[3] = KvReCe;
  954. kvScale = (eeData[56] & 0x0F00) >> 8;
  955. for(int i = 0; i < 24; i++)
  956. {
  957. for(int j = 0; j < 32; j ++)
  958. {
  959. p = 32 * i +j;
  960. split = 2*(p/32 - (p/64)*2) + p%2;
  961. kvTemp[p] = KvT[split];
  962. kvTemp[p] = kvTemp[p] / pow(2,(double)kvScale);
  963. //kvTemp[p] = kvTemp[p] * mlx90640->offset[p];
  964. }
  965. }
  966. temp = fabs(kvTemp[0]);
  967. for(int i = 1; i < 768; i++)
  968. {
  969. if (fabs(kvTemp[i]) > temp)
  970. {
  971. temp = fabs(kvTemp[i]);
  972. }
  973. }
  974. kvScale = 0;
  975. while(temp < 64)
  976. {
  977. temp = temp*2;
  978. kvScale = kvScale + 1;
  979. }
  980. for(int i = 0; i < 768; i++)
  981. {
  982. temp = kvTemp[i] * pow(2,(double)kvScale);
  983. if (temp < 0)
  984. {
  985. mlx90640->kv[i] = (temp - 0.5);
  986. }
  987. else
  988. {
  989. mlx90640->kv[i] = (temp + 0.5);
  990. }
  991. }
  992. mlx90640->kvScale = kvScale;
  993. }
  994. //------------------------------------------------------------------------------
  995. void ExtractCPParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  996. {
  997. float alphaSP[2];
  998. int16_t offsetSP[2];
  999. float cpKv;
  1000. float cpKta;
  1001. uint8_t alphaScale;
  1002. uint8_t ktaScale1;
  1003. uint8_t kvScale;
  1004. alphaScale = ((eeData[32] & 0xF000) >> 12) + 27;
  1005. offsetSP[0] = (eeData[58] & 0x03FF);
  1006. if (offsetSP[0] > 511)
  1007. {
  1008. offsetSP[0] = offsetSP[0] - 1024;
  1009. }
  1010. offsetSP[1] = (eeData[58] & 0xFC00) >> 10;
  1011. if (offsetSP[1] > 31)
  1012. {
  1013. offsetSP[1] = offsetSP[1] - 64;
  1014. }
  1015. offsetSP[1] = offsetSP[1] + offsetSP[0];
  1016. alphaSP[0] = (eeData[57] & 0x03FF);
  1017. if (alphaSP[0] > 511)
  1018. {
  1019. alphaSP[0] = alphaSP[0] - 1024;
  1020. }
  1021. alphaSP[0] = alphaSP[0] / pow(2,(double)alphaScale);
  1022. alphaSP[1] = (eeData[57] & 0xFC00) >> 10;
  1023. if (alphaSP[1] > 31)
  1024. {
  1025. alphaSP[1] = alphaSP[1] - 64;
  1026. }
  1027. alphaSP[1] = (1 + alphaSP[1]/128) * alphaSP[0];
  1028. cpKta = (eeData[59] & 0x00FF);
  1029. if (cpKta > 127)
  1030. {
  1031. cpKta = cpKta - 256;
  1032. }
  1033. ktaScale1 = ((eeData[56] & 0x00F0) >> 4) + 8;
  1034. mlx90640->cpKta = cpKta / pow(2,(double)ktaScale1);
  1035. cpKv = (eeData[59] & 0xFF00) >> 8;
  1036. if (cpKv > 127)
  1037. {
  1038. cpKv = cpKv - 256;
  1039. }
  1040. kvScale = (eeData[56] & 0x0F00) >> 8;
  1041. mlx90640->cpKv = cpKv / pow(2,(double)kvScale);
  1042. mlx90640->cpAlpha[0] = alphaSP[0];
  1043. mlx90640->cpAlpha[1] = alphaSP[1];
  1044. mlx90640->cpOffset[0] = offsetSP[0];
  1045. mlx90640->cpOffset[1] = offsetSP[1];
  1046. }
  1047. //------------------------------------------------------------------------------
  1048. void ExtractCILCParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
  1049. {
  1050. float ilChessC[3];
  1051. uint8_t calibrationModeEE;
  1052. calibrationModeEE = (eeData[10] & 0x0800) >> 4;
  1053. calibrationModeEE = calibrationModeEE ^ 0x80;
  1054. ilChessC[0] = (eeData[53] & 0x003F);
  1055. if (ilChessC[0] > 31)
  1056. {
  1057. ilChessC[0] = ilChessC[0] - 64;
  1058. }
  1059. ilChessC[0] = ilChessC[0] / 16.0f;
  1060. ilChessC[1] = (eeData[53] & 0x07C0) >> 6;
  1061. if (ilChessC[1] > 15)
  1062. {
  1063. ilChessC[1] = ilChessC[1] - 32;
  1064. }
  1065. ilChessC[1] = ilChessC[1] / 2.0f;
  1066. ilChessC[2] = (eeData[53] & 0xF800) >> 11;
  1067. if (ilChessC[2] > 15)
  1068. {
  1069. ilChessC[2] = ilChessC[2] - 32;
  1070. }
  1071. ilChessC[2] = ilChessC[2] / 8.0f;
  1072. mlx90640->calibrationModeEE = calibrationModeEE;
  1073. mlx90640->ilChessC[0] = ilChessC[0];
  1074. mlx90640->ilChessC[1] = ilChessC[1];
  1075. mlx90640->ilChessC[2] = ilChessC[2];
  1076. }
  1077. //------------------------------------------------------------------------------
  1078. int ExtractDeviatingPixels(uint16_t *eeData, paramsMLX90640 *mlx90640)
  1079. {
  1080. uint16_t pixCnt = 0;
  1081. uint16_t brokenPixCnt = 0;
  1082. uint16_t outlierPixCnt = 0;
  1083. int warn = 0;
  1084. int i;
  1085. for(pixCnt = 0; pixCnt<5; pixCnt++)
  1086. {
  1087. mlx90640->brokenPixels[pixCnt] = 0xFFFF;
  1088. mlx90640->outlierPixels[pixCnt] = 0xFFFF;
  1089. }
  1090. pixCnt = 0;
  1091. while (pixCnt < 768 && brokenPixCnt < 5 && outlierPixCnt < 5)
  1092. {
  1093. if(eeData[pixCnt+64] == 0)
  1094. {
  1095. mlx90640->brokenPixels[brokenPixCnt] = pixCnt;
  1096. brokenPixCnt = brokenPixCnt + 1;
  1097. }
  1098. else if((eeData[pixCnt+64] & 0x0001) != 0)
  1099. {
  1100. mlx90640->outlierPixels[outlierPixCnt] = pixCnt;
  1101. outlierPixCnt = outlierPixCnt + 1;
  1102. }
  1103. pixCnt = pixCnt + 1;
  1104. }
  1105. if(brokenPixCnt > 4)
  1106. {
  1107. warn = -3;
  1108. }
  1109. else if(outlierPixCnt > 4)
  1110. {
  1111. warn = -4;
  1112. }
  1113. else if((brokenPixCnt + outlierPixCnt) > 4)
  1114. {
  1115. warn = -5;
  1116. }
  1117. else
  1118. {
  1119. for(pixCnt=0; pixCnt<brokenPixCnt; pixCnt++)
  1120. {
  1121. for(i=pixCnt+1; i<brokenPixCnt; i++)
  1122. {
  1123. warn = CheckAdjacentPixels(mlx90640->brokenPixels[pixCnt],mlx90640->brokenPixels[i]);
  1124. if(warn != 0)
  1125. {
  1126. return warn;
  1127. }
  1128. }
  1129. }
  1130. for(pixCnt=0; pixCnt<outlierPixCnt; pixCnt++)
  1131. {
  1132. for(i=pixCnt+1; i<outlierPixCnt; i++)
  1133. {
  1134. warn = CheckAdjacentPixels(mlx90640->outlierPixels[pixCnt],mlx90640->outlierPixels[i]);
  1135. if(warn != 0)
  1136. {
  1137. return warn;
  1138. }
  1139. }
  1140. }
  1141. for(pixCnt=0; pixCnt<brokenPixCnt; pixCnt++)
  1142. {
  1143. for(i=0; i<outlierPixCnt; i++)
  1144. {
  1145. warn = CheckAdjacentPixels(mlx90640->brokenPixels[pixCnt],mlx90640->outlierPixels[i]);
  1146. if(warn != 0)
  1147. {
  1148. return warn;
  1149. }
  1150. }
  1151. }
  1152. }
  1153. return warn;
  1154. }
  1155. //------------------------------------------------------------------------------
  1156. int CheckAdjacentPixels(uint16_t pix1, uint16_t pix2)
  1157. {
  1158. int pixPosDif;
  1159. pixPosDif = pix1 - pix2;
  1160. if(pixPosDif > -34 && pixPosDif < -30)
  1161. {
  1162. return -6;
  1163. }
  1164. if(pixPosDif > -2 && pixPosDif < 2)
  1165. {
  1166. return -6;
  1167. }
  1168. if(pixPosDif > 30 && pixPosDif < 34)
  1169. {
  1170. return -6;
  1171. }
  1172. return 0;
  1173. }
  1174. //------------------------------------------------------------------------------
  1175. float GetMedian(float *values, int n)
  1176. {
  1177. float temp;
  1178. for(int i=0; i<n-1; i++)
  1179. {
  1180. for(int j=i+1; j<n; j++)
  1181. {
  1182. if(values[j] < values[i])
  1183. {
  1184. temp = values[i];
  1185. values[i] = values[j];
  1186. values[j] = temp;
  1187. }
  1188. }
  1189. }
  1190. if(n%2==0)
  1191. {
  1192. return ((values[n/2] + values[n/2 - 1]) / 2.0);
  1193. }
  1194. else
  1195. {
  1196. return values[n/2];
  1197. }
  1198. }
  1199. //------------------------------------------------------------------------------
  1200. int IsPixelBad(uint16_t pixel,paramsMLX90640 *params)
  1201. {
  1202. for(int i=0; i<5; i++)
  1203. {
  1204. if(pixel == params->outlierPixels[i] || pixel == params->brokenPixels[i])
  1205. {
  1206. return 1;
  1207. }
  1208. }
  1209. return 0;
  1210. }
  1211. //------------------------------------------------------------------------------