Skip to content

Commit

Permalink
Added printf style debug output function and corresponding macro.
Browse files Browse the repository at this point in the history
Added grbl.on_report_ngc_parameters event.
Fixed silly mistakes in CAN code.
  • Loading branch information
terjeio committed Jul 9, 2024
1 parent eb7610c commit 5cff12e
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 18 deletions.
8 changes: 3 additions & 5 deletions canbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#define CANBUS_BUFFER_LEN 8
#endif
#ifndef CANBUS_BAUDRATE
#define CANBUS_BAUDRATE 0 // 125,000
#define CANBUS_BAUDRATE 0 // 125000
#endif

typedef struct {
Expand Down Expand Up @@ -96,8 +96,8 @@ ISR_CODE static bool ISR_FUNC(canbus_queue_rx)(canbus_message_t message, can_rx_
uint8_t next_head = (rx_buffer.head + 1) % CANBUS_BUFFER_LEN;

if((ok = next_head != rx_buffer.tail)) {
rx_buffer.rx[next_head].callback = callback;
rx_buffer.rx[next_head].message = message;
rx_buffer.rx[rx_buffer.head].callback = callback;
rx_buffer.rx[rx_buffer.head].message = message;

rx_buffer.head = next_head;
}
Expand Down Expand Up @@ -132,8 +132,6 @@ static status_code_t canbus_set_baud (setting_id_t id, uint_fast16_t value)
{
settings.canbus_baud = value;

can_set_baud(baud[settings.canbus_baud]);

return can_set_baud(baud[settings.canbus_baud]) ? Status_OK : Status_SettingValueOutOfRange;
}

Expand Down
18 changes: 17 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
## grblHAL changelog

<a name="20240709"/>Build 20240709

Core:

* For developers: added printf style debug output function and corresponding macro. See _grbl/stream.h_ for details. Added `grbl.on_report_ngc_parameters` event.

* Fixed silly mistakes in CAN code. Ref. [issue #179](https://github.com/grblHAL/STM32F4xx/issues/179#issuecomment-2217912406).

Drivers:

* SAM3X8E: [PR #25](https://github.com/grblHAL/SAM3X8E/pull/25), adds missing guards around references.

* STM32F1xx: added tentative board map for Creality v4.4.2 and v4.4.7. Ref. [issue #33](https://github.com/grblHAL/STM32F1xx/issues/33). Not tested!

---

<a name="20240704"/>Build 20240704

Core:
Expand All @@ -13,7 +29,7 @@ Drivers:

* ESP32: fixed WebUI regression. Ref. [issue #116](https://github.com/grblHAL/ESP32/issues/116).

* STM32F7xx, STM32F4xx: Added lowlevel CANbus API and enabled it for some boards.
* STM32F7xx, STM32F4xx: added lowlevel CANbus API and enabled it for some boards.

---

Expand Down
2 changes: 2 additions & 0 deletions core_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ typedef void (*on_unknown_accessory_override_ptr)(uint8_t cmd);
typedef bool (*on_unknown_realtime_cmd_ptr)(char c);
typedef void (*on_report_handlers_init_ptr)(void);
typedef void (*on_report_options_ptr)(bool newopt);
typedef void (*on_report_ngc_parameters_ptr)(void);
typedef void (*on_report_command_help_ptr)(void);
typedef const char *(*on_setting_get_description_ptr)(setting_id_t id);
typedef void (*on_global_settings_restore_ptr)(void);
Expand Down Expand Up @@ -156,6 +157,7 @@ typedef struct {
on_execute_realtime_ptr on_execute_delay;
on_unknown_accessory_override_ptr on_unknown_accessory_override;
on_report_options_ptr on_report_options;
on_report_ngc_parameters_ptr on_report_ngc_parameters;
on_report_command_help_ptr on_report_command_help; //!< Deprecated, use system_register_commands() to register new commands.
on_rt_reports_added_ptr on_rt_reports_added;
on_global_settings_restore_ptr on_global_settings_restore;
Expand Down
2 changes: 1 addition & 1 deletion grbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#else
#define GRBL_VERSION "1.1f"
#endif
#define GRBL_BUILD 20240704
#define GRBL_BUILD 20240709

#define GRBL_URL "https://github.com/grblHAL"

Expand Down
3 changes: 2 additions & 1 deletion messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ typedef enum {
typedef enum {
Message_Plain = 0,
Message_Info,
Message_Warning
Message_Warning,
Message_Debug
} message_type_t;

typedef struct {
Expand Down
19 changes: 13 additions & 6 deletions report.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

/*
This file functions as the primary feedback interface for Grbl. Any outgoing data, such
This file functions as the primary feedback interface for grblHAL. Any outgoing data, such
as the protocol status messages, feedback messages, and status reports, are stored here.
For the most part, these functions primarily are called from protocol.c methods. If a
different style feedback is desired (i.e. JSON), then a user can change these following
Expand Down Expand Up @@ -267,6 +267,10 @@ void report_message (const char *msg, message_type_t type)
hal.stream.write("Warning: ");
break;

case Message_Debug:
hal.stream.write("Debug: ");
break;

default:
break;
}
Expand Down Expand Up @@ -405,7 +409,7 @@ status_code_t report_help (char *args)
}


// Grbl settings print out.
// grblHAL settings print out.

static int cmp_settings (const void *a, const void *b)
{
Expand Down Expand Up @@ -508,7 +512,7 @@ void report_grbl_settings (bool all, void *data)

// Prints current probe parameters. Upon a probe command, these parameters are updated upon a
// successful probe or upon a failed probe with the G38.3 without errors command (if supported).
// These values are retained until Grbl is power-cycled, whereby they will be re-zeroed.
// These values are retained until grblHAL is power-cycled, whereby they will be re-zeroed.
void report_probe_parameters (void)
{
// Report in terms of machine position.
Expand Down Expand Up @@ -581,7 +585,7 @@ status_code_t report_named_ngc_parameter (char *arg)

#endif

// Prints Grbl NGC parameters (coordinate offsets, probing, tool table)
// Prints grblHAL NGC parameters (coordinate offsets, probing, tool table)
void report_ngc_parameters (void)
{
uint_fast8_t idx;
Expand Down Expand Up @@ -656,6 +660,9 @@ void report_ngc_parameters (void)
hal.stream.write(get_axis_value(sys.tlo_reference[plane.axis_linear] / settings.axis[plane.axis_linear].steps_per_mm));
hal.stream.write("]" ASCII_EOL);
}

if(grbl.on_report_ngc_parameters)
grbl.on_report_ngc_parameters();
}

static inline bool is_g92_active (void)
Expand Down Expand Up @@ -1082,8 +1089,8 @@ void report_build_info (char *line, bool extended)
}


// Prints the character string line Grbl has received from the user, which has been pre-parsed,
// and has been sent into protocol_execute_line() routine to be executed by Grbl.
// Prints the character string line grblHAL has received from the user, which has been pre-parsed,
// and has been sent into protocol_execute_line() routine to be executed by grblHAL.
void report_echo_line_received (char *line)
{
hal.stream.write("[echo: ");
Expand Down
47 changes: 47 additions & 0 deletions stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
#include "protocol.h"
#include "state_machine.h"

#if defined(DEBUG) || defined(DEBUGOUT)
#include <stdio.h>
#include <stdarg.h>
#ifndef DEBUG_BUFFER
#define DEBUG_BUFFER 100
#endif
#endif

static stream_rx_buffer_t rxbackup;

typedef struct {
Expand Down Expand Up @@ -664,6 +672,20 @@ void debug_writeln (const char *s)
}
}

void debug_printf (const char *fmt, ...)
{
char debug_out[DEBUG_BUFFER];

va_list args;
va_start(args, fmt);
vsnprintf(debug_out, sizeof(debug_out) - 1, fmt, args);
va_end(args);

debug_writeln(debug_out);
while(hal.stream.get_tx_buffer_count()) // Wait until message is delivered
grbl.on_execute_realtime(state_get());
}

static bool debug_claim_stream (io_stream_properties_t const *stream)
{
io_stream_t const *claimed = NULL;
Expand Down Expand Up @@ -694,4 +716,29 @@ bool debug_stream_init (void)
return hal.debug.write == debug_write;
}

#elif defined(DEBUG)

void debug_printf (const char *fmt, ...)
{
char debug_out[DEBUG_BUFFER];

va_list args;
va_start(args, fmt);
vsnprintf(debug_out, sizeof(debug_out) - 1, fmt, args);
va_end(args);

if(hal.stream.write) {
report_message(debug_out, Message_Debug);
while(hal.stream.get_tx_buffer_count()) // Wait until message is delivered
grbl.on_execute_realtime(state_get());
}
}

#else

void debug_printf (const char *fmt, ...)
{
// NOOP
}

#endif
18 changes: 14 additions & 4 deletions stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
Copyright (c) 2019-2023 Terje Io
Grbl is free software: you can redistribute it and/or modify
grblHAL 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.
Grbl is distributed in the hope that it will be useful,
grblHAL 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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/

/*! \file
Expand Down Expand Up @@ -357,11 +357,21 @@ io_stream_t const *stream_open_instance (uint8_t instance, uint32_t baud_rate, s

bool stream_set_description (const io_stream_t *stream, const char *description);

void debug_printf(const char *fmt, ...);

#if defined(DEBUG) || defined(DEBUGOUT)
#define DEBUG_PRINT 1
#ifdef DEBUGOUT
void debug_write (const char *s);
void debug_writeln (const char *s);
bool debug_stream_init (void);
#endif
#else
#define DEBUG_PRINT 0
#endif

#define debug_print(fmt, ...) \
do { if(DEBUG_PRINT) debug_printf(fmt, __VA_ARGS__); } while(0)

#ifdef __cplusplus
}
Expand Down

0 comments on commit 5cff12e

Please sign in to comment.