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
| #include "main.h" #include <stdint.h> #include "User_Systick_Config.h" #include "multi_button.h" #include <stdio.h> #include <signal.h> #include <stdlib.h> #include "SEGGER_RTT.h"
void SysTick_Handler(void) { button_ticks(); }
void Delay(uint32_t count) { for (; count > 0; count--) ; }
static Button btn1, btn2; static volatile int running = 1;
static int btn1_state = 0; static int btn2_state = 0;
void signal_handler(int sig) { if (sig == SIGINT) { printf("\nReceived SIGINT, exiting...\n"); running = 0; } }
uint8_t read_button_gpio(uint8_t button_id) { switch (button_id) { case 1: return (GPIO_ReadInputDataBit(GPIOA, GPIO_PIN_15) == Bit_SET); case 2: return btn2_state; default: return 0; } }
void btn1_single_click_handler(Button* btn) { (void)btn; printf("Button 1: Single Click\n"); }
void btn1_double_click_handler(Button* btn) { (void)btn; printf("Button 1: Double Click\n"); }
void btn1_long_press_start_handler(Button* btn) { (void)btn; printf("Button 1: Long Press Start\n"); }
void btn1_long_press_hold_handler(Button* btn) { (void)btn; printf("Button 1: Long Press Hold...\n"); }
void btn1_press_repeat_handler(Button* btn) { printf("Button 1: Press Repeat (count: %d)\n", button_get_repeat_count(btn)); }
void buttons_init(void) { button_init(&btn1, read_button_gpio, 0, 1); button_attach(&btn1, BTN_SINGLE_CLICK, btn1_single_click_handler); button_attach(&btn1, BTN_DOUBLE_CLICK, btn1_double_click_handler); button_attach(&btn1, BTN_LONG_PRESS_START, btn1_long_press_start_handler); button_attach(&btn1, BTN_LONG_PRESS_HOLD, btn1_long_press_hold_handler); button_attach(&btn1, BTN_PRESS_REPEAT, btn1_press_repeat_handler); button_start(&btn1);
}
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 main(void) { buttons_init(); Systick_MS_Config(SystemCoreClock);
KeyInputExtiInit(GPIOA, GPIO_PIN_15);
while (1) {
} }
|