-
Notifications
You must be signed in to change notification settings - Fork 2
/
mcodes.c
126 lines (98 loc) · 5.09 KB
/
mcodes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
mcode.c - user defined M-codes template
Part of grblHAL
Copyright (c) 2019-2021 Terje Io
Grbl 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,
but WITHOUT ANY WARRANTY; without even the implied warranty of
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/>.
*/
/*
* NOTE: this template is also a bare bones example for adding M100 with two parameters: P and Q
*/
#include <math.h>
#include <string.h>
#ifdef ARDUINO
#include "../grbl/hal.h"
#else
#include "grbl/hal.h"
#endif
static user_mcode_ptrs_t user_mcode;
// check - check if M-code is handled here.
// parameters: mcode - M-code to check for (some are predefined in user_mcode_t in grbl/gcode.h), use a cast if not.
// returns: mcode if handled, UserMCode_Ignore otherwise (UserMCode_Ignore is defined in grbl/gcode.h).
static user_mcode_t check (user_mcode_t mcode)
{
return mcode == UserMCode_Generic0
? mcode // Handled by us.
: (user_mcode.check ? user_mcode.check(mcode) : UserMCode_Ignore); // If another handler present then call it or return ignore.
}
// validate - validate parameters
// parameters: gc_block - pointer to parser_block_t struct (defined in grbl/gcode.h).
// gc_block->words - holds a bitfield of parameter words available.
// If float values are NAN (Not A Number) this means they are not available.
// If integer values has all bits set to 1 this means they are not available.
// returns: status_code_t enum (defined in grbl/gcode.h): Status_OK if validated ok, appropriate status from enum if not.
static status_code_t validate (parser_block_t *gc_block, parameter_words_t *deprecated)
{
status_code_t state = Status_GcodeValueWordMissing;
switch(gc_block->user_mcode) {
case UserMCode_Generic0:
if(gc_block->words.p && !isnanf(gc_block->values.p)) // Check if P parameter value is supplied.
state = Status_BadNumberFormat; // Return error if so.
if(gc_block->words.q && isnanf(gc_block->values.q)) // Check if Q parameter value is supplied.
state = Status_BadNumberFormat; // Return error if not.
if(state != Status_BadNumberFormat && gc_block->words.q) { // Are required parameters provided?
if(gc_block->values.q > 0.0f && gc_block->values.q <= 5.0f) // Yes, is Q parameter value in range (1-5)?
state = Status_OK; // Yes - return ok status.
else
state = Status_GcodeValueOutOfRange; // No - return error status.
if(gc_block->words.q) // If P parameter is present set
gc_block->values.p = 1.0f; // value to 1 for execution.
gc_block->words.p = gc_block->words.q = Off; // Claim parameters.
gc_block->user_mcode_sync = true; // Optional: execute command synchronized
}
break;
default:
state = Status_Unhandled;
break;
}
// If not handled by us and another handler present then call it.
return state == Status_Unhandled && user_mcode.validate ? user_mcode.validate(gc_block, deprecated) : state;
}
// execute - execute M-code
// parameters: state - sys.state (bitmap, defined in system.h)
// gc_block - pointer to parser_block_t struct (defined in grbl/gcode.h).
// returns: -
static void execute (sys_state_t state, parser_block_t *gc_block) {
bool handled = true;
switch(gc_block->user_mcode) {
case UserMCode_Generic0:
// do something: Q parameter value can be found in gc_block->values.q.
// P parameter has its value in gc_block->values.p set to 1 if present, NAN if not.
break;
default:
handled = false;
break;
}
if(!handled && user_mcode.execute) // If not handled by us and another handler present
user_mcode.execute(state, gc_block); // then call it.
}
// Set up HAL pointers for handling additional M-codes.
// Call this function on driver setup.
void mcodes_init (void)
{
// Save away current HAL pointers so that we can use them to keep
// any chain of M-code handlers intact.
memcpy(&user_mcode, &hal.user_mcode, sizeof(user_mcode_ptrs_t));
// Redirect HAL pointers to our code.
hal.user_mcode.check = check;
hal.user_mcode.validate = validate;
hal.user_mcode.execute = execute;
}