Skip to content
/ button Public

A lightweight MCU button library with debouncer.

License

Notifications You must be signed in to change notification settings

hmmbug/button

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Button Library

This is a simple button library for embedded systems. It provides an easy way to handle button presses and releases.

Features

  • Debouncing
  • Short and long press detection
  • Multiple button support

Installation

To use this library, include the button.h file in your project.

Usage

Connect the switch between an appropriate MCU pin and ground. An internal pull-up resistor will be used.

#include "button.h"

setup() {
    Button btn = Button(2);         // Use PIN 2
    btn.setShiftRegisterSize(12);   // optional
    btn.setMinPressDuration(10);    // optional
}

loop() {
    uint8_t btn_state = btn.read();
    switch(btn_state) {
        case STATE_SHORTPRESS:
            // there was a short press
            break;
        case STATE_LONGPRESS:
            // there was a long press
            break;
    }
}

API

void Button::setShiftRegisterSize(uint8_t size)

Sets the size of the shift register used for debouncing.

  • Parameters:
    • size: The size of the shift register.

void Button::setMinPressDuration(uint16_t d)

Sets the minimum duration for a press to be considered valid. Setting this to zero effectively disables checks for short presses.

  • Parameters:
    • d: The minimum press duration in milliseconds.

void Button::setLongPressDuration(uint16_t d)

Sets the minimum press duration of a long press.

  • Parameters:
    • d: The long press duration in milliseconds.

uint8_t Button::read()

Reads the current state of the button.

  • Returns:
    • The current state of the button, which can be STATE_SHORTPRESS, STATE_LONGPRESS, STATE_NOPRESS.

uint16_t Button::lastduration()

Gets the duration (ms) of the last press.

  • Returns:
    • The duration (ms) of the last press.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Releases

No releases published

Packages

No packages published

Languages