adding boolean configuration values, and extruder/laser modules enable
[clinton/Smoothieware.git] / src / modules / tools / laser / Laser.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 "mbed.h"
9 #include "libs/Module.h"
10 #include "libs/Kernel.h"
11 #include "modules/communication/utils/Gcode.h"
12 #include "modules/robot/Stepper.h"
13 #include "Laser.h"
14 #include "libs/nuts_bolts.h"
15
16 Laser::Laser(PinName pin) : laser_pin(pin){
17 this->laser_pin.period_us(10);
18 }
19
20 void Laser::on_module_loaded() {
21 if( this->kernel->config->value( laser_module_enable_checksum )->by_default(false)->as_bool() ){ return; }
22 this->register_for_event(ON_GCODE_EXECUTE);
23 this->register_for_event(ON_SPEED_CHANGE);
24 this->register_for_event(ON_PLAY);
25 this->register_for_event(ON_PAUSE);
26 this->register_for_event(ON_BLOCK_BEGIN);
27 this->register_for_event(ON_BLOCK_END);
28 }
29
30 // Turn laser off laser at the end of a move
31 void Laser::on_block_end(void* argument){
32 this->laser_pin = 0;
33 }
34
35 // Set laser power at the beginning of a block
36 void Laser::on_block_begin(void* argument){
37 this->set_proportional_power();
38 }
39
40 // When the play/pause button is set to pause, or a module calls the ON_PAUSE event
41 void Laser::on_pause(void* argument){
42 this->laser_pin = 0;
43 }
44
45 // When the play/pause button is set to play, or a module calls the ON_PLAY event
46 void Laser::on_play(void* argument){
47 this->set_proportional_power();
48 }
49
50
51 // Turn laser on/off depending on received GCodes
52 void Laser::on_gcode_execute(void* argument){
53 Gcode* gcode = static_cast<Gcode*>(argument);
54 this->laser_on = false;
55 if( gcode->has_letter('G' )){
56 int code = gcode->get_value('G');
57 if( code == 0 ){ // G0
58 this->laser_pin = 0;
59 this->laser_on = false;
60 }else if( code >= 1 && code <= 3 ){ // G1, G2, G3
61 this->laser_on = true;
62 }
63 }
64 }
65
66 void Laser::on_speed_change(void* argument){
67 this->set_proportional_power();
68 }
69
70 void Laser::set_proportional_power(){
71 if( this->laser_on && this->kernel->stepper->current_block ){
72 this->laser_pin = double(this->kernel->stepper->trapezoid_adjusted_rate)/double(this->kernel->stepper->current_block->nominal_rate);
73 }
74 }