Make buttons setup easier to change

This commit is contained in:
Lynn Ochs 2021-07-16 21:21:45 +02:00
parent aa653aecea
commit 2f78a78ed8

View File

@ -4,28 +4,34 @@
#include <stdio.h> #include <stdio.h>
#include "buttons.h" #include "buttons.h"
#define BUTTON_RCC RCC_GPIOA #define OPEN_RCC RCC_GPIOB
#define BUTTON_PORT GPIOA #define OPEN_PORT GPIOB
#define OPEN_BUTTON GPIO6 #define OPEN_BUTTON GPIO9
#define CLOSE_BUTTON GPIO7 #define CLOSE_RCC RCC_GPIOC
#define CLOSE_PORT GPIOC
#define CLOSE_BUTTON GPIO3
void buttons_setup() { void buttons_setup() {
rcc_periph_clock_enable(BUTTON_RCC); #define SETUP(BTN) \
gpio_mode_setup(BUTTON_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, OPEN_BUTTON | CLOSE_BUTTON); rcc_periph_clock_enable(BTN ## _RCC); \
gpio_set_output_options(BUTTON_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, OPEN_BUTTON | CLOSE_BUTTON); gpio_mode_setup(BTN ## _PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, BTN ## _BUTTON); \
gpio_set_output_options(BTN ## _PORT, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, BTN ## _BUTTON);
SETUP(OPEN);
SETUP(CLOSE);
#undef SETUP
} }
static uint32_t open_ticks = 0; static uint32_t open_ticks = 0;
static uint32_t close_ticks = 0; static uint32_t close_ticks = 0;
void buttons_open(uint32_t ticks) { void buttons_open(uint32_t ticks) {
gpio_clear(BUTTON_PORT, OPEN_BUTTON); gpio_clear(OPEN_PORT, OPEN_BUTTON);
printf("Open %lu", ticks); printf("Open %lu", ticks);
open_ticks = ticks; open_ticks = ticks;
} }
void buttons_close(uint32_t ticks) { void buttons_close(uint32_t ticks) {
gpio_clear(BUTTON_PORT, CLOSE_BUTTON); gpio_clear(CLOSE_PORT, CLOSE_BUTTON);
printf("Close %lu", ticks); printf("Close %lu", ticks);
close_ticks = ticks; close_ticks = ticks;
} }
@ -33,14 +39,15 @@ void buttons_close(uint32_t ticks) {
void buttons_release(void) { void buttons_release(void) {
open_ticks = close_ticks = 0; open_ticks = close_ticks = 0;
printf("Release"); printf("Release");
gpio_set(BUTTON_PORT, OPEN_BUTTON | CLOSE_BUTTON); gpio_set(OPEN_PORT, OPEN_BUTTON);
gpio_set(CLOSE_PORT, CLOSE_BUTTON);
} }
void buttons_tick(void) { void buttons_tick(void) {
if (open_ticks) { if (open_ticks) {
open_ticks--; open_ticks--;
if (!open_ticks) { if (!open_ticks) {
gpio_set(BUTTON_PORT, OPEN_BUTTON); gpio_set(OPEN_PORT, OPEN_BUTTON);
printf("Release open"); printf("Release open");
} }
} }
@ -48,7 +55,7 @@ void buttons_tick(void) {
close_ticks--; close_ticks--;
if (!close_ticks) { if (!close_ticks) {
printf("Release close"); printf("Release close");
gpio_set(BUTTON_PORT, CLOSE_BUTTON); gpio_set(OPEN_PORT, CLOSE_BUTTON);
} }
} }
} }