Commit | Line | Data |
---|---|---|
35089dc7 JM |
1 | #ifndef BUTTON_H |
2 | #define BUTTON_H | |
3 | ||
4 | #include <stdlib.h> | |
5 | #include "libs/LPC17xx/sLPC17xx.h" // smoothed mbed.h lib | |
6 | #include "libs/Kernel.h" | |
7 | #include "libs/utils.h" | |
8 | #include "libs/Pin.h" | |
9 | #include <string> | |
10 | #include "libs/Hook.h" | |
11 | ||
12 | ||
13 | class Button{ | |
14 | public: | |
15 | Button(){ | |
16 | this->counter = 0; | |
17 | this->value = false; | |
18 | this->up_hook = NULL; | |
19 | this->down_hook = NULL; | |
20 | this->button_pin = NULL; | |
21 | } | |
22 | ||
23 | Button* pin(Pin* passed_pin){ | |
24 | this->button_pin = passed_pin; | |
25 | return this; | |
26 | } | |
27 | ||
28 | void check_signal(){ | |
29 | check_signal(this->button_pin->get()?1:0); | |
30 | } | |
31 | ||
32 | void check_signal(int val){ | |
33 | bool start_value = this->value; | |
34 | if( val ){ | |
35 | if( this->counter < 5 ){ this->counter++; } | |
36 | if( this->counter == 5 ){ | |
37 | this->value = true; | |
38 | } | |
39 | }else{ | |
40 | if( this->counter > 0 ){ this->counter--; } | |
41 | if( this->counter == 0 ){ | |
42 | this->value = false; | |
43 | } | |
44 | } | |
45 | ||
46 | if( start_value != this->value ){ | |
47 | if( this->value ){ | |
48 | if( this->up_hook != NULL ){ | |
49 | this->up_hook->call(); | |
50 | } | |
51 | }else{ | |
52 | if( this->down_hook != NULL ){ | |
53 | this->down_hook->call(); | |
54 | } | |
55 | } | |
56 | } | |
57 | ||
58 | } | |
59 | ||
60 | bool get(){ | |
61 | return this->value; | |
62 | } | |
63 | ||
64 | ||
65 | template<typename T> Button* up_attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){ | |
66 | this->up_hook = new Hook(); | |
67 | this->up_hook->attach(optr, fptr); | |
68 | return this; | |
69 | } | |
70 | ||
71 | template<typename T> Button* down_attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){ | |
72 | this->down_hook = new Hook(); | |
73 | this->down_hook->attach(optr, fptr); | |
74 | return this; | |
75 | } | |
76 | ||
77 | private: | |
78 | Hook* up_hook; | |
79 | Hook* down_hook; | |
80 | bool value; | |
81 | char counter; | |
82 | Pin* button_pin; | |
83 | ||
84 | }; | |
85 | ||
86 | ||
87 | ||
88 | ||
89 | ||
90 | #endif |