From 73706276e827407ead310af1a9feed756878e501 Mon Sep 17 00:00:00 2001 From: Jim Morris Date: Wed, 23 Sep 2015 04:45:49 -0700 Subject: [PATCH] refactor ON_HALT, add THEKERNEL->is_halted() for modules that just need to test it, and remove a bunch of registrations for the ON_HALT event (saves memory) --- src/libs/Kernel.cpp | 9 +++++++++ src/libs/Kernel.h | 8 +++++++- src/libs/SlowTicker.cpp | 2 +- src/main.cpp | 8 ++------ src/modules/communication/GcodeDispatch.cpp | 12 ++---------- src/modules/communication/GcodeDispatch.h | 2 -- src/modules/robot/Robot.cpp | 11 ++--------- src/modules/robot/Robot.h | 6 ------ src/modules/tools/endstops/Endstops.cpp | 12 ++++++------ .../tools/temperaturecontrol/TemperatureControl.cpp | 2 +- src/modules/utils/PlayLed/PlayLed.cpp | 11 ++--------- src/modules/utils/PlayLed/PlayLed.h | 2 -- src/modules/utils/panel/Panel.cpp | 7 ------- src/modules/utils/panel/Panel.h | 3 --- src/modules/utils/panel/screens/MainMenuScreen.cpp | 4 ++-- src/modules/utils/panel/screens/WatchScreen.cpp | 2 +- src/modules/utils/player/Player.cpp | 11 ++--------- src/modules/utils/player/Player.h | 2 -- 18 files changed, 37 insertions(+), 77 deletions(-) diff --git a/src/libs/Kernel.cpp b/src/libs/Kernel.cpp index 5583b440..9ee2fd6e 100644 --- a/src/libs/Kernel.cpp +++ b/src/libs/Kernel.cpp @@ -35,11 +35,14 @@ #define base_stepping_frequency_checksum CHECKSUM("base_stepping_frequency") #define microseconds_per_step_pulse_checksum CHECKSUM("microseconds_per_step_pulse") #define acceleration_ticks_per_second_checksum CHECKSUM("acceleration_ticks_per_second") +#define disable_leds_checksum CHECKSUM("leds_disable") Kernel* Kernel::instance; // The kernel is the central point in Smoothie : it stores modules, and handles event calls Kernel::Kernel(){ + halted= false; + instance= this; // setup the Singleton instance of the kernel // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial @@ -85,6 +88,9 @@ Kernel::Kernel(){ this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number()); } + //some boards don't have leds.. TOO BAD! + this->use_leds= !this->config->value( disable_leds_checksum )->by_default(false)->as_bool(); + this->add_module( this->config ); this->add_module( this->serial ); @@ -153,6 +159,9 @@ void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod){ // Call a specific event with an argument void Kernel::call_event(_EVENT_ENUM id_event, void * argument){ + if(id_event == ON_HALT) { + this->halted= (argument == nullptr); + } for (auto m : hooks[id_event]) { (m->*kernel_callback_functions[id_event])(argument); } diff --git a/src/libs/Kernel.h b/src/libs/Kernel.h index 948ba2e7..27e2787f 100644 --- a/src/libs/Kernel.h +++ b/src/libs/Kernel.h @@ -44,6 +44,9 @@ class Kernel { bool kernel_has_event(_EVENT_ENUM id_event, Module *mod); void unregister_for_event(_EVENT_ENUM id_event, Module *module); + bool is_using_leds() const { return use_leds; } + bool is_halted() const { return halted; } + // These modules are available to all other modules SerialConsole* serial; StreamOutputPool* streams; @@ -59,7 +62,6 @@ class Kernel { SlowTicker* slow_ticker; StepTicker* step_ticker; Adc* adc; - bool use_leds; std::string current_path; uint32_t base_stepping_frequency; uint32_t acceleration_ticks_per_second; @@ -67,6 +69,10 @@ class Kernel { private: // When a module asks to be called for a specific event ( a hook ), this is where that request is remembered std::array, NUMBER_OF_DEFINED_EVENTS> hooks; + struct { + bool use_leds:1; + bool halted:1; + }; }; diff --git a/src/libs/SlowTicker.cpp b/src/libs/SlowTicker.cpp index e9438786..06ee7bba 100644 --- a/src/libs/SlowTicker.cpp +++ b/src/libs/SlowTicker.cpp @@ -112,7 +112,7 @@ extern GPIO leds[]; void SlowTicker::on_idle(void*) { static uint16_t ledcnt= 0; - if(THEKERNEL->use_leds) { + if(THEKERNEL->is_using_leds()) { // flash led 3 to show we are alive leds[2]= (ledcnt++ & 0x1000) ? 1 : 0; } diff --git a/src/main.cpp b/src/main.cpp index 8cf56af7..cc3e028f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,7 +58,6 @@ #define second_usb_serial_enable_checksum CHECKSUM("second_usb_serial_enable") #define disable_msd_checksum CHECKSUM("msd_disable") -#define disable_leds_checksum CHECKSUM("leds_disable") #define dfu_enable_checksum CHECKSUM("dfu_enable") // Watchdog wd(5000000, WDT_MRI); @@ -110,9 +109,6 @@ void init() { Version version; kernel->streams->printf(" Build version %s, Build date %s\r\n", version.get_build(), version.get_build_date()); - //some boards don't have leds.. TOO BAD! - kernel->use_leds= !kernel->config->value( disable_leds_checksum )->by_default(false)->as_bool(); - bool sdok= (sd.disk_initialize() == 0); if(!sdok) kernel->streams->printf("SDCard is disabled\r\n"); @@ -215,7 +211,7 @@ void init() { // clear up the config cache to save some memory kernel->config->config_cache_clear(); - if(kernel->use_leds) { + if(kernel->is_using_leds()) { // set some leds to indicate status... led0 init doe, led1 mainloop running, led2 idle loop running, led3 sdcard ok leds[0]= 1; // indicate we are done with init leds[3]= sdok?1:0; // 4th led inidicates sdcard is available (TODO maye should indicate config was found) @@ -249,7 +245,7 @@ int main() uint16_t cnt= 0; // Main loop while(1){ - if(THEKERNEL->use_leds) { + if(THEKERNEL->is_using_leds()) { // flash led 2 to show we are alive leds[1]= (cnt++ & 0x1000) ? 1 : 0; } diff --git a/src/modules/communication/GcodeDispatch.cpp b/src/modules/communication/GcodeDispatch.cpp index 1d5c2a66..f7706c5c 100644 --- a/src/modules/communication/GcodeDispatch.cpp +++ b/src/modules/communication/GcodeDispatch.cpp @@ -40,7 +40,6 @@ static bool is_allowed_mcode(int m) { GcodeDispatch::GcodeDispatch() { - halted= false; uploading = false; currentline = -1; last_g= 255; @@ -50,13 +49,6 @@ GcodeDispatch::GcodeDispatch() void GcodeDispatch::on_module_loaded() { this->register_for_event(ON_CONSOLE_LINE_RECEIVED); - this->register_for_event(ON_HALT); -} - -void GcodeDispatch::on_halt(void *arg) -{ - // set halt stream and ignore everything until M999 - this->halted= (arg == nullptr); } // When a command is received, if it is a Gcode, dispatch it as an object via an event @@ -139,11 +131,11 @@ try_again: //Prepare gcode for dispatch Gcode *gcode = new Gcode(single_command, new_message.stream); - if(halted) { + if(THEKERNEL->is_halted()) { // we ignore all commands until M999, unless it is in the exceptions list (like M105 get temp) if(gcode->has_m && gcode->m == 999) { THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt - halted= false; + // fall through and pass onto other modules }else if(!is_allowed_mcode(gcode->m)) { diff --git a/src/modules/communication/GcodeDispatch.h b/src/modules/communication/GcodeDispatch.h index 18c356d7..0fcdbd4e 100644 --- a/src/modules/communication/GcodeDispatch.h +++ b/src/modules/communication/GcodeDispatch.h @@ -21,7 +21,6 @@ public: virtual void on_module_loaded(); virtual void on_console_line_received(void *line); - void on_halt(void *arg); private: int currentline; @@ -30,7 +29,6 @@ private: uint8_t last_g; struct { bool uploading: 1; - bool halted: 1; }; }; diff --git a/src/modules/robot/Robot.cpp b/src/modules/robot/Robot.cpp index f27d43ed..514d8c45 100644 --- a/src/modules/robot/Robot.cpp +++ b/src/modules/robot/Robot.cpp @@ -133,14 +133,12 @@ Robot::Robot() seconds_per_minute = 60.0F; this->clearToolOffset(); this->compensationTransform= nullptr; - this->halted= false; } //Called when the module has just been loaded void Robot::on_module_loaded() { this->register_for_event(ON_GCODE_RECEIVED); - this->register_for_event(ON_HALT); // Configuration this->on_config_reload(this); @@ -271,11 +269,6 @@ void Robot::check_max_actuator_speeds() } } -void Robot::on_halt(void *arg) -{ - halted= (arg == nullptr); -} - //A GCode has been received //See if the current Gcode line has some orders for us void Robot::on_gcode_received(void *argument) @@ -745,7 +738,7 @@ void Robot::append_line(Gcode *gcode, float target[], float rate_mm_s ) // segment 0 is already done - it's the end point of the previous move so we start at segment 1 // We always add another point after this loop so we stop at segments-1, ie i < segments for (int i = 1; i < segments; i++) { - if(halted) return; // don't queue any more segments + if(THEKERNEL->is_halted()) return; // don't queue any more segments for(int axis = X_AXIS; axis <= Z_AXIS; axis++ ) segment_end[axis] = last_milestone[axis] + segment_delta[axis]; @@ -839,7 +832,7 @@ void Robot::append_arc(Gcode *gcode, float target[], float offset[], float radiu arc_target[this->plane_axis_2] = this->last_milestone[this->plane_axis_2]; for (i = 1; i < segments; i++) { // Increment (segments-1) - if(halted) return; // don't queue any more segments + if(THEKERNEL->is_halted()) return; // don't queue any more segments if (count < this->arc_correction ) { // Apply vector rotation matrix diff --git a/src/modules/robot/Robot.h b/src/modules/robot/Robot.h index e9e4eba9..3294ecf3 100644 --- a/src/modules/robot/Robot.h +++ b/src/modules/robot/Robot.h @@ -26,7 +26,6 @@ class Robot : public Module { void on_module_loaded(); void on_config_reload(void* argument); void on_gcode_received(void* argument); - void on_halt(void *arg); void reset_axis_position(float position, int axis); void reset_axis_position(float x, float y, float z); @@ -38,7 +37,6 @@ class Robot : public Module { float get_z_maxfeedrate() const { return this->max_speeds[2]; } void setToolOffset(const float offset[3]); float get_feed_rate() const { return feed_rate; } - bool is_halted() const { return halted; } BaseSolution* arm_solution; // Selected Arm solution ( millimeters to step calculation ) @@ -95,10 +93,6 @@ class Robot : public Module { StepperMotor* alpha_stepper_motor; StepperMotor* beta_stepper_motor; StepperMotor* gamma_stepper_motor; - - struct { - bool halted:1; - }; }; // Convert from inches to millimeters ( our internal storage unit ) if needed diff --git a/src/modules/tools/endstops/Endstops.cpp b/src/modules/tools/endstops/Endstops.cpp index a2b8267b..c78add94 100644 --- a/src/modules/tools/endstops/Endstops.cpp +++ b/src/modules/tools/endstops/Endstops.cpp @@ -380,7 +380,7 @@ bool Endstops::wait_for_homed(char axes_to_move) THEKERNEL->call_event(ON_IDLE); // check if on_halt (eg kill) - if(THEKERNEL->robot->is_halted()) return false; + if(THEKERNEL->is_halted()) return false; for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { if ( ( axes_to_move >> c ) & 1 ) { @@ -406,7 +406,7 @@ bool Endstops::wait_for_homed(char axes_to_move) void Endstops::do_homing_cartesian(char axes_to_move) { // check if on_halt (eg kill) - if(THEKERNEL->robot->is_halted()) return; + if(THEKERNEL->is_halted()) return; // this homing works for cartesian and delta printers // Start moving the axes to the origin @@ -466,7 +466,7 @@ bool Endstops::wait_for_homed_corexy(int axis) THEKERNEL->call_event(ON_IDLE); // check if on_halt (eg kill) - if(THEKERNEL->robot->is_halted()) return false; + if(THEKERNEL->is_halted()) return false; if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) { if ( debounce[axis] < debounce_count ) { @@ -489,7 +489,7 @@ bool Endstops::wait_for_homed_corexy(int axis) void Endstops::corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps) { // check if on_halt (eg kill) - if(THEKERNEL->robot->is_halted()) return; + if(THEKERNEL->is_halted()) return; this->status = MOVING_TO_ENDSTOP_FAST; this->feed_rate[X_AXIS]= fast_rate; @@ -643,7 +643,7 @@ void Endstops::on_gcode_received(void *argument) home(a); } // check if on_halt (eg kill) - if(THEKERNEL->robot->is_halted()) return; + if(THEKERNEL->is_halted()) return; } }else { @@ -652,7 +652,7 @@ void Endstops::on_gcode_received(void *argument) } // check if on_halt (eg kill) - if(THEKERNEL->robot->is_halted()) return; + if(THEKERNEL->is_halted()) return; if(home_all) { // for deltas this may be important rather than setting each individually diff --git a/src/modules/tools/temperaturecontrol/TemperatureControl.cpp b/src/modules/tools/temperaturecontrol/TemperatureControl.cpp index a9915f5f..e2bcb992 100644 --- a/src/modules/tools/temperaturecontrol/TemperatureControl.cpp +++ b/src/modules/tools/temperaturecontrol/TemperatureControl.cpp @@ -330,7 +330,7 @@ void TemperatureControl::on_gcode_received(void *argument) while ( get_temperature() < target_temperature ) { THEKERNEL->call_event(ON_IDLE, this); // check if ON_HALT was called (usually by kill button) - if(this->target_temperature == UNDEFINED) break; + if(THEKERNEL->is_halted() || this->target_temperature == UNDEFINED) break; } this->waiting = false; } diff --git a/src/modules/utils/PlayLed/PlayLed.cpp b/src/modules/utils/PlayLed/PlayLed.cpp index 5a41de53..6c845736 100644 --- a/src/modules/utils/PlayLed/PlayLed.cpp +++ b/src/modules/utils/PlayLed/PlayLed.cpp @@ -22,8 +22,6 @@ #define play_led_disable_checksum CHECKSUM("play_led_disable") PlayLed::PlayLed() { - - halted= false; cnt= 0; } @@ -35,7 +33,7 @@ void PlayLed::on_module_loaded() } on_config_reload(this); - this->register_for_event(ON_HALT); + THEKERNEL->slow_ticker->attach(12, this, &PlayLed::led_tick); } @@ -51,7 +49,7 @@ void PlayLed::on_config_reload(void *argument) uint32_t PlayLed::led_tick(uint32_t) { - if(this->halted) { + if(THEKERNEL->is_halted()) { led.set(!led.get()); return 0; } @@ -68,8 +66,3 @@ uint32_t PlayLed::led_tick(uint32_t) return 0; } - -void PlayLed::on_halt(void *arg) -{ - this->halted= (arg == nullptr); -} diff --git a/src/modules/utils/PlayLed/PlayLed.h b/src/modules/utils/PlayLed/PlayLed.h index 5d1da019..d139d8c9 100644 --- a/src/modules/utils/PlayLed/PlayLed.h +++ b/src/modules/utils/PlayLed/PlayLed.h @@ -12,14 +12,12 @@ public: void on_module_loaded(void); void on_config_reload(void *); - void on_halt(void *arg); private: uint32_t led_tick(uint32_t); Pin led; struct { uint8_t cnt:4; - bool halted:1; }; }; diff --git a/src/modules/utils/panel/Panel.cpp b/src/modules/utils/panel/Panel.cpp index 963e6695..562014bd 100644 --- a/src/modules/utils/panel/Panel.cpp +++ b/src/modules/utils/panel/Panel.cpp @@ -84,7 +84,6 @@ Panel::Panel() this->sd= nullptr; this->extmounter= nullptr; this->external_sd_enable= false; - this->halted= false; strcpy(this->playing_file, "Playing file"); } @@ -190,7 +189,6 @@ void Panel::on_module_loaded() // Register for events this->register_for_event(ON_IDLE); this->register_for_event(ON_MAIN_LOOP); - this->register_for_event(ON_HALT); this->register_for_event(ON_SET_PUBLIC_DATA); // Refresh timer @@ -676,8 +674,3 @@ void Panel::on_second_tick(void *arg) // TODO for panels with no sd card detect we need to poll to see if card is inserted - or not } } - -void Panel::on_halt(void *arg) -{ - halted= (arg == nullptr); -} diff --git a/src/modules/utils/panel/Panel.h b/src/modules/utils/panel/Panel.h index 2b7cfea7..e75dea0a 100644 --- a/src/modules/utils/panel/Panel.h +++ b/src/modules/utils/panel/Panel.h @@ -35,7 +35,6 @@ class Panel : public Module { uint32_t button_tick(uint32_t dummy); uint32_t encoder_tick(uint32_t dummy); void on_idle(void* argument); - void on_halt(void* argument); void on_main_loop(void* argument); void on_set_public_data(void* argument); void on_second_tick(void* argument); @@ -81,7 +80,6 @@ class Panel : public Module { string getMessage() { return message; } bool hasMessage() { return message.size() > 0; } - bool is_halted() const { return halted; } uint16_t get_screen_lines() const { return screen_lines; } @@ -142,7 +140,6 @@ class Panel : public Module { bool menu_changed:1; bool control_value_changed:1; bool external_sd_enable:1; - bool halted:1; volatile bool counter_changed:1; volatile bool click_changed:1; volatile bool refresh_flag:1; diff --git a/src/modules/utils/panel/screens/MainMenuScreen.cpp b/src/modules/utils/panel/screens/MainMenuScreen.cpp index 0de27f5f..2c4558d3 100644 --- a/src/modules/utils/panel/screens/MainMenuScreen.cpp +++ b/src/modules/utils/panel/screens/MainMenuScreen.cpp @@ -118,7 +118,7 @@ void MainMenuScreen::display_menu_line(uint16_t line) { switch ( line ) { case 0: THEPANEL->lcd->printf("Watch"); break; - case 1: if(THEPANEL->is_halted()) THEPANEL->lcd->printf("Clear HALT"); else THEPANEL->lcd->printf(THEPANEL->is_playing() ? "Abort" : "Play"); break; + case 1: if(THEKERNEL->is_halted()) THEPANEL->lcd->printf("Clear HALT"); else THEPANEL->lcd->printf(THEPANEL->is_playing() ? "Abort" : "Play"); break; case 2: THEPANEL->lcd->printf("Jog"); break; case 3: THEPANEL->lcd->printf("Prepare"); break; case 4: THEPANEL->lcd->printf("Custom"); break; @@ -132,7 +132,7 @@ void MainMenuScreen::clicked_menu_entry(uint16_t line) switch ( line ) { case 0: THEPANEL->enter_screen(this->watch_screen); break; case 1: - if(THEPANEL->is_halted()){ + if(THEKERNEL->is_halted()){ send_command("M999"); THEPANEL->enter_screen(this->watch_screen); }else if(THEPANEL->is_playing()) abort_playing(); diff --git a/src/modules/utils/panel/screens/WatchScreen.cpp b/src/modules/utils/panel/screens/WatchScreen.cpp index b9ec4202..91380199 100644 --- a/src/modules/utils/panel/screens/WatchScreen.cpp +++ b/src/modules/utils/panel/screens/WatchScreen.cpp @@ -275,7 +275,7 @@ const char *WatchScreen::get_status() if (THEPANEL->hasMessage()) return THEPANEL->getMessage().c_str(); - if (THEPANEL->is_halted()) + if (THEKERNEL->is_halted()) return "HALTED Reset or M999"; if (THEKERNEL->pauser->paused()) diff --git a/src/modules/utils/player/Player.cpp b/src/modules/utils/player/Player.cpp index 3a0f9261..e405e6e5 100644 --- a/src/modules/utils/player/Player.cpp +++ b/src/modules/utils/player/Player.cpp @@ -52,7 +52,6 @@ Player::Player() this->booted = false; this->elapsed_secs = 0; this->reply_stream = nullptr; - this->halted= false; this->suspended= false; this->suspend_loops= 0; } @@ -65,7 +64,6 @@ void Player::on_module_loaded() this->register_for_event(ON_GET_PUBLIC_DATA); this->register_for_event(ON_SET_PUBLIC_DATA); this->register_for_event(ON_GCODE_RECEIVED); - this->register_for_event(ON_HALT); this->on_boot_gcode = THEKERNEL->config->value(on_boot_gcode_checksum)->by_default("/sd/on_boot.gcode")->as_string(); this->on_boot_gcode_enable = THEKERNEL->config->value(on_boot_gcode_enable_checksum)->by_default(true)->as_bool(); @@ -77,11 +75,6 @@ void Player::on_module_loaded() this->leave_heaters_on = THEKERNEL->config->value(leave_heaters_on_suspend_checksum)->by_default(false)->as_bool(); } -void Player::on_halt(void *arg) -{ - halted= (arg == nullptr); -} - void Player::on_second_tick(void *) { if(this->playing_file) this->elapsed_secs++; @@ -222,7 +215,7 @@ void Player::on_gcode_received(void *argument) // When a new line is received, check if it is a command, and if it is, act upon it void Player::on_console_line_received( void *argument ) { - if(halted) return; // if in halted state ignore any commands + if(THEKERNEL->is_halted()) return; // if in halted state ignore any commands SerialMessage new_message = *static_cast(argument); @@ -387,7 +380,7 @@ void Player::on_main_loop(void *argument) } if( this->playing_file ) { - if(halted) { + if(THEKERNEL->is_halted()) { abort_command("1", &(StreamOutput::NullStream)); return; } diff --git a/src/modules/utils/player/Player.h b/src/modules/utils/player/Player.h index ed0c3596..c651d90c 100644 --- a/src/modules/utils/player/Player.h +++ b/src/modules/utils/player/Player.h @@ -27,7 +27,6 @@ class Player : public Module { void on_console_line_received( void* argument ); void on_main_loop( void* argument ); void on_second_tick(void* argument); - void on_halt(void* argument); void on_get_public_data(void* argument); void on_set_public_data(void* argument); void on_gcode_received(void *argument); @@ -60,7 +59,6 @@ class Player : public Module { bool on_boot_gcode_enable:1; bool booted:1; bool playing_file:1; - bool halted:1; bool suspended:1; bool saved_inch_mode:1; bool saved_absolute_mode:1; -- 2.20.1