SLS AVR Lib 0.1a
 
Loading...
Searching...
No Matches
button.h File Reference

A AVR helper for working with individual buttons. More...

#include <string.h>
#include <stdint.h>
#include <util/atomic.h>
#include <sls-avr/avr.h>

Go to the source code of this file.

Data Structures

struct  btn_info_struct
 Information about a button. More...
 

Macros

#define BTN_MAX_SCAN_COUNT   254U
 Limit on the number of cycles for which a press is considered complete.
 
#define BTN_LONG_COUNT   40U
 The number that is compared to the counter of inputs to the btn_proc function in the pressed state 2-254. When the set value is reached, the button can receive the long pressed status.
 
#define BTN_ALLOW_LONG   0
 Allows long pressed status.
 
#define BTN_RESET_UNUSED_COUNT   0
 The number that is compared to the counter of unused click 0-254. When the set value is reached, the btn_info_struct structure is reset to its default state.
 
#define BTN_DOWN_COUNT   3U
 The number that is compared to the counter of inputs to the btn_proc function in the pressed state 1-254. When the set value is reached, the button can receive the pressed status.
 
#define BTN_UP_COUNT   2U
 The number that is compared to the counter of inputs to the btn_proc function in the unpressed state 0-254. When the set value is reached, the button takes on the triggered status.
 
#define BTN_FAST_SOME_CODE   0
 If enabled then in the btn_reset function, instead of resetting, only the processing flag is set.
 
#define BTN_INFO_STATE_DEFAULT   0
 Default state value. See btn_info_struct.
 
#define BTN_INFO_STRUCT_DEFAULT   {._counter = 0, ._up_counter = 0, .state = BTN_INFO_STATE_DEFAULT}
 Default button information structure btn_info_struct.
 
#define btn_is_holded(_state)
 Is button holded.
 
#define btn_is_ready(_state)
 Completed but unprocessed click.
 
#define btn_is_clicked(_state)
 Tests normal click.
 
#define btn_is_long_clicked(_state)
 Tests long click.
 
#define btn_set_processed(_state)
 Is the button taken into account.
 
#define BTN_ATOMIC_FUNCTIONS   0
 Allow ATOMIC_BLOCK wrappers for button processing.
 

Typedefs

typedef volatile btn_info_struct btn_info_t
 Information about a button type. See btn_info_struct.
 

Functions

static void btn_reset (btn_info_t *const btn_info)
 Resets the button state to default.
 
void btn_proc (btn_info_t *const btn_info, const bool is_now_hold)
 Update counters and button press stages for a button information structure.
 
static void btn_reset_atomic (btn_info_t *const btn_info)
 Such as the btn_reset, but executed atomically.
 
static void btn_reset_atomic_fegi (btn_info_t *const btn_info)
 Such as the btn_reset, but executed atomically with force enabling ot the global interrupts.
 
static void btn_proc_atomic (btn_info_t *const btn_info, const bool is_now_hold)
 Such as the btn_proc, but executed atomically.
 
static void btn_proc_atomic_fegi (btn_info_t *const btn_info, const bool is_now_hold)
 Such as the btn_proc, but executed atomically with force enabling ot the global interrupts.
 
static btn_info_struct btn_get_info (btn_info_t *const btn_info)
 Actions before events proc.
 
static btn_info_struct btn_get_info_fegi (btn_info_t *const btn_info)
 Such as the btn_get_info, but with force enabling ot the global interrupts.
 

Detailed Description

A AVR helper for working with individual buttons.

Author
Simon Litt simon.nosp@m.@1it.nosp@m.t.net https://coding.1itt.net, https://github.com/SimonLitt
#include <sls-avr/avr.h>
...
void init_buttons(void) {
PIN_TO_READ(BTN_1_PORT, BTN_1_PIN);
PIN_TO_READ(BTN_2_PORT, BTN_2_PIN);
}
void buttons_loop(void) {
static btn_info_t btn_1_info = BTN_INFO_STRUCT_DEFAULT;
btn_proc(&btn_1_info, !(PIN_READ(BTN_1_PORT, BTN_1_PIN)));
if (btn_is_clicked(btn_1_info.state)) {
on_click1();
btn_reset(&btn_1_info);
}
static btn_info_t btn_2_info = BTN_INFO_STRUCT_DEFAULT;
btn_proc(&btn_2_info, !(PIN_READ(BTN_2_PORT, BTN_2_PIN)));
if (btn_is_clicked(btn_2_info.state)) {
on_click2();
btn_reset(&btn_2_info);
}
}
...
AVR lib.
static void btn_reset(btn_info_t *const btn_info)
Resets the button state to default.
Definition button.h:226
volatile btn_info_struct btn_info_t
Information about a button type. See btn_info_struct.
Definition button.h:166
#define BTN_INFO_STRUCT_DEFAULT
Default button information structure btn_info_struct.
Definition button.h:171
#define btn_is_clicked(_state)
Tests normal click.
Definition button.h:190
void btn_proc(btn_info_t *const btn_info, const bool is_now_hold)
Update counters and button press stages for a button information structure.
#define PIN_TO_READ
Definition gpio.h:497
#define PIN_READ(_p, _b)
Definition gpio.h:464
byte_t state
Stages and states flags of click processing.
Definition button.h:163

Macro Definition Documentation

◆ BTN_ALLOW_LONG

#define BTN_ALLOW_LONG   0

Allows long pressed status.

◆ BTN_ATOMIC_FUNCTIONS

#define BTN_ATOMIC_FUNCTIONS   0

Allow ATOMIC_BLOCK wrappers for button processing.

Remarks
Sometimes it's more convenient to handle keystrokes within an interrupt. However, it's not recommended to linger in an interrupt for too long.

◆ BTN_DOWN_COUNT

#define BTN_DOWN_COUNT   3U

The number that is compared to the counter of inputs to the btn_proc function in the pressed state 1-254. When the set value is reached, the button can receive the pressed status.

◆ BTN_FAST_SOME_CODE

#define BTN_FAST_SOME_CODE   0

If enabled then in the btn_reset function, instead of resetting, only the processing flag is set.

Remarks
This allows for the same code to be achieved with the BTN_UP_COUNT macro on and off, but disables re-triggering.

◆ BTN_INFO_STATE_DEFAULT

#define BTN_INFO_STATE_DEFAULT   0

Default state value. See btn_info_struct.

◆ BTN_INFO_STRUCT_DEFAULT

#define BTN_INFO_STRUCT_DEFAULT   {._counter = 0, ._up_counter = 0, .state = BTN_INFO_STATE_DEFAULT}

Default button information structure btn_info_struct.

◆ btn_is_clicked

#define btn_is_clicked ( _state)
Value:
(flag_is_set((_state), __BTN_STATE_SHORT_CLICK_BIT))
#define flag_is_set(_val, _b)
Test whether bit _b in flags _val is set.
Definition defs.h:141

Tests normal click.

Parameters
_stateStages of click processing
Returns
Is clicked

◆ btn_is_holded

#define btn_is_holded ( _state)
Value:
((_state) & (__BTN_IS_HOLDED))

Is button holded.

Parameters
_stateStages of click processing
Returns
Is holded

◆ btn_is_long_clicked

#define btn_is_long_clicked ( _state)

Tests long click.

Parameters
_stateStages of click processing
Returns
Is long clicked

◆ btn_is_ready

#define btn_is_ready ( _state)
Value:
((_state) & (__BTN_IS_CLICKED))

Completed but unprocessed click.

Parameters
_stateStages of click processing
Returns
Is clicked

◆ BTN_LONG_COUNT

#define BTN_LONG_COUNT   40U

The number that is compared to the counter of inputs to the btn_proc function in the pressed state 2-254. When the set value is reached, the button can receive the long pressed status.

◆ BTN_MAX_SCAN_COUNT

#define BTN_MAX_SCAN_COUNT   254U

Limit on the number of cycles for which a press is considered complete.

◆ BTN_RESET_UNUSED_COUNT

#define BTN_RESET_UNUSED_COUNT   0

The number that is compared to the counter of unused click 0-254. When the set value is reached, the btn_info_struct structure is reset to its default state.

◆ btn_set_processed

#define btn_set_processed ( _state)
Value:
((_state) = ((_state) & ~(__BTN_IS_CLICKED)) | _BTN_STAGE_PROCESSED)

Is the button taken into account.

It is necessary so that when released, the status does not change to clicked. Used when the button can be a macro button(function modifier).

Parameters
_stateStages of click processing

◆ BTN_UP_COUNT

#define BTN_UP_COUNT   2U

The number that is compared to the counter of inputs to the btn_proc function in the unpressed state 0-254. When the set value is reached, the button takes on the triggered status.

Remarks
If the value is O then the click is considered to have been counted even before the button is released. In this case, resetting the state will lead to a re-trigger, if this is undesirable, it needs to set the processing flag see btn_set_processed.

Typedef Documentation

◆ btn_info_t

typedef volatile btn_info_struct btn_info_t

Information about a button type. See btn_info_struct.

Function Documentation

◆ btn_get_info()

static btn_info_struct btn_get_info ( btn_info_t *const btn_info)
inlinestatic

Actions before events proc.

Parameters
[out]btn_infoInformation about a button.
Returns
Currently copy of btn_info_struct

◆ btn_get_info_fegi()

static btn_info_struct btn_get_info_fegi ( btn_info_t *const btn_info)
inlinestatic

Such as the btn_get_info, but with force enabling ot the global interrupts.

Parameters
[out]btn_infoInformation about a button.

◆ btn_proc()

void btn_proc ( btn_info_t *const btn_info,
const bool is_now_hold )

Update counters and button press stages for a button information structure.

If not called from an interrupt, or if other interrupts are enabled in the interrupt, then it should be executed atomically.

Parameters
[out]btn_infoInformation about a button.
[in]is_now_holdIs the button currently pressed?

◆ btn_proc_atomic()

static void btn_proc_atomic ( btn_info_t *const btn_info,
const bool is_now_hold )
inlinestatic

Such as the btn_proc, but executed atomically.

Parameters
[out]btn_infoInformation about a button.
[in]is_now_holdIs the button currently pressed?

◆ btn_proc_atomic_fegi()

static void btn_proc_atomic_fegi ( btn_info_t *const btn_info,
const bool is_now_hold )
inlinestatic

Such as the btn_proc, but executed atomically with force enabling ot the global interrupts.

Parameters
[out]btn_infoInformation about a button.
[in]is_now_holdIs the button currently pressed?

◆ btn_reset()

static void btn_reset ( btn_info_t *const btn_info)
inlinestatic

Resets the button state to default.

Parameters
[out]btn_infoInformation about a button.

◆ btn_reset_atomic()

static void btn_reset_atomic ( btn_info_t *const btn_info)
inlinestatic

Such as the btn_reset, but executed atomically.

Parameters
[out]btn_infoInformation about a button.

◆ btn_reset_atomic_fegi()

static void btn_reset_atomic_fegi ( btn_info_t *const btn_info)
inlinestatic

Such as the btn_reset, but executed atomically with force enabling ot the global interrupts.

Parameters
[out]btn_infoInformation about a button.