2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
8 #include "libs/Module.h"
9 #include "libs/Kernel.h"
10 #include "libs/SerialMessage.h"
14 #include "modules/robot/Conveyor.h"
16 #include "MRI_Hooks.h"
20 Switch::Switch(uint16_t name
){
21 this->name_checksum
= name
;
22 //this->dummy_stream = &(StreamOutput::NullStream);
25 void Switch::on_module_loaded(){
26 this->input_pin_state
= true;
27 this->switch_state
= true;
29 register_for_event(ON_CONFIG_RELOAD
);
30 this->register_for_event(ON_GCODE_RECEIVED
);
31 this->register_for_event(ON_GCODE_EXECUTE
);
34 this->on_config_reload(this);
37 this->kernel
->slow_ticker
->attach( 100, this, &Switch::pinpoll_tick
);
40 this->kernel
->slow_ticker
->attach(1000, &output_pin
, &Pwm::on_tick
);
45 void Switch::on_config_reload(void* argument
){
46 this->input_pin
.from_string(this->kernel
->config
->value(switch_checksum
, this->name_checksum
, input_pin_checksum
)->by_default("nc")->as_string())->as_input();
47 this->input_pin_behavior
= this->kernel
->config
->value(switch_checksum
, this->name_checksum
, input_pin_behavior_checksum
)->by_default(momentary_checksum
)->as_number();
48 this->input_on_command
= this->kernel
->config
->value(switch_checksum
, this->name_checksum
, input_on_command_checksum
)->by_default("")->as_string();
49 this->input_off_command
= this->kernel
->config
->value(switch_checksum
, this->name_checksum
, input_off_command_checksum
)->by_default("")->as_string();
50 this->switch_state
= this->kernel
->config
->value(switch_checksum
, this->name_checksum
, startup_state_checksum
)->by_default(false)->as_bool();
51 this->output_pin
.from_string(this->kernel
->config
->value(switch_checksum
, this->name_checksum
, output_pin_checksum
)->by_default("nc")->as_string())->as_output();
52 this->output_pin
.set(this->kernel
->config
->value(switch_checksum
, this->name_checksum
, startup_state_checksum
)->by_default(0)->as_number() );
53 this->output_on_command
= this->kernel
->config
->value(switch_checksum
, this->name_checksum
, output_on_command_checksum
)->by_default("")->as_string();
54 this->output_off_command
= this->kernel
->config
->value(switch_checksum
, this->name_checksum
, output_off_command_checksum
)->by_default("")->as_string();
56 set_low_on_debug(output_pin
.port_number
, output_pin
.pin
);
60 void Switch::on_gcode_received(void* argument
){
61 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
62 // Add the gcode to the queue ourselves if we need it
63 if( ( input_on_command
.length() > 0 && gcode
->command
.compare(0, input_on_command
.length(), input_on_command
) )
64 || ( input_off_command
.length() > 0 && gcode
->command
.compare(0, input_off_command
.length(), input_off_command
) ) ){
65 gcode
->mark_as_taken();
66 if( this->kernel
->conveyor
->queue
.size() == 0 ){
67 this->kernel
->call_event(ON_GCODE_EXECUTE
, gcode
);
69 Block
* block
= this->kernel
->conveyor
->queue
.get_ref( this->kernel
->conveyor
->queue
.size() - 1 );
70 block
->append_gcode(gcode
);
75 // Turn pin on and off
76 void Switch::on_gcode_execute(void* argument
){
77 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
78 if(gcode
->command
.compare(0, input_on_command
.length(), input_on_command
)){
79 if (gcode
->has_letter('S'))
81 int v
= gcode
->get_value('S') * output_pin
.max_pwm() / 256.0;
83 this->output_pin
.pwm(v
);
85 this->output_pin
.set(0);
90 this->output_pin
.set(1);
93 else if(gcode
->command
.compare(0, input_off_command
.length(), input_off_command
)){
95 this->output_pin
.set(0);
99 //TODO: Make this use InterruptIn
100 //Check the state of the button and act accordingly
101 uint32_t Switch::pinpoll_tick(uint32_t dummy
){
103 if(this->input_pin_state
!= this->input_pin
.get()){
104 this->input_pin_state
= this->input_pin
.get();
106 if( this->input_pin_state
){
107 // if switch is a toggle switch
108 if( this->input_pin_behavior
== toggle_checksum
){
110 // else default is momentary
115 // else if button released
117 // if switch is momentary
118 if( !this->input_pin_behavior
== toggle_checksum
){
127 this->switch_state
= !this->switch_state
;
128 if( this->switch_state
){
129 this->send_gcode( this->output_on_command
, &(StreamOutput::NullStream
) );
131 this->send_gcode( this->output_off_command
, &(StreamOutput::NullStream
) );
135 void Switch::send_gcode(std::string msg
, StreamOutput
* stream
) {
136 struct SerialMessage message
;
137 message
.message
= msg
;
138 message
.stream
= stream
;
139 this->kernel
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);