provide one method to add gcode to blocks, reduce code duplication
[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
16 #include "MRI_Hooks.h"
17
18 Switch::Switch(){}
19
20 Switch::Switch(uint16_t name){
21 this->name_checksum = name;
22 //this->dummy_stream = &(StreamOutput::NullStream);
23 }
24
25 void Switch::on_module_loaded(){
26 this->input_pin_state = true;
27 this->switch_state = true;
28 this->switch_changed = false;
29
30 register_for_event(ON_CONFIG_RELOAD);
31 this->register_for_event(ON_GCODE_RECEIVED);
32 this->register_for_event(ON_GCODE_EXECUTE);
33 this->register_for_event(ON_MAIN_LOOP);
34
35 // Settings
36 this->on_config_reload(this);
37
38 // input pin polling
39 THEKERNEL->slow_ticker->attach( 100, this, &Switch::pinpoll_tick);
40
41 // PWM
42 THEKERNEL->slow_ticker->attach(1000, &output_pin, &Pwm::on_tick);
43 }
44
45
46 // Get config
47 void Switch::on_config_reload(void* argument){
48 this->input_pin.from_string(THEKERNEL->config->value(switch_checksum, this->name_checksum, input_pin_checksum )->by_default("nc")->as_string())->as_input();
49 this->input_pin_behavior = THEKERNEL->config->value(switch_checksum, this->name_checksum, input_pin_behavior_checksum )->by_default(momentary_checksum)->as_number();
50 this->input_on_command = THEKERNEL->config->value(switch_checksum, this->name_checksum, input_on_command_checksum )->by_default("")->as_string();
51 this->input_off_command = THEKERNEL->config->value(switch_checksum, this->name_checksum, input_off_command_checksum )->by_default("")->as_string();
52 this->output_pin.from_string(THEKERNEL->config->value(switch_checksum, this->name_checksum, output_pin_checksum )->by_default("nc")->as_string())->as_output();
53 this->output_on_command = THEKERNEL->config->value(switch_checksum, this->name_checksum, output_on_command_checksum )->by_default("")->as_string();
54 this->output_off_command = THEKERNEL->config->value(switch_checksum, this->name_checksum, output_off_command_checksum )->by_default("")->as_string();
55 this->switch_state = THEKERNEL->config->value(switch_checksum, this->name_checksum, startup_state_checksum )->by_default(false)->as_bool();
56 this->switch_value = THEKERNEL->config->value(switch_checksum, this->name_checksum, startup_value_checksum )->by_default(this->output_pin.max_pwm())->as_number();
57 if(this->switch_state)
58 this->output_pin.set(this->switch_value);
59 else
60 this->output_pin.set(0);
61
62 set_low_on_debug(output_pin.port_number, output_pin.pin);
63 }
64
65
66 void Switch::on_gcode_received(void* argument){
67 Gcode* gcode = static_cast<Gcode*>(argument);
68 // Add the gcode to the queue ourselves if we need it
69 if (( input_on_command.length() > 0 && ! gcode->command.compare(0, input_on_command.length(), input_on_command) ) ||
70 ( input_off_command.length() > 0 && ! gcode->command.compare(0, input_off_command.length(), input_off_command) ) )
71 {
72 THEKERNEL->conveyor->append_gcode(gcode);
73 }
74 }
75
76 // Turn pin on and off
77 void Switch::on_gcode_execute(void* argument){
78 Gcode* gcode = static_cast<Gcode*>(argument);
79 if(! gcode->command.compare(0, input_on_command.length(), input_on_command)){
80 if (gcode->has_letter('S'))
81 {
82 int v = gcode->get_value('S') * output_pin.max_pwm() / 256.0;
83 if (v) {
84 this->output_pin.pwm(v);
85 this->switch_value = v;
86 this->switch_state = true;
87 }
88 else {
89 this->output_pin.set(0);
90 this->switch_state = false;
91 }
92 }
93 else
94 {
95 // Turn pin on
96 this->output_pin.pwm(this->switch_value);
97 this->switch_state = true;
98 }
99 }
100 else if(! gcode->command.compare(0, input_off_command.length(), input_off_command)){
101 // Turn pin off
102 this->output_pin.set(0);
103 this->switch_state = false;
104 }
105 }
106
107 void Switch::on_main_loop(void* argument){
108 if(this->switch_changed){
109 if(this->switch_state){
110 this->send_gcode( this->output_on_command, &(StreamOutput::NullStream) );
111 this->output_pin.pwm(this->switch_value);
112 }else{
113 this->send_gcode( this->output_off_command, &(StreamOutput::NullStream) );
114 this->output_pin.set(0);
115 }
116 this->switch_changed=false;
117 }
118 }
119
120 //TODO: Make this use InterruptIn
121 //Check the state of the button and act accordingly
122 uint32_t Switch::pinpoll_tick(uint32_t dummy){
123 // If pin changed
124 bool current_state = this->input_pin.get();
125 if(this->input_pin_state != current_state){
126 this->input_pin_state = current_state;
127 // If pin high
128 if( this->input_pin_state ){
129 // if switch is a toggle switch
130 if( this->input_pin_behavior == toggle_checksum ){
131 this->flip();
132 // else default is momentary
133 }
134 else{
135 this->flip();
136 }
137 // else if button released
138 }else{
139 // if switch is momentary
140 if( !this->input_pin_behavior == toggle_checksum ){
141 this->flip();
142 }
143 }
144 }
145 return 0;
146 }
147
148 void Switch::flip(){
149 this->switch_state = !this->switch_state;
150 this->switch_changed = true;
151 }
152
153 void Switch::send_gcode(std::string msg, StreamOutput* stream) {
154 struct SerialMessage message;
155 message.message = msg;
156 message.stream = stream;
157 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
158 }
159