Merge pull request #169 from logxen/switch
[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
29 register_for_event(ON_CONFIG_RELOAD);
30 this->register_for_event(ON_GCODE_RECEIVED);
31 this->register_for_event(ON_GCODE_EXECUTE);
32
33 // Settings
34 this->on_config_reload(this);
35
36 // input pin polling
37 this->kernel->slow_ticker->attach( 100, this, &Switch::pinpoll_tick);
38
39 // PWM
40 this->kernel->slow_ticker->attach(1000, &output_pin, &Pwm::on_tick);
41 }
42
43
44 // Get config
45 void Switch::on_config_reload(void* argument){
46 this->input_pin.from_string(this->kernel->config->value(switch_checksum, this->name_checksum, input_pin_checksum )->by_default("nc")->as_string())->as_input();
47 this->input_pin_behavior = this->kernel->config->value(switch_checksum, this->name_checksum, input_pin_behavior_checksum )->by_default(momentary_checksum)->as_number();
48 this->input_on_command = this->kernel->config->value(switch_checksum, this->name_checksum, input_on_command_checksum )->by_default("")->as_string();
49 this->input_off_command = this->kernel->config->value(switch_checksum, this->name_checksum, input_off_command_checksum )->by_default("")->as_string();
50 this->switch_state = this->kernel->config->value(switch_checksum, this->name_checksum, startup_state_checksum )->by_default(false)->as_bool();
51 this->output_pin.from_string(this->kernel->config->value(switch_checksum, this->name_checksum, output_pin_checksum )->by_default("nc")->as_string())->as_output();
52 this->output_pin.set(this->kernel->config->value(switch_checksum, this->name_checksum, startup_state_checksum )->by_default(0)->as_number() );
53 this->output_on_command = this->kernel->config->value(switch_checksum, this->name_checksum, output_on_command_checksum )->by_default("")->as_string();
54 this->output_off_command = this->kernel->config->value(switch_checksum, this->name_checksum, output_off_command_checksum )->by_default("")->as_string();
55
56 set_low_on_debug(output_pin.port_number, output_pin.pin);
57 }
58
59
60 void Switch::on_gcode_received(void* argument){
61 Gcode* gcode = static_cast<Gcode*>(argument);
62 // Add the gcode to the queue ourselves if we need it
63 if( ( input_on_command.length() > 0 && gcode->command.compare(0, input_on_command.length(), input_on_command) )
64 || ( input_off_command.length() > 0 && gcode->command.compare(0, input_off_command.length(), input_off_command) ) ){
65 gcode->mark_as_taken();
66 if( this->kernel->conveyor->queue.size() == 0 ){
67 this->kernel->call_event(ON_GCODE_EXECUTE, gcode );
68 }else{
69 Block* block = this->kernel->conveyor->queue.get_ref( this->kernel->conveyor->queue.size() - 1 );
70 block->append_gcode(gcode);
71 }
72 }
73 }
74
75 // Turn pin on and off
76 void Switch::on_gcode_execute(void* argument){
77 Gcode* gcode = static_cast<Gcode*>(argument);
78 if(gcode->command.compare(0, input_on_command.length(), input_on_command)){
79 if (gcode->has_letter('S'))
80 {
81 int v = gcode->get_value('S') * output_pin.max_pwm() / 256.0;
82 if (v)
83 this->output_pin.pwm(v);
84 else
85 this->output_pin.set(0);
86 }
87 else
88 {
89 // Turn pin on
90 this->output_pin.set(1);
91 }
92 }
93 else if(gcode->command.compare(0, input_off_command.length(), input_off_command)){
94 // Turn pin off
95 this->output_pin.set(0);
96 }
97 }
98
99 //TODO: Make this use InterruptIn
100 //Check the state of the button and act accordingly
101 uint32_t Switch::pinpoll_tick(uint32_t dummy){
102 // If pin changed
103 if(this->input_pin_state != this->input_pin.get()){
104 this->input_pin_state = this->input_pin.get();
105 // If pin high
106 if( this->input_pin_state ){
107 // if switch is a toggle switch
108 if( this->input_pin_behavior == toggle_checksum ){
109 this->flip();
110 // else default is momentary
111 }
112 else{
113 this->flip();
114 }
115 // else if button released
116 }else{
117 // if switch is momentary
118 if( !this->input_pin_behavior == toggle_checksum ){
119 this->flip();
120 }
121 }
122 }
123 return 0;
124 }
125
126 void Switch::flip(){
127 this->switch_state = !this->switch_state;
128 if( this->switch_state ){
129 this->send_gcode( this->output_on_command, &(StreamOutput::NullStream) );
130 }else{
131 this->send_gcode( this->output_off_command, &(StreamOutput::NullStream) );
132 }
133 }
134
135 void Switch::send_gcode(std::string msg, StreamOutput* stream) {
136 struct SerialMessage message;
137 message.message = msg;
138 message.stream = stream;
139 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
140 }
141