This repository was archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOnePush.cpp
More file actions
63 lines (53 loc) · 1.35 KB
/
OnePush.cpp
File metadata and controls
63 lines (53 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
OnePush - v1.0 - November 19, 2014.
Arduino library for doing many things with a single button.
Created by William Koch.
Released into the public domain.
*/
#include "OnePush.h"
#include "../Debounce/Debounce.h"
#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
// Creates an OnePush Object with 1 level.
OnePush::OnePush(byte button) : Debounce(button) {
_levels = 1;
_level = 0;
_lastCount = OnePush::count();
}
// Creates an OnePush Object with custom levels.
OnePush::OnePush(byte button, byte levels) : Debounce(button){
_levels = levels;
_level = 0;
_lastCount = OnePush::count();
}
// Updates everything
void OnePush::update() {
OnePush::read();
if (OnePush::count() != _lastCount) {
OnePush::next();
}
}
// Returns the current status, false for level 0, true otherwise.
boolean OnePush::status() {
OnePush::update();
return (_level == 0 ? false : true);
}
byte OnePush::state() {
OnePush::update();
return OnePush::status() ? HIGH : LOW;
}
// Returns the current level.
byte OnePush::level() {
return _level;
}
// Manually sets the level to any value from 0 up to levels.
byte OnePush::set(byte level) {
if (BETWEEN(level, 0, _levels)) {
_level = level;
}
return _level;
}
// Moves to the next level.
void OnePush::next() {
_lastCount = OnePush::count();
_level < _levels ? _level++ : _level = 0;
}