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
| #include "main.h" #include "log.h" #include <stdint.h> #include "User_Systick_Config.h" #include "bits_button.h" #include <stdio.h> #include <signal.h> #include <stdlib.h> #include "SEGGER_RTT.h"
typedef enum { USER_BUTTON_0 = 0, USER_BUTTON_1, USER_BUTTON_2, USER_BUTTON_3, USER_BUTTON_4, USER_BUTTON_5, USER_BUTTON_6, USER_BUTTON_7, USER_BUTTON_8, USER_BUTTON_9, USER_BUTTON_INVALID, USER_BUTTON_MAX,
USER_BUTTON_COMBO_0 = 0x100, USER_BUTTON_COMBO_1, USER_BUTTON_COMBO_2, USER_BUTTON_COMBO_3, USER_BUTTON_COMBO_MAX, } user_button_t;
static const bits_btn_obj_param_t defaul_param = {.long_press_period_triger_ms = BITS_BTN_LONG_PRESS_PERIOD_TRIGER_MS, .long_press_start_time_ms = BITS_BTN_LONG_PRESS_START_TIME_MS, .short_press_time_ms = BITS_BTN_SHORT_TIME_MS, .time_window_time_ms = BITS_BTN_TIME_WINDOW_TIME_MS}; button_obj_t btns[] = { BITS_BUTTON_INIT(USER_BUTTON_0, 0, &defaul_param), BITS_BUTTON_INIT(USER_BUTTON_1, 1, &defaul_param), };
button_obj_combo_t btns_combo[] = { BITS_BUTTON_COMBO_INIT( USER_BUTTON_COMBO_0, 0, &defaul_param, ((uint16_t[]){USER_BUTTON_0, USER_BUTTON_1}), 2, 1), };
uint8_t read_key_state(struct button_obj_t *btn) { uint8_t _id = btn->key_id; switch(_id) { case USER_BUTTON_0: return (GPIO_ReadInputDataBit(GPIOA, GPIO_PIN_15) == Bit_SET); break;
default: return 0; break; }
return 0; }
void SysTick_Handler(void) { bits_button_ticks(); }
void Delay(uint32_t count) { for (; count > 0; count--) ; }
void KeyInputExtiInit(GPIO_Module* GPIOx, uint16_t Pin) { GPIO_InitType GPIO_InitStructure; EXTI_InitType EXTI_InitStructure; NVIC_InitType NVIC_InitStructure;
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);
GPIO_InitStruct(&GPIO_InitStructure); GPIO_InitStructure.Pin = Pin; GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Input; GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
}
int my_log_printf(const char* format, ...) {
va_list args; va_start(args, format); int result = vprintf(format, args); va_end(args);
return result; }
int main(void) { bits_button_init(btns, ARRAY_SIZE(btns), btns_combo, ARRAY_SIZE(btns_combo), read_key_state, NULL, my_log_printf);
log_init(); Systick_MS_Config(SystemCoreClock);
KeyInputExtiInit(GPIOA, GPIO_PIN_15);
while (1) { bits_btn_result_t result = {0}; int32_t res = bits_button_get_key_result(&result);
if(res == true) {
} } }
|