Commit bfbc7f9a authored by Adrien Oliva's avatar Adrien Oliva

Initial import

parents
This is a simple button manager for any embedded board that supports Arduino
framework.
See examples folder to know how to use.
This library is released under the terms of WTF-PL:
```
Copyright (C) 2017 Adrien Oliva <olivaa+gitlab@yapbreak.fr>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
```
#include <Arduino.h>
#include "button.h"
// Create a button connected to pin 4.
button_t my_button(4);
void click(void *arg)
{
Serial.println("Click");
}
void doubleclick(void *arg)
{
Serial.println("Double click");
}
void setup() {
Serial.begin(115200);
my_button.on_single_click(click, NULL);
my_button.on_double_click(doubleclick, NULL);
my_button.on_long_click([&](void *arg) {
Serial.println("Long click");
}, NULL);
}
void loop() {
my_button.loop();
}
#include <Arduino.h>
#include "button.h"
#define DOUBLECLICK_TIMER 250
#define LONGCLICK_TIMER 2000
button_t::button_t(const uint8_t pin)
: m_pin(pin)
{
pinMode(m_pin, INPUT);
}
button_t::~button_t()
{
}
void button_t::on_single_click(button_callback_t callback, void *arg)
{
m_single_click_callback = callback;
m_single_arg = arg;
}
void button_t::on_double_click(button_callback_t callback, void *arg)
{
m_double_click_callback = callback;
m_double_arg = arg;
}
void button_t::on_long_click(button_callback_t callback, void *arg)
{
m_long_click_callback = callback;
m_long_arg = arg;
}
void button_t::loop()
{
int current_state = digitalRead(m_pin);
unsigned long t = millis();
if (!m_last_down && (current_state == LOW)) {
m_keypress_time = t + LONGCLICK_TIMER;
m_long_enable = true;
}
if (m_last_down && current_state) {
if (m_long_enable && m_keypress_time < t) {
if (m_long_click_callback)
m_long_click_callback(m_long_arg);
} else {
m_event_count++;
m_next_event_time = t + DOUBLECLICK_TIMER;
}
m_long_enable = false;
} else if (m_event_count != 0) {
if (m_next_event_time < t) {
switch (m_event_count) {
case 1:
if (m_single_click_callback)
m_single_click_callback(m_single_arg);
break;
case 2:
if (m_double_click_callback)
m_double_click_callback(m_double_arg);
break;
}
m_event_count = 0;
}
}
m_last_down = (current_state == LOW);
}
/* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright 2017 Adrien Oliva <olivaa+gitlab@yapbreak.fr>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*
*/
#ifndef __BUTTON_H__
#define __BUTTON_H__
#include <functional>
typedef std::function<void(void *)> button_callback_t;
class button_t {
private:
const uint8_t m_pin;
bool m_last_down = false;
unsigned long m_next_event_time = 0;
unsigned long m_keypress_time = 0;
char m_event_count = 0;
bool m_long_enable = false;
button_callback_t m_single_click_callback = NULL;
button_callback_t m_double_click_callback = NULL;
button_callback_t m_long_click_callback = NULL;
void *m_single_arg = NULL;
void *m_double_arg = NULL;
void *m_long_arg = NULL;
public:
button_t(const uint8_t pin);
~button_t();
void on_single_click(button_callback_t callback, void *arg);
void on_double_click(button_callback_t callback, void *arg);
void on_long_click(button_callback_t callback, void *arg);
void loop();
};
#endif /* __BUTTON_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment