Consolidate common spindle functions
[clinton/Smoothieware.git] / src / modules / tools / spindle / ModbusSpindleControl.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/Pin.h"
11 #include "mbed.h"
12 #include "Modbus.h"
13 #include "Config.h"
14 #include "checksumm.h"
15 #include "ConfigValue.h"
16 #include "ModbusSpindleControl.h"
17
18 #define spindle_checksum CHECKSUM("spindle")
19 #define spindle_rx_pin_checksum CHECKSUM("rx_pin")
20 #define spindle_tx_pin_checksum CHECKSUM("tx_pin")
21 #define spindle_dir_pin_checksum CHECKSUM("dir_pin")
22
23 void ModbusSpindleControl::on_module_loaded()
24 {
25
26 spindle_on = false;
27 PinName rx_pin;
28 PinName tx_pin;
29 PinName dir_pin;
30
31 // preparing PinName objects from the config string
32 {
33 Pin *smoothie_pin = new Pin();
34 smoothie_pin->from_string(THEKERNEL->config->value(spindle_checksum, spindle_rx_pin_checksum)->by_default("nc")->as_string());
35 smoothie_pin->as_input();
36 rx_pin = port_pin((PortName)smoothie_pin->port_number, smoothie_pin->pin);
37
38 smoothie_pin->from_string(THEKERNEL->config->value(spindle_checksum, spindle_tx_pin_checksum)->by_default("nc")->as_string());
39 smoothie_pin->as_input();
40 tx_pin = port_pin((PortName)smoothie_pin->port_number, smoothie_pin->pin);
41
42 smoothie_pin->from_string(THEKERNEL->config->value(spindle_checksum, spindle_dir_pin_checksum)->by_default("nc")->as_string());
43 smoothie_pin->as_input();
44 dir_pin = port_pin((PortName)smoothie_pin->port_number, smoothie_pin->pin);
45
46 delete smoothie_pin;
47 }
48
49 // setup the Modbus interface
50 modbus = new Modbus(tx_pin, rx_pin, dir_pin);
51 }
52