-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1190 from spark/feature/setup-button-mirror
MODE/SETUP button mirroring
- Loading branch information
Showing
33 changed files
with
717 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "button.h" | ||
|
||
extern __IO uint16_t BUTTON_DEBOUNCED_TIME[]; | ||
|
||
void BUTTON_Init_Ext() { | ||
if (BUTTON_Is_Pressed(BUTTON1)) | ||
TIM_ITConfig(TIM1, TIM_IT_CC4, ENABLE); | ||
} | ||
|
||
uint8_t BUTTON_Is_Pressed(Button_TypeDef button) { | ||
return BUTTON_GetState(BUTTON1) == BUTTON1_PRESSED; | ||
} | ||
|
||
uint16_t BUTTON_Pressed_Time(Button_TypeDef button) { | ||
return BUTTON_DEBOUNCED_TIME[BUTTON1]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef __BUTTON_H | ||
#define __BUTTON_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include "hw_config.h" | ||
|
||
void BUTTON_Init_Ext(); | ||
uint8_t BUTTON_Is_Pressed(Button_TypeDef button); | ||
uint16_t BUTTON_Pressed_Time(Button_TypeDef button); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* __BUTTON_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include "button.h" | ||
#include "dct.h" | ||
#include "hal_irq_flag.h" | ||
#include <string.h> | ||
|
||
/** | ||
* @brief This function handles BUTTON EXTI Handler. | ||
* @param None | ||
* @retval None | ||
*/ | ||
void BUTTON_Irq_Handler(uint16_t exti) | ||
{ | ||
if (EXTI_GetITStatus(exti) != RESET) | ||
{ | ||
/* Clear the EXTI line pending bit */ | ||
EXTI_ClearITPendingBit(exti); | ||
|
||
BUTTON_Check_Irq(BUTTON1, exti); | ||
BUTTON_Check_Irq(BUTTON1_MIRROR, exti); | ||
} | ||
} | ||
|
||
void BUTTON_Check_Irq(uint16_t button, uint16_t exti) { | ||
if (HAL_Buttons[button].exti_line == exti) | ||
{ | ||
HAL_Buttons[button].debounce_time = 0x00; | ||
HAL_Buttons[button].active = 1; | ||
|
||
/* Disable button Interrupt */ | ||
BUTTON_EXTI_Config(button, DISABLE); | ||
|
||
/* Enable TIM2 CC1 Interrupt */ | ||
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); | ||
} | ||
} | ||
|
||
void BUTTON_Check_State(uint16_t button, uint8_t pressed) { | ||
if (HAL_Buttons[button].exti_line && BUTTON_GetState(button) == pressed) | ||
{ | ||
if (!HAL_Buttons[button].active) | ||
HAL_Buttons[button].active = 1; | ||
HAL_Buttons[button].debounce_time += BUTTON_DEBOUNCE_INTERVAL; | ||
} | ||
else if (HAL_Buttons[button].active) | ||
{ | ||
HAL_Buttons[button].active = 0; | ||
/* Enable button Interrupt */ | ||
BUTTON_EXTI_Config(button, ENABLE); | ||
} | ||
} | ||
|
||
int BUTTON_Debounce() { | ||
BUTTON_Check_State(BUTTON1, BUTTON1_PRESSED); | ||
BUTTON_Check_State(BUTTON1_MIRROR, HAL_Buttons[BUTTON1_MIRROR].exti_trigger == EXTI_Trigger_Rising ? 1 : 0); | ||
|
||
int pressed = HAL_Buttons[BUTTON1].active + HAL_Buttons[BUTTON1_MIRROR].active; | ||
if (pressed == 0) { | ||
/* Disable TIM2 CC1 Interrupt */ | ||
TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE); | ||
} | ||
|
||
return pressed; | ||
} | ||
|
||
void BUTTON_Init_Ext() { | ||
const button_config_t* conf = (const button_config_t*)dct_read_app_data(DCT_MODE_BUTTON_MIRROR_OFFSET); | ||
|
||
if (conf->active == 0xAA && conf->debounce_time == 0xBBCC) { | ||
int32_t state = HAL_disable_irq(); | ||
memcpy((void*)&HAL_Buttons[BUTTON1_MIRROR], (void*)conf, sizeof(button_config_t)); | ||
HAL_Buttons[BUTTON1_MIRROR].active = 0; | ||
HAL_Buttons[BUTTON1_MIRROR].debounce_time = 0; | ||
BUTTON_Init(BUTTON1_MIRROR, BUTTON_MODE_EXTI); | ||
HAL_enable_irq(state); | ||
} | ||
|
||
if (BUTTON_Debounce()) | ||
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); | ||
} | ||
|
||
uint8_t BUTTON_Is_Pressed(Button_TypeDef button) { | ||
uint8_t pressed = 0; | ||
pressed = HAL_Buttons[button].active; | ||
|
||
if (button == BUTTON1 && HAL_Buttons[BUTTON1_MIRROR].exti_line) { | ||
pressed |= BUTTON_Is_Pressed(BUTTON1_MIRROR); | ||
} | ||
|
||
return pressed; | ||
} | ||
|
||
uint16_t BUTTON_Pressed_Time(Button_TypeDef button) { | ||
uint16_t pressed = 0; | ||
|
||
pressed = HAL_Buttons[button].debounce_time; | ||
if (button == BUTTON1 && HAL_Buttons[BUTTON1_MIRROR].exti_line) { | ||
if (BUTTON_Pressed_Time(BUTTON1_MIRROR) > pressed) | ||
pressed = BUTTON_Pressed_Time(BUTTON1_MIRROR); | ||
} | ||
|
||
return pressed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef __BUTTON_H | ||
#define __BUTTON_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include "hw_config.h" | ||
|
||
void BUTTON_Init_Ext(); | ||
uint8_t BUTTON_Is_Pressed(Button_TypeDef button); | ||
uint16_t BUTTON_Pressed_Time(Button_TypeDef button); | ||
|
||
|
||
void BUTTON_Irq_Handler(uint16_t exti); | ||
void BUTTON_Check_Irq(uint16_t button, uint16_t exti); | ||
void BUTTON_Check_State(uint16_t button, uint8_t pressed); | ||
int BUTTON_Debounce(); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* __BUTTON_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "../src/stm32f2xx/hal_irq_flag.c" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.