-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAMG88_ex.c
158 lines (115 loc) · 3.92 KB
/
AMG88_ex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*!\file AMG88_ex.c
** \author SMFSW
** \copyright MIT (c) 2017-2024, SMFSW
** \brief AMG88 Driver extensions
** \details AMG88: Infrared Array Sensor (Grid-EYE)
**/
/****************************************************************/
#include "AMG88.h"
#if defined(HAL_I2C_MODULE_ENABLED)
#if defined(I2C_AMG88)
/****************************************************************/
// std libs
#include <math.h>
/****************************************************************/
float AMG88_Convert_Thermistor_Raw(const uint16_t therm)
{
uAMG88_REG__TEMP TEMP;
float tmp;
TEMP.Word = therm;
tmp = TEMP.Bits.temp * 0.0625f;
return TEMP.Bits.sign ? -tmp : tmp;
}
float AMG88_Convert_Pixel_Raw(const uint16_t pixel)
{
// VAL is signed 12b, result done by shifting to 16b signed and multiplying by granularity (0.25�C) divided by 16
return (float) ((int16_t) (pixel * 16) * 0.015625f);
}
uint16_t AMG88_Convert_Temp_To_Int(const float temp)
{
uint16_t val = (uint16_t) (fabs(temp) / 0.25f);
if (temp < 0.0f) { val |= 0x800; }
return val;
}
FctERR NONNULL__ AMG88_Get_thermistor_temp(AMG88_t * const pCpnt, float * temp)
{
FctERR err;
uint16_t VAL;
err = AMG88_Get_Thermistor_Raw(pCpnt, &VAL);
if (err) { return err; }
*temp = AMG88_Convert_Thermistor_Raw(VAL);
return err;
}
FctERR NONNULL__ AMG88_Get_pixel_temp(AMG88_t * const pCpnt, float * temp, const uint8_t pixel)
{
FctERR err;
uint16_t VAL;
err = AMG88_Get_Pixel_Raw(pCpnt, &VAL, pixel);
if (err) { return err; }
*temp = AMG88_Convert_Pixel_Raw(VAL);
return err;
}
FctERR NONNULL__ AMG88_Get_pixels_temp(AMG88_t * const pCpnt, float temp[64])
{
FctERR err;
uint8_t raw[128];
err = AMG88_Read(pCpnt->cfg.slave_inst, raw, AMG88__T01L, sizeof(raw));
if (err) { return err; }
for (uintCPU_t i = 0 ; i < sizeof(raw) / 2 ; i++)
{
uint16_t val = MAKEWORD(raw[i * 2], raw[(i * 2) + 1]);
temp[i] = AMG88_Convert_Pixel_Raw(val);
}
return err;
}
FctERR NONNULL__ AMG88_Set_Interrupt_LVLH(AMG88_t * const pCpnt, const float temp)
{
uint16_t Temp;
uint8_t LEVEL[2];
Temp = AMG88_Convert_Temp_To_Int(temp);
LEVEL[0] = LOBYTE(Temp);
LEVEL[1] = HIBYTE(Temp);
return AMG88_Write(pCpnt->cfg.slave_inst, LEVEL, AMG88__INTHL, sizeof(LEVEL));
}
FctERR NONNULL__ AMG88_Set_Interrupt_LVLL(AMG88_t * const pCpnt, const float temp)
{
uint16_t Temp;
uint8_t LEVEL[2];
Temp = AMG88_Convert_Temp_To_Int(temp);
LEVEL[0] = LOBYTE(Temp);
LEVEL[1] = HIBYTE(Temp);
return AMG88_Write(pCpnt->cfg.slave_inst, LEVEL, AMG88__INTLL, sizeof(LEVEL));
}
FctERR NONNULL__ AMG88_Set_Interrupt_HYS(AMG88_t * const pCpnt, const float temp)
{
uint16_t Temp;
uint8_t LEVEL[2];
Temp = AMG88_Convert_Temp_To_Int(temp);
LEVEL[0] = LOBYTE(Temp);
LEVEL[1] = HIBYTE(Temp);
return AMG88_Write(pCpnt->cfg.slave_inst, LEVEL, AMG88__IHYSL, sizeof(LEVEL));
}
FctERR NONNULL__ AMG88_Set_Interrupt_Levels(AMG88_t * const pCpnt, const float temp_LVLH, const float temp_LVLL, const float temp_HYS)
{
uint16_t Temp;
uint8_t LEVEL[6];
Temp = AMG88_Convert_Temp_To_Int(temp_LVLH);
LEVEL[0] = LOBYTE(Temp);
LEVEL[1] = HIBYTE(Temp);
Temp = AMG88_Convert_Temp_To_Int(temp_LVLL);
LEVEL[2] = LOBYTE(Temp);
LEVEL[3] = HIBYTE(Temp);
Temp = AMG88_Convert_Temp_To_Int(temp_HYS);
LEVEL[4] = LOBYTE(Temp);
LEVEL[5] = HIBYTE(Temp);
return AMG88_Write(pCpnt->cfg.slave_inst, LEVEL, AMG88__INTHL, sizeof(LEVEL));
}
/****************************************************************/
__WEAK FctERR NONNULL__ AMG88_INT_GPIO_Init(AMG88_t * const pCpnt, GPIO_TypeDef * const GPIOx, const uint16_t GPIO_Pin, const GPIO_PinState GPIO_Active) {
return I2C_peripheral_GPIO_init(&pCpnt->cfg.INT_GPIO, GPIOx, GPIO_Pin, GPIO_Active); }
__WEAK FctERR NONNULL__ AMG88_INT_GPIO_Get(AMG88_t * const pCpnt, bool * const pState) {
return I2C_peripheral_GPIO_get(&pCpnt->cfg.INT_GPIO, pState); }
/****************************************************************/
#endif
#endif
/****************************************************************/