grblHAL core  20241107
core_handlers.h
Go to the documentation of this file.
1 /*
2  core_handlers.h - core event handler entry points and some reporting entry points
3 
4  Part of grblHAL
5 
6  Copyright (c) 2020-2024 Terje Io
7 
8  grblHAL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  grblHAL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
26 #pragma once
27 
28 #include "system.h"
29 #include "stream.h"
30 #include "alarms.h"
31 #include "errors.h"
32 #include "settings.h"
33 #include "report.h"
34 #include "planner.h"
35 #include "machine_limits.h"
36 #include "vfs.h"
37 
38 typedef enum {
46 
47 /* TODO: add to grbl pointers so that a different formatting (xml, json etc) of reports may be implemented by a plugin?
48 typedef struct {
49  void (*report_echo_line_received)(char *line);
50  void (*report_realtime_status)(void);
51  void (*report_probe_parameters)(void);
52  void (*report_ngc_parameters)(void);
53  void (*report_gcode_modes)(void);
54  void (*report_startup_line)(uint8_t n, char *line);
55  void (*report_execute_startup_message)(char *line, status_code_t status_code);
56 } grbl_report_t;
57 */
58 
59 // Report entry points set by core at startup and reset.
60 
61 typedef void (*init_message_ptr)(void);
62 typedef void (*help_message_ptr)(void);
63 typedef status_code_t (*status_message_ptr)(status_code_t status_code);
66 
67 typedef struct {
68  init_message_ptr init_message; //<! Prints system welcome message.
69  help_message_ptr help_message; //<! Prints system help message.
70  status_message_ptr status_message; //<! Prints system status messages.
71  feedback_message_ptr feedback_message; //<! Prints miscellaneous feedback messages.
72  alarm_message_ptr alarm_message; //<! Prints alarm message.
74 } report_t;
75 
76 // Core event handler and other entry points.
77 // Most of the event handlers defaults to NULL, a few is set up to call a dummy handler for simpler code.
78 
79 typedef bool (*enqueue_gcode_ptr)(char *data);
80 typedef bool (*protocol_enqueue_realtime_command_ptr)(char c);
81 typedef bool (*travel_limits_ptr)(float *target, axes_signals_t axes, bool is_cartesian);
82 typedef bool (*arc_limits_ptr)(coord_data_t *target, coord_data_t *position, point_2d_t center, float radius, plane_t plane, int32_t turns);
83 
84 typedef void (*jog_limits_ptr)(float *target, float *position);
85 typedef bool (*home_machine_ptr)(axes_signals_t cycle, axes_signals_t auto_square);
86 
88 typedef void (*on_state_change_ptr)(sys_state_t state);
89 typedef void (*on_override_changed_ptr)(override_changed_t override);
90 typedef void (*on_spindle_programmed_ptr)(spindle_ptrs_t *spindle, spindle_state_t state, float rpm, spindle_rpm_mode_t mode);
91 typedef void (*on_wco_changed_ptr)(void);
92 typedef void (*on_program_completed_ptr)(program_flow_t program_flow, bool check_mode);
93 typedef void (*on_execute_realtime_ptr)(sys_state_t state);
94 typedef void (*on_unknown_accessory_override_ptr)(uint8_t cmd);
95 typedef bool (*on_unknown_realtime_cmd_ptr)(char c);
96 typedef void (*on_report_handlers_init_ptr)(void);
97 typedef void (*on_report_options_ptr)(bool newopt);
98 typedef void (*on_report_ngc_parameters_ptr)(void);
99 typedef void (*on_report_command_help_ptr)(void);
100 typedef const char *(*on_setting_get_description_ptr)(setting_id_t id);
101 typedef void (*on_global_settings_restore_ptr)(void);
103 typedef void (*on_unknown_feedback_message_ptr)(stream_write_ptr stream_write);
104 typedef void (*on_stream_changed_ptr)(stream_type_t type);
105 typedef bool (*on_laser_ppi_enable_ptr)(uint_fast16_t ppi, uint_fast16_t pulse_length);
106 typedef void (*on_homing_rate_set_ptr)(axes_signals_t axes, float rate, homing_mode_t mode);
107 
108 // NOTE: cycle contains the axis flags of the executed homing cycle, success will be true when all the configured cycles are completed.
109 typedef void (*on_homing_completed_ptr)(axes_signals_t cycle, bool success);
110 
111 typedef bool (*on_probe_toolsetter_ptr)(tool_data_t *tool, coord_data_t *position, bool at_g59_3, bool on);
112 typedef bool (*on_probe_start_ptr)(axes_signals_t axes, float *target, plan_line_data_t *pl_data);
113 typedef void (*on_probe_completed_ptr)(void);
114 typedef void (*on_tool_selected_ptr)(tool_data_t *tool);
115 typedef void (*on_tool_changed_ptr)(tool_data_t *tool);
116 typedef void (*on_toolchange_ack_ptr)(void);
117 typedef void (*on_reset_ptr)(void);
118 typedef void (*on_jog_cancel_ptr)(sys_state_t state);
119 typedef bool (*on_spindle_select_ptr)(spindle_ptrs_t *spindle);
120 typedef void (*on_spindle_selected_ptr)(spindle_ptrs_t *spindle);
121 typedef status_code_t (*on_gcode_message_ptr)(char *msg);
123 typedef const char *(*on_set_axis_setting_unit_ptr)(setting_id_t setting_id, uint_fast8_t axis_idx);
124 typedef status_code_t (*on_file_open_ptr)(const char *fname, vfs_file_t *handle, bool stream);
125 typedef status_code_t (*on_file_end_ptr)(vfs_file_t *handle, status_code_t status);
126 typedef status_code_t (*on_unknown_sys_command_ptr)(sys_state_t state, char *line); // return Status_Unhandled.
127 typedef status_code_t (*on_user_command_ptr)(char *line);
128 typedef sys_commands_t *(*on_get_commands_ptr)(void);
129 typedef status_code_t (*on_macro_execute_ptr)(macro_id_t macro); // macro implementations _must_ claim hal.stream.read to stream macros!
130 typedef void (*on_macro_return_ptr)(void);
131 
132 typedef bool (*write_tool_data_ptr)(tool_data_t *tool_data);
133 typedef bool (*read_tool_data_ptr)(tool_id_t tool_id, tool_data_t *tool_data);
134 typedef bool (*clear_tool_data_ptr)(void);
135 
136 typedef struct {
137  uint32_t n_tools;
142 } tool_table_t;
143 
144 /*****************
145  * User M-code *
146  *****************/
147 
148 typedef enum {
153 
159 
180 typedef status_code_t (*user_mcode_validate_ptr)(parser_block_t *gc_block);
181 
191 typedef void (*user_mcode_execute_ptr)(sys_state_t state, parser_block_t *gc_block);
192 
200 typedef struct {
205 
206 
207 typedef struct {
208  // report entry points set by core at reset.
210  //
212  // grbl core events - may be subscribed to by drivers or by the core.
258  // core entry points - set up by core before driver_init() is called.
267 } grbl_t;
268 
269 extern grbl_t grbl;
270 
271 /*EOF*/
alarm_code_t
Definition: alarms.h:28
alarm_details_t *(* on_get_alarms_ptr)(void)
Definition: alarms.h:62
void(* on_parser_init_ptr)(parser_state_t *gc_state)
Definition: core_handlers.h:87
const char *(* on_set_axis_setting_unit_ptr)(setting_id_t setting_id, uint_fast8_t axis_idx)
Definition: core_handlers.h:123
grbl_t grbl
Definition: grbllib.c:87
void(* on_spindle_selected_ptr)(spindle_ptrs_t *spindle)
Definition: core_handlers.h:120
void(* on_homing_rate_set_ptr)(axes_signals_t axes, float rate, homing_mode_t mode)
Definition: core_handlers.h:106
void(* on_report_options_ptr)(bool newopt)
Definition: core_handlers.h:97
bool(* write_tool_data_ptr)(tool_data_t *tool_data)
Definition: core_handlers.h:132
void(* on_unknown_feedback_message_ptr)(stream_write_ptr stream_write)
Definition: core_handlers.h:103
void(* on_jog_cancel_ptr)(sys_state_t state)
Definition: core_handlers.h:118
void(* on_realtime_report_ptr)(stream_write_ptr stream_write, report_tracking_flags_t report)
Definition: core_handlers.h:102
status_code_t(* on_gcode_message_ptr)(char *msg)
Definition: core_handlers.h:121
void(* on_probe_completed_ptr)(void)
Definition: core_handlers.h:113
void(* on_toolchange_ack_ptr)(void)
Definition: core_handlers.h:116
const char *(* on_setting_get_description_ptr)(setting_id_t id)
Definition: core_handlers.h:100
void(* on_state_change_ptr)(sys_state_t state)
Definition: core_handlers.h:88
void(* on_spindle_programmed_ptr)(spindle_ptrs_t *spindle, spindle_state_t state, float rpm, spindle_rpm_mode_t mode)
Definition: core_handlers.h:90
bool(* on_laser_ppi_enable_ptr)(uint_fast16_t ppi, uint_fast16_t pulse_length)
Definition: core_handlers.h:105
bool(* on_probe_toolsetter_ptr)(tool_data_t *tool, coord_data_t *position, bool at_g59_3, bool on)
Definition: core_handlers.h:111
void(* on_report_ngc_parameters_ptr)(void)
Definition: core_handlers.h:98
void(* on_unknown_accessory_override_ptr)(uint8_t cmd)
Definition: core_handlers.h:94
void(* on_homing_completed_ptr)(axes_signals_t cycle, bool success)
Definition: core_handlers.h:109
void(* on_reset_ptr)(void)
Definition: core_handlers.h:117
status_code_t(* on_file_end_ptr)(vfs_file_t *handle, status_code_t status)
Definition: core_handlers.h:125
void(* on_tool_changed_ptr)(tool_data_t *tool)
Definition: core_handlers.h:115
void(* on_tool_selected_ptr)(tool_data_t *tool)
Definition: core_handlers.h:114
void(* on_wco_changed_ptr)(void)
Definition: core_handlers.h:91
void(* on_macro_return_ptr)(void)
Definition: core_handlers.h:130
void(* jog_limits_ptr)(float *target, float *position)
Definition: core_handlers.h:84
void(* on_execute_realtime_ptr)(sys_state_t state)
Definition: core_handlers.h:93
bool(* on_unknown_realtime_cmd_ptr)(char c)
Definition: core_handlers.h:95
void(* on_report_handlers_init_ptr)(void)
Definition: core_handlers.h:96
status_code_t(* user_mcode_validate_ptr)(parser_block_t *gc_block)
Pointer to function for validating parameters for a user defined M-code.
Definition: core_handlers.h:180
status_code_t(* on_user_command_ptr)(char *line)
Definition: core_handlers.h:127
void(* on_override_changed_ptr)(override_changed_t override)
Definition: core_handlers.h:89
void(* help_message_ptr)(void)
Definition: core_handlers.h:62
bool(* protocol_enqueue_realtime_command_ptr)(char c)
Definition: core_handlers.h:80
override_changed_t
Definition: core_handlers.h:38
@ OverrideChanged_FanState
Definition: core_handlers.h:44
@ OverrideChanged_CoolantState
Definition: core_handlers.h:43
@ OverrideChanged_FeedRate
Definition: core_handlers.h:39
@ OverrideChanged_SpindleRPM
Definition: core_handlers.h:41
@ OverrideChanged_SpindleState
Definition: core_handlers.h:42
@ OverrideChanged_RapidRate
Definition: core_handlers.h:40
bool(* clear_tool_data_ptr)(void)
Definition: core_handlers.h:134
status_code_t(* on_unknown_sys_command_ptr)(sys_state_t state, char *line)
Definition: core_handlers.h:126
user_mcode_type_t
Definition: core_handlers.h:148
@ UserMCode_Unsupported
0 - M-code is not supported.
Definition: core_handlers.h:149
@ UserMCode_Normal
1 - M-code is supported.
Definition: core_handlers.h:150
@ UserMCode_NoValueWords
2 - M-code supports valueless parameter words.
Definition: core_handlers.h:151
void(* on_program_completed_ptr)(program_flow_t program_flow, bool check_mode)
Definition: core_handlers.h:92
status_code_t(* status_message_ptr)(status_code_t status_code)
Definition: core_handlers.h:63
alarm_code_t(* alarm_message_ptr)(alarm_code_t alarm_code)
Definition: core_handlers.h:65
sys_commands_t *(* on_get_commands_ptr)(void)
Definition: core_handlers.h:128
bool(* on_spindle_select_ptr)(spindle_ptrs_t *spindle)
Definition: core_handlers.h:119
bool(* travel_limits_ptr)(float *target, axes_signals_t axes, bool is_cartesian)
Definition: core_handlers.h:81
bool(* arc_limits_ptr)(coord_data_t *target, coord_data_t *position, point_2d_t center, float radius, plane_t plane, int32_t turns)
Definition: core_handlers.h:82
bool(* read_tool_data_ptr)(tool_id_t tool_id, tool_data_t *tool_data)
Definition: core_handlers.h:133
void(* init_message_ptr)(void)
Definition: core_handlers.h:61
status_code_t(* on_macro_execute_ptr)(macro_id_t macro)
Definition: core_handlers.h:129
void(* on_rt_reports_added_ptr)(report_tracking_flags_t report)
Definition: core_handlers.h:122
void(* on_stream_changed_ptr)(stream_type_t type)
Definition: core_handlers.h:104
bool(* on_probe_start_ptr)(axes_signals_t axes, float *target, plan_line_data_t *pl_data)
Definition: core_handlers.h:112
void(* on_global_settings_restore_ptr)(void)
Definition: core_handlers.h:101
void(* user_mcode_execute_ptr)(sys_state_t state, parser_block_t *gc_block)
Pointer to function for executing a user defined M-code.
Definition: core_handlers.h:191
void(* on_report_command_help_ptr)(void)
Definition: core_handlers.h:99
status_code_t(* on_file_open_ptr)(const char *fname, vfs_file_t *handle, bool stream)
Definition: core_handlers.h:124
bool(* home_machine_ptr)(axes_signals_t cycle, axes_signals_t auto_square)
Definition: core_handlers.h:85
user_mcode_type_t(* user_mcode_check_ptr)(user_mcode_t mcode)
Pointer to function for checking if user defined M-code is supported.
Definition: core_handlers.h:158
bool(* enqueue_gcode_ptr)(char *data)
Definition: core_handlers.h:79
message_code_t(* feedback_message_ptr)(message_code_t message_code)
Definition: core_handlers.h:64
error_details_t *(* on_get_errors_ptr)(void)
Definition: errors.h:132
parser_state_t gc_state
Definition: gcode.c:116
uint32_t tool_id_t
Definition: gcode.h:34
user_mcode_t
Definition: gcode.h:233
program_flow_t
Definition: gcode.h:198
uint16_t macro_id_t
Definition: gcode.h:35
spindle_rpm_mode_t
Definition: gcode.h:189
homing_mode_t
Definition: machine_limits.h:30
message_code_t
Definition: messages.h:26
setting_details_t *(* on_get_settings_ptr)(void)
Definition: settings.h:1027
bool(* setting_output_ptr)(const setting_detail_t *setting, uint_fast16_t offset, void *data)
Definition: settings.h:993
setting_id_t
Definition: settings.h:46
bool(* enqueue_realtime_command_ptr)(char c)
Pointer to function for extracting real-time commands from the input stream and enqueue them for proc...
Definition: stream.h:142
void(* stream_write_ptr)(const char *s)
Pointer to function for writing a null terminated string to the output stream.
Definition: stream.h:123
stream_type_t
Definition: stream.h:82
Definition: core_handlers.h:207
enqueue_gcode_ptr enqueue_gcode
Definition: core_handlers.h:263
arc_limits_ptr check_arc_travel_limits
Definition: core_handlers.h:261
on_rt_reports_added_ptr on_rt_reports_added
Definition: core_handlers.h:226
home_machine_ptr home_machine
Definition: core_handlers.h:259
on_file_open_ptr on_file_open
Called when a file is opened for streaming.
Definition: core_handlers.h:255
on_probe_completed_ptr on_probe_completed
Definition: core_handlers.h:243
on_reset_ptr on_reset
Called from interrupt context.
Definition: core_handlers.h:254
on_spindle_programmed_ptr on_spindle_programmed
Definition: core_handlers.h:217
on_execute_realtime_ptr on_execute_realtime
Definition: core_handlers.h:220
enqueue_realtime_command_ptr enqueue_realtime_command
Definition: core_handlers.h:264
on_homing_completed_ptr on_homing_completed
Definition: core_handlers.h:240
on_unknown_accessory_override_ptr on_unknown_accessory_override
Definition: core_handlers.h:222
on_set_axis_setting_unit_ptr on_set_axis_setting_unit
Definition: core_handlers.h:244
on_unknown_sys_command_ptr on_unknown_sys_command
return Status_Unhandled if not handled.
Definition: core_handlers.h:235
on_unknown_realtime_cmd_ptr on_unknown_realtime_cmd
Definition: core_handlers.h:234
on_gcode_message_ptr on_gcode_message
Called on output of message parsed from gcode. NOTE: string pointed to is freed after this call.
Definition: core_handlers.h:245
on_file_end_ptr on_file_end
Called when a file opened for streaming reaches the end.
Definition: core_handlers.h:256
jog_limits_ptr apply_jog_limits
Definition: core_handlers.h:262
report_t report
Definition: core_handlers.h:209
on_global_settings_restore_ptr on_global_settings_restore
Definition: core_handlers.h:227
on_override_changed_ptr on_override_changed
Definition: core_handlers.h:215
on_parser_init_ptr on_parser_init
Definition: core_handlers.h:213
on_execute_realtime_ptr on_execute_delay
Definition: core_handlers.h:221
user_mcode_ptrs_t user_mcode
Optional handlers for user defined M-codes.
Definition: core_handlers.h:257
on_wco_changed_ptr on_wco_changed
Definition: core_handlers.h:218
on_unknown_feedback_message_ptr on_unknown_feedback_message
Definition: core_handlers.h:233
on_homing_rate_set_ptr on_homing_rate_set
Definition: core_handlers.h:239
on_gcode_message_ptr on_gcode_comment
Called when a plain gcode comment has been parsed.
Definition: core_handlers.h:246
on_toolchange_ack_ptr on_toolchange_ack
Called from interrupt context.
Definition: core_handlers.h:249
on_tool_changed_ptr on_tool_changed
Called after executing M6 or M61.
Definition: core_handlers.h:248
on_realtime_report_ptr on_realtime_report
Definition: core_handlers.h:232
on_stream_changed_ptr on_stream_changed
Definition: core_handlers.h:238
on_tool_selected_ptr on_tool_selected
Called prior to executing M6 or after executing M61.
Definition: core_handlers.h:247
on_report_handlers_init_ptr on_report_handlers_init
Definition: core_handlers.h:216
on_report_command_help_ptr on_report_command_help
Deprecated, use system_register_commands() to register new commands.
Definition: core_handlers.h:225
tool_table_t tool_table
Definition: core_handlers.h:211
on_jog_cancel_ptr on_jog_cancel
Called from interrupt context.
Definition: core_handlers.h:250
on_report_options_ptr on_report_options
Definition: core_handlers.h:223
on_macro_return_ptr on_macro_return
NOTE: will be cleared on a hal.driver_reset call.
Definition: core_handlers.h:266
on_probe_start_ptr on_probe_start
Definition: core_handlers.h:242
on_probe_toolsetter_ptr on_probe_toolsetter
Definition: core_handlers.h:241
on_spindle_selected_ptr on_spindle_selected
Called when spindle is selected, do not change HAL pointers here!
Definition: core_handlers.h:253
on_get_commands_ptr on_get_commands
Deprecated, use system_register_commands() to register new commands.
Definition: core_handlers.h:236
on_get_errors_ptr on_get_errors
Definition: core_handlers.h:230
on_program_completed_ptr on_program_completed
Definition: core_handlers.h:219
on_macro_execute_ptr on_macro_execute
Definition: core_handlers.h:265
on_setting_get_description_ptr on_setting_get_description
Definition: core_handlers.h:228
on_spindle_select_ptr on_spindle_select
Called before spindle is selected, hook in HAL overrides here.
Definition: core_handlers.h:252
travel_limits_ptr check_travel_limits
Definition: core_handlers.h:260
on_laser_ppi_enable_ptr on_laser_ppi_enable
Definition: core_handlers.h:251
on_get_settings_ptr on_get_settings
Definition: core_handlers.h:231
on_report_ngc_parameters_ptr on_report_ngc_parameters
Definition: core_handlers.h:224
on_user_command_ptr on_user_command
Definition: core_handlers.h:237
on_state_change_ptr on_state_change
Definition: core_handlers.h:214
on_get_alarms_ptr on_get_alarms
Definition: core_handlers.h:229
Parser block structure.
Definition: gcode.h:636
Parser state.
Definition: gcode.h:591
Definition: planner.h:87
Definition: core_handlers.h:67
init_message_ptr init_message
Definition: core_handlers.h:68
feedback_message_ptr feedback_message
Definition: core_handlers.h:71
alarm_message_ptr alarm_message
Definition: core_handlers.h:72
setting_output_ptr setting
Definition: core_handlers.h:73
status_message_ptr status_message
Definition: core_handlers.h:70
help_message_ptr help_message
Definition: core_handlers.h:69
Handlers and data for spindle support.
Definition: spindle_control.h:270
Definition: system.h:343
Tool data.
Definition: gcode.h:581
Definition: core_handlers.h:136
uint32_t n_tools
Definition: core_handlers.h:137
clear_tool_data_ptr clear
Definition: core_handlers.h:141
write_tool_data_ptr write
Definition: core_handlers.h:140
tool_data_t * tool
Array of tool data, size must be n_tools + 1.
Definition: core_handlers.h:138
read_tool_data_ptr read
Definition: core_handlers.h:139
Optional handlers for user defined M-codes.
Definition: core_handlers.h:200
user_mcode_execute_ptr execute
Handler for executing a user defined M-code.
Definition: core_handlers.h:203
user_mcode_validate_ptr validate
Handler for validating parameters for a user defined M-code.
Definition: core_handlers.h:202
user_mcode_check_ptr check
Handler for checking if a user defined M-code is supported.
Definition: core_handlers.h:201
Definition: vfs.h:80
uint_fast16_t sys_state_t
See sys_state.
Definition: system.h:108
Definition: nuts_bolts.h:130
Coordinate data.
Definition: gcode.h:342
Axis index to plane assignment.
Definition: gcode.h:393
Definition: nuts_bolts.h:146
Definition: system.h:195
Definition: spindle_control.h:59