update firmware.bin
[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 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);
57
58 // Settings
59 this->on_config_reload(this);
60 }
61
62
63 // Get config
64 void Switch::on_config_reload(void *argument)
65 {
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();
75
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
79
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
86 } else {
87 this->output_pin.set(false);
88 }
89 } else {
90 this->output_pin.set(this->switch_state);
91 }
92 }
93
94 set_low_on_debug(output_pin.port_number, output_pin.pin);
95
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;
99
100 if(!input_on_command.empty()) {
101 Gcode gc(input_on_command, NULL);
102 if(gc.has_g) {
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;
108 }
109 }
110 if(!input_off_command.empty()) {
111 Gcode gc(input_off_command, NULL);
112 if(gc.has_g) {
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;
118 }
119 }
120
121 if(input_pin.connected()) {
122 // set to initial state
123 this->input_pin_state = this->input_pin.get();
124 // input pin polling
125 THEKERNEL->slow_ticker->attach( 100, this, &Switch::pinpoll_tick);
126 }
127
128 if(this->output_type == PWM && this->output_pin.connected()) {
129 // PWM
130 THEKERNEL->slow_ticker->attach(1000, &this->output_pin, &Pwm::on_tick);
131 }
132 }
133
134 bool Switch::match_input_on_gcode(const Gcode *gcode) const
135 {
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));
138 }
139
140 bool Switch::match_input_off_gcode(const Gcode *gcode) const
141 {
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));
144 }
145
146 void Switch::on_gcode_received(void *argument)
147 {
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);
152 }
153 }
154
155 // Turn pin on and off
156 void Switch::on_gcode_execute(void *argument)
157 {
158 Gcode *gcode = static_cast<Gcode *>(argument);
159
160 if(match_input_on_gcode(gcode)) {
161 int v;
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);
168 } else {
169 this->output_pin.pwm(this->switch_value);
170 this->switch_state= (this->switch_value > 0);
171 }
172 } else {
173 // logic pin turn on
174 this->output_pin.set(true);
175 this->switch_state = true;
176 }
177 } else if(match_input_off_gcode(gcode)) {
178 this->switch_state = false;
179 if (this->output_type == PWM) {
180 // PWM output pin
181 this->output_pin.set(false);
182 } else {
183 // logic pin turn off
184 this->output_pin.set(false);
185 }
186 }
187 }
188
189 void Switch::on_get_public_data(void *argument)
190 {
191 PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument);
192
193 if(!pdr->starts_with(switch_checksum)) return;
194
195 if(!pdr->second_element_is(this->name_checksum)) return; // likely fan, but could be anything
196
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;
203
204 pdr->set_data_ptr(&pad);
205 pdr->set_taken();
206 }
207
208 void Switch::on_set_public_data(void *argument)
209 {
210 PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument);
211
212 if(!pdr->starts_with(switch_checksum)) return;
213
214 if(!pdr->second_element_is(this->name_checksum)) return; // likely fan, but could be anything
215
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;
220 pdr->set_taken();
221 this->switch_changed= true;
222
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;
227 pdr->set_taken();
228 }
229 }
230
231 void Switch::on_main_loop(void *argument)
232 {
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
239 else
240 this->output_pin.set(true);
241 }
242
243 } else {
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);
248 else
249 this->output_pin.set(false);
250 }
251 }
252 this->switch_changed = false;
253 }
254 }
255
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)
259 {
260 if(!input_pin.connected()) return 0;
261
262 // If pin changed
263 bool current_state = this->input_pin.get();
264 if(this->input_pin_state != current_state) {
265 this->input_pin_state = current_state;
266 // If pin high
267 if( this->input_pin_state ) {
268 // if switch is a toggle switch
269 if( this->input_pin_behavior == toggle_checksum ) {
270 this->flip();
271 // else default is momentary
272 } else {
273 this->flip();
274 }
275 // else if button released
276 } else {
277 // if switch is momentary
278 if( this->input_pin_behavior == momentary_checksum ) {
279 this->flip();
280 }
281 }
282 }
283 return 0;
284 }
285
286 void Switch::flip()
287 {
288 this->switch_state = !this->switch_state;
289 this->switch_changed = true;
290 }
291
292 void Switch::send_gcode(std::string msg, StreamOutput *stream)
293 {
294 struct SerialMessage message;
295 message.message = msg;
296 message.stream = stream;
297 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
298 }
299