summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/BSP/MESHBEAN/src/buttons.c
blob: 8867a82bddb7e202a56babfe7cdc7aaa53a446a5 (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
/**************************************************************************//**
\file  buttons.c

\brief Implementation of buttons interface.

\author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

\internal
  History:
    30/05/08 A. Khromykh - Created
*******************************************************************************/
#if APP_DISABLE_BSP != 1

/******************************************************************************
                   Includes section
******************************************************************************/
#include <types.h>
#include <buttons.h>
#include <irq.h>
#include <appTimer.h>
#include <bspTaskManager.h>
#include <gpio.h>

/******************************************************************************
                   Define(s) section
******************************************************************************/
#define BSP_readKEY0() GPIO_IRQ_6_read()
#define BSP_readKEY1() GPIO_IRQ_7_read()
#define PRESSED           1
#define RELEASED          0
#define BSP_BUTTONS_IDLE  0
#define BSP_BUTTONS_BUSY  1

/******************************************************************************
                   Types section
******************************************************************************/
typedef struct
{
  uint8_t currentState0 : 1;
  uint8_t currentState1 : 1;
  uint8_t wasPressed0   : 1;
  uint8_t wasPressed1   : 1;
  uint8_t waitReleased0 : 1;
  uint8_t waitReleased1 : 1;
} BSP_buttonsAction_t;

/******************************************************************************
                   Prototypes section
******************************************************************************/
/**************************************************************************//**
\brief  HAL's event handlers about KEY 0 has changed state.
******************************************************************************/
void bspKey0InterruptHandler(void);

/**************************************************************************//**
\brief  HAL's event handlers about KEY 1 has changed state.
******************************************************************************/
void bspKey1InterruptHandler(void);

/******************************************************************************
                   Global variables section
******************************************************************************/
static uint8_t state = BSP_BUTTONS_IDLE;
static volatile BSP_buttonsAction_t buttonsAction;
static BSP_ButtonsEventFunc_t bspButtonPressHandle;   // callback
static BSP_ButtonsEventFunc_t bspButtonReleaseHandle; // callback

/******************************************************************************
                   Implementations section
******************************************************************************/
/**************************************************************************//**
\brief Initializes buttons module.
******************************************************************************/
static void bspInitButtons(void)
{
  HAL_RegisterIrq(IRQ_6, IRQ_LOW_LEVEL, bspKey0InterruptHandler);
  HAL_RegisterIrq(IRQ_7, IRQ_LOW_LEVEL, bspKey1InterruptHandler);

  if (BSP_readKEY0())
    buttonsAction.currentState0 = RELEASED;
  else
    buttonsAction.currentState0 = PRESSED;

  if (BSP_readKEY1())
    buttonsAction.currentState1 = RELEASED;
  else
    buttonsAction.currentState1 = PRESSED;

  HAL_EnableIrq(IRQ_6);
  HAL_EnableIrq(IRQ_7);
}

/**************************************************************************//**
\brief Registers handlers for button events.

\param[in]
    pressed - the handler to process pressing the button
\param[in]
    released - the handler to process releasing the button
\param[in]
    bn - button number.
\return
  BC_FAIL - buttons module is busy, \n
  BC_SUCCESS in other case.
******************************************************************************/
result_t BSP_OpenButtons(void (*pressed)(uint8_t bn), void (*released)(uint8_t bn))
{
  if (state != BSP_BUTTONS_IDLE)
    return BC_FAIL;
  state = BSP_BUTTONS_BUSY;
  bspButtonPressHandle = pressed;
  bspButtonReleaseHandle = released;
  bspInitButtons();
  return BC_SUCCESS;
};

/**************************************************************************//**
\brief Cancel buttons handlers.
\return
  BC_FAIL - buttons module was not opened, \n
  BC_SUCCESS in other case.
******************************************************************************/
result_t BSP_CloseButtons(void)
{
  if (state != BSP_BUTTONS_BUSY)
    return BC_FAIL;
  HAL_UnregisterIrq(IRQ_6);
  HAL_UnregisterIrq(IRQ_7);
  bspButtonPressHandle = NULL;
  bspButtonReleaseHandle = NULL;
  state = BSP_BUTTONS_IDLE;
  return BC_SUCCESS;
};

/**************************************************************************//**
\brief Reads state of buttons.

\return
    Current buttons state in a binary way. \n
    Bit 0 defines state of the button 1, \n
    bit 1 defines state of the button 2.
******************************************************************************/
uint8_t BSP_ReadButtonsState(void)
{
  uint8_t state = 0;

  if (buttonsAction.currentState0)
    state = 0x01;

  if (buttonsAction.currentState1)
    state |= 0x02;

  return state;
}

/**************************************************************************//**
\brief  HAL's event about KEY has changed state.
******************************************************************************/
void bspKey0InterruptHandler(void)
{
  HAL_DisableIrq(IRQ_6);
  buttonsAction.currentState0 = PRESSED;
  buttonsAction.wasPressed0 = 1;
  bspPostTask(BSP_BUTTONS);
}

/**************************************************************************//**
\brief  HAL's event about KEY has changed state.
******************************************************************************/
void bspKey1InterruptHandler(void)
{
  HAL_DisableIrq(IRQ_7);
  buttonsAction.currentState1 = PRESSED;
  buttonsAction.wasPressed1 = 1;
  bspPostTask(BSP_BUTTONS);
}

/**************************************************************************//**
\brief  BSP's event about KEY has changed state.
******************************************************************************/
void bspButtonsHandler(void)
{
  if (buttonsAction.wasPressed0)
  {
    buttonsAction.wasPressed0 = 0;
    buttonsAction.waitReleased0 = 1;
    if (NULL != bspButtonPressHandle)
      bspButtonPressHandle(BSP_KEY0);
  }

  if (buttonsAction.waitReleased0)
  {
    if (BSP_readKEY0())
    {
      buttonsAction.waitReleased0 = 0;
      buttonsAction.currentState0 = RELEASED;
      if (NULL != bspButtonReleaseHandle)
        bspButtonReleaseHandle(BSP_KEY0);
      HAL_EnableIrq(IRQ_6);
    }
    else
    {
      bspPostTask(BSP_BUTTONS);
    }
  }

  if (buttonsAction.wasPressed1)
  {
    buttonsAction.wasPressed1 = 0;
    buttonsAction.waitReleased1 = 1;
    if (NULL != bspButtonPressHandle)
      bspButtonPressHandle(BSP_KEY1);
  }

  if (buttonsAction.waitReleased1)
  {
    if (BSP_readKEY1())
    {
      buttonsAction.waitReleased1 = 0;
      buttonsAction.currentState1 = RELEASED;
      if (NULL != bspButtonReleaseHandle)
        bspButtonReleaseHandle(BSP_KEY1);
      HAL_EnableIrq(IRQ_7);
    }
    else
    {
      bspPostTask(BSP_BUTTONS);
    }
  }
}

#endif // APP_DISABLE_BSP != 1

// end of buttons.c