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 "StreamOutput.h"
23 #include "StreamOutputPool.h"
27 #include "MRI_Hooks.h"
29 #define startup_state_checksum CHECKSUM("startup_state")
30 #define startup_value_checksum CHECKSUM("startup_value")
31 #define input_pin_checksum CHECKSUM("input_pin")
32 #define input_pin_behavior_checksum CHECKSUM("input_pin_behavior")
33 #define toggle_checksum CHECKSUM("toggle")
34 #define momentary_checksum CHECKSUM("momentary")
35 #define input_on_command_checksum CHECKSUM("input_on_command")
36 #define input_off_command_checksum CHECKSUM("input_off_command")
37 #define output_pin_checksum CHECKSUM("output_pin")
38 #define output_type_checksum CHECKSUM("output_type")
39 #define max_pwm_checksum CHECKSUM("max_pwm")
40 #define output_on_command_checksum CHECKSUM("output_on_command")
41 #define output_off_command_checksum CHECKSUM("output_off_command")
42 #define pwm_period_ms_checksum CHECKSUM("pwm_period_ms")
43 #define failsafe_checksum CHECKSUM("failsafe_set_to")
44 #define ignore_onhalt_checksum CHECKSUM("ignore_on_halt")
48 Switch::Switch(uint16_t name
)
50 this->name_checksum
= name
;
51 //this->dummy_stream = &(StreamOutput::NullStream);
54 // set the pin to the fail safe value on halt
55 void Switch::on_halt(void *arg
)
58 if(this->ignore_on_halt
) return;
60 // set pin to failsafe value
61 switch(this->output_type
) {
62 case DIGITAL
: this->digital_pin
->set(this->failsafe
); break;
63 case SIGMADELTA
: this->sigmadelta_pin
->set(this->failsafe
); break;
64 case HWPWM
: this->pwm_pin
->write(0); break;
67 this->switch_state
= this->failsafe
;
71 void Switch::on_module_loaded()
73 this->switch_changed
= false;
75 this->register_for_event(ON_GCODE_RECEIVED
);
76 this->register_for_event(ON_MAIN_LOOP
);
77 this->register_for_event(ON_GET_PUBLIC_DATA
);
78 this->register_for_event(ON_SET_PUBLIC_DATA
);
79 this->register_for_event(ON_HALT
);
82 this->on_config_reload(this);
86 void Switch::on_config_reload(void *argument
)
88 this->input_pin
.from_string( THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_pin_checksum
)->by_default("nc")->as_string())->as_input();
89 this->input_pin_behavior
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_pin_behavior_checksum
)->by_default(momentary_checksum
)->as_number();
90 std::string input_on_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_on_command_checksum
)->by_default("")->as_string();
91 std::string input_off_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_off_command_checksum
)->by_default("")->as_string();
92 this->output_on_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_on_command_checksum
)->by_default("")->as_string();
93 this->output_off_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_off_command_checksum
)->by_default("")->as_string();
94 this->switch_state
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, startup_state_checksum
)->by_default(false)->as_bool();
95 string type
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_type_checksum
)->by_default("pwm")->as_string();
96 this->failsafe
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, failsafe_checksum
)->by_default(0)->as_number();
97 this->ignore_on_halt
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, ignore_onhalt_checksum
)->by_default(false)->as_bool();
100 this->output_type
= SIGMADELTA
;
101 this->sigmadelta_pin
= new Pwm();
102 this->sigmadelta_pin
->from_string(THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_pin_checksum
)->by_default("nc")->as_string())->as_output();
103 if(this->sigmadelta_pin
->connected()) {
105 set_high_on_debug(sigmadelta_pin
->port_number
, sigmadelta_pin
->pin
);
107 set_low_on_debug(sigmadelta_pin
->port_number
, sigmadelta_pin
->pin
);
110 this->output_type
= NONE
;
111 delete this->sigmadelta_pin
;
112 this->sigmadelta_pin
= nullptr;
115 }else if(type
== "digital"){
116 this->output_type
= DIGITAL
;
117 this->digital_pin
= new Pin();
118 this->digital_pin
->from_string(THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_pin_checksum
)->by_default("nc")->as_string())->as_output();
119 if(this->digital_pin
->connected()) {
121 set_high_on_debug(digital_pin
->port_number
, digital_pin
->pin
);
123 set_low_on_debug(digital_pin
->port_number
, digital_pin
->pin
);
126 this->output_type
= NONE
;
127 delete this->digital_pin
;
128 this->digital_pin
= nullptr;
131 }else if(type
== "hwpwm"){
132 this->output_type
= HWPWM
;
134 pin
->from_string(THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_pin_checksum
)->by_default("nc")->as_string())->as_output();
135 this->pwm_pin
= pin
->hardware_pwm();
137 set_high_on_debug(pin
->port_number
, pin
->pin
);
139 set_low_on_debug(pin
->port_number
, pin
->pin
);
142 if(this->pwm_pin
== nullptr) {
143 THEKERNEL
->streams
->printf("Selected Switch output pin is not PWM capable - disabled");
144 this->output_type
= NONE
;
148 this->output_type
= NONE
;
151 if(this->output_type
== SIGMADELTA
) {
152 this->sigmadelta_pin
->max_pwm(THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, max_pwm_checksum
)->by_default(255)->as_number());
153 this->switch_value
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, startup_value_checksum
)->by_default(this->sigmadelta_pin
->max_pwm())->as_number();
154 if(this->switch_state
) {
155 this->sigmadelta_pin
->pwm(this->switch_value
); // will be truncated to max_pwm
157 this->sigmadelta_pin
->set(false);
160 } else if(this->output_type
== HWPWM
) {
162 float p
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, pwm_period_ms_checksum
)->by_default(20)->as_number() * 1000.0F
; // ms but fractions are allowed
163 this->pwm_pin
->period_us(p
);
165 // default is 0% duty cycle
166 this->switch_value
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, startup_value_checksum
)->by_default(0)->as_number();
167 if(this->switch_state
) {
168 this->pwm_pin
->write(this->switch_value
/100.0F
);
170 this->pwm_pin
->write(0);
173 } else if(this->output_type
== DIGITAL
){
174 this->digital_pin
->set(this->switch_state
);
177 // Set the on/off command codes, Use GCode to do the parsing
178 input_on_command_letter
= 0;
179 input_off_command_letter
= 0;
181 if(!input_on_command
.empty()) {
182 Gcode
gc(input_on_command
, NULL
);
184 input_on_command_letter
= 'G';
185 input_on_command_code
= gc
.g
;
186 } else if(gc
.has_m
) {
187 input_on_command_letter
= 'M';
188 input_on_command_code
= gc
.m
;
191 if(!input_off_command
.empty()) {
192 Gcode
gc(input_off_command
, NULL
);
194 input_off_command_letter
= 'G';
195 input_off_command_code
= gc
.g
;
196 } else if(gc
.has_m
) {
197 input_off_command_letter
= 'M';
198 input_off_command_code
= gc
.m
;
202 if(input_pin
.connected()) {
203 // set to initial state
204 this->input_pin_state
= this->input_pin
.get();
206 THEKERNEL
->slow_ticker
->attach( 100, this, &Switch::pinpoll_tick
);
209 if(this->output_type
== SIGMADELTA
) {
211 THEKERNEL
->slow_ticker
->attach(1000, this->sigmadelta_pin
, &Pwm::on_tick
);
215 bool Switch::match_input_on_gcode(const Gcode
*gcode
) const
217 return ((input_on_command_letter
== 'M' && gcode
->has_m
&& gcode
->m
== input_on_command_code
) ||
218 (input_on_command_letter
== 'G' && gcode
->has_g
&& gcode
->g
== input_on_command_code
));
221 bool Switch::match_input_off_gcode(const Gcode
*gcode
) const
223 return ((input_off_command_letter
== 'M' && gcode
->has_m
&& gcode
->m
== input_off_command_code
) ||
224 (input_off_command_letter
== 'G' && gcode
->has_g
&& gcode
->g
== input_off_command_code
));
227 void Switch::on_gcode_received(void *argument
)
229 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
230 // Add the gcode to the queue ourselves if we need it
231 if (!(match_input_on_gcode(gcode
) || match_input_off_gcode(gcode
))) {
235 // we need to sync this with the queue, so we need to wait for queue to empty, however due to certain slicers
236 // issuing redundant swicth on calls regularly we need to optimize by making sure the value is actually changing
237 // hence we need to do the wait for queue in each case rather than just once at the start
238 if(match_input_on_gcode(gcode
)) {
239 if (this->output_type
== SIGMADELTA
) {
240 // SIGMADELTA output pin turn on (or off if S0)
241 if(gcode
->has_letter('S')) {
242 int v
= round(gcode
->get_value('S') * sigmadelta_pin
->max_pwm() / 255.0); // scale by max_pwm so input of 255 and max_pwm of 128 would set value to 128
243 if(v
!= this->sigmadelta_pin
->get_pwm()){ // optimize... ignore if already set to the same pwm
245 THEKERNEL
->conveyor
->wait_for_empty_queue();
246 this->sigmadelta_pin
->pwm(v
);
247 this->switch_state
= (v
> 0);
251 THEKERNEL
->conveyor
->wait_for_empty_queue();
252 this->sigmadelta_pin
->pwm(this->switch_value
);
253 this->switch_state
= (this->switch_value
> 0);
256 } else if (this->output_type
== HWPWM
) {
258 THEKERNEL
->conveyor
->wait_for_empty_queue();
259 // PWM output pin set duty cycle 0 - 100
260 if(gcode
->has_letter('S')) {
261 float v
= gcode
->get_value('S');
264 this->pwm_pin
->write(v
/100.0F
);
265 this->switch_state
= (v
!= 0);
267 this->pwm_pin
->write(this->switch_value
);
268 this->switch_state
= (this->switch_value
!= 0);
271 } else if (this->output_type
== DIGITAL
) {
273 THEKERNEL
->conveyor
->wait_for_empty_queue();
275 this->digital_pin
->set(true);
276 this->switch_state
= true;
279 } else if(match_input_off_gcode(gcode
)) {
281 THEKERNEL
->conveyor
->wait_for_empty_queue();
282 this->switch_state
= false;
283 if (this->output_type
== SIGMADELTA
) {
284 // SIGMADELTA output pin
285 this->sigmadelta_pin
->set(false);
287 } else if (this->output_type
== HWPWM
) {
288 this->pwm_pin
->write(0);
290 } else if (this->output_type
== DIGITAL
) {
291 // logic pin turn off
292 this->digital_pin
->set(false);
297 void Switch::on_get_public_data(void *argument
)
299 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
301 if(!pdr
->starts_with(switch_checksum
)) return;
303 if(!pdr
->second_element_is(this->name_checksum
)) return; // likely fan, but could be anything
305 // ok this is targeted at us, so send back the requested data
306 // caller has provided the location to write the state to
307 struct pad_switch
*pad
= static_cast<struct pad_switch
*>(pdr
->get_data_ptr());
308 pad
->name
= this->name_checksum
;
309 pad
->state
= this->switch_state
;
310 pad
->value
= this->switch_value
;
314 void Switch::on_set_public_data(void *argument
)
316 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
318 if(!pdr
->starts_with(switch_checksum
)) return;
320 if(!pdr
->second_element_is(this->name_checksum
)) return; // likely fan, but could be anything
322 // ok this is targeted at us, so set the value
323 if(pdr
->third_element_is(state_checksum
)) {
324 bool t
= *static_cast<bool *>(pdr
->get_data_ptr());
325 this->switch_state
= t
;
327 this->switch_changed
= true;
329 } else if(pdr
->third_element_is(value_checksum
)) {
330 float t
= *static_cast<float *>(pdr
->get_data_ptr());
331 this->switch_value
= t
;
332 this->switch_changed
= true;
337 void Switch::on_main_loop(void *argument
)
339 if(this->switch_changed
) {
340 if(this->switch_state
) {
341 if(!this->output_on_command
.empty()) this->send_gcode( this->output_on_command
, &(StreamOutput::NullStream
) );
343 if(this->output_type
== SIGMADELTA
) {
344 this->sigmadelta_pin
->pwm(this->switch_value
); // this requires the value has been set otherwise it switches on to whatever it last was
346 } else if (this->output_type
== HWPWM
) {
347 this->pwm_pin
->write(this->switch_value
/100.0F
);
349 } else if (this->output_type
== DIGITAL
) {
350 this->digital_pin
->set(true);
355 if(!this->output_off_command
.empty()) this->send_gcode( this->output_off_command
, &(StreamOutput::NullStream
) );
357 if(this->output_type
== SIGMADELTA
) {
358 this->sigmadelta_pin
->set(false);
360 } else if (this->output_type
== HWPWM
) {
361 this->pwm_pin
->write(0);
363 } else if (this->output_type
== DIGITAL
) {
364 this->digital_pin
->set(false);
367 this->switch_changed
= false;
371 // TODO Make this use InterruptIn
372 // Check the state of the button and act accordingly
373 uint32_t Switch::pinpoll_tick(uint32_t dummy
)
375 if(!input_pin
.connected()) return 0;
378 bool current_state
= this->input_pin
.get();
379 if(this->input_pin_state
!= current_state
) {
380 this->input_pin_state
= current_state
;
382 if( this->input_pin_state
) {
383 // if switch is a toggle switch
384 if( this->input_pin_behavior
== toggle_checksum
) {
386 // else default is momentary
390 // else if button released
392 // if switch is momentary
393 if( this->input_pin_behavior
== momentary_checksum
) {
403 this->switch_state
= !this->switch_state
;
404 this->switch_changed
= true;
407 void Switch::send_gcode(std::string msg
, StreamOutput
*stream
)
409 struct SerialMessage message
;
410 message
.message
= msg
;
411 message
.stream
= stream
;
412 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);