Merge remote-tracking branch 'upstream/master' into edge
[clinton/Smoothieware.git] / src / modules / utils / pausebutton / PauseButton.cpp
CommitLineData
81b547a1
AW
1#include "libs/Kernel.h"
2#include "PauseButton.h"
3#include "libs/nuts_bolts.h"
4#include "libs/utils.h"
61134a65
JM
5#include "Config.h"
6#include "SlowTicker.h"
9bce85f9
L
7#include "libs/SerialMessage.h"
8#include "libs/StreamOutput.h"
61134a65 9#include "Pauser.h"
7af0714f 10#include "checksumm.h"
8d54c34c 11#include "ConfigValue.h"
61134a65 12
81b547a1
AW
13using namespace std;
14
4e722c00
JM
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
b547a424 20PauseButton::PauseButton(){}
81b547a1
AW
21
22void PauseButton::on_module_loaded(){
23 this->button_state = true;
81b547a1 24
314ab8f7
MM
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();
81b547a1 27
9bce85f9 28 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
4e722c00
JM
29
30 if(this->enable) THEKERNEL->slow_ticker->attach( 100, this, &PauseButton::button_tick );
81b547a1
AW
31}
32
7b49793d 33//TODO: Make this use InterruptIn
7dc903db 34//Check the state of the button and act accordingly based on current pause state
8b8b3339 35uint32_t PauseButton::button_tick(uint32_t dummy){
2d9cf0e9 36 if(!this->enable) return 0;
df27a6a3 37 // If button changed
62529560
MM
38 bool newstate = this->button.get();
39 if(this->button_state != newstate){
40 this->button_state = newstate;
df27a6a3 41 // If button pressed
81b547a1 42 if( this->button_state ){
7dc903db 43 if( THEKERNEL->pauser->paused() ){
314ab8f7 44 THEKERNEL->pauser->release();
7dc903db
JM
45 }else{
46 THEKERNEL->pauser->take();
df27a6a3
MM
47 }
48 }
81b547a1 49 }
f03d3c1c 50 return 0;
81b547a1 51}
9bce85f9
L
52
53// When a new line is received, check if it is a command, and if it is, act upon it
54void 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) {
7dc903db 65 if( !THEKERNEL->pauser->paused() ){
9bce85f9
L
66 THEKERNEL->pauser->take();
67 }
7dc903db
JM
68
69 }else if (checksum == unfreeze_command_checksum) {
70 if( THEKERNEL->pauser->paused() ){
9bce85f9
L
71 THEKERNEL->pauser->release();
72 }
73 }
74}
75