Commit | Line | Data |
---|---|---|
df27a6a3 | 1 | /* |
cc1d3b1f AW |
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. | |
df27a6a3 | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
cc1d3b1f AW |
6 | */ |
7 | ||
8 | #include "libs/Module.h" | |
9 | #include "libs/Kernel.h" | |
10 | #include <math.h> | |
11 | #include "Switch.h" | |
12 | #include "libs/Pin.h" | |
3c4f2dd8 | 13 | #include "modules/robot/Conveyor.h" |
cc1d3b1f | 14 | |
8f91e4e6 MM |
15 | #include "MRI_Hooks.h" |
16 | ||
cc1d3b1f AW |
17 | Switch::Switch(){} |
18 | ||
19 | Switch::Switch(uint16_t name){ | |
20 | this->name_checksum = name; | |
21 | } | |
22 | ||
23 | void Switch::on_module_loaded(){ | |
476dcb96 | 24 | register_for_event(ON_CONFIG_RELOAD); |
3c4f2dd8 | 25 | this->register_for_event(ON_GCODE_RECEIVED); |
cc1d3b1f | 26 | this->register_for_event(ON_GCODE_EXECUTE); |
bca315cc | 27 | |
cc1d3b1f AW |
28 | // Settings |
29 | this->on_config_reload(this); | |
30 | ||
bca315cc | 31 | // PWM |
cb3460e9 | 32 | this->kernel->slow_ticker->attach(1000, &output_pin, &Pwm::on_tick); |
cc1d3b1f AW |
33 | } |
34 | ||
35 | ||
36 | // Get config | |
37 | void Switch::on_config_reload(void* argument){ | |
11f8ba4e MM |
38 | this->on_m_code = this->kernel->config->value(switch_checksum, this->name_checksum, on_m_code_checksum )->required()->as_number(); |
39 | this->off_m_code = this->kernel->config->value(switch_checksum, this->name_checksum, off_m_code_checksum )->required()->as_number(); | |
cb3460e9 MM |
40 | this->output_pin.from_string(this->kernel->config->value(switch_checksum, this->name_checksum, output_pin_checksum )->required()->as_string())->as_output(); |
41 | this->output_pin.set(this->kernel->config->value(switch_checksum, this->name_checksum, startup_state_checksum )->by_default(0)->as_number() ); | |
8f91e4e6 | 42 | |
cb3460e9 | 43 | set_low_on_debug(output_pin.port_number, output_pin.pin); |
cc1d3b1f AW |
44 | } |
45 | ||
3c4f2dd8 AW |
46 | |
47 | void Switch::on_gcode_received(void* argument){ | |
48 | Gcode* gcode = static_cast<Gcode*>(argument); | |
49 | // Add the gcode to the queue ourselves if we need it | |
50 | if( gcode->has_m && ( gcode->m == this->on_m_code || gcode->m == this->off_m_code ) ){ | |
51 | if( this->kernel->conveyor->queue.size() == 0 ){ | |
52 | this->kernel->call_event(ON_GCODE_EXECUTE, gcode ); | |
53 | }else{ | |
54 | Block* block = this->kernel->conveyor->queue.get_ref( this->kernel->conveyor->queue.size() - 1 ); | |
55 | block->append_gcode(gcode); | |
3c4f2dd8 AW |
56 | } |
57 | } | |
58 | } | |
59 | ||
cc1d3b1f AW |
60 | // Turn pin on and off |
61 | void Switch::on_gcode_execute(void* argument){ | |
62 | Gcode* gcode = static_cast<Gcode*>(argument); | |
c68c2e8b AW |
63 | if( gcode->has_m){ |
64 | int code = gcode->m; | |
df27a6a3 | 65 | if( code == this->on_m_code ){ |
bca315cc MM |
66 | if (gcode->has_letter('S')) |
67 | { | |
cb3460e9 | 68 | int v = gcode->get_value('S') * output_pin.max_pwm() / 256.0; |
bca315cc | 69 | if (v) |
cb3460e9 | 70 | this->output_pin.pwm(v); |
bca315cc | 71 | else |
cb3460e9 | 72 | this->output_pin.set(0); |
bca315cc MM |
73 | } |
74 | else | |
75 | { | |
76 | // Turn pin on | |
cb3460e9 | 77 | this->output_pin.set(1); |
bca315cc | 78 | } |
df27a6a3 MM |
79 | } |
80 | if( code == this->off_m_code ){ | |
81 | // Turn pin off | |
cb3460e9 | 82 | this->output_pin.set(0); |
df27a6a3 | 83 | } |
cc1d3b1f AW |
84 | } |
85 | } |