Wikipédia de RayMind
Arduino librairie buttons : Différence entre versions
| Ligne 27 : | Ligne 27 : | ||
}<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> | <br> | ||
</tt></div> | </tt></div> | ||
[[Catégorie:Projets de développement]] | [[Catégorie:Projets de développement]] | ||
Version du 15 février 2012 à 21:44
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)
*/