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
) ) )
72 THEKERNEL
->conveyor
->append_gcode(gcode
);
76 // Turn pin on and off
77 void Switch::on_gcode_execute(void* argument
){
78 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
79 if(! gcode
->command
.compare(0, input_on_command
.length(), input_on_command
)){
80 if (gcode
->has_letter('S'))
82 int v
= gcode
->get_value('S') * output_pin
.max_pwm() / 256.0;
84 this->output_pin
.pwm(v
);
85 this->switch_value
= v
;
86 this->switch_state
= true;
89 this->output_pin
.set(0);
90 this->switch_state
= false;
96 this->output_pin
.pwm(this->switch_value
);
97 this->switch_state
= true;
100 else if(! gcode
->command
.compare(0, input_off_command
.length(), input_off_command
)){
102 this->output_pin
.set(0);
103 this->switch_state
= false;
107 void Switch::on_main_loop(void* argument
){
108 if(this->switch_changed
){
109 if(this->switch_state
){
110 this->send_gcode( this->output_on_command
, &(StreamOutput::NullStream
) );
111 this->output_pin
.pwm(this->switch_value
);
113 this->send_gcode( this->output_off_command
, &(StreamOutput::NullStream
) );
114 this->output_pin
.set(0);
116 this->switch_changed
=false;
120 //TODO: Make this use InterruptIn
121 //Check the state of the button and act accordingly
122 uint32_t Switch::pinpoll_tick(uint32_t dummy
){
124 bool current_state
= this->input_pin
.get();
125 if(this->input_pin_state
!= current_state
){
126 this->input_pin_state
= current_state
;
128 if( this->input_pin_state
){
129 // if switch is a toggle switch
130 if( this->input_pin_behavior
== toggle_checksum
){
132 // else default is momentary
137 // else if button released
139 // if switch is momentary
140 if( !this->input_pin_behavior
== toggle_checksum
){
149 this->switch_state
= !this->switch_state
;
150 this->switch_changed
= true;
153 void Switch::send_gcode(std::string msg
, StreamOutput
* stream
) {
154 struct SerialMessage message
;
155 message
.message
= msg
;
156 message
.stream
= stream
;
157 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);