WXK
2023-08-30 cebd82085108b30288a6357fae633e811dbea248
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*******************************************************************************
 * File Name         : Lis3dhApp.c
 * Description       :
 * Created on        : 2021Äê5ÔÂ30ÈÕ
 * Author            : www.hido-studio.com
 *******************************************************************************/
 
/*******************************************************************************
 *                              Include Files                                  *
 *******************************************************************************/
#include "Lis3dhApp.h"
#include "lis3dh_reg.h"
#include "HIDO_Debug.h"
#include "RTC.h"
#include "SPI.h"
#include "string.h"
#include "Device.h"
#include "LCD.h"
 
/*******************************************************************************
 *                                  Macro                                      *
 *******************************************************************************/
 
/*******************************************************************************
 *                             Type Definition                                 *
 *******************************************************************************/
typedef union{
  int16_t i16bit[3];
  uint8_t u8bit[6];
} axis3bit16_t;
 
/*******************************************************************************
 *                             Local Variable                                  *
 *******************************************************************************/
static stmdev_ctx_t dev_ctx;
static HIDO_BOOL l_bWakeUp = HIDO_FALSE;
static HIDO_UINT32 l_u32MoveTS = 0;
 
/*******************************************************************************
 *                        Local Function Declaration                           *
 *******************************************************************************/
 
/*******************************************************************************
 *                             Local Function                                  *
 *******************************************************************************/
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2021Äê5ÔÂ30ÈÕ
 *******************************************************************************/
static HIDO_INT32 platform_init(HIDO_VOID)
{
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
    SPI_Enable(SPI_ID_3D);
 
    return HIDO_OK;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2021Äê5ÔÂ30ÈÕ
 *******************************************************************************/
static HIDO_INT32 platform_write(HIDO_VOID *handle, HIDO_UINT8 reg, HIDO_UINT8 *bufp, HIDO_UINT16 len)
{
    HIDO_UINT16 i = 0;
    
    for(i = 0; i < len; i++)
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
        SPI_Write(SPI_ID_3D, &reg, 1);
        SPI_Write(SPI_ID_3D, &bufp[i], 1);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
        reg++;
    }
    
    return HIDO_OK;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2021Äê5ÔÂ30ÈÕ
 *******************************************************************************/
static HIDO_INT32 platform_read(HIDO_VOID *handle, HIDO_UINT8 reg, HIDO_UINT8 *bufp,
                             HIDO_UINT16 len)
{
    HIDO_UINT16 i = 0;
    
    reg |= 0x80;
    for(i = 0; i < len; i++)
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
        SPI_Write(SPI_ID_3D, &reg, 1);
        SPI_ReadWrite(SPI_ID_3D, &bufp[i], HIDO_NULL, 1);
        reg++;
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
    }
    
    return HIDO_OK;
}
 
/*******************************************************************************
 *                             Global Function                                 *
 *******************************************************************************/
/*******************************************************************************
 * Function Name     : Lis3dhApp_ISR
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2021Äê5ÔÂ30ÈÕ
 *******************************************************************************/
HIDO_VOID Lis3dhApp_ISR(HIDO_UINT16 _u16Pin)
{
    if((HIDO_NULL == dev_ctx.read_reg) || (HIDO_NULL == dev_ctx.write_reg))
    {
        return;
    }
 
    l_bWakeUp = HIDO_TRUE;
}
 
/*******************************************************************************
 * Function Name     : Lis3dhApp_IsWakeUp
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2021Äê5ÔÂ30ÈÕ
 *******************************************************************************/
HIDO_BOOL Lis3dhApp_IsWakeUp(HIDO_VOID)
{
    HIDO_BOOL bWakeUp = l_bWakeUp;
    l_bWakeUp = HIDO_FALSE;
 
    return bWakeUp;
}
 
/*******************************************************************************
 * Function Name     : Lis3dhApp_GetMoveState
 * Description       : 
 * Input             : 
 * Output            : 
 * Return            : 
 * Author            : www.hido-studio.com
 * Modified Date:    : 2021Äê5ÔÂ30ÈÕ
 *******************************************************************************/
HIDO_UINT8 Lis3dhApp_GetMoveState(HIDO_VOID)
{
    if((RTC_GetSeconds() - l_u32MoveTS) < Device_GetSettings()->m_stManDownAlarm.m_u16MotionlessTime)
    {
        return 1;
    }
 
    return 0;
}
 
/*******************************************************************************
 * Function Name     : Lis3dhApp_ClearMoveState
 * Description       : 
 * Input             : 
 * Output            : 
 * Return            : 
 * Author            : www.hido-studio.com
 * Modified Date:    : 2021Äê5ÔÂ30ÈÕ
 *******************************************************************************/
HIDO_INT32 Lis3dhApp_ClearMoveState(HIDO_VOID)
{
    l_u32MoveTS = RTC_GetSeconds();
 
    return HIDO_OK;
}
 
/*******************************************************************************
 * Function Name     : Lis3dhApp_Init
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2021Äê5ÔÂ30ÈÕ
 *******************************************************************************/
HIDO_INT32 Lis3dhApp_Poll(HIDO_VOID)
{
    lis3dh_int1_src_t src;
 
    lis3dh_int1_gen_source_get(&dev_ctx, &src);
 
    if (1 == src.yh)
    {
        LCD_SetFlip(HIDO_FALSE);
    }
 
    if (1 == src.xl)
    {
        LCD_SetFlip(HIDO_TRUE);
    }
 
    lis3dh_reg_t reg;
    HIDO_FLOAT acc_x, acc_y, acc_z, acc_g;
    axis3bit16_t data_raw_acceleration;
 
    /* Read output only if new value available */
    lis3dh_xl_data_ready_get(&dev_ctx, &reg.byte);
    if (reg.byte)
    {
        /* Read accelerometer data */
        memset(data_raw_acceleration.u8bit, 0x00, 3 * sizeof(int16_t));
        lis3dh_acceleration_raw_get(&dev_ctx, data_raw_acceleration.u8bit);
        acc_x = lis3dh_from_fs2_hr_to_mg(data_raw_acceleration.i16bit[0]);
        acc_y = lis3dh_from_fs2_hr_to_mg(data_raw_acceleration.i16bit[1]);
        acc_z = lis3dh_from_fs2_hr_to_mg(data_raw_acceleration.i16bit[2]);
 
        HIDO_FLOAT fDis = 50;
 
        if(Device_GetSettings()->m_stManDownAlarm.m_u8Sensitivity != 0)
        {
            fDis = 80;
        }
 
        if (fabs(acc_x) > fDis || fabs(acc_y) > fDis || fabs(acc_z) > fDis)
        {
//            HIDO_Debug("acc_x=%.2f acc_y=%.2f acc_z=%.2f\r\n", acc_x, acc_y, acc_z);
            l_u32MoveTS = RTC_GetSeconds();
        }
    }
    return HIDO_OK;
}
 
/*******************************************************************************
 * Function Name     : Lis3dhApp_ClearINT
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2021Äê5ÔÂ30ÈÕ
 *******************************************************************************/
HIDO_INT32 Lis3dhApp_ClearINT(HIDO_VOID)
{
    lis3dh_int1_src_t src;
 
    /*
     * Read INT pin 1 in polling mode
     * or read src status register
     */
    lis3dh_int1_gen_source_get(&dev_ctx, &src);
    while (src.xh || src.yh || src.zh)
    {
        lis3dh_int1_gen_source_get(&dev_ctx, &src);
    }
    
    return HIDO_OK;
}
 
/*******************************************************************************
 * Function Name     : Lis3dhApp_Init
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2021Äê5ÔÂ30ÈÕ
 *******************************************************************************/
HIDO_INT32 Lis3dhApp_Init(HIDO_VOID)
{
    /*
     *  Initialize mems driver interface
     */
 
    HIDO_UINT8 whoamI = 0;
 
    lis3dh_int1_cfg_t int1_cfg;
    lis3dh_ctrl_reg3_t ctrl_reg3;
    HIDO_UINT8 dummy;
 
    dev_ctx.write_reg = platform_write;
    dev_ctx.read_reg = platform_read;
    dev_ctx.handle = HIDO_NULL;
 
    /*
     * Initialize platform specific hardware
     */
    platform_init();
 
    /*
     *  Check device ID
     */
    lis3dh_device_id_get(&dev_ctx, &whoamI);
    if (whoamI != LIS3DH_ID)
    {
        return HIDO_ERR;
    }
 
    lis3dh_full_scale_set(&dev_ctx, LIS3DH_2g);
    lis3dh_int1_gen_threshold_set(&dev_ctx, 20);
    lis3dh_operating_mode_set(&dev_ctx, LIS3DH_HR_12bit);
    lis3dh_data_rate_set(&dev_ctx, LIS3DH_ODR_100Hz);
 
    int1_cfg.xlie = PROPERTY_ENABLE;
    int1_cfg.xhie = PROPERTY_ENABLE;
    int1_cfg.ylie = PROPERTY_ENABLE;
 
    int1_cfg.yhie = PROPERTY_ENABLE;
    int1_cfg.zlie = PROPERTY_ENABLE;
    int1_cfg.zhie = PROPERTY_ENABLE;
 
    int1_cfg._6d = PROPERTY_ENABLE;
    int1_cfg.aoi = PROPERTY_ENABLE;
    lis3dh_int1_gen_conf_set(&dev_ctx, &int1_cfg);
 
    Lis3dhApp_ISR(0);
    HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
 
    return HIDO_OK;
}