STM32F4DoorControl/buttons.c

55 lines
1.3 KiB
C

#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <stdint.h>
#include <stdio.h>
#include "buttons.h"
#define BUTTON_RCC RCC_GPIOA
#define BUTTON_PORT GPIOA
#define OPEN_BUTTON GPIO6
#define CLOSE_BUTTON GPIO7
void buttons_setup() {
rcc_periph_clock_enable(BUTTON_RCC);
gpio_mode_setup(BUTTON_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, OPEN_BUTTON | CLOSE_BUTTON);
gpio_set_output_options(BUTTON_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, OPEN_BUTTON | CLOSE_BUTTON);
}
static uint32_t open_ticks = 0;
static uint32_t close_ticks = 0;
void buttons_open(uint32_t ticks) {
gpio_set(BUTTON_PORT, OPEN_BUTTON);
printf("Open %lu", ticks);
open_ticks = ticks;
}
void buttons_close(uint32_t ticks) {
gpio_set(BUTTON_PORT, CLOSE_BUTTON);
printf("Close %lu", ticks);
close_ticks = ticks;
}
void buttons_release(void) {
open_ticks = close_ticks = 0;
printf("Release");
gpio_clear(BUTTON_PORT, OPEN_BUTTON | CLOSE_BUTTON);
}
void buttons_tick(void) {
if (open_ticks) {
open_ticks--;
if (!open_ticks) {
gpio_clear(BUTTON_PORT, OPEN_BUTTON);
printf("Release open");
}
}
if (close_ticks) {
close_ticks--;
if (!close_ticks) {
printf("Release close");
gpio_clear(BUTTON_PORT, CLOSE_BUTTON);
}
}
}