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 this->register_for_event(ON_GCODE_RECEIVED
);
53 this->register_for_event(ON_GCODE_EXECUTE
);
54 this->register_for_event(ON_MAIN_LOOP
);
55 this->register_for_event(ON_GET_PUBLIC_DATA
);
56 this->register_for_event(ON_SET_PUBLIC_DATA
);
59 this->on_config_reload(this);
64 void Switch::on_config_reload(void *argument
)
66 this->input_pin
.from_string( THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_pin_checksum
)->by_default("nc")->as_string())->as_input();
67 this->input_pin_behavior
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_pin_behavior_checksum
)->by_default(momentary_checksum
)->as_number();
68 std::string input_on_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_on_command_checksum
)->by_default("")->as_string();
69 std::string input_off_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_off_command_checksum
)->by_default("")->as_string();
70 this->output_pin
.from_string(THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_pin_checksum
)->by_default("nc")->as_string())->as_output();
71 this->output_on_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_on_command_checksum
)->by_default("")->as_string();
72 this->output_off_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_off_command_checksum
)->by_default("")->as_string();
73 this->switch_state
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, startup_state_checksum
)->by_default(false)->as_bool();
74 string type
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_type_checksum
)->by_default("pwm")->as_string();
76 if(type
== "pwm") this->output_type
= PWM
;
77 else if(type
== "digital") this->output_type
= DIGITAL
;
78 else this->output_type
= PWM
; // unkown type default to pwm
80 if(this->output_pin
.connected()) {
81 if(this->output_type
== PWM
) {
82 this->output_pin
.max_pwm(THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, max_pwm_checksum
)->by_default(255)->as_number());
83 this->switch_value
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, startup_value_checksum
)->by_default(this->output_pin
.max_pwm())->as_number();
84 if(this->switch_state
) {
85 this->output_pin
.pwm(this->switch_value
); // will be truncated to max_pwm
87 this->output_pin
.set(false);
90 this->output_pin
.set(this->switch_state
);
94 set_low_on_debug(output_pin
.port_number
, output_pin
.pin
);
96 // Set the on/off command codes, Use GCode to do the parsing
97 input_on_command_letter
= 0;
98 input_off_command_letter
= 0;
100 if(!input_on_command
.empty()) {
101 Gcode
gc(input_on_command
, NULL
);
103 input_on_command_letter
= 'G';
104 input_on_command_code
= gc
.g
;
105 } else if(gc
.has_m
) {
106 input_on_command_letter
= 'M';
107 input_on_command_code
= gc
.m
;
110 if(!input_off_command
.empty()) {
111 Gcode
gc(input_off_command
, NULL
);
113 input_off_command_letter
= 'G';
114 input_off_command_code
= gc
.g
;
115 } else if(gc
.has_m
) {
116 input_off_command_letter
= 'M';
117 input_off_command_code
= gc
.m
;
121 if(input_pin
.connected()) {
122 // set to initial state
123 this->input_pin_state
= this->input_pin
.get();
125 THEKERNEL
->slow_ticker
->attach( 100, this, &Switch::pinpoll_tick
);
128 if(this->output_type
== PWM
&& this->output_pin
.connected()) {
130 THEKERNEL
->slow_ticker
->attach(1000, &this->output_pin
, &Pwm::on_tick
);
134 bool Switch::match_input_on_gcode(const Gcode
*gcode
) const
136 return ((input_on_command_letter
== 'M' && gcode
->has_m
&& gcode
->m
== input_on_command_code
) ||
137 (input_on_command_letter
== 'G' && gcode
->has_g
&& gcode
->g
== input_on_command_code
));
140 bool Switch::match_input_off_gcode(const Gcode
*gcode
) const
142 return ((input_off_command_letter
== 'M' && gcode
->has_m
&& gcode
->m
== input_off_command_code
) ||
143 (input_off_command_letter
== 'G' && gcode
->has_g
&& gcode
->g
== input_off_command_code
));
146 void Switch::on_gcode_received(void *argument
)
148 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
149 // Add the gcode to the queue ourselves if we need it
150 if (match_input_on_gcode(gcode
) || match_input_off_gcode(gcode
)) {
151 THEKERNEL
->conveyor
->append_gcode(gcode
);
155 // Turn pin on and off
156 void Switch::on_gcode_execute(void *argument
)
158 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
160 if(match_input_on_gcode(gcode
)) {
162 if (this->output_type
== PWM
) {
163 // PWM output pin turn on (or off if S0)
164 if(gcode
->has_letter('S')) {
165 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
166 this->output_pin
.pwm(v
);
167 this->switch_state
= (v
> 0);
169 this->output_pin
.pwm(this->switch_value
);
170 this->switch_state
= (this->switch_value
> 0);
174 this->output_pin
.set(true);
175 this->switch_state
= true;
177 } else if(match_input_off_gcode(gcode
)) {
178 this->switch_state
= false;
179 if (this->output_type
== PWM
) {
181 this->output_pin
.set(false);
183 // logic pin turn off
184 this->output_pin
.set(false);
189 void Switch::on_get_public_data(void *argument
)
191 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
193 if(!pdr
->starts_with(switch_checksum
)) return;
195 if(!pdr
->second_element_is(this->name_checksum
)) return; // likely fan, but could be anything
197 // ok this is targeted at us, so send back the requested data
198 // this must be static as it will be accessed long after we have returned
199 static struct pad_switch pad
;
200 pad
.name
= this->name_checksum
;
201 pad
.state
= this->switch_state
;
202 pad
.value
= this->switch_value
;
204 pdr
->set_data_ptr(&pad
);
208 void Switch::on_set_public_data(void *argument
)
210 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
212 if(!pdr
->starts_with(switch_checksum
)) return;
214 if(!pdr
->second_element_is(this->name_checksum
)) return; // likely fan, but could be anything
216 // ok this is targeted at us, so set the value
217 if(pdr
->third_element_is(state_checksum
)) {
218 bool t
= *static_cast<bool *>(pdr
->get_data_ptr());
219 this->switch_state
= t
;
221 this->switch_changed
= true;
223 } else if(pdr
->third_element_is(value_checksum
)) {
224 float t
= *static_cast<float *>(pdr
->get_data_ptr());
225 this->switch_value
= t
;
226 this->switch_changed
= true;
231 void Switch::on_main_loop(void *argument
)
233 if(this->switch_changed
) {
234 if(this->switch_state
) {
235 if(!this->output_on_command
.empty()) this->send_gcode( this->output_on_command
, &(StreamOutput::NullStream
) );
236 if(this->output_pin
.connected()) {
237 if(this->output_type
== PWM
)
238 this->output_pin
.pwm(this->switch_value
); // this requires the value has been set otherwise it switches on to whatever it last was
240 this->output_pin
.set(true);
244 if(!this->output_off_command
.empty()) this->send_gcode( this->output_off_command
, &(StreamOutput::NullStream
) );
245 if(this->output_pin
.connected()) {
246 if(this->output_type
== PWM
)
247 this->output_pin
.set(false);
249 this->output_pin
.set(false);
252 this->switch_changed
= false;
256 // TODO Make this use InterruptIn
257 // Check the state of the button and act accordingly
258 uint32_t Switch::pinpoll_tick(uint32_t dummy
)
260 if(!input_pin
.connected()) return 0;
263 bool current_state
= this->input_pin
.get();
264 if(this->input_pin_state
!= current_state
) {
265 this->input_pin_state
= current_state
;
267 if( this->input_pin_state
) {
268 // if switch is a toggle switch
269 if( this->input_pin_behavior
== toggle_checksum
) {
271 // else default is momentary
275 // else if button released
277 // if switch is momentary
278 if( this->input_pin_behavior
== momentary_checksum
) {
288 this->switch_state
= !this->switch_state
;
289 this->switch_changed
= true;
292 void Switch::send_gcode(std::string msg
, StreamOutput
*stream
)
294 struct SerialMessage message
;
295 message
.message
= msg
;
296 message
.stream
= stream
;
297 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);