STM32F4DoorControl/buttons.c

63 lines
1.4 KiB
C

#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <stdint.h>
#include <stdio.h>
#include "buttons.h"
#define OPEN_RCC RCC_GPIOB
#define OPEN_PORT GPIOB
#define OPEN_BUTTON GPIO9
#define CLOSE_RCC RCC_GPIOC
#define CLOSE_PORT GPIOC
#define CLOSE_BUTTON GPIO3
void buttons_setup() {
#define SETUP(BTN) \
rcc_periph_clock_enable(BTN ## _RCC); \
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
buttons_release();
}
static uint32_t open_ticks = 0;
static uint32_t close_ticks = 0;
void buttons_open(uint32_t ticks) {
gpio_clear(OPEN_PORT, OPEN_BUTTON);
printf("Open %lu", ticks);
open_ticks = ticks;
}
void buttons_close(uint32_t ticks) {
gpio_clear(CLOSE_PORT, CLOSE_BUTTON);
printf("Close %lu", ticks);
close_ticks = ticks;
}
void buttons_release(void) {
open_ticks = close_ticks = 0;
printf("Release");
gpio_set(OPEN_PORT, OPEN_BUTTON);
gpio_set(CLOSE_PORT, CLOSE_BUTTON);
}
void buttons_tick(void) {
if (open_ticks) {
open_ticks--;
if (!open_ticks) {
gpio_set(OPEN_PORT, OPEN_BUTTON);
printf("Release open");
}
}
if (close_ticks) {
close_ticks--;
if (!close_ticks) {
printf("Release close");
gpio_set(CLOSE_PORT, CLOSE_BUTTON);
}
}
}