refactor panel screens into 3dprinter and cnc
[clinton/Smoothieware.git] / src / modules / utils / panel / Button.cpp
1 #include "Button.h"
2
3 #include "libs/Kernel.h"
4 #include "libs/utils.h"
5 #include "libs/Pin.h"
6 #include "libs/Hook.h"
7
8 Button::Button()
9 {
10 this->counter = 0;
11 this->value = false;
12 this->up_hook = NULL;
13 this->down_hook = NULL;
14 this->button_pin = NULL;
15 this->repeat = false;
16 this->first_timer = 0;
17 this->second_timer = 0;
18 this->longpress_delay = 0;
19 }
20
21 Button *Button::pin(Pin *passed_pin)
22 {
23 this->button_pin = passed_pin;
24 return this;
25 }
26
27 void Button::check_signal()
28 {
29 check_signal(this->button_pin->get() ? 1 : 0);
30 }
31
32 void Button::check_signal(int val)
33 {
34 bool start_value = this->value;
35 if ( val ) {
36 if ( this->counter < 5 ) {
37 this->counter++;
38 }
39 if ( this->counter == 5 ) {
40 this->value = true;
41 }
42 } else {
43 if ( this->counter > 0 ) {
44 this->counter--;
45 }
46 if ( this->counter == 0 ) {
47 this->value = false;
48 }
49 }
50
51 if ( start_value != this->value ) {
52 if ( this->value ) {
53 if ( this->up_hook != NULL ) {
54 this->up_hook->call();
55 this->first_timer = 0;
56 this->second_timer = 0;
57 this->repeat = false;
58 }
59 } else {
60 if ( this->down_hook != NULL ) {
61 this->down_hook->call();
62 }
63 }
64 }
65 //auto repeat button presses
66 if(this->longpress_delay > 0) {
67 if(this->value) {
68 if(this->repeat) {
69 this->second_timer++;
70 if(this->second_timer == 10) {
71 this->up_hook->call();
72 this->second_timer = 0;
73 }
74 } else {
75 this->first_timer++;
76 if(this->first_timer == longpress_delay) {
77 this->repeat = true;
78 this->first_timer = 0;
79 }
80 }
81 }
82 }
83 }
84
85 void Button::set_longpress_delay(int delay)
86 {
87 this->longpress_delay = delay;
88 }
89
90 bool Button::get()
91 {
92 return this->value;
93 }