Merge branch 'master' into edge
[clinton/Smoothieware.git] / src / libs / Kernel.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
8using namespace std;
9#include <vector>
10#include "libs/Kernel.h"
11#include "libs/Module.h"
12#include "libs/Config.h"
13#include "mbed.h"
14#include "libs/nuts_bolts.h"
15
16#include "modules/communication/SerialConsole.h"
17#include "modules/communication/GcodeDispatch.h"
18#include "modules/robot/Planner.h"
19#include "modules/robot/Robot.h"
20#include "modules/robot/Stepper.h"
21
22// List of callback functions, ordered as their corresponding events
23const ModuleCallback kernel_callback_functions[NUMBER_OF_DEFINED_EVENTS] = {
24 &Module::on_main_loop,
25 &Module::on_console_line_received,
26 &Module::on_gcode_received,
27 &Module::on_stepper_wake_up,
28 &Module::on_gcode_execute,
29 &Module::on_speed_change,
30 &Module::on_block_begin,
31 &Module::on_block_end
32};
33
34#define baud_rate_setting_ckeckusm 10922
35
36// The kernel is the central point in Smoothie : it stores modules, and handles event calls
37Kernel::Kernel(){
38
39 // Config first, because we need the baud_rate setting before we start serial
40 this->config = new Config();
41 // Serial second, because the other modules might want to say something
42 this->serial = new SerialConsole(USBTX, USBRX, this->config->get(baud_rate_setting_ckeckusm));
43
44 this->add_module( this->config );
45 this->add_module( this->serial );
46
47 // Core modules
48 this->gcode_dispatch = new GcodeDispatch();
49 this->robot = new Robot();
50 this->stepper = new Stepper();
51 this->planner = new Planner();
52
53 this->add_module( this->gcode_dispatch );
54 this->add_module( this->robot );
55 this->add_module( this->stepper );
56 this->add_module( this->planner );
57
58}
59
60void Kernel::add_module(Module* module){
61 module->kernel = this;
62 module->on_module_loaded();
63}
64
65void Kernel::register_for_event(unsigned int id_event, Module* module){
66 this->hooks[id_event].push_back(module);
67}
68
69void Kernel::call_event(unsigned int id_event){
70 for(unsigned int i=0; i < this->hooks[id_event].size(); i++){
71 (this->hooks[id_event][i]->*kernel_callback_functions[id_event])(this);
72 }
73}
74
75void Kernel::call_event(unsigned int id_event, void * argument){
76 for(unsigned int i=0; i < this->hooks[id_event].size(); i++){
77 (this->hooks[id_event][i]->*kernel_callback_functions[id_event])(argument);
78 }
79}