From 0a289f8bf023d23039bd0962d41c6a971acb4b72 Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Thu, 23 Jun 2016 16:40:31 -0700 Subject: [PATCH] Totally DRY status messages using X-Macros --- src/messages.def | 111 +++++++++++++++++++++++++---------------------- src/status.c | 24 +++++++--- src/status.h | 64 +++------------------------ 3 files changed, 83 insertions(+), 116 deletions(-) diff --git a/src/messages.def b/src/messages.def index bb816ba..d152ba0 100644 --- a/src/messages.def +++ b/src/messages.def @@ -25,56 +25,61 @@ \******************************************************************************/ -// Must keep these aligned with defs in status.h - -MSG(00, "OK") -MSG(01, "Eagain") -MSG(02, "Noop") -MSG(03, "Complete") -MSG(04, "No such device") -MSG(05, "Buffer full") -MSG(06, "Buffer full - fatal") -MSG(07, "EEPROM data invalid") -MSG(08, "Motor error") -MSG(09, "Internal error") - -MSG(10, "Move time is infinite") -MSG(11, "Move time is NAN") - -MSG(12, "Unrecognized command or config name") -MSG(13, "Invalid or malformed command") -MSG(14, "Bad number format") -MSG(15, "Parameter is read-only") -MSG(16, "Parameter cannot be read") -MSG(17, "Command not accepted at this time") -MSG(18, "Input exceeds max length") -MSG(19, "Input less than minimum value") -MSG(20, "Input exceeds maximum value") -MSG(21, "Input value range error") - -MSG(22, "Gcode command unsupported") -MSG(23, "M code unsupported") -MSG(24, "Axis word missing") -MSG(25, "Feedrate not specified") -MSG(26, "Arc specification error") -MSG(27, "Arc specification error - missing axis") -// current longest message: 56 char -MSG(28, "Arc specification error - radius arc out of tolerance") -MSG(29, "Arc specification error - endpoint is starting point") - -MSG(30, "Move less than minimum length") -MSG(31, "Move less than minimum time") -MSG(32, "Machine is alarmed - Command not processed") -MSG(33, "Limit switch hit - Shutdown occurred") - -MSG(34, "Soft limit exceeded") - -MSG(35, "Homing cycle failed") -MSG(36, "Homing Error - Bad or no axis specified") -MSG(37, "Homing Error - Search velocity is zero") -MSG(38, "Homing Error - Latch velocity is zero") -MSG(39, "Homing Error - Travel min & max are the same") -MSG(40, "Homing Error - Negative latch backoff") -MSG(41, "Homing Error - Homing switches misconfigured") - -MSG(42, "Probe cycle failed") +// OS, communications and low-level status +MSG(OK, "OK") +MSG(EAGAIN, "Run command again") +MSG(NOOP, "No op") +MSG(COMPLETE, "Complete") +MSG(NO_SUCH_DEVICE, "No such device") +MSG(BUFFER_FULL, "Buffer full") +MSG(BUFFER_FULL_FATAL, "Buffer full - fatal") +MSG(EEPROM_DATA_INVALID, "EEPROM data invalid") +MSG(MOTOR_ERROR, "Motor error") +MSG(INTERNAL_ERROR, "Internal error") + +MSG(PREP_LINE_MOVE_TIME_IS_INFINITE, "Move time is infinite") +MSG(PREP_LINE_MOVE_TIME_IS_NAN, "Move time is NAN") + +// Generic data input errors +MSG(UNRECOGNIZED_NAME, "Unrecognized command or config name") +MSG(INVALID_OR_MALFORMED_COMMAND, "Invalid or malformed command") +MSG(BAD_NUMBER_FORMAT, "Bad number format") +MSG(PARAMETER_IS_READ_ONLY, "Parameter is read-only") +MSG(PARAMETER_CANNOT_BE_READ, "Parameter cannot be read") +MSG(COMMAND_NOT_ACCEPTED, "Command not accepted at this time") +MSG(INPUT_EXCEEDS_MAX_LENGTH, "Input exceeds max length") +MSG(INPUT_LESS_THAN_MIN_VALUE, "Input less than minimum value") +MSG(INPUT_EXCEEDS_MAX_VALUE, "Input exceeds maximum value") +MSG(INPUT_VALUE_RANGE_ERROR, "Input value range error") + +// Gcode errors & warnings (Most originate from NIST) +MSG(GCODE_COMMAND_UNSUPPORTED, "Gcode command unsupported") +MSG(MCODE_COMMAND_UNSUPPORTED, "M code unsupported") +MSG(GCODE_AXIS_IS_MISSING, "Axis word missing") +MSG(GCODE_FEEDRATE_NOT_SPECIFIED, "Feedrate not specified") +MSG(ARC_SPECIFICATION_ERROR, "Arc specification error") +MSG(ARC_AXIS_MISSING_FOR_SELECTED_PLANE, "Arc missing axis") +MSG(ARC_RADIUS_OUT_OF_TOLERANCE, "Arc radius arc out of tolerance") +MSG(ARC_ENDPOINT_IS_STARTING_POINT, "Arc endpoint is starting point") + +// Errors and warnings +MSG(MINIMUM_LENGTH_MOVE, "Move less than minimum length") +MSG(MINIMUM_TIME_MOVE, "Move less than minimum time") +MSG(MACHINE_ALARMED, "Machine is alarmed - Command not processed") +MSG(LIMIT_SWITCH_HIT, "Limit switch hit - Shutdown occurred") +MSG(SOFT_LIMIT_EXCEEDED, "Soft limit exceeded") + +// Homing +MSG(HOMING_CYCLE_FAILED, "Homing cycle failed") +MSG(HOMING_ERROR_BAD_OR_NO_AXIS, "Homing Error - Bad or no axis specified") +MSG(HOMING_ERROR_ZERO_SEARCH_VELOCITY, "Homing Error - Search velocity is zero") +MSG(HOMING_ERROR_ZERO_LATCH_VELOCITY, "Homing Error - Latch velocity is zero") +MSG(HOMING_ERROR_TRAVEL_MIN_MAX_IDENTICAL, + "Homing Error - Travel min & max are the same") +MSG(HOMING_ERROR_NEGATIVE_LATCH_BACKOFF, + "Homing Error - Negative latch backoff") +MSG(HOMING_ERROR_SWITCH_MISCONFIGURATION, + "Homing Error - Homing switches misconfigured") + +// Probing +MSG(PROBE_CYCLE_FAILED, "Probe cycle failed") diff --git a/src/status.c b/src/status.c index 42afac6..f036473 100644 --- a/src/status.c +++ b/src/status.c @@ -31,12 +31,12 @@ stat_t status_code; // allocate a variable for the ritorno macro -#define MSG(N, TEXT) static const char stat_##N[] PROGMEM = TEXT; +#define MSG(NAME, TEXT) static const char stat_##NAME[] PROGMEM = TEXT; #include "messages.def" #undef MSG static const char *const stat_msg[] PROGMEM = { -#define MSG(N, TEXT) stat_##N, +#define MSG(NAME, TEXT) stat_##NAME, #include "messages.def" #undef MSG }; @@ -47,8 +47,20 @@ const char *status_to_pgmstr(stat_t status) { } -/// Return the status message -void status_error_P(const char *location, const char *msg, stat_t status) { - printf_P(PSTR("\nERROR: %S: %S: %S (%d)\n"), - msg, location, status_to_pgmstr(status), status); +stat_t status_error(stat_t status) { + return status_error_P(0, 0, status); +} + + +stat_t status_error_P(const char *location, const char *msg, stat_t status) { + printf_P(PSTR("\n{\"error\": %d, \"code\": %d"), + status_to_pgmstr(status), status); + + if (msg) printf_P(PSTR(", \"msg\": %S"), msg); + if (location) printf_P(PSTR(", \"location\": %S"), location); + + putchar('}'); + putchar('\n'); + + return status; } diff --git a/src/status.h b/src/status.h index bb9d89b..65350bf 100644 --- a/src/status.h +++ b/src/status.h @@ -35,69 +35,19 @@ #define ritorno(a) if ((status_code = a) != STAT_OK) {return status_code;} typedef enum { - // OS, communications and low-level status - STAT_OK, // function completed OK - STAT_EAGAIN, // function would block (call again) - STAT_NOOP, // function had no-operation - STAT_COMPLETE, // operation is complete - STAT_NO_SUCH_DEVICE, - STAT_BUFFER_FULL, - STAT_BUFFER_FULL_FATAL, - STAT_EEPROM_DATA_INVALID, - STAT_MOTOR_ERROR, - STAT_INTERNAL_ERROR, // unrecoverable internal error - - STAT_PREP_LINE_MOVE_TIME_IS_INFINITE, - STAT_PREP_LINE_MOVE_TIME_IS_NAN, - - // Generic data input errors - STAT_UNRECOGNIZED_NAME, - STAT_INVALID_OR_MALFORMED_COMMAND, - STAT_BAD_NUMBER_FORMAT, - STAT_PARAMETER_IS_READ_ONLY, - STAT_PARAMETER_CANNOT_BE_READ, - STAT_COMMAND_NOT_ACCEPTED, - STAT_INPUT_EXCEEDS_MAX_LENGTH, - STAT_INPUT_LESS_THAN_MIN_VALUE, - STAT_INPUT_EXCEEDS_MAX_VALUE, - STAT_INPUT_VALUE_RANGE_ERROR, - - // Gcode errors & warnings (Most originate from NIST) - // Fascinating: http://www.cncalarms.com/ - STAT_GCODE_COMMAND_UNSUPPORTED, - STAT_MCODE_COMMAND_UNSUPPORTED, - STAT_GCODE_AXIS_IS_MISSING, - STAT_GCODE_FEEDRATE_NOT_SPECIFIED, - STAT_ARC_SPECIFICATION_ERROR, - STAT_ARC_AXIS_MISSING_FOR_SELECTED_PLANE, - STAT_ARC_RADIUS_OUT_OF_TOLERANCE, - STAT_ARC_ENDPOINT_IS_STARTING_POINT, - - // Errors and warnings - STAT_MINIMUM_LENGTH_MOVE, // move is less than minimum length - STAT_MINIMUM_TIME_MOVE, // move is less than minimum time - STAT_MACHINE_ALARMED, // machine is alarmed - STAT_LIMIT_SWITCH_HIT, // a limit switch was hit - - STAT_SOFT_LIMIT_EXCEEDED, - - STAT_HOMING_CYCLE_FAILED, // homing cycle did not complete - STAT_HOMING_ERROR_BAD_OR_NO_AXIS, - STAT_HOMING_ERROR_ZERO_SEARCH_VELOCITY, - STAT_HOMING_ERROR_ZERO_LATCH_VELOCITY, - STAT_HOMING_ERROR_TRAVEL_MIN_MAX_IDENTICAL, - STAT_HOMING_ERROR_NEGATIVE_LATCH_BACKOFF, - STAT_HOMING_ERROR_SWITCH_MISCONFIGURATION, - - STAT_PROBE_CYCLE_FAILED, // probing cycle did not complete - // Do not exceed 255 +#define MSG(NAME, TEXT) STAT_##NAME, +#include "messages.def" +#undef MSG + + STAT_MAX_VALUE = 255 // Do not exceed 255 } stat_t; extern stat_t status_code; const char *status_to_pgmstr(stat_t status); -void status_error_P(const char *location, const char *msg, stat_t status); +stat_t status_error(stat_t status); +stat_t status_error_P(const char *location, const char *msg, stat_t status); #define TO_STRING(x) _TO_STRING(x) #define _TO_STRING(x) #x -- 2.27.0