Welcome to mirror list, hosted at ThFree Co, Russian Federation.

motenv_server_app.c « App « STM32_WPAN « BLE_Sensor « BLE « Applications « STM32WB5MM-DK « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07058a53163ecaf6edc91a9c236ee452cf8dd93a (plain)
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
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : motenv_server_app.c
  * Description        : MOTENV Server Application
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2019-2021 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* USER CODE BEGIN UserCode */
/* Includes ------------------------------------------------------------------*/
#include "app_common.h"
#include "dbg_trace.h"
#include "ble.h"
#include "motenv_server_app.h"
#include "stm32_seq.h"

#include "wb5m_sensor_stm.h"
#include "env_server_app.h"
#include "motion_server_app.h"

#include "stm32wb5mm_dk.h"
#include "stm32wb5mm_dk_lcd.h"
#include "stm32_lcd.h"

/* Private defines -----------------------------------------------------------*/
#define ENVIRONMENT_UPDATE_PERIOD       (uint32_t)(0.5*1000*1000/CFG_TS_TICK_VAL) /*500ms*/
#define ACC_GYRO_MAG_UPDATE_PERIOD      (uint32_t)(0.05*1000*1000/CFG_TS_TICK_VAL) /*50ms (20Hz)*/

/* Private typedef -----------------------------------------------------------*/

/**
 * @brief  MOTENV Server Context structure definition
 *         Include just the Timer Ids for the Notifications
 */
typedef struct
{
  uint8_t AccGyroMag_Update_Timer_Id;
  uint8_t Env_Update_Timer_Id;
} MOTENV_Server_App_Context_t;

/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/**
 * START of Section BLE_APP_CONTEXT
 */
PLACE_IN_SECTION("BLE_APP_CONTEXT") static MOTENV_Server_App_Context_t MOTENV_Server_App_Context;

/**
 * END of Section BLE_APP_CONTEXT
 */
/* Global variables ----------------------------------------------------------*/
extern int debug_trace_enabled;

/* Private function prototypes -----------------------------------------------*/
static void MOTENV_AccGyroMagUpdate_Timer_Callback(void);
static void MOTENV_EnvUpdate_Timer_Callback(void);
static void MOTENV_APP_context_Init(void);

/* Functions Definition ------------------------------------------------------*/

/* Public functions ----------------------------------------------------------*/

/**
 * @brief  Handle the request from the GATT Client
 *         (e.g., on notification enabling/disabling request, start/stop the timer)
 * @param  pNotification: Request data coming from the GATT Client
 * @retval None
 */
void MOTENV_STM_App_Notification(MOTENV_STM_App_Notification_evt_t *pNotification)
{
  switch(pNotification->Motenv_Evt_Opcode)
  {
    /*
     * Env char notification enabled
     */
    case HW_ENV_NOTIFY_ENABLED_EVT:
      ENV_Set_Notification_Status(1);
      if(debug_trace_enabled == 1){
        APP_DBG_MSG("-- TEMPLATE APPLICATION SERVER : ENV NOTIFICATION ENABLED\n");
        APP_DBG_MSG(" \n\r");
      }
      /* Start the timer used to update the Env characteristic */
      HW_TS_Start(MOTENV_Server_App_Context.Env_Update_Timer_Id, ENVIRONMENT_UPDATE_PERIOD);
      break; /* HW_ENV_NOTIFY_ENABLED_EVT */

    /*
     * Motion char notification enabled
     */
    case HW_MOTION_NOTIFY_ENABLED_EVT:
      UTIL_LCD_ClearStringLine(2);
      UTIL_LCD_ClearStringLine(3);
      UTIL_LCD_ClearStringLine(4);
      BSP_LCD_Refresh(0);
      MOTION_Set_Notification_Status(1);
      if(debug_trace_enabled == 1){
        APP_DBG_MSG("-- TEMPLATE APPLICATION SERVER : MOTION NOTIFICATION ENABLED\n");
        APP_DBG_MSG(" \n\r");
      }
      /* Start the timer used to update the AccGyroMag characteristic */
      HW_TS_Start(MOTENV_Server_App_Context.AccGyroMag_Update_Timer_Id, ACC_GYRO_MAG_UPDATE_PERIOD);
      break; /* HW_MOTION_NOTIFY_ENABLED_EVT */

    /*
     * Env char notification disabled
     */
    case HW_ENV_NOTIFY_DISABLED_EVT:
      UTIL_LCD_ClearStringLine(2);
      UTIL_LCD_ClearStringLine(3);
      UTIL_LCD_ClearStringLine(4);
      BSP_LCD_Refresh(0);
      ENV_Set_Notification_Status(0);
      if(debug_trace_enabled == 1){
        APP_DBG_MSG("-- TEMPLATE APPLICATION SERVER : ENV NOTIFICATION DISABLED\n");
        APP_DBG_MSG(" \n\r");
      }
      /* Stop the timer used to update the Env characteristic */
      HW_TS_Stop(MOTENV_Server_App_Context.Env_Update_Timer_Id);
      break; /* HW_ENV_NOTIFY_DISABLED_EVT */

    /*
     * Motion char notification disabled
     */
    case HW_MOTION_NOTIFY_DISABLED_EVT:
      UTIL_LCD_ClearStringLine(2);
      UTIL_LCD_ClearStringLine(3);
      UTIL_LCD_ClearStringLine(4);
      BSP_LCD_Refresh(0);
      MOTION_Set_Notification_Status(0);
      if(debug_trace_enabled == 1){
        APP_DBG_MSG("-- TEMPLATE APPLICATION SERVER : MOTION NOTIFICATION DISABLED\n");
        APP_DBG_MSG(" \n\r");
      }
      /* Stop the timer used to update the Motion characteristic */
      HW_TS_Stop(MOTENV_Server_App_Context.AccGyroMag_Update_Timer_Id);
      break; /* HW_ENV_NOTIFY_DISABLED_EVT */

    /*
     * Env char read request
     */
    case HW_ENV_READ_EVT:
      ENV_Update();
      if(debug_trace_enabled == 1){
        APP_DBG_MSG("-- TEMPLATE APPLICATION SERVER : ENV READ\n");
        APP_DBG_MSG(" \n\r");
      }
      break; /* HW_ENV_READ_EVT */
      
    default:
      break; /* DEFAULT */
  }

  return;
}

/**
 * @brief  Handle disconnection (Stop all timers)
 * @param  None
 * @retval None
 */
void MOTENV_APP_HandleDisconnection( void )
{
  ENV_Set_Notification_Status(0);
  /* Stop the timer used to update the Env characteristic */
  HW_TS_Stop(MOTENV_Server_App_Context.Env_Update_Timer_Id);

  MOTION_Set_Notification_Status(0);
  /* Stop the timer used to update the Motion characteristic */
  HW_TS_Stop(MOTENV_Server_App_Context.AccGyroMag_Update_Timer_Id);
}

/**
 * @brief  Init the MOTENV APP (Register Tasks, Create Notification timers)
 * @param  None
 * @retval None
 */
void MOTENV_APP_Init(void)
{
  UTIL_SEQ_RegTask( 1<<CFG_TASK_NOTIFY_ACC_GYRO_MAG_ID, UTIL_SEQ_RFU, MOTION_Send_Notification_Task);
  /* Create timer to get the AccGyroMag params and update charecteristic */
  HW_TS_Create(CFG_TIM_PROC_ID_ISR,
        &(MOTENV_Server_App_Context.AccGyroMag_Update_Timer_Id),
        hw_ts_Repeated,
        MOTENV_AccGyroMagUpdate_Timer_Callback);

  UTIL_SEQ_RegTask( 1<<CFG_TASK_NOTIFY_ENVIRONMENT_ID, UTIL_SEQ_RFU, ENV_Send_Notification_Task);
  /* Create timer to change the Environment params and update charecteristic */
  HW_TS_Create(CFG_TIM_PROC_ID_ISR,
        &(MOTENV_Server_App_Context.Env_Update_Timer_Id),
        hw_ts_Repeated,
        MOTENV_EnvUpdate_Timer_Callback);

  /**
   * Initialize MOTENV application context
   */
  MOTENV_APP_context_Init();

  return;
}

/* Private functions ---------------------------------------------------------*/

/**
 * @brief  On timeout, trigger the task
 *         for Motion Char (Acc-Gyro-Mag) notification
 * @param  None
 * @retval None
 */
static void MOTENV_AccGyroMagUpdate_Timer_Callback(void)
{
  UTIL_SEQ_SetTask(1<<CFG_TASK_NOTIFY_ACC_GYRO_MAG_ID, CFG_SCH_PRIO_0);
}

/**
 * @brief  On timeout, trigger the task
 *         for Environmental Char notification
 * @param  None
 * @retval None
 */
static void MOTENV_EnvUpdate_Timer_Callback(void)
{
  UTIL_SEQ_SetTask(1<<CFG_TASK_NOTIFY_ENVIRONMENT_ID, CFG_SCH_PRIO_0);
}

/**
 * @brief  Init Context for each Service exposed by MOTENV Server App
 * @param  None
 * @retval None
 */
static void MOTENV_APP_context_Init(void)
{
  /* Init ENV context */
  ENV_Context_Init();

  /* Init MOTION Context */
  MOTION_Context_Init();

}

/* USER CODE END UserCode */