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"
19 #include "MRI_Hooks.h"
21 #define startup_state_checksum CHECKSUM("startup_state")
22 #define startup_value_checksum CHECKSUM("startup_value")
23 #define input_pin_checksum CHECKSUM("input_pin")
24 #define input_pin_behavior_checksum CHECKSUM("input_pin_behavior")
25 #define toggle_checksum CHECKSUM("toggle")
26 #define momentary_checksum CHECKSUM("momentary")
27 #define input_on_command_checksum CHECKSUM("input_on_command")
28 #define input_off_command_checksum CHECKSUM("input_off_command")
29 #define output_pin_checksum CHECKSUM("output_pin")
30 #define output_on_command_checksum CHECKSUM("output_on_command")
31 #define output_off_command_checksum CHECKSUM("output_off_command")
35 Switch::Switch(uint16_t name
){
36 this->name_checksum
= name
;
37 //this->dummy_stream = &(StreamOutput::NullStream);
40 void Switch::on_module_loaded(){
41 this->input_pin_state
= true;
42 this->switch_state
= true;
43 this->switch_changed
= false;
45 register_for_event(ON_CONFIG_RELOAD
);
46 this->register_for_event(ON_GCODE_RECEIVED
);
47 this->register_for_event(ON_GCODE_EXECUTE
);
48 this->register_for_event(ON_MAIN_LOOP
);
49 this->register_for_event(ON_GET_PUBLIC_DATA
);
50 this->register_for_event(ON_SET_PUBLIC_DATA
);
53 this->on_config_reload(this);
56 THEKERNEL
->slow_ticker
->attach( 100, this, &Switch::pinpoll_tick
);
59 THEKERNEL
->slow_ticker
->attach(1000, &output_pin
, &Pwm::on_tick
);
64 void Switch::on_config_reload(void* argument
){
65 this->input_pin
.from_string( THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_pin_checksum
)->by_default("nc")->as_string())->as_input();
66 this->input_pin_behavior
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_pin_behavior_checksum
)->by_default(momentary_checksum
)->as_number();
67 std::string input_on_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_on_command_checksum
)->by_default("")->as_string();
68 std::string input_off_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, input_off_command_checksum
)->by_default("")->as_string();
69 this->output_pin
.from_string(THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_pin_checksum
)->by_default("nc")->as_string())->as_output();
70 this->output_on_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_on_command_checksum
)->by_default("")->as_string();
71 this->output_off_command
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, output_off_command_checksum
)->by_default("")->as_string();
72 this->switch_state
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, startup_state_checksum
)->by_default(false)->as_bool();
73 this->switch_value
= THEKERNEL
->config
->value(switch_checksum
, this->name_checksum
, startup_value_checksum
)->by_default(this->output_pin
.max_pwm())->as_number();
74 if(this->switch_state
)
75 this->output_pin
.set(this->switch_value
);
77 this->output_pin
.set(0);
79 set_low_on_debug(output_pin
.port_number
, output_pin
.pin
);
81 // Set the on/off command codes, Use GCode to do the parsing
82 input_on_command_letter
= 0;
83 input_off_command_letter
= 0;
85 if(!input_on_command
.empty()) {
86 Gcode
gc(input_on_command
, NULL
);
88 input_on_command_letter
= 'G';
89 input_on_command_code
= gc
.g
;
91 input_on_command_letter
= 'M';
92 input_on_command_code
= gc
.m
;
95 if(!input_off_command
.empty()) {
96 Gcode
gc(input_off_command
, NULL
);
98 input_off_command_letter
= 'G';
99 input_off_command_code
= gc
.g
;
100 } else if(gc
.has_m
) {
101 input_off_command_letter
= 'M';
102 input_off_command_code
= gc
.m
;
108 bool Switch::match_input_gcode(const Gcode
* gcode
) const {
109 return ((input_on_command_letter
== 'M' && gcode
->has_m
&& gcode
->m
== input_on_command_code
) ||
110 (input_on_command_letter
== 'G' && gcode
->has_g
&& gcode
->g
== input_on_command_code
));
113 bool Switch::match_output_gcode(const Gcode
* gcode
) const {
114 return ((input_off_command_letter
== 'M' && gcode
->has_m
&& gcode
->m
== input_off_command_code
) ||
115 (input_off_command_letter
== 'G' && gcode
->has_g
&& gcode
->g
== input_off_command_code
));
118 void Switch::on_gcode_received(void* argument
){
119 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
120 // Add the gcode to the queue ourselves if we need it
121 if (match_input_gcode(gcode
) || match_output_gcode(gcode
)) {
122 THEKERNEL
->conveyor
->append_gcode(gcode
);
126 // Turn pin on and off
127 void Switch::on_gcode_execute(void* argument
){
128 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
130 if(match_input_gcode(gcode
)) {
131 if (gcode
->has_letter('S'))
133 int v
= gcode
->get_value('S') * output_pin
.max_pwm() / 256.0;
135 this->output_pin
.pwm(v
);
136 this->switch_value
= v
;
137 this->switch_state
= true;
140 this->output_pin
.set(0);
141 this->switch_state
= false;
146 this->output_pin
.pwm(this->switch_value
);
147 this->switch_state
= true;
150 } else if(match_output_gcode(gcode
)) {
152 this->output_pin
.set(0);
153 this->switch_state
= false;
157 void Switch::on_get_public_data(void* argument
){
158 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
160 if(!pdr
->starts_with(switch_checksum
)) return;
162 if(!pdr
->second_element_is(this->name_checksum
)) return; // likely fan, but could be anything
164 // ok this is targeted at us, so send back the requested data
165 // this must be static as it will be accessed long after we have returned
166 static struct pad_switch pad
;
167 pad
.name
= this->name_checksum
;
168 pad
.state
= this->switch_state
;
169 pad
.value
= this->switch_value
;
171 pdr
->set_data_ptr(&pad
);
175 void Switch::on_set_public_data(void* argument
){
176 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
178 if(!pdr
->starts_with(switch_checksum
)) return;
180 if(!pdr
->second_element_is(this->name_checksum
)) return; // likely fan, but could be anything
182 // ok this is targeted at us, so set the value
183 if(pdr
->third_element_is(state_checksum
)) {
184 bool t
= *static_cast<bool*>(pdr
->get_data_ptr());
185 this->switch_state
= t
;
188 else if(pdr
->third_element_is(value_checksum
)) {
189 float t
= *static_cast<float*>(pdr
->get_data_ptr());
190 this->switch_value
= t
;
195 void Switch::on_main_loop(void* argument
){
196 if(this->switch_changed
){
197 if(this->switch_state
){
198 this->send_gcode( this->output_on_command
, &(StreamOutput::NullStream
) );
199 this->output_pin
.pwm(this->switch_value
);
201 this->send_gcode( this->output_off_command
, &(StreamOutput::NullStream
) );
202 this->output_pin
.set(0);
204 this->switch_changed
=false;
208 //TODO: Make this use InterruptIn
209 //Check the state of the button and act accordingly
210 uint32_t Switch::pinpoll_tick(uint32_t dummy
){
212 bool current_state
= this->input_pin
.get();
213 if(this->input_pin_state
!= current_state
){
214 this->input_pin_state
= current_state
;
216 if( this->input_pin_state
){
217 // if switch is a toggle switch
218 if( this->input_pin_behavior
== toggle_checksum
){
220 // else default is momentary
225 // else if button released
227 // if switch is momentary
228 if( !this->input_pin_behavior
== toggle_checksum
){
237 this->switch_state
= !this->switch_state
;
238 this->switch_changed
= true;
241 void Switch::send_gcode(std::string msg
, StreamOutput
* stream
) {
242 struct SerialMessage message
;
243 message
.message
= msg
;
244 message
.stream
= stream
;
245 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);