Merge remote-tracking branch 'upstream/master' into edge
[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 "Config.h"
6 #include "SlowTicker.h"
7 #include "libs/SerialMessage.h"
8 #include "libs/StreamOutput.h"
9 #include "Pauser.h"
10 #include "checksumm.h"
11 #include "ConfigValue.h"
12
13 using namespace std;
14
15 #define pause_button_enable_checksum CHECKSUM("pause_button_enable")
16 #define pause_button_pin_checksum CHECKSUM("pause_button_pin")
17 #define freeze_command_checksum CHECKSUM("freeze")
18 #define unfreeze_command_checksum CHECKSUM("unfreeze")
19
20 PauseButton::PauseButton(){}
21
22 void PauseButton::on_module_loaded(){
23 this->button_state = true;
24
25 this->enable = THEKERNEL->config->value( pause_button_enable_checksum )->by_default(false)->as_bool();
26 this->button.from_string( THEKERNEL->config->value( pause_button_pin_checksum )->by_default("2.12")->as_string())->as_input();
27
28 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
29
30 if(this->enable) THEKERNEL->slow_ticker->attach( 100, this, &PauseButton::button_tick );
31 }
32
33 //TODO: Make this use InterruptIn
34 //Check the state of the button and act accordingly based on current pause state
35 uint32_t PauseButton::button_tick(uint32_t dummy){
36 if(!this->enable) return 0;
37 // If button changed
38 bool newstate = this->button.get();
39 if(this->button_state != newstate){
40 this->button_state = newstate;
41 // If button pressed
42 if( this->button_state ){
43 if( THEKERNEL->pauser->paused() ){
44 THEKERNEL->pauser->release();
45 }else{
46 THEKERNEL->pauser->take();
47 }
48 }
49 }
50 return 0;
51 }
52
53 // When a new line is received, check if it is a command, and if it is, act upon it
54 void PauseButton::on_console_line_received( void *argument )
55 {
56 SerialMessage new_message = *static_cast<SerialMessage *>(argument);
57
58 // ignore comments and blank lines and if this is a G code then also ignore it
59 char first_char = new_message.message[0];
60 if(strchr(";( \n\rGMTN", first_char) != NULL) return;
61
62 int checksum = get_checksum(shift_parameter(new_message.message));
63
64 if (checksum == freeze_command_checksum) {
65 if( !THEKERNEL->pauser->paused() ){
66 THEKERNEL->pauser->take();
67 }
68
69 }else if (checksum == unfreeze_command_checksum) {
70 if( THEKERNEL->pauser->paused() ){
71 THEKERNEL->pauser->release();
72 }
73 }
74 }
75