01218270b2f7bcea759e6a6fa0823c89f0a1a952
[clinton/Smoothieware.git] / src / modules / utils / pausebutton / PauseButton.cpp
1 #include "libs/Kernel.h"
2 #include "PauseButton.h"
3 #include "libs/nuts_bolts.h"
4 #include "libs/utils.h"
5 #include <string>
6 using namespace std;
7
8 PauseButton::PauseButton(){}
9
10 void PauseButton::on_module_loaded(){
11 this->button_state = true;
12 this->play_state = true;
13
14 this->enable = THEKERNEL->config->value( pause_button_enable_checksum )->by_default(false)->as_bool();
15 this->button.from_string( THEKERNEL->config->value( pause_button_pin_checksum )->by_default("2.12")->as_string())->as_input();
16
17 THEKERNEL->slow_ticker->attach( 100, this, &PauseButton::button_tick );
18 }
19
20 //TODO: Make this use InterruptIn
21 //Check the state of the button and act accordingly
22 uint32_t PauseButton::button_tick(uint32_t dummy){
23 if(!this->enable) return 0;
24 // If button changed
25 bool newstate = this->button.get();
26 if(this->button_state != newstate){
27 this->button_state = newstate;
28 // If button pressed
29 if( this->button_state ){
30 if( this->play_state ){
31 this->play_state = false;
32 THEKERNEL->pauser->take();
33 }else{
34 this->play_state = true;
35 THEKERNEL->pauser->release();
36 }
37 }
38 }
39 return 0;
40 }