1 #include "libs/Kernel.h"
2 #include "PauseButton.h"
3 #include "libs/nuts_bolts.h"
4 #include "libs/utils.h"
6 #include "SlowTicker.h"
7 #include "libs/SerialMessage.h"
8 #include "libs/StreamOutput.h"
10 #include "checksumm.h"
11 #include "ConfigValue.h"
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")
20 PauseButton::PauseButton(){}
22 void PauseButton::on_module_loaded(){
23 this->button_state
= true;
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();
28 this->register_for_event(ON_CONSOLE_LINE_RECEIVED
);
30 if(this->enable
) THEKERNEL
->slow_ticker
->attach( 100, this, &PauseButton::button_tick
);
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;
38 bool newstate
= this->button
.get();
39 if(this->button_state
!= newstate
){
40 this->button_state
= newstate
;
42 if( this->button_state
){
43 if( THEKERNEL
->pauser
->paused() ){
44 THEKERNEL
->pauser
->release();
46 THEKERNEL
->pauser
->take();
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
)
56 SerialMessage new_message
= *static_cast<SerialMessage
*>(argument
);
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;
62 int checksum
= get_checksum(shift_parameter(new_message
.message
));
64 if (checksum
== freeze_command_checksum
) {
65 if( !THEKERNEL
->pauser
->paused() ){
66 THEKERNEL
->pauser
->take();
69 }else if (checksum
== unfreeze_command_checksum
) {
70 if( THEKERNEL
->pauser
->paused() ){
71 THEKERNEL
->pauser
->release();