-
Notifications
You must be signed in to change notification settings - Fork 0
/
openpnp.c
235 lines (184 loc) · 7.11 KB
/
openpnp.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
openpnp.c - plugin for for OpenPNP required M-codes
Part of grblHAL
Copyright (c) 2021-2024 Terje Io
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.
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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#if OPENPNP_ENABLE
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "grbl/hal.h"
#include "grbl/protocol.h"
static user_mcode_ptrs_t user_mcode;
static on_report_options_ptr on_report_options;
static uint8_t tport;
static user_mcode_type_t userMCodeCheck (user_mcode_t mcode)
{
return mcode == OpenPNP_SetPinState || mcode == OpenPNP_GetADCReading || mcode == OpenPNP_GetCurrentPosition || mcode == OpenPNP_FirmwareInfo ||
mcode == OpenPNP_SetAcceleration || mcode == OpenPNP_FinishMoves || mcode == OpenPNP_SettingsReset
? UserMCode_Normal
: (user_mcode.check ? user_mcode.check(mcode) : UserMCode_Unsupported);
}
static status_code_t userMCodeValidate (parser_block_t *gc_block)
{
status_code_t state = Status_GcodeValueWordMissing;
switch(gc_block->user_mcode) {
case OpenPNP_SetPinState:
if(gc_block->words.p && gc_block->words.s) {
if(gc_block->values.p <= 255.0f && (uint8_t)gc_block->values.p < hal.port.num_digital_out) {
gc_block->words.p = gc_block->words.s = Off;
state = Status_OK;
} else
state = Status_InvalidStatement;
}
break;
case OpenPNP_GetADCReading:
if(gc_block->words.t) {
if(gc_block->values.t < hal.port.num_analog_in) {
gc_block->words.t = Off;
state = Status_OK;
} else
state = Status_InvalidStatement;
}
break;
case OpenPNP_GetCurrentPosition:
gc_block->words.d = gc_block->words.r = Off;
state = Status_OK;
break;
case OpenPNP_SetAcceleration:
gc_block->words.p = gc_block->words.r = gc_block->words.s = gc_block->words.t = Off;
// TODO: add validation
state = Status_OK;
break;
case OpenPNP_FirmwareInfo:
case OpenPNP_FinishMoves:
case OpenPNP_SettingsReset:
state = Status_OK;
break;
default:
state = Status_Unhandled;
break;
}
return state == Status_Unhandled && user_mcode.validate ? user_mcode.validate(gc_block) : state;
}
static void report_position (bool real, bool detailed)
{
uint_fast8_t idx;
int32_t current_position[N_AXIS];
float print_position[N_AXIS];
char buf[(STRLEN_COORDVALUE + 4) * N_AXIS];
if(real || detailed) {
memcpy(current_position, sys.position, sizeof(sys.position));
if(real)
system_convert_array_steps_to_mpos(print_position, current_position);
} else
memcpy(print_position, gc_state.position, sizeof(print_position));
*buf = '\0';
for(idx = 0; idx < N_AXIS; idx++) {
print_position[idx] -= gc_get_offset(idx, true);
strcat(buf, axis_letter[idx]);
strcat(buf, ":");
strcat(buf, ftoa(print_position[idx], N_DECIMAL_COORDVALUE_MM)); // always mm and 3 decimals?
strcat(buf, idx == N_AXIS - 1 ? (detailed ? " " : ASCII_EOL) : " ");
}
hal.stream.write(buf);
if(detailed) {
*buf = '\0';
hal.stream.write("Count ");
for (idx = 0; idx < N_AXIS; idx++) {
print_position[idx] -= gc_get_offset(idx, true);
strcat(buf, axis_letter[idx]);
strcat(buf, ":");
itoa(current_position[idx], strchr(buf, '\0'), 10);
strcat(buf, idx == N_AXIS - 1 ? ASCII_EOL : " ");
}
hal.stream.write(buf);
}
}
static void report_temperature (void *data)
{
// int32_t v = hal.port.wait_on_input(false, tport, WaitMode_Immediate, 0.0f);
// format output -> T:21.17 /0.0000 B:21.04 /0.0000 @:0 B@:0
}
static void userMCodeExecute (uint_fast16_t state, parser_block_t *gc_block)
{
bool handled = true;
if (state != STATE_CHECK_MODE)
switch(gc_block->user_mcode) {
case OpenPNP_SetPinState:
hal.port.digital_out(gc_block->values.p, gc_block->values.s != 0.0f);
break;
case OpenPNP_GetADCReading: // Request temperature report
tport = gc_block->values.t;
protocol_enqueue_foreground_task(report_temperature, NULL);
break;
case OpenPNP_GetCurrentPosition:
report_position(gc_block->words.r, gc_block->words.d);
break;
case OpenPNP_FirmwareInfo:
hal.stream.write("FIRMWARE_NAME:grblHAL ");
hal.stream.write("FIRMWARE_URL:https%3A//github.com/grblHAL ");
hal.stream.write("FIRMWARE_VERSION:" GRBL_VERSION " ");
hal.stream.write("FIRMWARE_BUILD:");
hal.stream.write(uitoa(GRBL_BUILD));
hal.stream.write(ASCII_EOL);
break;
case OpenPNP_SetAcceleration: // Set acceleration
{
uint_fast8_t idx = N_AXIS;
protocol_buffer_synchronize();
do {
idx--;
if(gc_block->words.s || idx == X_AXIS || idx == Y_AXIS)
settings_override_acceleration(idx, gc_block->words.s ? gc_block->values.s : gc_block->values.t);
else
settings_override_acceleration(idx, gc_block->values.p);
} while(idx);
}
break;
case OpenPNP_FinishMoves: // Wait for buffered motions to complete
protocol_buffer_synchronize();
break;
case OpenPNP_SettingsReset: // Restore acceleration to configured values
{
uint_fast8_t idx = N_AXIS;
protocol_buffer_synchronize();
do {
settings_override_acceleration(--idx, 0.0f);
} while(idx);
}
break;
default:
handled = false;
break;
}
if(!handled && user_mcode.execute)
user_mcode.execute(state, gc_block);
}
static void onReportOptions (bool newopt)
{
on_report_options(newopt);
if(!newopt)
report_plugin("OpenPNP", "0.05");
}
void openpnp_init (void)
{
memcpy(&user_mcode, &grbl.user_mcode, sizeof(user_mcode_ptrs_t));
grbl.user_mcode.check = userMCodeCheck;
grbl.user_mcode.validate = userMCodeValidate;
grbl.user_mcode.execute = userMCodeExecute;
on_report_options = grbl.on_report_options;
grbl.on_report_options = onReportOptions;
}
#endif