Added ON_IDLE event to Kernel and Module, but it isn't called yet
[clinton/Smoothieware.git] / src / libs / Kernel.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 using namespace std;
9 #include <vector>
10 #include "libs/Kernel.h"
11 #include "libs/Module.h"
12 #include "libs/Config.h"
13 #include "libs/nuts_bolts.h"
14 #include "libs/SlowTicker.h"
15 #include "libs/Adc.h"
16 #include "libs/Digipot.h"
17 #include "libs/Pauser.h"
18
19 #include "modules/communication/SerialConsole.h"
20 #include "modules/communication/GcodeDispatch.h"
21 #include "modules/robot/Planner.h"
22 #include "modules/robot/Robot.h"
23 #include "modules/robot/Stepper.h"
24 #include "modules/robot/Player.h"
25
26
27 // List of callback functions, ordered as their corresponding events
28 const ModuleCallback kernel_callback_functions[NUMBER_OF_DEFINED_EVENTS] = {
29 &Module::on_main_loop,
30 &Module::on_console_line_received,
31 &Module::on_gcode_received,
32 &Module::on_stepper_wake_up,
33 &Module::on_gcode_execute,
34 &Module::on_speed_change,
35 &Module::on_block_begin,
36 &Module::on_block_end,
37 &Module::on_config_reload,
38 &Module::on_play,
39 &Module::on_pause,
40 &Module::on_idle
41 };
42
43 #define baud_rate_setting_ckeckusm 10922
44 #define uart0_checksum 16877
45
46 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
47 Kernel::Kernel(){
48
49 // Config first, because we need the baud_rate setting before we start serial
50 this->config = new Config();
51 // Serial second, because the other modules might want to say something
52 this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_ckeckusm)->by_default(9600)->as_number());
53
54 this->add_module( this->config );
55 this->add_module( this->serial );
56
57 // HAL stuff
58 this->slow_ticker = new SlowTicker();
59 this->step_ticker = new StepTicker();
60 this->adc = new Adc();
61 this->digipot = new Digipot();
62
63 // LPC17xx-specific
64 NVIC_SetPriority(TIMER0_IRQn, 1);
65 NVIC_SetPriority(TIMER2_IRQn, 2);
66
67 // Core modules
68 this->add_module( this->gcode_dispatch = new GcodeDispatch() );
69 this->add_module( this->robot = new Robot() );
70 this->add_module( this->stepper = new Stepper() );
71 this->add_module( this->planner = new Planner() );
72 this->add_module( this->player = new Player() );
73 this->add_module( this->pauser = new Pauser() );
74 }
75
76 void Kernel::add_module(Module* module){
77 module->kernel = this;
78 module->on_module_loaded();
79 module->register_for_event(ON_CONFIG_RELOAD);
80 }
81
82 void Kernel::register_for_event(unsigned int id_event, Module* module){
83 this->hooks[id_event].push_back(module);
84 }
85
86 void Kernel::call_event(unsigned int id_event){
87 for(unsigned int i=0; i < this->hooks[id_event].size(); i++){
88 (this->hooks[id_event][i]->*kernel_callback_functions[id_event])(this);
89 }
90 }
91
92 void Kernel::call_event(unsigned int id_event, void * argument){
93 for(unsigned int i=0; i < this->hooks[id_event].size(); i++){
94 (this->hooks[id_event][i]->*kernel_callback_functions[id_event])(argument);
95 }
96 }