Merge remote-tracking branch 'upstream/edge' into upstreamedge
[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 #include "SlowTicker.h"
18 #include "Config.h"
19 #include "Gcode.h"
20 #include "checksumm.h"
21 #include "ConfigValue.h"
22 #include "libs/StreamOutput.h"
23
24 #include "MRI_Hooks.h"
25
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")
39
40 Switch::Switch() {}
41
42 Switch::Switch(uint16_t name)
43 {
44 this->name_checksum = name;
45 //this->dummy_stream = &(StreamOutput::NullStream);
46 }
47
48 void Switch::on_module_loaded()
49 {
50 this->switch_changed = false;
51
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);
58
59 // Settings
60 this->on_config_reload(this);
61 }
62
63
64 // Get config
65 void Switch::on_config_reload(void *argument)
66 {
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();
76
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
80
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
85
86 } else {
87 this->output_pin.set(this->switch_state);
88 }
89 }
90
91 this->switch_value = THEKERNEL->config->value(switch_checksum, this->name_checksum, startup_value_checksum )->by_default(this->output_pin.max_pwm())->as_number();
92
93 set_low_on_debug(output_pin.port_number, output_pin.pin);
94
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;
98
99 if(!input_on_command.empty()) {
100 Gcode gc(input_on_command, NULL);
101 if(gc.has_g) {
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;
107 }
108 }
109 if(!input_off_command.empty()) {
110 Gcode gc(input_off_command, NULL);
111 if(gc.has_g) {
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;
117 }
118 }
119
120 if(input_pin.connected()) {
121 // set to initial state
122 this->input_pin_state = this->input_pin.get();
123 // input pin polling
124 THEKERNEL->slow_ticker->attach( 100, this, &Switch::pinpoll_tick);
125 }
126
127 if(this->output_type == PWM && this->output_pin.connected()) {
128 // PWM
129 THEKERNEL->slow_ticker->attach(1000, &this->output_pin, &Pwm::on_tick);
130 }
131 }
132
133 bool Switch::match_input_on_gcode(const Gcode *gcode) const
134 {
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));
137 }
138
139 bool Switch::match_input_off_gcode(const Gcode *gcode) const
140 {
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));
143 }
144
145 void Switch::on_gcode_received(void *argument)
146 {
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);
151 }
152 }
153
154 // Turn pin on and off
155 void Switch::on_gcode_execute(void *argument)
156 {
157 Gcode *gcode = static_cast<Gcode *>(argument);
158
159 if(match_input_on_gcode(gcode)) {
160 if (this->output_type == PWM) {
161 // PWM output pin
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
164 if (v > 0) {
165 this->output_pin.pwm(v);
166 this->switch_value = v;
167 this->switch_state = true;
168 } else {
169 this->output_pin.pwm(0);
170 this->switch_state = false;
171 }
172
173 } else {
174 // Turn pin full on
175 this->output_pin.pwm(255); // will be truncated to max_pwm if set
176 this->switch_state = true;
177 }
178
179 } else {
180 // logic pin turn on
181 this->output_pin.set(true);
182 }
183
184 } else if(match_input_off_gcode(gcode)) {
185 if (this->output_type == PWM) {
186 // PWM output pin
187 this->output_pin.pwm(0);
188 this->switch_state = false;
189 } else {
190 // logic pin turn off
191 this->output_pin.set(false);
192 this->switch_state = false;
193 }
194 }
195 }
196
197 void Switch::on_get_public_data(void *argument)
198 {
199 PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument);
200
201 if(!pdr->starts_with(switch_checksum)) return;
202
203 if(!pdr->second_element_is(this->name_checksum)) return; // likely fan, but could be anything
204
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;
211
212 pdr->set_data_ptr(&pad);
213 pdr->set_taken();
214 }
215
216 void Switch::on_set_public_data(void *argument)
217 {
218 PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument);
219
220 if(!pdr->starts_with(switch_checksum)) return;
221
222 if(!pdr->second_element_is(this->name_checksum)) return; // likely fan, but could be anything
223
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;
228 pdr->set_taken();
229 this->switch_changed= true;
230
231 } else if(pdr->third_element_is(value_checksum)) {
232 float t = *static_cast<float *>(pdr->get_data_ptr());
233 this->switch_value = t;
234 pdr->set_taken();
235 }
236 }
237
238 void Switch::on_main_loop(void *argument)
239 {
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
246 else
247 this->output_pin.set(true);
248 }
249
250 } else {
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);
255 else
256 this->output_pin.set(false);
257 }
258 }
259 this->switch_changed = false;
260 }
261 }
262
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)
266 {
267 if(!input_pin.connected()) return 0;
268
269 // If pin changed
270 bool current_state = this->input_pin.get();
271 if(this->input_pin_state != current_state) {
272 this->input_pin_state = current_state;
273 // If pin high
274 if( this->input_pin_state ) {
275 // if switch is a toggle switch
276 if( this->input_pin_behavior == toggle_checksum ) {
277 this->flip();
278 // else default is momentary
279 } else {
280 this->flip();
281 }
282 // else if button released
283 } else {
284 // if switch is momentary
285 if( this->input_pin_behavior == momentary_checksum ) {
286 this->flip();
287 }
288 }
289 }
290 return 0;
291 }
292
293 void Switch::flip()
294 {
295 this->switch_state = !this->switch_state;
296 this->switch_changed = true;
297 }
298
299 void Switch::send_gcode(std::string msg, StreamOutput *stream)
300 {
301 struct SerialMessage message;
302 message.message = msg;
303 message.stream = stream;
304 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
305 }
306