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"
13 #include "modules/robot/Conveyor.h"
15 #include "MRI_Hooks.h"
19 Switch::Switch(uint16_t name
){
20 this->name_checksum
= name
;
23 void Switch::on_module_loaded(){
24 register_for_event(ON_CONFIG_RELOAD
);
25 this->register_for_event(ON_GCODE_RECEIVED
);
26 this->register_for_event(ON_GCODE_EXECUTE
);
29 this->on_config_reload(this);
32 this->kernel
->slow_ticker
->attach(1000, &output_pin
, &Pwm::on_tick
);
37 void Switch::on_config_reload(void* argument
){
38 this->on_m_code
= this->kernel
->config
->value(switch_checksum
, this->name_checksum
, on_m_code_checksum
)->required()->as_number();
39 this->off_m_code
= this->kernel
->config
->value(switch_checksum
, this->name_checksum
, off_m_code_checksum
)->required()->as_number();
40 this->output_pin
.from_string(this->kernel
->config
->value(switch_checksum
, this->name_checksum
, output_pin_checksum
)->required()->as_string())->as_output();
41 this->output_pin
.set(this->kernel
->config
->value(switch_checksum
, this->name_checksum
, startup_state_checksum
)->by_default(0)->as_number() );
43 set_low_on_debug(output_pin
.port_number
, output_pin
.pin
);
47 void Switch::on_gcode_received(void* argument
){
48 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
49 // Add the gcode to the queue ourselves if we need it
50 if( gcode
->has_m
&& ( gcode
->m
== this->on_m_code
|| gcode
->m
== this->off_m_code
) ){
51 gcode
->mark_as_taken();
52 if( this->kernel
->conveyor
->queue
.size() == 0 ){
53 this->kernel
->call_event(ON_GCODE_EXECUTE
, gcode
);
55 Block
* block
= this->kernel
->conveyor
->queue
.get_ref( this->kernel
->conveyor
->queue
.size() - 1 );
56 block
->append_gcode(gcode
);
61 // Turn pin on and off
62 void Switch::on_gcode_execute(void* argument
){
63 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
66 if( code
== this->on_m_code
){
67 if (gcode
->has_letter('S'))
69 int v
= gcode
->get_value('S') * output_pin
.max_pwm() / 256.0;
71 this->output_pin
.pwm(v
);
73 this->output_pin
.set(0);
78 this->output_pin
.set(1);
81 if( code
== this->off_m_code
){
83 this->output_pin
.set(0);