54#ifndef SLS_AVR_SINGLE_BUTTON_H_
55#define SLS_AVR_SINGLE_BUTTON_H_
59#include <util/atomic.h>
62#define BTN_MAX_SCAN_COUNT 254U
65# define BTN_LONG_COUNT 40U
66# if (BTN_LONG_COUNT > BTN_MAX_SCAN_COUNT)
67# error "Long Pressed state counter should be less or equal to 254!"
69# if (BTN_LONG_COUNT <= BTN_DOWN_COUNT)
70# error "Long Pressed state counter should be more then #BTN_DOWN_COUNT!"
75# define BTN_ALLOW_LONG 0
78#ifndef BTN_RESET_UNUSED_COUNT
79# define BTN_RESET_UNUSED_COUNT 0
81#if (BTN_RESET_UNUSED_COUNT < 0) || (BTN_RESET_UNUSED_COUNT > BTN_MAX_SCAN_COUNT)
82# error "Reset counter should be in range 0-254!"
86#define BTN_DOWN_COUNT 3U
89#if (BTN_DOWN_COUNT < 1) || (BTN_DOWN_COUNT > BTN_MAX_SCAN_COUNT)
90# error "Pressed state counter should be in range 2-254!"
94# define BTN_UP_COUNT 2U
96#if (BTN_UP_COUNT < 0) || (BTN_UP_COUNT > BTN_MAX_SCAN_COUNT)
97# error "Released state counter should be in range 1-254!"
103# error "Long presses are incompatible with fast processing!"
105# define __BTN_ALLOW_FAST_SCAN
109#ifndef BTN_FAST_SOME_CODE
110# define BTN_FAST_SOME_CODE 0
114#if BTN_FAST_SOME_CODE && (defined(__BTN_ALLOW_FAST_SCAN) || !BTN_ALLOW_LONG)
115# define __BTN_LONG_DEFINITIONS
123#define __BTN_STAGE_MAY_SHORT_BIT 0
125# define __BTN_STAGE_MAY_LONG_BIT 1
127#define __BTN_STAGE_PROCESSED_BIT 2
129#define __BTN_STATE_SHORT_CLICK_BIT 4
131# define __BTN_STATE_LONG_CLICK_BIT 5
137#define _BTN_STAGE_MAY_SHORT (_BV(__BTN_STAGE_MAY_SHORT_BIT))
138#define _BTN_STAGE_MAY_LONG (_BV(__BTN_STAGE_MAY_LONG_BIT))
139#define _BTN_STAGE_PROCESSED (_BV(__BTN_STAGE_PROCESSED_BIT))
142#define _BTN_STATE_SHORT_CLICK (_BV(__BTN_STATE_SHORT_CLICK_BIT))
144# define _BTN_STATE_LONG_CLICK (_BV(__BTN_STATE_LONG_CLICK_BIT))
148# define __BTN_IS_CLICKED (_BTN_STATE_SHORT_CLICK | _BTN_STATE_LONG_CLICK)
149# define __BTN_IS_HOLDED (_BTN_STAGE_MAY_SHORT | _BTN_STAGE_MAY_LONG)
151# define __BTN_IS_CLICKED _BTN_STATE_SHORT_CLICK
152# define __BTN_IS_HOLDED _BTN_STAGE_MAY_SHORT
160 #ifndef __BTN_ALLOW_FAST_SCAN
168#define BTN_INFO_STATE_DEFAULT 0
170#ifndef __BTN_ALLOW_FAST_SCAN
171# define BTN_INFO_STRUCT_DEFAULT {._counter = 0, ._up_counter = 0, .state = BTN_INFO_STATE_DEFAULT}
173# define BTN_INFO_STRUCT_DEFAULT {._counter = 0, .state = BTN_INFO_STATE_DEFAULT}
179#define btn_is_holded(_state) ((_state) & (__BTN_IS_HOLDED))
185#define btn_is_ready(_state) ((_state) & (__BTN_IS_CLICKED))
190#define btn_is_clicked(_state) (flag_is_set((_state), __BTN_STATE_SHORT_CLICK_BIT))
192#if defined(__DOXYGEN__)
197# define btn_is_long_clicked(_state)
201# define btn_is_long_clicked(_state) (flag_is_set((_state), __BTN_STATE_LONG_CLICK_BIT))
202#elifdef __BTN_LONG_DEFINITIONS
203# define btn_is_long_clicked(_state) false
210#define btn_set_processed(_state) ((_state) = ((_state) & ~(__BTN_IS_CLICKED)) | _BTN_STAGE_PROCESSED)
212#ifndef BTN_ATOMIC_FUNCTIONS
213# define BTN_ATOMIC_FUNCTIONS 0
217static inline void _btn_reset(
btn_info_t *
const btn_info) {
219 (*btn_info) = _def_btn_info_struct;
227 #if defined(__BTN_ALLOW_FAST_SCAN) && BTN_FAST_SOME_CODE
230 _btn_reset(btn_info);
242#if defined(__DOXYGEN__)
277#if BTN_ATOMIC_FUNCTIONS
279 #if defined(__BTN_ALLOW_FAST_SCAN) && BTN_FAST_SOME_CODE
282 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
283 _btn_reset(btn_info);
289 #if defined(__BTN_ALLOW_FAST_SCAN) && BTN_FAST_SOME_CODE
292 ATOMIC_BLOCK(ATOMIC_FORCEON) {
293 _btn_reset(btn_info);
299 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
305 ATOMIC_BLOCK(ATOMIC_FORCEON) {
312 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
313 info_struct = *btn_info;
320 ATOMIC_BLOCK(ATOMIC_FORCEON) {
321 info_struct = *btn_info;
uint8_t byte_t
8-bit type
Definition defs.h:64
Information about a button.
Definition button.h:158
byte_t state
Stages and states flags of click processing.
Definition button.h:163
uint8_t _counter
Clamped or released state cycle counter.
Definition button.h:159
uint8_t _up_counter
Counter of cycles of released state after pressing.
Definition button.h:161