| 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 | this->play_state = true; |
| 25 | |
| 26 | this->enable = THEKERNEL->config->value( pause_button_enable_checksum )->by_default(false)->as_bool(); |
| 27 | this->button.from_string( THEKERNEL->config->value( pause_button_pin_checksum )->by_default("2.12")->as_string())->as_input(); |
| 28 | |
| 29 | this->register_for_event(ON_CONSOLE_LINE_RECEIVED); |
| 30 | |
| 31 | if(this->enable) THEKERNEL->slow_ticker->attach( 100, this, &PauseButton::button_tick ); |
| 32 | } |
| 33 | |
| 34 | //TODO: Make this use InterruptIn |
| 35 | //Check the state of the button and act accordingly |
| 36 | uint32_t PauseButton::button_tick(uint32_t dummy){ |
| 37 | if(!this->enable) return 0; |
| 38 | // If button changed |
| 39 | bool newstate = this->button.get(); |
| 40 | if(this->button_state != newstate){ |
| 41 | this->button_state = newstate; |
| 42 | // If button pressed |
| 43 | if( this->button_state ){ |
| 44 | if( this->play_state ){ |
| 45 | this->play_state = false; |
| 46 | THEKERNEL->pauser->take(); |
| 47 | }else{ |
| 48 | this->play_state = true; |
| 49 | THEKERNEL->pauser->release(); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | // When a new line is received, check if it is a command, and if it is, act upon it |
| 57 | void PauseButton::on_console_line_received( void *argument ) |
| 58 | { |
| 59 | SerialMessage new_message = *static_cast<SerialMessage *>(argument); |
| 60 | |
| 61 | // ignore comments and blank lines and if this is a G code then also ignore it |
| 62 | char first_char = new_message.message[0]; |
| 63 | if(strchr(";( \n\rGMTN", first_char) != NULL) return; |
| 64 | |
| 65 | int checksum = get_checksum(shift_parameter(new_message.message)); |
| 66 | |
| 67 | if (checksum == freeze_command_checksum) { |
| 68 | if( this->play_state ){ |
| 69 | this->play_state = false; |
| 70 | THEKERNEL->pauser->take(); |
| 71 | } |
| 72 | } |
| 73 | else if (checksum == unfreeze_command_checksum) { |
| 74 | if( ! this->play_state ){ |
| 75 | this->play_state = true; |
| 76 | THEKERNEL->pauser->release(); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |