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" |
61134a65 | 12 | |
81b547a1 AW |
13 | using 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 | 20 | PauseButton::PauseButton(){} |
81b547a1 AW |
21 | |
22 | void PauseButton::on_module_loaded(){ | |
23 | this->button_state = true; | |
24 | this->play_state = true; | |
81b547a1 | 25 | |
314ab8f7 MM |
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(); | |
81b547a1 | 28 | |
9bce85f9 | 29 | this->register_for_event(ON_CONSOLE_LINE_RECEIVED); |
4e722c00 JM |
30 | |
31 | if(this->enable) THEKERNEL->slow_ticker->attach( 100, this, &PauseButton::button_tick ); | |
81b547a1 AW |
32 | } |
33 | ||
7b49793d | 34 | //TODO: Make this use InterruptIn |
81b547a1 | 35 | //Check the state of the button and act accordingly |
8b8b3339 | 36 | uint32_t PauseButton::button_tick(uint32_t dummy){ |
2d9cf0e9 | 37 | if(!this->enable) return 0; |
df27a6a3 | 38 | // If button changed |
62529560 MM |
39 | bool newstate = this->button.get(); |
40 | if(this->button_state != newstate){ | |
41 | this->button_state = newstate; | |
df27a6a3 | 42 | // If button pressed |
81b547a1 AW |
43 | if( this->button_state ){ |
44 | if( this->play_state ){ | |
45 | this->play_state = false; | |
314ab8f7 | 46 | THEKERNEL->pauser->take(); |
81b547a1 AW |
47 | }else{ |
48 | this->play_state = true; | |
314ab8f7 | 49 | THEKERNEL->pauser->release(); |
df27a6a3 MM |
50 | } |
51 | } | |
81b547a1 | 52 | } |
f03d3c1c | 53 | return 0; |
81b547a1 | 54 | } |
9bce85f9 L |
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 |