STM32F4DoorControl/buttons.c

63 lines
1.4 KiB
C
Raw Normal View History

2021-02-19 17:01:56 +00:00
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <stdint.h>
#include <stdio.h>
#include "buttons.h"
2021-07-16 19:21:45 +00:00
#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
2021-02-19 17:01:56 +00:00
void buttons_setup() {
2021-07-16 19:21:45 +00:00
#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
2021-07-16 22:52:42 +00:00
buttons_release();
2021-02-19 17:01:56 +00:00
}
static uint32_t open_ticks = 0;
static uint32_t close_ticks = 0;
void buttons_open(uint32_t ticks) {
2021-07-16 19:21:45 +00:00
gpio_clear(OPEN_PORT, OPEN_BUTTON);
2021-02-19 17:01:56 +00:00
printf("Open %lu", ticks);
open_ticks = ticks;
}
void buttons_close(uint32_t ticks) {
2021-07-16 19:21:45 +00:00
gpio_clear(CLOSE_PORT, CLOSE_BUTTON);
2021-02-19 17:01:56 +00:00
printf("Close %lu", ticks);
close_ticks = ticks;
}
void buttons_release(void) {
open_ticks = close_ticks = 0;
printf("Release");
2021-07-16 19:21:45 +00:00
gpio_set(OPEN_PORT, OPEN_BUTTON);
gpio_set(CLOSE_PORT, CLOSE_BUTTON);
2021-02-19 17:01:56 +00:00
}
void buttons_tick(void) {
if (open_ticks) {
open_ticks--;
if (!open_ticks) {
2021-07-16 19:21:45 +00:00
gpio_set(OPEN_PORT, OPEN_BUTTON);
2021-02-19 17:01:56 +00:00
printf("Release open");
}
}
if (close_ticks) {
close_ticks--;
if (!close_ticks) {
printf("Release close");
2021-07-16 22:52:42 +00:00
gpio_set(CLOSE_PORT, CLOSE_BUTTON);
2021-02-19 17:01:56 +00:00
}
}
}