-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
274 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
TroykaButton | ||
============ | ||
|
||
Библиотека поможет считывать состояния кнопки: нажатие, отжатие и зажатие. | ||
Программный модуль также позволяет программно избавиться от дребезга контактов. | ||
|
||
Библиотека будет полезна для кнопок: | ||
* [Кнопка (Troyka-модуль)](http://amperka.ru/product/troyka-button) | ||
* [Кнопка со светодиодом (Troyka-модуль)](http://amperka.ru/product/troyka-led-button) | ||
* [Сенсорная кнопка (Troyka-модуль)](http://amperka.ru/product/troyka-touch-sensor) | ||
* [Четырёхкнопочная клавиатура (Troyka-модуль)](http://amperka.ru/product/troyka-quad-switch) | ||
* [Кнопка тактовая](http://amperka.ru/product/tactile-button) | ||
* [Кнопка тактовая с колпачком](http://amperka.ru/product/big-tactile-button) | ||
|
||
Установка библиотеки | ||
==================== | ||
|
||
В Arduino IDE выберите пункт меню «Скетч» → «Импортировать библиотеку» → | ||
«Добавить библиотеку…». В появившемся окне выберите скачаный архив с | ||
библиотекой. Установка завершена. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "TroykaButton.h" | ||
|
||
// создаём объект для работы с кнопкой с параметром номер пина | ||
TroykaButton button(4); | ||
// также есть два необзятельных параметра: | ||
// время длительного зажатия кнопки: по умолчанию 2000 мc | ||
// выбор подтяжки кнопки: по умолчанию подтяжка к минусу | ||
// TroykaButton button(4, 2000, true); | ||
void setup() { | ||
// открываем монитор Serial порта | ||
Serial.begin(9600); | ||
// начало работы с кнопкой | ||
button.begin(); | ||
} | ||
|
||
void loop() | ||
{ | ||
// считываем данные с кнопки | ||
button.read(); | ||
// опеределяем клик кнопки | ||
if (button.justPressed()) { | ||
Serial.println("Key on press"); | ||
} | ||
// определяем отжатие кнопки | ||
if (button.justReleased()) { | ||
Serial.println("Key is release"); | ||
} | ||
// определяем длительное зажатие кнопки | ||
if (button.isHold()) { | ||
Serial.println("Key on long press"); | ||
} | ||
// определяем короткое нажатие кнопки | ||
if (button.isClick()) { | ||
Serial.println("Key is clicked"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
####################################### | ||
# Syntax Coloring Map TroykaButton | ||
####################################### | ||
|
||
####################################### | ||
# Datatypes (KEYWORD1) | ||
####################################### | ||
|
||
TroykaButton KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
|
||
begin KEYWORD2 | ||
read KEYWORD2 | ||
justPressed KEYWORD2 | ||
justReleased KEYWORD2 | ||
isHold KEYWORD2 | ||
isClick KEYWORD2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name=TroykaButton | ||
version=1.0.0 | ||
author=Igor Dementiev <[email protected]> | ||
maintainer=Amperka <amperka.ru> | ||
sentence=Allows you to read state from the button. | ||
paragraph=Supported events are clicked, released and longPressed. | ||
category=Communication | ||
url=https://github.com/amperka/TroykaButton | ||
architectures=* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/****************************************************************************/ | ||
// Function: Cpp file for TroykaButton | ||
// Arduino IDE: Arduino-1.8.5 | ||
// Author: Igor Dementiev | ||
// Date: Sep 09,2018 | ||
// Version: v1.0 | ||
// by www.amperka.ru | ||
/****************************************************************************/ | ||
|
||
#include "TroykaButton.h" | ||
|
||
TroykaButton::TroykaButton(uint8_t pin, uint32_t timeHold, bool pullUP) { | ||
_pin = pin; | ||
_timeHold = timeHold; | ||
_pullUP = pullUP; | ||
} | ||
|
||
// инициализация кнопки | ||
void TroykaButton::begin() { | ||
pinMode(_pin, INPUT); | ||
_msButtonState = 0; | ||
_buttonStateOld = ON_RELEASE; | ||
_isClick = false; | ||
} | ||
|
||
// считывание состояние кнопки | ||
void TroykaButton::read() { | ||
_stateButton = false; | ||
_isClick = false; | ||
bool buttonStateNow = !digitalRead(_pin); | ||
if (!_pullUP) { | ||
buttonStateNow = !buttonStateNow; | ||
} | ||
if (buttonStateNow && buttonStateNow != _buttonStateWas && millis() - _msButtonState > DEBOUNCE_TIME) { | ||
_buttonStateNowLong = false; | ||
_msButtonState = millis(); | ||
_stateButton = ON_PRESS; | ||
_buttonStateOld = _stateButton; | ||
} | ||
if (!buttonStateNow && buttonStateNow != _buttonStateWas && millis() - _msButtonState > DEBOUNCE_TIME) { | ||
_msButtonState = millis(); | ||
_stateButton = ON_RELEASE; | ||
} | ||
if (buttonStateNow && !_buttonStateNowLong && millis() - _msButtonState > _timeHold) { | ||
_buttonStateNowLong = true; | ||
_msButtonState = millis(); | ||
_stateButton = ON_PRESS_LONG; | ||
_buttonStateOld = _stateButton; | ||
} | ||
if (_stateButton == ON_RELEASE && _buttonStateOld == ON_PRESS ) { | ||
_buttonStateOld = ON_RELEASE; | ||
_isClick = true; | ||
} | ||
_buttonStateWas = buttonStateNow; | ||
} | ||
|
||
// определение клика кнопки | ||
bool TroykaButton::justPressed() const { | ||
return _stateButton == ON_PRESS ? 1 : 0; | ||
} | ||
|
||
// определение отжатие кнопки | ||
bool TroykaButton::justReleased() const { | ||
return _stateButton == ON_RELEASE ? 1 : 0; | ||
} | ||
|
||
// определение зажатие кнопки (по умолчанию 2 секунды) | ||
bool TroykaButton::isHold() const { | ||
return _stateButton == ON_PRESS_LONG ? 1 : 0; | ||
} | ||
|
||
// определение короткого клика, если сработал метод isHold() клик не сработает. | ||
bool TroykaButton::isClick() const { | ||
return _isClick; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/****************************************************************************/ | ||
// Function: Header file for TroykaButton | ||
// Arduino IDE: Arduino-1.8.5 | ||
// Author: Igor Dementiev | ||
// Date: Sep 09,2018 | ||
// Version: v1.0 | ||
// by www.amperka.ru | ||
/****************************************************************************/ | ||
|
||
#ifndef _TROYKA_BUTTON_H_ | ||
#define _TROYKA_BUTTON_H_ | ||
|
||
#if defined(ARDUINO) && (ARDUINO >= 100) | ||
#include <Arduino.h> | ||
#else | ||
#include <WProgram.h> | ||
#endif | ||
|
||
#define DEBOUNCE_TIME 50 | ||
#define ON_PRESS 1 | ||
#define ON_PRESS_LONG 2 | ||
#define ON_RELEASE 3 | ||
|
||
class TroykaButton | ||
{ | ||
public: | ||
TroykaButton(uint8_t pin, uint32_t timeHold = 2000, bool pullUP = true); | ||
// инициализация кнопки | ||
void begin(); | ||
// считывание данных с кнопки | ||
void read(); | ||
// определение клика кнопки | ||
bool justPressed() const; | ||
// определение отжатие кнопки | ||
bool justReleased() const; | ||
// определение зажатие кнопки (по умолчанию 2 секунды) | ||
bool isHold() const; | ||
// определение короткого клика, если сработал метод isHold() клик не сработает. | ||
bool isClick() const; | ||
private: | ||
// номера пина | ||
uint8_t _pin; | ||
// время длительного зажатия кнопки | ||
uint32_t _timeHold; | ||
// выбор подтяжки | ||
bool _pullUP; | ||
// короткое нажатие кнопки (клик) | ||
bool _isClick; | ||
// состояние кнопки | ||
uint8_t _stateButton; | ||
// предыдущие состояние кнопки | ||
uint8_t _buttonStateOld; | ||
// ранее состояние кнопки | ||
bool _buttonStateWas; | ||
// длинное нажатие | ||
bool _buttonStateNowLong; | ||
// время нажатия кнопки | ||
uint32_t _msButtonState; | ||
}; | ||
|
||
#endif |