removed most mbed.h includes
[clinton/Smoothieware.git] / src / modules / tools / laser / Laser.cpp
CommitLineData
4cff3ded
AW
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"
a2cd92c0 14#include "libs/nuts_bolts.h"
4cff3ded
AW
15
16Laser::Laser(PinName pin) : laser_pin(pin){
17 this->laser_pin.period_us(10);
18}
19
20void Laser::on_module_loaded() {
436a2cd1 21 if( !this->kernel->config->value( laser_module_enable_checksum )->by_default(false)->as_bool() ){ return; }
4cff3ded
AW
22 this->register_for_event(ON_GCODE_EXECUTE);
23 this->register_for_event(ON_SPEED_CHANGE);
befcf5cc
AW
24 this->register_for_event(ON_PLAY);
25 this->register_for_event(ON_PAUSE);
a2cd92c0
AW
26 this->register_for_event(ON_BLOCK_BEGIN);
27 this->register_for_event(ON_BLOCK_END);
4cff3ded
AW
28}
29
a2cd92c0
AW
30// Turn laser off laser at the end of a move
31void Laser::on_block_end(void* argument){
32 this->laser_pin = 0;
a2cd92c0
AW
33}
34
35// Set laser power at the beginning of a block
36void Laser::on_block_begin(void* argument){
37 this->set_proportional_power();
38}
befcf5cc
AW
39
40// When the play/pause button is set to pause, or a module calls the ON_PAUSE event
41void 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
46void Laser::on_play(void* argument){
a2cd92c0 47 this->set_proportional_power();
befcf5cc
AW
48}
49
4cff3ded
AW
50// Turn laser on/off depending on received GCodes
51void Laser::on_gcode_execute(void* argument){
52 Gcode* gcode = static_cast<Gcode*>(argument);
b66fb830 53 this->laser_on = false;
4cff3ded
AW
54 if( gcode->has_letter('G' )){
55 int code = gcode->get_value('G');
a2cd92c0 56 if( code == 0 ){ // G0
4cff3ded
AW
57 this->laser_pin = 0;
58 this->laser_on = false;
a2cd92c0 59 }else if( code >= 1 && code <= 3 ){ // G1, G2, G3
4cff3ded
AW
60 this->laser_on = true;
61 }
62 }
63}
64
6f57fb56 65// We follow the stepper module here, so speed must be proportional
4cff3ded 66void Laser::on_speed_change(void* argument){
a2cd92c0
AW
67 this->set_proportional_power();
68}
69
70void 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);
4cff3ded
AW
73 }
74}