Wikipédia de RayMind
Arduino librairie buttons : Différence entre versions
(Page créée avec « {| align="right" | __TOC__ |} ==librairies/buttons/examples/sandbox== ===sandbox.ino=== <div style="border-left:6px solid lightgray;padding-left:10px;"><tt style="font-size:1... ») |
|||
| (2 révisions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 5 : | Ligne 5 : | ||
===sandbox.ino=== | ===sandbox.ino=== | ||
<div style="border-left:6px solid lightgray;padding-left:10px;"><tt style="font-size:14px;"> | <div style="border-left:6px solid lightgray;padding-left:10px;"><tt style="font-size:14px;"> | ||
| − | <nowiki>#</nowiki>include <buttons.h | + | <nowiki>#</nowiki>include <<span style="color: #CC6600;">buttons</span>.h><br> |
<br> | <br> | ||
| − | Button B;<br> | + | <span style="color: #CC6600;">Button</span> B;<br> |
<br> | <br> | ||
| − | void setup() {<br> | + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>setup</b></span>() {<br> |
| − | B.setMode(OneShotTimer); // Return "ON" first (as a OneShot),<br> | + | B.<span style="color: #CC6600;">setMode</span>(<span style="color: #006699;">OneShotTimer</span>); <span style="color: #7E7E7E;">// Return "ON" first (as a OneShot),</span><br> |
| − | B.setTimer(1500); // then after 1.5 seconds, return "Hold".<br> | + | B.<span style="color: #CC6600;">setTimer</span>(1500); <span style="color: #7E7E7E;">// then after 1.5 seconds, return "Hold".</span><br> |
| − | B.setRefresh(500); // Return Hold every 500ms<br> | + | B.<span style="color: #CC6600;">setRefresh</span>(500); <span style="color: #7E7E7E;">// Return Hold every 500ms</span><br> |
}<br> | }<br> | ||
<br> | <br> | ||
| − | void loop() {<br> | + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>loop</b></span>() {<br> |
| − | switch (B.check()) {<br> | + | <span style="color: #CC6600;">switch</span> (B.<span style="color: #CC6600;">check</span>()) {<br> |
| − | case ON:<br> | + | <span style="color: #CC6600;">case</span> <span style="color: #006699;">ON</span>:<br> |
| − | // Do something on first press (oneshot)<br> | + | <span style="color: #7E7E7E;">// Do something on first press (oneshot)</span><br> |
| − | break;<br> | + | <span style="color: #CC6600;">break</span>;<br> |
| − | case Hold:<br> | + | <span style="color: #CC6600;">case</span> <span style="color: #006699;">Hold</span>:<br> |
| − | // Do something every 500 ms<br> | + | <span style="color: #7E7E7E;">// Do something every 500 ms</span><br> |
| − | break;<br> | + | <span style="color: #CC6600;">break</span>;<br> |
| − | default:<br> | + | <span style="color: #CC6600;">default</span>:<br> |
| − | break;<br> | + | <span style="color: #CC6600;">break</span>;<br> |
}<br> | }<br> | ||
}<br> | }<br> | ||
| + | <br> | ||
| + | </tt></div> | ||
| + | ==librairies/buttons== | ||
| + | ===buttons.h=== | ||
| + | <div style="border-left:6px solid lightgray;padding-left:10px;"><tt style="font-size:14px;"> | ||
| + | <span style="color: #7E7E7E;">/*</span><br> | ||
| + | <span style="color: #7E7E7E;"> * buttons.h</span><br> | ||
| + | <span style="color: #7E7E7E;"> * One-shot and hold functions for buttons.</span><br> | ||
| + | <span style="color: #7E7E7E;"> * Created by Franky on 29/01/09.</span><br> | ||
| + | <span style="color: #7E7E7E;"> * Licensed under LGPL (free to modify and use as you wish)</span><br> | ||
| + | <span style="color: #7E7E7E;"> */</span><br> | ||
| + | <br> | ||
| + | <nowiki>#</nowiki>include <inttypes.h><br> | ||
| + | <br> | ||
| + | <nowiki>#</nowiki>define <span style="color: #006699;">OneShot</span> 0<br> | ||
| + | <nowiki>#</nowiki>define <span style="color: #006699;">Memory</span> 1<br> | ||
| + | <nowiki>#</nowiki>define <span style="color: #006699;">Timer</span> 2<br> | ||
| + | <nowiki>#</nowiki>define <span style="color: #006699;">OneShotTimer</span> 3<br> | ||
| + | <nowiki>#</nowiki>define <span style="color: #006699;">MemoryTimer</span> 4<br> | ||
| + | <br> | ||
| + | <nowiki>#</nowiki>define <span style="color: #006699;">ON</span> 1<br> | ||
| + | <nowiki>#</nowiki>define <span style="color: #006699;">OFF</span> 0<br> | ||
| + | <nowiki>#</nowiki>define <span style="color: #006699;">Pressed</span> 2<br> | ||
| + | <nowiki>#</nowiki>define <span style="color: #006699;">Released</span> 3<br> | ||
| + | <nowiki>#</nowiki>define <span style="color: #006699;">Hold</span> 4<br> | ||
| + | <br> | ||
| + | typedef uint8_t <span style="color: #CC6600;">byte</span>;<br> | ||
| + | <br> | ||
| + | <br> | ||
| + | <span style="color: #CC6600;">class</span> <span style="color: #CC6600;">Button</span> {<br> | ||
| + | <span style="color: #CC6600;">public</span><span style="color: #7E7E7E;"><b>:</b></span><br> | ||
| + | <span style="color: #CC6600;">Button</span>();<br> | ||
| + | <span style="color: #CC6600;">Button</span>(<span style="color: #CC6600;">byte</span> type);<br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">assign</span>(<span style="color: #CC6600;">byte</span> pin);<br> | ||
| + | <span style="color: #CC6600;">byte</span> <span style="color: #CC6600;">check</span>();<br> | ||
| + | <span style="color: #CC6600;">byte</span> <span style="color: #CC6600;">check</span>(<span style="color: #CC6600;">byte</span> mode_v);<br> | ||
| + | <span style="color: #7E7E7E;">// Setters</span><br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">setMode</span>(<span style="color: #CC6600;">byte</span> type_v);<br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">setTimer</span>(<span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">int</span> t);<br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">setRefresh</span>(<span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">int</span> r);<br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">turnOnPullUp</span>();<br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">turnOffPullUp</span>();<br> | ||
| + | <br> | ||
| + | <br> | ||
| + | <span style="color: #CC6600;">private</span><span style="color: #7E7E7E;"><b>:</b></span> <br> | ||
| + | <span style="color: #CC6600;">byte</span> pin;<br> | ||
| + | <span style="color: #CC6600;">byte</span> mode;<br> | ||
| + | <span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">long</span> hold_timer;<br> | ||
| + | <span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">long</span> refresh_timer;<br> | ||
| + | <span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">int</span> hold_level;<br> | ||
| + | <span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">int</span> hold_refresh;<br> | ||
| + | bool previous;<br> | ||
| + | };<br> | ||
| + | <br> | ||
| + | <br> | ||
| + | <br> | ||
| + | <span style="color: #7E7E7E;">/*</span><br> | ||
| + | <span style="color: #7E7E7E;"> </span><br> | ||
| + | <span style="color: #7E7E7E;"> Button modes:</span><br> | ||
| + | <span style="color: #7E7E7E;"> </span><br> | ||
| + | <span style="color: #7E7E7E;"> - OneShot: OneShot only, returns 2 values (ON/OFF)</span><br> | ||
| + | <span style="color: #7E7E7E;"> - Memory: Returns (Pressed/ON/Released/OFF)</span><br> | ||
| + | <span style="color: #7E7E7E;"> - Timer: Hold System (OFF/ON/Hold)</span><br> | ||
| + | <span style="color: #7E7E7E;"> - OneShotTimer: Combi OneShot & Timer (ON/Hold/OFF)</span><br> | ||
| + | <span style="color: #7E7E7E;"> - MemoryTimer: Combi Memory & Timer </span><br> | ||
| + | <span style="color: #7E7E7E;"> (Pressed/ON/Hold/Released/OFF)</span><br> | ||
| + | <span style="color: #7E7E7E;"> </span><br> | ||
| + | <span style="color: #7E7E7E;"> */</span><br> | ||
| + | <br> | ||
| + | </tt></div> | ||
| + | ===buttons.cpp=== | ||
| + | <div style="border-left:6px solid lightgray;padding-left:10px;"><tt style="font-size:14px;"> | ||
| + | <span style="color: #7E7E7E;">/*</span><br> | ||
| + | <span style="color: #7E7E7E;"> * buttons.cpp</span><br> | ||
| + | <span style="color: #7E7E7E;"> * One-shot and hold functions for buttons.</span><br> | ||
| + | <span style="color: #7E7E7E;"> * Created by Franky on 29/01/09.</span><br> | ||
| + | <span style="color: #7E7E7E;"> * Licensed under LGPL (free to modify and use as you wish)</span><br> | ||
| + | <span style="color: #7E7E7E;"> */</span><br> | ||
| + | <br> | ||
| + | <br> | ||
| + | <br> | ||
| + | <nowiki>#</nowiki>include <span style="color: #006699;">"Arduino.h"</span><br> | ||
| + | <nowiki>#</nowiki>include <span style="color: #006699;">"buttons.h"</span><br> | ||
| + | <br> | ||
| + | <br> | ||
| + | <span style="color: #CC6600;">Button</span><span style="color: #7E7E7E;"><b>:</b></span>:<span style="color: #CC6600;">Button</span>() { <br> | ||
| + | previous = <span style="color: #CC6600;">false</span>;<br> | ||
| + | mode = <span style="color: #006699;">OneShot</span>;<br> | ||
| + | hold_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | refresh_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | hold_level = 1000; <span style="color: #7E7E7E;">// 1 second</span><br> | ||
| + | hold_refresh = 100; <span style="color: #7E7E7E;">// 100 ms</span><br> | ||
| + | pin = 0;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">Button</span><span style="color: #7E7E7E;"><b>:</b></span>:<span style="color: #CC6600;">Button</span>(<span style="color: #CC6600;">byte</span> mode_v) {<br> | ||
| + | previous = <span style="color: #CC6600;">false</span>;<br> | ||
| + | mode = mode_v;<br> | ||
| + | hold_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | refresh_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | hold_level = 1000;<br> | ||
| + | hold_refresh = 100;<br> | ||
| + | pin = 0;<br> | ||
| + | }<br> | ||
| + | <br> | ||
| + | <br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">Button</span>::<span style="color: #CC6600;">assign</span>(<span style="color: #CC6600;">byte</span> pin_v) { <br> | ||
| + | pin = pin_v; <br> | ||
| + | <span style="color: #CC6600;">pinMode</span>(pin, <span style="color: #006699;">INPUT</span>);<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">Button</span>::<span style="color: #CC6600;">turnOnPullUp</span>() { <span style="color: #CC6600;">if</span> (pin) <span style="color: #CC6600;">digitalWrite</span>(pin, <span style="color: #006699;">HIGH</span>); }<br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">Button</span>::<span style="color: #CC6600;">turnOffPullUp</span>() { <span style="color: #CC6600;">if</span> (pin) <span style="color: #CC6600;">digitalWrite</span>(pin, <span style="color: #006699;">LOW</span>); } <br> | ||
| + | <br> | ||
| + | <span style="color: #7E7E7E;">// Setters</span><br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">Button</span>::<span style="color: #CC6600;">setMode</span>(<span style="color: #CC6600;">byte</span> mode_v) { mode = mode_v; }<br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">Button</span>::<span style="color: #CC6600;">setTimer</span>(<span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">int</span> t) { <span style="color: #CC6600;">if</span> (mode > <span style="color: #006699;">Memory</span>) hold_level = (<span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">long</span>)t; }<br> | ||
| + | <span style="color: #CC6600;">void</span> <span style="color: #CC6600;">Button</span>::<span style="color: #CC6600;">setRefresh</span>(<span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">int</span> r) { hold_refresh = r; }<br> | ||
| + | <br> | ||
| + | <span style="color: #CC6600;">byte</span> <span style="color: #CC6600;">Button</span>::<span style="color: #CC6600;">check</span>(<span style="color: #CC6600;">byte</span> mode_v) {<br> | ||
| + | <span style="color: #CC6600;">byte</span> mem = mode;<br> | ||
| + | mode = mode_v;<br> | ||
| + | <span style="color: #CC6600;">byte</span> res = <span style="color: #CC6600;">check</span>();<br> | ||
| + | mode = mem;<br> | ||
| + | <span style="color: #CC6600;">return</span> res;<br> | ||
| + | }<br> | ||
| + | <br> | ||
| + | <br> | ||
| + | <span style="color: #CC6600;">byte</span> <span style="color: #CC6600;">Button</span>::<span style="color: #CC6600;">check</span>() {<br> | ||
| + | <span style="color: #CC6600;">if</span> (<span style="color: #CC6600;">digitalRead</span>(pin)) {<br> | ||
| + | <span style="color: #CC6600;">switch</span> (mode) {<br> | ||
| + | <span style="color: #CC6600;">case</span> <span style="color: #006699;">OneShot</span>:<br> | ||
| + | <span style="color: #CC6600;">if</span> (previous) {<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">OFF</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> {<br> | ||
| + | previous = <span style="color: #CC6600;">true</span>;<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">ON</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">break</span>;<br> | ||
| + | <span style="color: #CC6600;">case</span> <span style="color: #006699;">Memory</span>:<br> | ||
| + | <span style="color: #CC6600;">if</span> (previous) {<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">ON</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> {<br> | ||
| + | previous = <span style="color: #CC6600;">true</span>;<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">Pressed</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">break</span>;<br> | ||
| + | <span style="color: #CC6600;">case</span> <span style="color: #006699;">Timer</span>:<br> | ||
| + | <span style="color: #CC6600;">if</span> (previous) {<br> | ||
| + | <span style="color: #7E7E7E;">// Is treshold reached?</span><br> | ||
| + | <span style="color: #CC6600;">if</span> (<span style="color: #CC6600;">millis</span>() > hold_timer+hold_level) {<br> | ||
| + | <span style="color: #CC6600;">if</span> (<span style="color: #CC6600;">millis</span>() > refresh_timer+hold_refresh) {<br> | ||
| + | refresh_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">Hold</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> <span style="color: #CC6600;">return</span> <span style="color: #006699;">OFF</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> {<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">ON</span>;<br> | ||
| + | }<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> {<br> | ||
| + | previous = <span style="color: #CC6600;">true</span>;<br> | ||
| + | hold_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | refresh_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">ON</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">break</span>;<br> | ||
| + | <span style="color: #CC6600;">case</span> <span style="color: #006699;">OneShotTimer</span>:<br> | ||
| + | <span style="color: #CC6600;">if</span> (previous) {<br> | ||
| + | <span style="color: #7E7E7E;">// Is treshold reached?</span><br> | ||
| + | <span style="color: #CC6600;">if</span> (<span style="color: #CC6600;">millis</span>() > hold_timer+hold_level) {<br> | ||
| + | <span style="color: #CC6600;">if</span> (<span style="color: #CC6600;">millis</span>() > refresh_timer+hold_refresh) {<br> | ||
| + | refresh_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">Hold</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> <span style="color: #CC6600;">return</span> <span style="color: #006699;">OFF</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> {<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">OFF</span>;<br> | ||
| + | }<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> {<br> | ||
| + | previous = <span style="color: #CC6600;">true</span>;<br> | ||
| + | hold_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | refresh_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">ON</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">break</span>;<br> | ||
| + | <span style="color: #CC6600;">case</span> <span style="color: #006699;">MemoryTimer</span>:<br> | ||
| + | <span style="color: #CC6600;">if</span> (previous) {<br> | ||
| + | <span style="color: #7E7E7E;">// Is treshold reached?</span><br> | ||
| + | <span style="color: #CC6600;">if</span> (<span style="color: #CC6600;">millis</span>() > hold_timer+hold_level) {<br> | ||
| + | <span style="color: #CC6600;">if</span> (<span style="color: #CC6600;">millis</span>() > refresh_timer+hold_refresh) {<br> | ||
| + | refresh_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">Hold</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> <span style="color: #CC6600;">return</span> <span style="color: #006699;">OFF</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> {<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">ON</span>;<br> | ||
| + | }<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> {<br> | ||
| + | previous = <span style="color: #CC6600;">true</span>;<br> | ||
| + | hold_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | refresh_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">Pressed</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">break</span>;<br> | ||
| + | <span style="color: #CC6600;">default</span>:<br> | ||
| + | <span style="color: #CC6600;">break</span>;<br> | ||
| + | }<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">else</span> {<br> | ||
| + | <span style="color: #CC6600;">if</span> (previous && (mode == 1 || mode == 4)) {<br> | ||
| + | previous = <span style="color: #CC6600;">false</span>;<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">Released</span>;<br> | ||
| + | }<br> | ||
| + | previous = <span style="color: #CC6600;">false</span>;<br> | ||
| + | hold_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | refresh_timer = <span style="color: #CC6600;">millis</span>();<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">OFF</span>;<br> | ||
| + | }<br> | ||
| + | <span style="color: #CC6600;">return</span> <span style="color: #006699;">OFF</span>;<br> | ||
| + | }<br> | ||
| + | <br> | ||
| + | <br> | ||
</tt></div> | </tt></div> | ||
[[Catégorie:Projets de développement]] | [[Catégorie:Projets de développement]] | ||
Version actuelle datée du 15 février 2012 à 21:45
librairies/buttons/examples/sandbox
sandbox.ino
#include <buttons.h>
Button B;
void setup() {
B.setMode(OneShotTimer); // Return "ON" first (as a OneShot),
B.setTimer(1500); // then after 1.5 seconds, return "Hold".
B.setRefresh(500); // Return Hold every 500ms
}
void loop() {
switch (B.check()) {
case ON:
// Do something on first press (oneshot)
break;
case Hold:
// Do something every 500 ms
break;
default:
break;
}
}
librairies/buttons
buttons.h
/*
* buttons.h
* One-shot and hold functions for buttons.
* Created by Franky on 29/01/09.
* Licensed under LGPL (free to modify and use as you wish)
*/
#include <inttypes.h>
#define OneShot 0
#define Memory 1
#define Timer 2
#define OneShotTimer 3
#define MemoryTimer 4
#define ON 1
#define OFF 0
#define Pressed 2
#define Released 3
#define Hold 4
typedef uint8_t byte;
class Button {
public:
Button();
Button(byte type);
void assign(byte pin);
byte check();
byte check(byte mode_v);
// Setters
void setMode(byte type_v);
void setTimer(unsigned int t);
void setRefresh(unsigned int r);
void turnOnPullUp();
void turnOffPullUp();
private:
byte pin;
byte mode;
unsigned long hold_timer;
unsigned long refresh_timer;
unsigned int hold_level;
unsigned int hold_refresh;
bool previous;
};
/*
Button modes:
- OneShot: OneShot only, returns 2 values (ON/OFF)
- Memory: Returns (Pressed/ON/Released/OFF)
- Timer: Hold System (OFF/ON/Hold)
- OneShotTimer: Combi OneShot & Timer (ON/Hold/OFF)
- MemoryTimer: Combi Memory & Timer
(Pressed/ON/Hold/Released/OFF)
*/
buttons.cpp
/*
* buttons.cpp
* One-shot and hold functions for buttons.
* Created by Franky on 29/01/09.
* Licensed under LGPL (free to modify and use as you wish)
*/
#include "Arduino.h"
#include "buttons.h"
Button::Button() {
previous = false;
mode = OneShot;
hold_timer = millis();
refresh_timer = millis();
hold_level = 1000; // 1 second
hold_refresh = 100; // 100 ms
pin = 0;
}
Button::Button(byte mode_v) {
previous = false;
mode = mode_v;
hold_timer = millis();
refresh_timer = millis();
hold_level = 1000;
hold_refresh = 100;
pin = 0;
}
void Button::assign(byte pin_v) {
pin = pin_v;
pinMode(pin, INPUT);
}
void Button::turnOnPullUp() { if (pin) digitalWrite(pin, HIGH); }
void Button::turnOffPullUp() { if (pin) digitalWrite(pin, LOW); }
// Setters
void Button::setMode(byte mode_v) { mode = mode_v; }
void Button::setTimer(unsigned int t) { if (mode > Memory) hold_level = (unsigned long)t; }
void Button::setRefresh(unsigned int r) { hold_refresh = r; }
byte Button::check(byte mode_v) {
byte mem = mode;
mode = mode_v;
byte res = check();
mode = mem;
return res;
}
byte Button::check() {
if (digitalRead(pin)) {
switch (mode) {
case OneShot:
if (previous) {
return OFF;
}
else {
previous = true;
return ON;
}
break;
case Memory:
if (previous) {
return ON;
}
else {
previous = true;
return Pressed;
}
break;
case Timer:
if (previous) {
// Is treshold reached?
if (millis() > hold_timer+hold_level) {
if (millis() > refresh_timer+hold_refresh) {
refresh_timer = millis();
return Hold;
}
else return OFF;
}
else {
return ON;
}
}
else {
previous = true;
hold_timer = millis();
refresh_timer = millis();
return ON;
}
break;
case OneShotTimer:
if (previous) {
// Is treshold reached?
if (millis() > hold_timer+hold_level) {
if (millis() > refresh_timer+hold_refresh) {
refresh_timer = millis();
return Hold;
}
else return OFF;
}
else {
return OFF;
}
}
else {
previous = true;
hold_timer = millis();
refresh_timer = millis();
return ON;
}
break;
case MemoryTimer:
if (previous) {
// Is treshold reached?
if (millis() > hold_timer+hold_level) {
if (millis() > refresh_timer+hold_refresh) {
refresh_timer = millis();
return Hold;
}
else return OFF;
}
else {
return ON;
}
}
else {
previous = true;
hold_timer = millis();
refresh_timer = millis();
return Pressed;
}
break;
default:
break;
}
}
else {
if (previous && (mode == 1 || mode == 4)) {
previous = false;
return Released;
}
previous = false;
hold_timer = millis();
refresh_timer = millis();
return OFF;
}
return OFF;
}