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

env_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: 9fcc3329040bf284f19deb6e18a80e9b73614865 (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
/**
  ******************************************************************************
  * File Name          : env_server_app.c
  * Description        : Handle HW/Environmental Service/Char
  ******************************************************************************
  * @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.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "app_common.h"
#include "ble.h"
#include "dbg_trace.h"

#include "motenv_server_app.h"
#include "env_server_app.h"

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

/* Private defines -----------------------------------------------------------*/
#define PRESSURE_BYTES          (4)
#define HUMIDITY_BYTES          (2)
#define TEMPERATURE_BYTES       (2)

#define VALUE_LEN_ENV           (2+PRESSURE_BYTES+HUMIDITY_BYTES+TEMPERATURE_BYTES/*Temp2*/+TEMPERATURE_BYTES/*Temp1*/)

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

/**
 * @brief  HW/Environmental Service/Char Context structure definition
 */
typedef struct
{
  uint8_t  NotificationStatus;

  int32_t PressureValue;
  uint16_t HumidityValue;
  int16_t TemperatureValue[1];
  uint8_t hasPressure;
  uint8_t hasHumidity;
  uint8_t hasTemperature;
} ENV_Server_App_Context_t;

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

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

/**
 * @brief  Environmental Capabilities
 */

PLACE_IN_SECTION("BLE_APP_CONTEXT") static ENV_Server_App_Context_t ENV_Server_App_Context;

/* Global variables ----------------------------------------------------------*/
extern int debug_trace_enabled;
extern uint8_t manuf_data[14];

/* Private function prototypes -----------------------------------------------*/
static void ENV_Handle_Sensor(void);
static void EnvSensor_GetCaps(void);

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

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

/**
 * @brief  Init the HW/Environmental Service/Char Context
 * @param  None
 * @retval None
 */
void ENV_Context_Init(void)
{
  /* Env Sensors */

  ENV_Server_App_Context.hasPressure = 0;
  ENV_Server_App_Context.hasHumidity = 0;
  ENV_Server_App_Context.hasTemperature = 0;

  ENV_Set_Notification_Status(0);

  /* Check Env caps */
  EnvSensor_GetCaps();
}

/**
 * @brief  Set the notification status (enabled/disabled)
 * @param  status The new notification status
 * @retval None
 */
void ENV_Set_Notification_Status(uint8_t status)
{
  ENV_Server_App_Context.NotificationStatus = status;
}

/**
 * @brief  Send a notification for Environmental char
 * @param  None
 * @retval None
 */
void ENV_Send_Notification_Task(void)
{

  if(ENV_Server_App_Context.NotificationStatus)
  {
    if(debug_trace_enabled == 1){
      APP_DBG_MSG("-- ENV APPLICATION SERVER : NOTIFY CLIENT WITH NEW ENV PARAMETER VALUE \n ");
      APP_DBG_MSG(" \n\r");
    }
    ENV_Update();
  }
  else
  {
    if(debug_trace_enabled == 1){
      APP_DBG_MSG("-- ENV APPLICATION SERVER : CAN'T INFORM CLIENT - NOTIFICATION DISABLED\n ");
    }
  }

  return;
}

/**
 * @brief  Update the Environmental char value
 * @param  None
 * @retval None
 */
void ENV_Update(void)
{
  uint8_t tempIndex = 0;
  uint8_t value[VALUE_LEN_ENV];
  uint8_t BuffPos = 2;
  /* Read ENV values */
  ENV_Handle_Sensor();

  /* Timestamp */
  STORE_LE_16(value, (HAL_GetTick()>>3));

  if(ENV_Server_App_Context.hasPressure == 1)
  {
    STORE_LE_32(&value[BuffPos], ENV_Server_App_Context.PressureValue);
  }
  BuffPos += PRESSURE_BYTES;
  
  if(ENV_Server_App_Context.hasHumidity == 1)
  {
    STORE_LE_16(&value[BuffPos], ENV_Server_App_Context.HumidityValue);    
  }
  BuffPos += HUMIDITY_BYTES;

  for(tempIndex = 0; tempIndex < ENV_Server_App_Context.hasTemperature; tempIndex++)
  {
    STORE_LE_16(&value[BuffPos], ENV_Server_App_Context.TemperatureValue[tempIndex]);
    BuffPos += TEMPERATURE_BYTES;
  }
  
  MOTENV_STM_App_Update_Char(ENV_CHAR_UUID, VALUE_LEN_ENV, (uint8_t *)&value);

  return;
}

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

/**
 * @brief  Parse the values read by Environmental sensors
 * @param  None
 * @retval None
 */
static void ENV_Handle_Sensor(void)
{
  float temperature = 0.0;
  int32_t decPart= 0, intPart=0;

  char tempValue[16];
  
  UTIL_LCD_ClearStringLine(2);
  UTIL_LCD_ClearStringLine(3);
  UTIL_LCD_ClearStringLine(4);
  
  if(ENV_Server_App_Context.hasTemperature >= 1)
  {    
    BSP_ENV_SENSOR_GetValue(ENV_SENSOR_STTS22H_0, ENV_TEMPERATURE, &temperature);
    sprintf(tempValue,"Temp 1 : %2.1f C",temperature);
    UTIL_LCD_DisplayStringAtLine(3,(uint8_t*)tempValue);
    MCR_BLUEMS_F2I_1D(temperature, intPart, decPart);
    ENV_Server_App_Context.TemperatureValue[0] = intPart*10+decPart;    
  }
  BSP_LCD_Refresh(0);
}

/**
 * @brief  Check the Environmental active capabilities and set the ADV data accordingly
 * @param  None
 * @retval None
 */
static void EnvSensor_GetCaps(void)
{
 
  ENV_Server_App_Context.hasPressure = 0;
  ENV_Server_App_Context.hasHumidity = 0;
  ENV_Server_App_Context.hasTemperature = 1;

  /* Update BLE ADV field (Env) */
  if(ENV_Server_App_Context.hasTemperature > 1)
  {
    manuf_data[5] |= 0x05; /* Two Temperature values*/
  }
  else if(ENV_Server_App_Context.hasTemperature == 1)
  {
    manuf_data[5] |= 0x04; /* One Temperature value*/
  }

  if(ENV_Server_App_Context.hasHumidity)
  {
    manuf_data[5] |= 0x08; /* Humidity value */
  }

  if(ENV_Server_App_Context.hasPressure)
  {
    manuf_data[5] |= 0x10; /* Pressure value*/
  }
}