Init
This commit is contained in:
57
button.c
Normal file
57
button.c
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2017-2024 Oleg Borodin <onborodin@gmail.com>
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
#include <button.h>
|
||||
#include <tool.h>
|
||||
|
||||
static bool button_is_pressed(button_t *button);
|
||||
|
||||
void button_init(button_t *button, uintptr_t ddraddr, uintptr_t portaddr, uintptr_t pinaddr, uint8_t outnum) {
|
||||
button->ddraddr = ddraddr;
|
||||
button->portaddr = portaddr;
|
||||
button->pinaddr = pinaddr;
|
||||
button->outnum = outnum;
|
||||
}
|
||||
|
||||
void button_setup(button_t *button) {
|
||||
REG_SETDOWN_BIT(_MMIO_BYTE(button->ddraddr), button->outnum);
|
||||
REG_SETUP_BIT(_MMIO_BYTE(button->portaddr), button->outnum);
|
||||
}
|
||||
|
||||
static bool button_is_pressed(button_t *button) {
|
||||
if (REG_BIT_VALUE(_MMIO_BYTE(button->pinaddr), button->outnum)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool button_was_pressed(button_t *button) {
|
||||
bool was_pressed = button->was_pressed;
|
||||
if (was_pressed) {
|
||||
button->was_pressed = false;
|
||||
button->push_time = 0;
|
||||
}
|
||||
return was_pressed;
|
||||
}
|
||||
|
||||
void button_reset(button_t *button) {
|
||||
button->was_pressed = false;
|
||||
button->push_time = 0;
|
||||
}
|
||||
|
||||
#define BUTTON_PRESS_TIME 30
|
||||
|
||||
void button_handle(button_t *button) {
|
||||
if (button_is_pressed(button)) {
|
||||
button->push_time++;
|
||||
}
|
||||
if (button->push_time > BUTTON_PRESS_TIME) {
|
||||
button->was_pressed = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user