Commit | Line | Data |
---|---|---|
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" |
e558f082 | 12 | #include "StreamOutputPool.h" |
61134a65 | 13 | |
81b547a1 AW |
14 | using namespace std; |
15 | ||
4e722c00 | 16 | #define pause_button_enable_checksum CHECKSUM("pause_button_enable") |
e558f082 | 17 | #define kill_button_enable_checksum CHECKSUM("kill_button_enable") |
4e722c00 | 18 | #define pause_button_pin_checksum CHECKSUM("pause_button_pin") |
e558f082 | 19 | #define kill_button_pin_checksum CHECKSUM("kill_button_pin") |
4e722c00 | 20 | |
76217df5 JM |
21 | PauseButton::PauseButton() |
22 | { | |
e558f082 | 23 | this->button_state = true; |
76217df5 | 24 | this->killed = false; |
e558f082 | 25 | } |
81b547a1 | 26 | |
76217df5 JM |
27 | void PauseButton::on_module_loaded() |
28 | { | |
e558f082 | 29 | this->pause_enable = THEKERNEL->config->value( pause_button_enable_checksum )->by_default(false)->as_bool(); |
5dfe966f | 30 | this->kill_enable = THEKERNEL->config->value( kill_button_enable_checksum )->by_default(false)->as_bool(); |
e558f082 | 31 | this->pause_button.from_string( THEKERNEL->config->value( pause_button_pin_checksum )->by_default("2.12")->as_string())->as_input(); |
5dfe966f | 32 | this->kill_button.from_string( THEKERNEL->config->value( kill_button_pin_checksum )->by_default("nc")->as_string())->as_input(); |
81b547a1 | 33 | |
5dfe966f | 34 | if(this->kill_enable && this->kill_button.connected() && pause_button.equals(kill_button)) { |
e558f082 | 35 | // kill button takes priority |
76217df5 JM |
36 | this->pause_enable = false; |
37 | ||
38 | } else if(this->kill_enable && !this->kill_button.connected() && !this->pause_enable && pause_button.connected()) { | |
39 | // use pause button for kill button | |
40 | this->kill_button = this->pause_button; | |
e558f082 | 41 | } |
81b547a1 | 42 | |
9bce85f9 | 43 | this->register_for_event(ON_CONSOLE_LINE_RECEIVED); |
4e722c00 | 44 | |
e558f082 JM |
45 | if( (this->pause_enable && this->pause_button.connected()) || (this->kill_enable && this->kill_button.connected()) ) { |
46 | THEKERNEL->slow_ticker->attach( 100, this, &PauseButton::button_tick ); | |
47 | } | |
81b547a1 AW |
48 | } |
49 | ||
7b49793d | 50 | //TODO: Make this use InterruptIn |
7dc903db | 51 | //Check the state of the button and act accordingly based on current pause state |
76217df5 JM |
52 | uint32_t PauseButton::button_tick(uint32_t dummy) |
53 | { | |
e558f082 JM |
54 | // If pause button changed |
55 | if(this->pause_enable && this->pause_button.connected()) { | |
56 | bool newstate = this->pause_button.get(); | |
76217df5 | 57 | if(this->button_state != newstate) { |
e558f082 JM |
58 | this->button_state = newstate; |
59 | // If button pressed | |
76217df5 JM |
60 | if( this->button_state ) { |
61 | if( THEKERNEL->pauser->paused() ) { | |
e558f082 | 62 | THEKERNEL->pauser->release(); |
76217df5 | 63 | } else { |
e558f082 JM |
64 | THEKERNEL->pauser->take(); |
65 | } | |
df27a6a3 MM |
66 | } |
67 | } | |
81b547a1 | 68 | } |
e558f082 | 69 | |
76217df5 JM |
70 | if(!this->killed && this->kill_enable && this->kill_button.connected() && !this->kill_button.get()) { |
71 | this->killed = true; | |
72 | THEKERNEL->call_event(ON_HALT); | |
73 | //THEKERNEL->streams->printf("Kill button pressed - reset required to continue\r\n"); // not in an interrupt | |
e558f082 JM |
74 | } |
75 | ||
f03d3c1c | 76 | return 0; |
81b547a1 | 77 | } |
9bce85f9 L |
78 | |
79 | // When a new line is received, check if it is a command, and if it is, act upon it | |
80 | void PauseButton::on_console_line_received( void *argument ) | |
81 | { | |
82 | SerialMessage new_message = *static_cast<SerialMessage *>(argument); | |
83 | ||
84 | // ignore comments and blank lines and if this is a G code then also ignore it | |
85 | char first_char = new_message.message[0]; | |
86 | if(strchr(";( \n\rGMTN", first_char) != NULL) return; | |
87 | ||
76217df5 | 88 | string cmd = shift_parameter(new_message.message); |
9bce85f9 | 89 | |
e558f082 | 90 | if (cmd == "freeze") { |
76217df5 | 91 | if( !THEKERNEL->pauser->paused() ) { |
9bce85f9 L |
92 | THEKERNEL->pauser->take(); |
93 | } | |
7dc903db | 94 | |
76217df5 JM |
95 | } else if (cmd == "unfreeze") { |
96 | if( THEKERNEL->pauser->paused() ) { | |
9bce85f9 L |
97 | THEKERNEL->pauser->release(); |
98 | } | |
99 | } | |
100 | } | |
101 |