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

bas_app.c « App « STM32_WPAN « BLE_Hid « BLE « Applications « P-NUCLEO-WB55.Nucleo « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1ef3d8f7efae92f4021186201e5d744b23d5485f (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
/**
  ******************************************************************************
 * @file    bas_app.c
 * @author  MCD Application Team
 * @brief   Battery Service Application
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2020-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 "dbg_trace.h"
#include "bas_app.h"
#include "ble.h"
#include "stm32_seq.h"

#include "app_ble.h"


/* Private typedef -----------------------------------------------------------*/
typedef struct
{
  uint16_t  Level;
  uint8_t   TimerLevel_Id;
} BSAAPP_Context_t;


/* Private defines -----------------------------------------------------------*/
#define BASAPP_DEFAULT_BAT_LEVEL       100  /**100% */
#define BASAPP_DEFAULT_BAT_LEVEL_CHG   (5000000/CFG_TS_TICK_VAL)  /**< 5s */


/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/**
 * START of Section BLE_APP_CONTEXT
 */

PLACE_IN_SECTION("BLE_APP_CONTEXT") BSAAPP_Context_t BASAPP_Context[BLE_CFG_BAS_NUMBER];

/**
 * END of Section BLE_APP_CONTEXT
 */

/* Global variables ----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void BASAPP_UpdateLevel( void );


/* Functions Definition ------------------------------------------------------*/
/* Private functions ----------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

void BAS_Notification(BAS_Notification_evt_t *pNotification)
{
  switch(pNotification->BAS_Evt_Opcode)
  {
    case BAS_LEVEL_NOT_ENABLED_EVT:
      {
        HW_TS_Stop(BASAPP_Context[pNotification->ServiceInstance].TimerLevel_Id);
        HW_TS_Start(BASAPP_Context[pNotification->ServiceInstance].TimerLevel_Id, BASAPP_DEFAULT_BAT_LEVEL_CHG);
      }
      break;

    case BAS_LEVEL_NOT_DISABLED_EVT:
      {
        HW_TS_Stop(BASAPP_Context[pNotification->ServiceInstance].TimerLevel_Id);
      }
      break;

    case BAS_LEVEL_READ_EVT:
      {
        if(BASAPP_Context[pNotification->ServiceInstance].Level > 0)
          BASAPP_Context[pNotification->ServiceInstance].Level -= 1;
        else
          BASAPP_Context[pNotification->ServiceInstance].Level = BASAPP_DEFAULT_BAT_LEVEL;
        BAS_Update_Char(BATTERY_LEVEL_CHAR_UUID,
                        pNotification->ServiceInstance,
                        (uint8_t *)&BASAPP_Context[pNotification->ServiceInstance].Level);
      }
      break;

    default:
      break;
  }

  return;
}

void BASAPP_Init(uint8_t index)
{
  UTIL_SEQ_RegTask( 1<< CFG_TASK_BAS_LEVEL_REQ_ID, UTIL_SEQ_RFU, BASAPP_Level );

  
  /**
   * Initialize Level
   */
  BASAPP_Context[index].Level = BASAPP_DEFAULT_BAT_LEVEL;
  BAS_Update_Char(BATTERY_LEVEL_CHAR_UUID, index, (uint8_t *)&BASAPP_Context[index].Level);

  /**
   * Create timer for Battery Level
   */
  HW_TS_Create(CFG_TIM_PROC_ID_ISR, &(BASAPP_Context[index].TimerLevel_Id), hw_ts_Repeated, BASAPP_UpdateLevel);

  return;
}


void BASAPP_Level(void)
{
  uint8_t index;
  
  for(index = 0; index < BLE_CFG_BAS_NUMBER; index++)
  {
    if(BASAPP_Context[index].Level > 0)
      BASAPP_Context[index].Level -= 1;
    else
      BASAPP_Context[index].Level = BASAPP_DEFAULT_BAT_LEVEL;

    BAS_Update_Char(BATTERY_LEVEL_CHAR_UUID, index, (uint8_t *)&BASAPP_Context[index].Level);
  }

  return;
}

static void BASAPP_UpdateLevel( void )
{
  /**
   * The code shall be executed in the background as aci command may be sent
   * The background is the only place where the application can make sure a new aci command
   * is not sent if there is a pending one
   */
  UTIL_SEQ_SetTask( 1<<CFG_TASK_BAS_LEVEL_REQ_ID, CFG_SCH_PRIO_0);

  return;
}