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"
15 #include "PublicDataRequest.h"
16 #include "SwitchPublicAccess.h"
17 #include "SlowTicker.h"
20 #include "checksumm.h"
21 #include "ConfigValue.h"
22 #include "libs/StreamOutput.h"
24 #include "MRI_Hooks.h"
26 #define startup_state_checksum CHECKSUM("startup_state")
27 #define startup_value_checksum CHECKSUM("startup_value")
28 #define input_pin_checksum CHECKSUM("input_pin")
29 #define input_pin_behavior_checksum CHECKSUM("input_pin_behavior")
30 #define toggle_checksum CHECKSUM("toggle")
31 #define momentary_checksum CHECKSUM("momentary")
32 #define input_on_command_checksum CHECKSUM("input_on_command")
33 #define input_off_command_checksum CHECKSUM("input_off_command")
34 #define output_pin_checksum CHECKSUM("output_pin")
35 #define output_type_checksum CHECKSUM("output_type")
36 #define max_pwm_checksum CHECKSUM("max_pwm")
37 #define output_on_command_checksum CHECKSUM("output_on_command")
38 #define output_off_command_checksum CHECKSUM("output_off_command")
42 Switch::Switch(uint16_t name
)
44 this->name_checksum
= name
;
45 //this->dummy_stream = &(StreamOutput::NullStream);
48 void Switch::on_module_loaded()
50 this->switch_changed
= false;
52 register_for_event(ON_CONFIG_RELOAD
);
53 this->register_for_event(ON_GCODE_RECEIVED
);
54 this->register_for_event(ON_GCODE_EXECUTE
);
55 this->register_for_event(ON_MAIN_LOOP
);
56 this->register_for_event(ON_GET_PUBLIC_DATA
);
57 this->register_for_event(ON_SET_PUBLIC_DATA
);
60 this->on_config_reload(this);
65 void Switch::on_config_reload(void *argument
)
67 this->input_pin
.from_string( THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_pin_checksum
)->by_default("nc")->as_string())->as_input();
68 this->input_pin_behavior
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_pin_behavior_checksum
)->by_default(momentary_checksum
)->as_number();
69 std::string input_on_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_on_command_checksum
)->by_default("")->as_string();
70 std::string input_off_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_off_command_checksum
)->by_default("")->as_string();
71 this->output_pin
.from_string(THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_pin_checksum
)->by_default("nc")->as_string())->as_output();
72 this->output_on_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_on_command_checksum
)->by_default("")->as_string();
73 this->output_off_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_off_command_checksum
)->by_default("")->as_string();
74 this->switch_state
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, startup_state_checksum
)->by_default(false)->as_bool();
75 string type
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_type_checksum
)->by_default("pwm")->as_string();
77 if(type
== "pwm") this->output_type
= PWM
;
78 else if(type
== "digital") this->output_type
= DIGITAL
;
79 else this->output_type
= PWM
; // unkown type default to pwm
81 if(this->output_pin
.connected()) {
82 if(this->output_type
== PWM
) {
83 this->output_pin
.max_pwm(THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, max_pwm_checksum
)->by_default(255)->as_number());
84 this->output_pin
.pwm(this->switch_state
? 255 : 0); // will be truncated to max_pwm
87 this->output_pin
.set(this->switch_state
);
91 this->switch_value
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, startup_value_checksum
)->by_default(this->output_pin
.max_pwm())->as_number();
93 set_low_on_debug(output_pin
.port_number
, output_pin
.pin
);
95 // Set the on/off command codes, Use GCode to do the parsing
96 input_on_command_letter
= 0;
97 input_off_command_letter
= 0;
99 if(!input_on_command
.empty()) {
100 Gcode
gc(input_on_command
, NULL
);
102 input_on_command_letter
= 'G';
103 input_on_command_code
= gc
.g
;
104 } else if(gc
.has_m
) {
105 input_on_command_letter
= 'M';
106 input_on_command_code
= gc
.m
;
109 if(!input_off_command
.empty()) {
110 Gcode
gc(input_off_command
, NULL
);
112 input_off_command_letter
= 'G';
113 input_off_command_code
= gc
.g
;
114 } else if(gc
.has_m
) {
115 input_off_command_letter
= 'M';
116 input_off_command_code
= gc
.m
;
120 if(input_pin
.connected()) {
121 // set to initial state
122 this->input_pin_state
= this->input_pin
.get();
124 THEKERNEL
->slow_ticker
->attach( 100, this, &Switch::pinpoll_tick
);
127 if(this->output_type
== PWM
&& this->output_pin
.connected()) {
129 THEKERNEL
->slow_ticker
->attach(1000, &this->output_pin
, &Pwm::on_tick
);
133 bool Switch::match_input_on_gcode(const Gcode
*gcode
) const
135 return ((input_on_command_letter
== 'M' && gcode
->has_m
&& gcode
->m
== input_on_command_code
) ||
136 (input_on_command_letter
== 'G' && gcode
->has_g
&& gcode
->g
== input_on_command_code
));
139 bool Switch::match_input_off_gcode(const Gcode
*gcode
) const
141 return ((input_off_command_letter
== 'M' && gcode
->has_m
&& gcode
->m
== input_off_command_code
) ||
142 (input_off_command_letter
== 'G' && gcode
->has_g
&& gcode
->g
== input_off_command_code
));
145 void Switch::on_gcode_received(void *argument
)
147 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
148 // Add the gcode to the queue ourselves if we need it
149 if (match_input_on_gcode(gcode
) || match_input_off_gcode(gcode
)) {
150 THEKERNEL
->conveyor
->append_gcode(gcode
);
154 // Turn pin on and off
155 void Switch::on_gcode_execute(void *argument
)
157 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
159 if(match_input_on_gcode(gcode
)) {
160 if (this->output_type
== PWM
) {
162 if(gcode
->has_letter('S')) {
163 int v
= round(gcode
->get_value('S') * output_pin
.max_pwm() / 255.0); // scale by max_pwm so input of 255 and max_pwm of 128 would set value to 128
165 this->output_pin
.pwm(v
);
166 this->switch_value
= v
;
167 this->switch_state
= true;
169 this->output_pin
.pwm(0);
170 this->switch_state
= false;
175 this->output_pin
.pwm(255); // will be truncated to max_pwm if set
176 this->switch_state
= true;
181 this->output_pin
.set(true);
184 } else if(match_input_off_gcode(gcode
)) {
185 if (this->output_type
== PWM
) {
187 this->output_pin
.pwm(0);
188 this->switch_state
= false;
190 // logic pin turn off
191 this->output_pin
.set(false);
192 this->switch_state
= false;
197 void Switch::on_get_public_data(void *argument
)
199 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
201 if(!pdr
->starts_with(switch_checksum
)) return;
203 if(!pdr
->second_element_is(this->name_checksum
)) return; // likely fan, but could be anything
205 // ok this is targeted at us, so send back the requested data
206 // this must be static as it will be accessed long after we have returned
207 static struct pad_switch pad
;
208 pad
.name
= this->name_checksum
;
209 pad
.state
= this->switch_state
;
210 pad
.value
= this->switch_value
;
212 pdr
->set_data_ptr(&pad
);
216 void Switch::on_set_public_data(void *argument
)
218 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
220 if(!pdr
->starts_with(switch_checksum
)) return;
222 if(!pdr
->second_element_is(this->name_checksum
)) return; // likely fan, but could be anything
224 // ok this is targeted at us, so set the value
225 if(pdr
->third_element_is(state_checksum
)) {
226 bool t
= *static_cast<bool *>(pdr
->get_data_ptr());
227 this->switch_state
= t
;
229 this->switch_changed
= true;
231 } else if(pdr
->third_element_is(value_checksum
)) {
232 float t
= *static_cast<float *>(pdr
->get_data_ptr());
233 this->switch_value
= t
;
238 void Switch::on_main_loop(void *argument
)
240 if(this->switch_changed
) {
241 if(this->switch_state
) {
242 if(!this->output_on_command
.empty()) this->send_gcode( this->output_on_command
, &(StreamOutput::NullStream
) );
243 if(this->output_pin
.connected()) {
244 if(this->output_type
== PWM
)
245 this->output_pin
.pwm(this->switch_value
); // this requires the value has been set otherwise it swicthes on to whatever it last was
247 this->output_pin
.set(true);
251 if(!this->output_off_command
.empty()) this->send_gcode( this->output_off_command
, &(StreamOutput::NullStream
) );
252 if(this->output_pin
.connected()) {
253 if(this->output_type
== PWM
)
254 this->output_pin
.pwm(0);
256 this->output_pin
.set(false);
259 this->switch_changed
= false;
263 // TODO Make this use InterruptIn
264 // Check the state of the button and act accordingly
265 uint32_t Switch::pinpoll_tick(uint32_t dummy
)
267 if(!input_pin
.connected()) return 0;
270 bool current_state
= this->input_pin
.get();
271 if(this->input_pin_state
!= current_state
) {
272 this->input_pin_state
= current_state
;
274 if( this->input_pin_state
) {
275 // if switch is a toggle switch
276 if( this->input_pin_behavior
== toggle_checksum
) {
278 // else default is momentary
282 // else if button released
284 // if switch is momentary
285 if( this->input_pin_behavior
== momentary_checksum
) {
295 this->switch_state
= !this->switch_state
;
296 this->switch_changed
= true;
299 void Switch::send_gcode(std::string msg
, StreamOutput
*stream
)
301 struct SerialMessage message
;
302 message
.message
= msg
;
303 message
.stream
= stream
;
304 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);