summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/BSP/RCB/src/buttons.c
diff options
context:
space:
mode:
authorFlorent Duchon2012-12-26 17:36:00 +0100
committerFlorent Duchon2013-02-13 21:21:12 +0100
commitb24866225a6301d3a663f874725e83c012dc25d3 (patch)
treeca527a2aab9abcdfbaf244c53ca63f0c531892b0 /digital/zigbit/bitcloud/stack/Components/BSP/RCB/src/buttons.c
parent2ba279f4eb2f23fa08a7c13465d16ae6ba5d0f96 (diff)
digital/beacon: add bitcloud stack into common directory digital/zigbit
Diffstat (limited to 'digital/zigbit/bitcloud/stack/Components/BSP/RCB/src/buttons.c')
-rw-r--r--digital/zigbit/bitcloud/stack/Components/BSP/RCB/src/buttons.c187
1 files changed, 187 insertions, 0 deletions
diff --git a/digital/zigbit/bitcloud/stack/Components/BSP/RCB/src/buttons.c b/digital/zigbit/bitcloud/stack/Components/BSP/RCB/src/buttons.c
new file mode 100644
index 00000000..8139394a
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/BSP/RCB/src/buttons.c
@@ -0,0 +1,187 @@
+/**************************************************************************//**
+\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:
+ 21.08.09 A. Taradov - 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_E5_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 wasPressed0 : 1;
+ uint8_t waitReleased0 : 1;
+} BSP_buttonsAction_t;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+/**************************************************************************//**
+\brief HAL's event handlers about KEY 0 has changed state.
+******************************************************************************/
+void bspKey0InterruptHandler(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)
+{
+ GPIO_E5_make_in();
+ GPIO_E5_make_pullup();
+
+ HAL_RegisterIrq(IRQ_5, IRQ_LOW_LEVEL, bspKey0InterruptHandler);
+
+ if (BSP_readKEY0())
+ buttonsAction.currentState0 = RELEASED;
+ else
+ buttonsAction.currentState0 = PRESSED;
+
+ HAL_EnableIrq(IRQ_5);
+}
+
+/**************************************************************************//**
+\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_5);
+ 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;
+
+ return state;
+}
+
+/**************************************************************************//**
+\brief HAL's event about KEY has changed state.
+******************************************************************************/
+void bspKey0InterruptHandler(void)
+{
+ HAL_DisableIrq(IRQ_5);
+ buttonsAction.currentState0 = PRESSED;
+ buttonsAction.wasPressed0 = 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_5);
+ }
+ else
+ {
+ bspPostTask(BSP_BUTTONS);
+ }
+ }
+}
+
+#endif // APP_DISABLE_BSP != 1
+
+// end of buttons.c