1
0
mirror of https://gitee.com/openLuat/LuatOS synced 2025-08-17 22:18:03 +08:00

add:添加mlx90640-library(测试版)

This commit is contained in:
Dozingfiretruck 2022-01-19 21:01:01 +08:00
parent 1b49d787f9
commit 24d4c5efc5
5 changed files with 1875 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,72 @@
/**
* @copyright (C) 2017 Melexis N.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef _MLX90640_API_H_
#define _MLX90640_API_H_
#define SCALEALPHA 0.000001
typedef struct
{
int16_t kVdd;
int16_t vdd25;
float KvPTAT;
float KtPTAT;
uint16_t vPTAT25;
float alphaPTAT;
int16_t gainEE;
float tgc;
float cpKv;
float cpKta;
uint8_t resolutionEE;
uint8_t calibrationModeEE;
float KsTa;
float ksTo[5];
int16_t ct[5];
uint16_t alpha[768];
uint8_t alphaScale;
int16_t offset[768];
int8_t kta[768];
uint8_t ktaScale;
int8_t kv[768];
uint8_t kvScale;
float cpAlpha[2];
int16_t cpOffset[2];
float ilChessC[3];
uint16_t brokenPixels[5];
uint16_t outlierPixels[5];
} paramsMLX90640;
int MLX90640_DumpEE(uint8_t slaveAddr, uint16_t *eeData);
int MLX90640_SynchFrame(uint8_t slaveAddr);
int MLX90640_TriggerMeasurement(uint8_t slaveAddr);
int MLX90640_GetFrameData(uint8_t slaveAddr, uint16_t *frameData);
int MLX90640_ExtractParameters(uint16_t *eeData, paramsMLX90640 *mlx90640);
float MLX90640_GetVdd(uint16_t *frameData, const paramsMLX90640 *params);
float MLX90640_GetTa(uint16_t *frameData, const paramsMLX90640 *params);
void MLX90640_GetImage(uint16_t *frameData, const paramsMLX90640 *params, float *result);
void MLX90640_CalculateTo(uint16_t *frameData, const paramsMLX90640 *params, float emissivity, float tr, float *result);
int MLX90640_SetResolution(uint8_t slaveAddr, uint8_t resolution);
int MLX90640_GetCurResolution(uint8_t slaveAddr);
int MLX90640_SetRefreshRate(uint8_t slaveAddr, uint8_t refreshRate);
int MLX90640_GetRefreshRate(uint8_t slaveAddr);
int MLX90640_GetSubPageNumber(uint16_t *frameData);
int MLX90640_GetCurMode(uint8_t slaveAddr);
int MLX90640_SetInterleavedMode(uint8_t slaveAddr);
int MLX90640_SetChessMode(uint8_t slaveAddr);
void MLX90640_BadPixelsCorrection(uint16_t *pixels, float *to, int mode, paramsMLX90640 *params);
#endif

View File

@ -0,0 +1,76 @@
/**
* @copyright (C) 2017 Melexis N.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "luat_i2c.h"
#include "MLX90640_I2C_Driver.h"
void MLX90640_I2CInit()
{
luat_i2c_setup(0, 1, NULL);
}
int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data)
{
int ret = 0;
int cnt = 0;
int i = 0;
char i2cData[1664] = {0};
uint16_t *p;
p = data;
char cmd[2] = {0,0};
cmd[0] = startAddress >> 8;
cmd[1] = startAddress & 0x00FF;
ret = luat_i2c_send(0, slaveAddr, cmd, 2);
if (ret != 0)return -1;
ret = luat_i2c_recv(0, slaveAddr, i2cData, 2*nMemAddressRead);
if (ret != 0)return -1;
for(cnt=0; cnt < nMemAddressRead; cnt++)
{
i = cnt << 1;
*p++ = (uint16_t)i2cData[i]*256 + (uint16_t)i2cData[i+1];
}
return 0;
}
int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data)
{
int ret = 0;
static uint16_t dataCheck;
uint8_t cmd[4];
cmd[0] = writeAddress >> 8;
cmd[1] = writeAddress & 0x00FF;
cmd[2] = data >> 8;
cmd[3] = data & 0x00FF;
ret = luat_i2c_send(0, slaveAddr, cmd, 4);
if (ret != 0)return -1;
ret = MLX90640_I2CRead(slaveAddr,writeAddress,1, &dataCheck);
if (ret != 0)return -1;
if ( dataCheck != data)
{
return -2;
}
return 0;
}

View File

@ -0,0 +1,27 @@
/**
* @copyright (C) 2017 Melexis N.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef _MLX90640_I2C_Driver_H_
#define _MLX90640_I2C_Driver_H_
#include <stdint.h>
void MLX90640_I2CInit(void);
int MLX90640_I2CGeneralReset(void);
int MLX90640_I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data);
int MLX90640_I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data);
void MLX90640_I2CFreqSet(int freq);
#endif

View File

@ -0,0 +1,128 @@
#include <MLX90640_I2C_Driver.h>
#include <MLX90640_API.h>
#include <math.h>
#include "luat_base.h"
#include "luat_lcd.h"
#define LUAT_LOG_TAG "mlx90640"
#include "luat_log.h"
static luat_lcd_conf_t* lcd_conf;
#define FPS2HZ 0x02
#define FPS4HZ 0x03
#define FPS8HZ 0x04
#define FPS16HZ 0x05
#define FPS32HZ 0x06
#define MLX90640_ADDR 0x33
#define RefreshRate FPS4HZ
#define TA_SHIFT 8 //Default shift for MLX90640 in open air
static uint16_t eeMLX90640[832];
static float mlx90640To[768];
uint16_t frame[834];
float emissivity=0.95;
int status;
const uint16_t camColors[] = {0x480F,
0x400F,0x400F,0x400F,0x4010,0x3810,0x3810,0x3810,0x3810,0x3010,0x3010,
0x3010,0x2810,0x2810,0x2810,0x2810,0x2010,0x2010,0x2010,0x1810,0x1810,
0x1811,0x1811,0x1011,0x1011,0x1011,0x0811,0x0811,0x0811,0x0011,0x0011,
0x0011,0x0011,0x0011,0x0031,0x0031,0x0051,0x0072,0x0072,0x0092,0x00B2,
0x00B2,0x00D2,0x00F2,0x00F2,0x0112,0x0132,0x0152,0x0152,0x0172,0x0192,
0x0192,0x01B2,0x01D2,0x01F3,0x01F3,0x0213,0x0233,0x0253,0x0253,0x0273,
0x0293,0x02B3,0x02D3,0x02D3,0x02F3,0x0313,0x0333,0x0333,0x0353,0x0373,
0x0394,0x03B4,0x03D4,0x03D4,0x03F4,0x0414,0x0434,0x0454,0x0474,0x0474,
0x0494,0x04B4,0x04D4,0x04F4,0x0514,0x0534,0x0534,0x0554,0x0554,0x0574,
0x0574,0x0573,0x0573,0x0573,0x0572,0x0572,0x0572,0x0571,0x0591,0x0591,
0x0590,0x0590,0x058F,0x058F,0x058F,0x058E,0x05AE,0x05AE,0x05AD,0x05AD,
0x05AD,0x05AC,0x05AC,0x05AB,0x05CB,0x05CB,0x05CA,0x05CA,0x05CA,0x05C9,
0x05C9,0x05C8,0x05E8,0x05E8,0x05E7,0x05E7,0x05E6,0x05E6,0x05E6,0x05E5,
0x05E5,0x0604,0x0604,0x0604,0x0603,0x0603,0x0602,0x0602,0x0601,0x0621,
0x0621,0x0620,0x0620,0x0620,0x0620,0x0E20,0x0E20,0x0E40,0x1640,0x1640,
0x1E40,0x1E40,0x2640,0x2640,0x2E40,0x2E60,0x3660,0x3660,0x3E60,0x3E60,
0x3E60,0x4660,0x4660,0x4E60,0x4E80,0x5680,0x5680,0x5E80,0x5E80,0x6680,
0x6680,0x6E80,0x6EA0,0x76A0,0x76A0,0x7EA0,0x7EA0,0x86A0,0x86A0,0x8EA0,
0x8EC0,0x96C0,0x96C0,0x9EC0,0x9EC0,0xA6C0,0xAEC0,0xAEC0,0xB6E0,0xB6E0,
0xBEE0,0xBEE0,0xC6E0,0xC6E0,0xCEE0,0xCEE0,0xD6E0,0xD700,0xDF00,0xDEE0,
0xDEC0,0xDEA0,0xDE80,0xDE80,0xE660,0xE640,0xE620,0xE600,0xE5E0,0xE5C0,
0xE5A0,0xE580,0xE560,0xE540,0xE520,0xE500,0xE4E0,0xE4C0,0xE4A0,0xE480,
0xE460,0xEC40,0xEC20,0xEC00,0xEBE0,0xEBC0,0xEBA0,0xEB80,0xEB60,0xEB40,
0xEB20,0xEB00,0xEAE0,0xEAC0,0xEAA0,0xEA80,0xEA60,0xEA40,0xF220,0xF200,
0xF1E0,0xF1C0,0xF1A0,0xF180,0xF160,0xF140,0xF100,0xF0E0,0xF0C0,0xF0A0,
0xF080,0xF060,0xF040,0xF020,0xF800,};
uint8_t tempto255(float temp){
return (uint8_t)round((temp+40)*255/340);
}
static int mlx90640_init(lua_State *L){
paramsMLX90640 mlx90640;
lcd_conf = luat_lcd_get_default();
MLX90640_I2CInit();
luat_timer_mdelay(50);
MLX90640_SetRefreshRate(MLX90640_ADDR, RefreshRate);//测量速率1Hz(0~7对应0.5,1,2,4,8,16,32,64Hz)
MLX90640_SetChessMode(MLX90640_ADDR);
status = MLX90640_DumpEE(MLX90640_ADDR, eeMLX90640); //读取像素校正参数
if (status != 0){
LLOGW("load system parameters error with code:%d",status);
return 0;
}
status = MLX90640_ExtractParameters(eeMLX90640, &mlx90640); //解析校正参数(计算温度时需要)
if (status != 0) {
LLOGW("Parameter extraction failed with error code:%d",status);
return 0;
}
while (1){
luat_timer_mdelay(10);
int status = MLX90640_GetFrameData(MLX90640_ADDR, frame); //读取一帧原始数据
if (status < 0){
LLOGD("GetFrame Error: %d",status);
}
float vdd = MLX90640_GetVdd(frame, &mlx90640); //计算 Vdd这句可有可无
float Ta = MLX90640_GetTa(frame, &mlx90640); //计算实时外壳温度
//计算环境温度用于温度补偿
float tr = Ta - TA_SHIFT; //Reflected temperature based on the sensor ambient temperature
//手册上说的环境温度可以用外壳温度-8℃
// LLOGD("vdd: %f Tr: %f",vdd,tr);
MLX90640_CalculateTo(frame, &mlx90640, emissivity , tr, mlx90640To); //计算像素点温度
MLX90640_BadPixelsCorrection(mlx90640.brokenPixels, mlx90640To, 1, &mlx90640); //坏点处理
MLX90640_BadPixelsCorrection(mlx90640.outlierPixels, mlx90640To, 1, &mlx90640); //坏点处理
int x,y = 0;
// uint8_t mul = 3;
for(int i = 0; i < 768; i++){
if(i%32 == 0 && i != 0){
x = 0;
// y+=mul;
y++;
// printf("\n");
}
// uint8_t mul_size = mul*mul;
// uint16_t draw_data[mul_size];
// for (size_t m = 0; m < mul_size; m++){
// draw_data[m] = camColors[tempto255(mlx90640To[i])];
// }
// luat_lcd_draw(lcd_conf,x, y, x+mul-1, y+mul-1, draw_data);
// x+=mul;
luat_lcd_draw(lcd_conf,x, y, x, y, &(camColors[tempto255(mlx90640To[i])]));
x++;
}
}
}
#include "rotable.h"
static const rotable_Reg mlx90640[] =
{
{"init", mlx90640_init, 0},
{ NULL, NULL , 0}
};
LUAMOD_API int luaopen_mlx90640( lua_State *L ) {
luat_newlib(L, mlx90640);
return 1;
}