34d0925a55653835cc59494cd70c5fecf791cc47
[clinton/Smoothieware.git] / src / modules / tools / switch / Switch.cpp
1 /*
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/>.
6 */
7
8 #include "libs/Module.h"
9 #include "libs/Kernel.h"
10 #include "libs/SerialMessage.h"
11 #include <math.h>
12 #include "Switch.h"
13 #include "libs/Pin.h"
14 #include "modules/robot/Conveyor.h"
15 #include "PublicDataRequest.h"
16 #include "SwitchPublicAccess.h"
17
18
19 #include "MRI_Hooks.h"
20
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")
32
33 Switch::Switch(){}
34
35 Switch::Switch(uint16_t name){
36 this->name_checksum = name;
37 //this->dummy_stream = &(StreamOutput::NullStream);
38 }
39
40 void Switch::on_module_loaded(){
41 this->input_pin_state = true;
42 this->switch_state = true;
43 this->switch_changed = false;
44
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);
51
52 // Settings
53 this->on_config_reload(this);
54
55 // input pin polling
56 THEKERNEL->slow_ticker->attach( 100, this, &Switch::pinpoll_tick);
57
58 // PWM
59 THEKERNEL->slow_ticker->attach(1000, &output_pin, &Pwm::on_tick);
60 }
61
62
63 // Get config
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);
76 else
77 this->output_pin.set(0);
78
79 set_low_on_debug(output_pin.port_number, output_pin.pin);
80
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;
84
85 if(!input_on_command.empty()) {
86 Gcode gc(input_on_command, NULL);
87 if(gc.has_g){
88 input_on_command_letter= 'G';
89 input_on_command_code= gc.g;
90 } else if(gc.has_m) {
91 input_on_command_letter= 'M';
92 input_on_command_code= gc.m;
93 }
94 }
95 if(!input_off_command.empty()) {
96 Gcode gc(input_off_command, NULL);
97 if(gc.has_g){
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;
103 }
104 }
105
106 }
107
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));
111 }
112
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));
116 }
117
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);
123 }
124 }
125
126 // Turn pin on and off
127 void Switch::on_gcode_execute(void* argument){
128 Gcode* gcode = static_cast<Gcode*>(argument);
129
130 if(match_input_gcode(gcode)) {
131 if (gcode->has_letter('S'))
132 {
133 int v = gcode->get_value('S') * output_pin.max_pwm() / 256.0;
134 if (v) {
135 this->output_pin.pwm(v);
136 this->switch_value = v;
137 this->switch_state = true;
138 }
139 else {
140 this->output_pin.set(0);
141 this->switch_state = false;
142 }
143
144 } else {
145 // Turn pin on
146 this->output_pin.pwm(this->switch_value);
147 this->switch_state = true;
148 }
149
150 } else if(match_output_gcode(gcode)) {
151 // Turn pin off
152 this->output_pin.set(0);
153 this->switch_state = false;
154 }
155 }
156
157 void Switch::on_get_public_data(void* argument){
158 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
159
160 if(!pdr->starts_with(switch_checksum)) return;
161
162 if(!pdr->second_element_is(this->name_checksum)) return; // likely fan, but could be anything
163
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;
170
171 pdr->set_data_ptr(&pad);
172 pdr->set_taken();
173 }
174
175 void Switch::on_set_public_data(void* argument){
176 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
177
178 if(!pdr->starts_with(switch_checksum)) return;
179
180 if(!pdr->second_element_is(this->name_checksum)) return; // likely fan, but could be anything
181
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;
186 pdr->set_taken();
187 }
188 else if(pdr->third_element_is(value_checksum)) {
189 float t= *static_cast<float*>(pdr->get_data_ptr());
190 this->switch_value = t;
191 pdr->set_taken();
192 }
193 }
194
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);
200 }else{
201 this->send_gcode( this->output_off_command, &(StreamOutput::NullStream) );
202 this->output_pin.set(0);
203 }
204 this->switch_changed=false;
205 }
206 }
207
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){
211 // If pin changed
212 bool current_state = this->input_pin.get();
213 if(this->input_pin_state != current_state){
214 this->input_pin_state = current_state;
215 // If pin high
216 if( this->input_pin_state ){
217 // if switch is a toggle switch
218 if( this->input_pin_behavior == toggle_checksum ){
219 this->flip();
220 // else default is momentary
221 }
222 else{
223 this->flip();
224 }
225 // else if button released
226 }else{
227 // if switch is momentary
228 if( !this->input_pin_behavior == toggle_checksum ){
229 this->flip();
230 }
231 }
232 }
233 return 0;
234 }
235
236 void Switch::flip(){
237 this->switch_state = !this->switch_state;
238 this->switch_changed = true;
239 }
240
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 );
246 }
247