Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
Language: Cpp
BasedOnStyle: LLVM
UseTab: ForIndentation
TabWidth: 4
IndentWidth: 4
BreakBeforeBraces: Allman
ColumnLimit: 0
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
SortIncludes: false
...
69 changes: 69 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: CI

on:
push:
pull_request:

jobs:
format:
name: clang-format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check formatting
uses: jidicula/clang-format-action@v4.13.0
with:
clang-format-version: "17"
check-path: "."

lint:
name: arduino-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Lint library
uses: arduino/arduino-lint-action@v1
with:
# Button is already in the Library Manager index, so lint against the
# rules for libraries being updated.
library-manager: update
compliance: strict

compile:
name: compile examples
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- fqbn: arduino:avr:uno
platforms: |
- name: arduino:avr
- fqbn: esp8266:esp8266:generic
platforms: |
- name: esp8266:esp8266
source-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json
steps:
- uses: actions/checkout@v4
- name: Compile example sketches
uses: arduino/compile-sketches@v1
with:
fqbn: ${{ matrix.fqbn }}
platforms: ${{ matrix.platforms }}
libraries: |
- source-path: ./
sketch-paths: |
- examples/basic_usage

test:
name: native unit tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Install PlatformIO
run: pip install --upgrade platformio
- name: Run tests
run: pio test -e native
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.pio/
.pioenvs/
.piolibdeps/
.vscode/
*.o
*.a
33 changes: 0 additions & 33 deletions Button.h

This file was deleted.

19 changes: 12 additions & 7 deletions examples/basic_usage/basic_usage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ Button button1(2); // Connect your button between pin 2 and GND
Button button2(3); // Connect your button between pin 3 and GND
Button button3(4); // Connect your button between pin 4 and GND

void setup() {
void setup()
{
button1.begin();
button2.begin();
button3.begin();

while (!Serial) { }; // for Leos

while (!Serial)
{
}; // for Leos
Serial.begin(9600);
}

void loop() {
void loop()
{
if (button1.pressed())
Serial.println("Button 1 pressed");

if (button2.released())
Serial.println("Button 2 released");

if (button3.toggled()) {

if (button3.toggled())
{
if (button3.read() == Button::PRESSED)
Serial.println("Button 3 has been pressed");
else
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Button KEYWORD1
# Methods and Functions (KEYWORD2)
#######################################

begin KEYWORD2
read KEYWORD2
toggled KEYWORD2
pressed KEYWORD2
Expand Down
19 changes: 19 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "Button",
"version": "1.2.0",
"description": "Button is a tiny library to make reading buttons very simple. It handles debouncing automatically, and monitoring of state.",
"keywords": "button, debounce, input, switch",
"repository": {
"type": "git",
"url": "https://github.com/madleech/Button.git"
},
"authors": [
{
"name": "Michael Adams",
"url": "https://github.com/madleech"
}
],
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"
}
8 changes: 4 additions & 4 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=Button
version=1.1.0
author=Michael Adams <arduino@michaeladams.org>
maintainer=Michael Adams <arduino@michaeladams.org>
version=1.2.0
author=Michael Adams <github@michaeladams.org>
maintainer=Michael Adams <github@michaeladams.org>
sentence=Button is a tiny library to make reading buttons very simple.
paragraph=It handles debouncing automatically, and monitoring of state.
category=Signal Input/Output
url=http://utrainia.com/
url=https://github.com/madleech/Button
architectures=*
18 changes: 18 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; PlatformIO project configuration for Button.
;
; pio test -e native -> run the native unit tests (mocks millis()/digitalRead())
; pio run -e uno -> compile-check against AVR (Arduino Uno)

[platformio]
src_dir = src

[env:native]
platform = native
lib_compat_mode = off
test_build_src = true
build_flags = -I test/mock

[env:uno]
platform = atmelavr
board = uno
framework = arduino
22 changes: 9 additions & 13 deletions Button.cpp → src/Button.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
/*
Button - a small library for Arduino to handle button debouncing
MIT licensed.
Button - a small library for Arduino to handle button debouncing

MIT licensed.
*/

#include "Button.h"
#include <Arduino.h>

Button::Button(uint8_t pin, uint16_t debounce_ms)
: _pin(pin)
, _delay(debounce_ms)
, _state(HIGH)
, _ignore_until(0)
, _has_changed(false)
: _pin(pin), _delay(debounce_ms), _state(HIGH), _ignore_until(0), _has_changed(false)
{
}

Expand All @@ -21,9 +17,9 @@ void Button::begin()
pinMode(_pin, INPUT_PULLUP);
}

//
//
// public methods
//
//

bool Button::read()
{
Expand All @@ -32,15 +28,15 @@ bool Button::read()
{
// ignore any changes during this period
}
// pin has changed

// pin has changed
else if (digitalRead(_pin) != _state)
{
_ignore_until = millis() + _delay;
_state = !_state;
_has_changed = true;
}

return _state;
}

Expand Down
33 changes: 33 additions & 0 deletions src/Button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Button - a small library for Arduino to handle button debouncing

MIT licensed.
*/

#ifndef Button_h
#define Button_h
#include "Arduino.h"

class Button
{
public:
Button(uint8_t pin, uint16_t debounce_ms = 100);
void begin();
bool read();
bool toggled();
bool pressed();
bool released();
bool has_changed();

const static bool PRESSED = LOW;
const static bool RELEASED = HIGH;

private:
uint8_t _pin;
uint16_t _delay;
bool _state;
uint32_t _ignore_until;
bool _has_changed;
};

#endif
27 changes: 27 additions & 0 deletions test/mock/Arduino.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Minimal Arduino.h mock for native unit tests.

Provides just enough of the Arduino runtime for Button to build and run
off-device. The test controls time via _mock_millis and the (single) pin
level via _mock_pin_state, both defined in the test translation unit.
*/

#ifndef _Button_test_Arduino_h
#define _Button_test_Arduino_h

#include <stdint.h>
#include <stddef.h>

#define LOW 0
#define HIGH 1
#define INPUT_PULLUP 2

// Controllable test state, defined by the test translation unit.
extern unsigned long _mock_millis;
extern int _mock_pin_state;

unsigned long millis();
void pinMode(uint8_t pin, uint8_t mode);
int digitalRead(uint8_t pin);

#endif
Loading
Loading