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;
28 this->switch_changed
= false;
30 register_for_event(ON_CONFIG_RELOAD
);
31 this->register_for_event(ON_GCODE_RECEIVED
);
32 this->register_for_event(ON_GCODE_EXECUTE
);
33 this->register_for_event(ON_MAIN_LOOP
);
36 this->on_config_reload(this);
39 THEKERNEL
->slow_ticker
->attach( 100, this, &Switch::pinpoll_tick
);
42 THEKERNEL
->slow_ticker
->attach(1000, &output_pin
, &Pwm::on_tick
);
47 void Switch::on_config_reload(void* argument
){
48 this->input_pin
.from_string(THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_pin_checksum
)->by_default("nc")->as_string())->as_input();
49 this->input_pin_behavior
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_pin_behavior_checksum
)->by_default(momentary_checksum
)->as_number();
50 this->input_on_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_on_command_checksum
)->by_default("")->as_string();
51 this->input_off_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_off_command_checksum
)->by_default("")->as_string();
52 this->output_pin
.from_string(THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_pin_checksum
)->by_default("nc")->as_string())->as_output();
53 this->output_on_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_on_command_checksum
)->by_default("")->as_string();
54 this->output_off_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_off_command_checksum
)->by_default("")->as_string();
55 this->switch_state
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, startup_state_checksum
)->by_default(false)->as_bool();
56 this->switch_value
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, startup_value_checksum
)->by_default(this->output_pin
.max_pwm())->as_number();
57 if(this->switch_state
)
58 this->output_pin
.set(this->switch_value
);
60 this->output_pin
.set(0);
62 set_low_on_debug(output_pin
.port_number
, output_pin
.pin
);
66 void Switch::on_gcode_received(void* argument
){
67 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
68 // Add the gcode to the queue ourselves if we need it
69 if( ( input_on_command
.length() > 0 && ! gcode
->command
.compare(0, input_on_command
.length(), input_on_command
) )
70 || ( input_off_command
.length() > 0 && ! gcode
->command
.compare(0, input_off_command
.length(), input_off_command
) ) ){
71 gcode
->mark_as_taken();
72 if( THEKERNEL
->conveyor
->queue
.size() == 0 ){
73 THEKERNEL
->call_event(ON_GCODE_EXECUTE
, gcode
);
75 Block
* block
= THEKERNEL
->conveyor
->queue
.get_ref( THEKERNEL
->conveyor
->queue
.size() - 1 );
76 block
->append_gcode(gcode
);
81 // Turn pin on and off
82 void Switch::on_gcode_execute(void* argument
){
83 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
84 if(! gcode
->command
.compare(0, input_on_command
.length(), input_on_command
)){
85 if (gcode
->has_letter('S'))
87 int v
= gcode
->get_value('S') * output_pin
.max_pwm() / 256.0;
89 this->output_pin
.pwm(v
);
90 this->switch_value
= v
;
91 this->switch_state
= true;
94 this->output_pin
.set(0);
95 this->switch_state
= false;
101 this->output_pin
.pwm(this->switch_value
);
102 this->switch_state
= true;
105 else if(! gcode
->command
.compare(0, input_off_command
.length(), input_off_command
)){
107 this->output_pin
.set(0);
108 this->switch_state
= false;
112 void Switch::on_main_loop(void* argument
){
113 if(this->switch_changed
){
114 if(this->switch_state
){
115 this->send_gcode( this->output_on_command
, &(StreamOutput::NullStream
) );
116 this->output_pin
.pwm(this->switch_value
);
118 this->send_gcode( this->output_off_command
, &(StreamOutput::NullStream
) );
119 this->output_pin
.set(0);
121 this->switch_changed
=false;
125 //TODO: Make this use InterruptIn
126 //Check the state of the button and act accordingly
127 uint32_t Switch::pinpoll_tick(uint32_t dummy
){
129 bool current_state
= this->input_pin
.get();
130 if(this->input_pin_state
!= current_state
){
131 this->input_pin_state
= current_state
;
133 if( this->input_pin_state
){
134 // if switch is a toggle switch
135 if( this->input_pin_behavior
== toggle_checksum
){
137 // else default is momentary
142 // else if button released
144 // if switch is momentary
145 if( !this->input_pin_behavior
== toggle_checksum
){
154 this->switch_state
= !this->switch_state
;
155 this->switch_changed
= true;
158 void Switch::send_gcode(std::string msg
, StreamOutput
* stream
) {
159 struct SerialMessage message
;
160 message
.message
= msg
;
161 message
.stream
= stream
;
162 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);