added make dfu, need to merge with existing upload
[clinton/Smoothieware.git] / src / modules / tools / switch / Switch.cpp
CommitLineData
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"
13
14Switch::Switch(){}
15
16Switch::Switch(uint16_t name){
17 this->name_checksum = name;
18}
19
20void Switch::on_module_loaded(){
21 this->register_for_event(ON_GCODE_EXECUTE);
bca315cc 22
cc1d3b1f
AW
23 // Settings
24 this->on_config_reload(this);
25
bca315cc 26 // PWM
11f8ba4e 27 this->kernel->slow_ticker->attach(1000, this->output_pin, &Pwm::on_tick);
cc1d3b1f
AW
28}
29
30
31// Get config
32void Switch::on_config_reload(void* argument){
11f8ba4e
MM
33 this->on_m_code = this->kernel->config->value(switch_checksum, this->name_checksum, on_m_code_checksum )->required()->as_number();
34 this->off_m_code = this->kernel->config->value(switch_checksum, this->name_checksum, off_m_code_checksum )->required()->as_number();
35 this->output_pin = this->kernel->config->value(switch_checksum, this->name_checksum, output_pin_checksum )->required()->as_pwm()->as_output();
36 this->output_pin->set(this->kernel->config->value(switch_checksum, this->name_checksum, startup_state_checksum )->by_default(0)->as_number() );
cc1d3b1f
AW
37}
38
39// Turn pin on and off
40void Switch::on_gcode_execute(void* argument){
41 Gcode* gcode = static_cast<Gcode*>(argument);
42 if( gcode->has_letter('M' )){
43 int code = gcode->get_value('M');
df27a6a3 44 if( code == this->on_m_code ){
bca315cc
MM
45 if (gcode->has_letter('S'))
46 {
11f8ba4e 47 int v = gcode->get_value('S') * output_pin->max_pwm() / 256.0;
bca315cc
MM
48 if (v)
49 this->output_pin->pwm(v);
50 else
51 this->output_pin->set(0);
52 }
53 else
54 {
55 // Turn pin on
56 this->output_pin->set(1);
57 }
df27a6a3
MM
58 }
59 if( code == this->off_m_code ){
60 // Turn pin off
cc1d3b1f 61 this->output_pin->set(0);
df27a6a3 62 }
cc1d3b1f
AW
63 }
64}