diff --git a/src/debug.cpp b/src/debug.cpp index 85a3de8..40eef19 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -31,6 +31,15 @@ void dbg_usart_P(const char *layer_P, const char *s_P) { hu::usart1.puts_P(s_P); } +void dbg_usart_sprintf_P(const char *fmt_P, ...) { + char tmp[30]; + va_list argptr; + va_start(argptr, fmt_P); + vsprintf(tmp, fmt_P, argptr); + va_end(argptr); + dbg_logic(tmp); +} + } // namespace debug #endif diff --git a/src/debug.h b/src/debug.h index 355178e..d84436e 100644 --- a/src/debug.h +++ b/src/debug.h @@ -5,24 +5,31 @@ #define DEBUG_LOGIC /// Enable DEBUG_LOGIC to compile debugging and error messages (beware of code base size ;) ) for the logic layer -#define DEBUG_MODULES +//#define DEBUG_MODULES /// Enable DEBUG_HAL to compile debugging and error messages (beware of code base size ;) ) for the logic layer -#define DEBUG_HAL +//#define DEBUG_HAL + +// workaround non-standard PSTR definition on non-AVR platforms +#ifndef __AVR__ +#define PSTR(x) x +#endif /// Debugging macros and tools namespace debug { -#ifdef DEBUG_LOGIC +#if defined(DEBUG_LOGIC) && defined(__AVR__) extern const char logic[]; #define dbg_logic(x) debug::dbg_usart(debug::logic, x) #define dbg_logic_P(x) debug::dbg_usart_P(debug::logic, x) +#define dbg_logic_sprintf_P(fmt, ...) debug::dbg_usart_sprintf_P(fmt, __VA_ARGS__) #else #define dbg_logic(x) /* */ #define dbg_logic_P(x) /* */ +#define dbg_logic_sprintf_P(fmt, ...) /* */ #endif -#ifdef DEBUG_MODULES +#if defined(DEBUG_MODULES) && defined(__AVR__) extern const char modules[]; #define dbg_modules(x) debug::dbg_usart(debug::modules, x) #define dbg_modules_P(x) debug::dbg_usart_P(debug::modules, x) @@ -31,7 +38,7 @@ extern const char modules[]; #define dbg_modules_P(x) /* */ #endif -#ifdef DEBUG_HAL +#if defined(DEBUG_HAL) && defined(__AVR__) extern const char hal[]; #define dbg_hal(x) debug::dbg_usart(debug::hal, x) #define dbg_hal_P(x) debug::dbg_usart_P(debug::hal, x) @@ -50,6 +57,10 @@ void dbg_usart(const char *layer_P, const char *s); /// @param layer PROGMEM string /// @param s PROGMEM string to be printed void dbg_usart_P(const char *layer_P, const char *s_P); + +/// Dump an error message onto the USART with sprintf-like formatting +/// @param fmt_P PROGMEM format string +void dbg_usart_sprintf_P(const char *fmt_P, ...); #endif } // namespace debug diff --git a/src/logic/feed_to_bondtech.cpp b/src/logic/feed_to_bondtech.cpp index 34def0c..20f94df 100644 --- a/src/logic/feed_to_bondtech.cpp +++ b/src/logic/feed_to_bondtech.cpp @@ -6,18 +6,12 @@ #include "../modules/leds.h" #include "../modules/motion.h" #include "../modules/permanent_storage.h" -#ifdef DEBUG_LOGIC -#include "../hal/usart.h" -#include -#include -#endif //DEBUG_LOGIC +#include "../debug.h" namespace logic { void FeedToBondtech::Reset(uint8_t maxRetries) { -#ifdef DEBUG_LOGIC - hu::usart1.puts("\nFeed to Bondtech\n\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("\nFeed to Bondtech\n\n")); state = EngagingIdler; this->maxRetries = maxRetries; ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::blink0); @@ -28,21 +22,14 @@ bool FeedToBondtech::Step() { switch (state) { case EngagingIdler: if (mi::idler.Engaged()) { -#ifdef DEBUG_LOGIC - uint16_t startSteps = mm::motion.CurPosition(mm::Pulley); - char str[30]; - sprintf_P(str, PSTR("\nPulley start steps %u\n\n"), startSteps); - hu::usart1.puts(str); -#endif //DEBUG_LOGIC + dbg_logic_sprintf_P(PSTR("\nPulley start steps %u\n\n"), mm::motion.CurPosition(mm::Pulley)); state = PushingFilament; mm::motion.PlanMove(config::defaultBowdenLength, config::pulleyFeedrate); //@@TODO constants - there was some strange acceleration sequence in the original FW, // we can probably hand over some array of constants for hand-tuned acceleration + leverage some smoothing in the stepper as well } return false; case PushingFilament: -#ifdef DEBUG_LOGIC - hu::usart1.puts("\nFeed to Bondtech --> Pushing\n\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("\nFeed to Bondtech --> Pushing\n\n")); if (mfs::fsensor.Pressed()) { mm::motion.AbortPlannedMoves(); // stop pushing filament mi::idler.Disengage(); @@ -55,22 +42,16 @@ bool FeedToBondtech::Step() { } return false; case DisengagingIdler: -#ifdef DEBUG_LOGIC - hu::usart1.puts("\nFeed to Bondtech --> DisengagingIdler\n\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("\nFeed to Bondtech --> DisengagingIdler\n\n")); if (!mi::idler.Engaged()) { state = OK; ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::on); } return false; case OK: -#ifdef DEBUG_LOGIC - hu::usart1.puts("\nFeed to Bondtech\n\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("\nFeed to Bondtech\n\n")); case Failed: -#ifdef DEBUG_LOGIC - hu::usart1.puts("\nFeed to Bondtech FAILED\n\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("\nFeed to Bondtech FAILED\n\n")); default: return true; } diff --git a/src/logic/load_filament.cpp b/src/logic/load_filament.cpp index f279527..298902a 100644 --- a/src/logic/load_filament.cpp +++ b/src/logic/load_filament.cpp @@ -7,11 +7,7 @@ #include "../modules/permanent_storage.h" #include "../modules/selector.h" #include "../modules/user_input.h" -#ifdef DEBUG_LOGIC -#include "../hal/usart.h" -#include -#include -#endif //DEBUG_LOGIC +#include "../debug.h" namespace logic { @@ -21,9 +17,7 @@ void LoadFilament::Reset(uint8_t param) { if (!CheckToolIndex(param)) { return; } -#ifdef DEBUG_LOGIC - hu::usart1.puts("Load Filament\n\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Load Filament\n\n")); state = ProgressCode::EngagingIdler; error = ErrorCode::RUNNING; mg::globals.SetActiveSlot(param); diff --git a/src/logic/tool_change.cpp b/src/logic/tool_change.cpp index 982aac7..c8b19d1 100644 --- a/src/logic/tool_change.cpp +++ b/src/logic/tool_change.cpp @@ -8,11 +8,7 @@ #include "../modules/permanent_storage.h" #include "../modules/selector.h" #include "../modules/user_input.h" -#ifdef DEBUG_LOGIC -#include "../hal/usart.h" -#include -#include -#endif //DEBUG_LOGIC +#include "../debug.h" namespace logic { @@ -24,10 +20,8 @@ void ToolChange::Reset(uint8_t param) { } if (param == mg::globals.ActiveSlot() && mg::globals.FilamentLoaded()) { -// we are already at the correct slot and the filament is loaded - nothing to do -#ifdef DEBUG_LOGIC - hu::usart1.puts("we are already at the correct slot and the filament is loaded - nothing to do\n"); -#endif //DEBUG_LOGIC + // we are already at the correct slot and the filament is loaded - nothing to do + dbg_logic_P(PSTR("we are already at the correct slot and the filament is loaded - nothing to do\n")); return; } @@ -36,17 +30,13 @@ void ToolChange::Reset(uint8_t param) { plannedSlot = param; if (mg::globals.FilamentLoaded()) { -#ifdef DEBUG_LOGIC - hu::usart1.puts("Filament is loaded --> unload\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Filament is loaded --> unload\n")); state = ProgressCode::UnloadingFilament; unl.Reset(mg::globals.ActiveSlot()); } else { state = ProgressCode::FeedingToFinda; error = ErrorCode::RUNNING; -#ifdef DEBUG_LOGIC - hu::usart1.puts("Filament is not loaded --> load\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Filament is not loaded --> load\n")); mg::globals.SetActiveSlot(plannedSlot); feed.Reset(true); } diff --git a/src/logic/unload_filament.cpp b/src/logic/unload_filament.cpp index e20a73d..66bf6cf 100644 --- a/src/logic/unload_filament.cpp +++ b/src/logic/unload_filament.cpp @@ -6,11 +6,7 @@ #include "../modules/motion.h" #include "../modules/permanent_storage.h" #include "../modules/user_input.h" -#ifdef DEBUG_LOGIC -#include "../hal/usart.h" -#include -#include -#endif //DEBUG_LOGIC +#include "../debug.h" namespace logic { diff --git a/src/modules/idler.cpp b/src/modules/idler.cpp index 0452e83..5df64f8 100644 --- a/src/modules/idler.cpp +++ b/src/modules/idler.cpp @@ -3,11 +3,7 @@ #include "leds.h" #include "motion.h" #include "permanent_storage.h" -#ifdef DEBUG_LOGIC -#include "../hal/usart.h" -#include -#include -#endif //DEBUG_LOGIC +#include "../debug.h" namespace modules { namespace idler { @@ -16,34 +12,24 @@ Idler idler; void Idler::PrepareMoveToPlannedSlot() { mm::motion.PlanMoveTo(SlotPosition(plannedSlot), mm::unitToAxisUnit(config::idlerFeedrate)); -#ifdef DEBUG_LOGIC - char str[30]; - sprintf_P(str, PSTR("Prepare Move Idler slot %d\n"), plannedSlot); - hu::usart1.puts(str); -#endif //DEBUG_LOGIC + dbg_logic_sprintf_P(PSTR("Prepare Move Idler slot %d\n"), plannedSlot); } void Idler::PlanHomingMove() { mm::motion.PlanMove(mm::unitToAxisUnit(-config::idlerLimits.lenght * 2), mm::unitToAxisUnit(config::idlerFeedrate)); -#ifdef DEBUG_LOGIC - hu::usart1.puts("Plan Homing Idler\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Plan Homing Idler\n")); } Idler::OperationResult Idler::Disengage() { if (state == Moving) { -#ifdef DEBUG_LOGIC - hu::usart1.puts("Moving --> Disengage refused\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Moving --> Disengage refused\n")); return OperationResult::Refused; } plannedSlot = IdleSlotIndex(); plannedEngage = false; if (!Engaged()) { -#ifdef DEBUG_LOGIC - hu::usart1.puts("Disengage Idler\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Disengage Idler\n")); return OperationResult::Accepted; } return InitMovement(mm::Idler); @@ -51,9 +37,7 @@ Idler::OperationResult Idler::Disengage() { Idler::OperationResult Idler::Engage(uint8_t slot) { if (state == Moving) { -#ifdef DEBUG_LOGIC - hu::usart1.puts("Moving --> Engage refused\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Moving --> Engage refused\n")); return OperationResult::Refused; } @@ -61,9 +45,7 @@ Idler::OperationResult Idler::Engage(uint8_t slot) { plannedEngage = true; if (Engaged()) { -#ifdef DEBUG_LOGIC - hu::usart1.puts("Engage Idler\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Engage Idler\n")); return OperationResult::Accepted; } @@ -81,21 +63,15 @@ bool Idler::Home() { bool Idler::Step() { switch (state) { case Moving: -#ifdef DEBUG_LOGIC - //hu::usart1.puts("Moving Idler\n"); -#endif //DEBUG_LOGIC + // dbg_logic_P(PSTR("Moving Idler\n")); PerformMove(mm::Idler); return false; case Homing: -#ifdef DEBUG_LOGIC - hu::usart1.puts("Homing Idler\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Homing Idler\n")); PerformHome(mm::Idler); return false; case Ready: -#ifdef DEBUG_LOGIC - //hu::usart1.puts("Idler Ready\n"); -#endif //DEBUG_LOGIC + // dbg_logic_P(PSTR("Idler Ready\n")); currentlyEngaged = plannedEngage; currentSlot = plannedSlot; @@ -104,9 +80,7 @@ bool Idler::Step() { return true; case Failed: -#ifdef DEBUG_LOGIC - hu::usart1.puts("Idler Failed\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Idler Failed\n")); default: return true; } diff --git a/src/modules/motion.cpp b/src/modules/motion.cpp index dcc09cc..97cd881 100644 --- a/src/modules/motion.cpp +++ b/src/modules/motion.cpp @@ -1,10 +1,6 @@ #include "motion.h" #include "../panic.h" -#ifdef DEBUG_LOGIC -#include "../hal/usart.h" -#include -#include -#endif //DEBUG_LOGIC +#include "../debug.h" // TODO: use proper timer abstraction #ifdef __AVR__ @@ -50,11 +46,7 @@ void Motion::StallGuardReset(Axis axis) { } void Motion::PlanMoveTo(Axis axis, pos_t pos, steps_t feed_rate, steps_t end_rate) { -#ifdef DEBUG_LOGIC - char str[30]; - sprintf_P(str, PSTR("Move axis %d to %u\n"), axis, pos); - hu::usart1.puts(str); -#endif //DEBUG_LOGIC + dbg_logic_sprintf_P(PSTR("Move axis %d to %u\n"), axis, pos); if (axisData[axis].ctrl.PlanMoveTo(pos, feed_rate, end_rate)) { // move was queued, prepare the axis diff --git a/src/modules/selector.cpp b/src/modules/selector.cpp index aba0826..b06b6b9 100644 --- a/src/modules/selector.cpp +++ b/src/modules/selector.cpp @@ -3,11 +3,7 @@ #include "leds.h" #include "motion.h" #include "permanent_storage.h" -#ifdef DEBUG_LOGIC -#include "../hal/usart.h" -#include -#include -#endif //DEBUG_LOGIC +#include "../debug.h" namespace modules { namespace selector { @@ -16,33 +12,23 @@ Selector selector; void Selector::PrepareMoveToPlannedSlot() { mm::motion.PlanMoveTo(SlotPosition(plannedSlot), mm::unitToAxisUnit(config::selectorFeedrate)); -#ifdef DEBUG_LOGIC - char str[30]; - sprintf_P(str, PSTR("Prepare Move Selector slot %d\n"), plannedSlot); - hu::usart1.puts(str); -#endif //DEBUG_LOGIC + dbg_logic_sprintf_P(PSTR("Prepare Move Selector slot %d\n"), plannedSlot); } void Selector::PlanHomingMove() { mm::motion.PlanMove(mm::unitToAxisUnit(config::selectorLimits.lenght * 2), mm::unitToAxisUnit(config::selectorFeedrate)); -#ifdef DEBUG_LOGIC - hu::usart1.puts("Plan Homing Selector\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Plan Homing Selector\n")); } Selector::OperationResult Selector::MoveToSlot(uint8_t slot) { if (state == Moving) { -#ifdef DEBUG_LOGIC - hu::usart1.puts("Moving --> Selector refused\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Moving --> Selector refused\n")); return OperationResult::Refused; } plannedSlot = slot; if (currentSlot == slot) { -#ifdef DEBUG_LOGIC - hu::usart1.puts("Moving Selector\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Moving Selector\n")); return OperationResult::Accepted; } return InitMovement(mm::Selector); @@ -59,27 +45,19 @@ bool Selector::Step() { switch (state) { case Moving: PerformMove(mm::Selector); -#ifdef DEBUG_LOGIC - //hu::usart1.puts("Moving Selector\n"); -#endif //DEBUG_LOGIC + //dbg_logic_P(PSTR("Moving Selector\n")); return false; case Homing: -#ifdef DEBUG_LOGIC - hu::usart1.puts("Homing Selector\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Homing Selector\n")); PerformHome(mm::Selector); return false; case Ready: -#ifdef DEBUG_LOGIC - //hu::usart1.puts("Selector Ready\n"); -#endif //DEBUG_LOGIC + //dbg_logic_P(PSTR("Selector Ready\n")); currentSlot = plannedSlot; mm::motion.Disable(mm::Selector); // turn off selector motor's power every time return true; case Failed: -#ifdef DEBUG_LOGIC - hu::usart1.puts("Selector Failed\n"); -#endif //DEBUG_LOGIC + dbg_logic_P(PSTR("Selector Failed\n")); default: return true; }