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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
#include <EEPROM.h>
// Yep, this is actually -*- c++ -*-
// Prototype of fn defined in Tools.pde
void send_tool_command(CircularBuffer::Cursor& cursor);
//initialize the firmware to default state.
inline void init_commands()
{
finishedCommands = 0;
}
//handle our packets.
void process_host_packets()
{
unsigned long start = millis();
unsigned long end = start + PACKET_TIMEOUT;
#ifdef ENABLE_COMMS_DEBUG
//Serial.print("IN: ");
#endif
//do we have a finished packet?
while (!hostPacket.isFinished())
{
if (Serial.available() > 0)
{
digitalWrite(DEBUG_PIN, HIGH);
//grab a byte and process it.
byte d = Serial.read();
hostPacket.process_byte(d);
#ifdef ENABLE_COMMS_DEBUG
//Serial.print(d, HEX);
//Serial.print(" ");
#endif
serial_rx_count++;
//keep us goign while we have data coming in.
start = millis();
end = start + PACKET_TIMEOUT;
if (hostPacket.getResponseCode() == RC_CRC_MISMATCH)
{
//host_crc_errors++;
#ifdef ENABLE_COMMS_DEBUG
Serial.println("Host CRC Mismatch");
#endif
}
digitalWrite(DEBUG_PIN, LOW);
}
//are we sure we wanna break mid-packet?
//have we timed out?
if (millis() >= end)
{
#ifdef ENABLE_COMMS_DEBUG
Serial.println("Host timeout");
#endif
break;
}
}
if (hostPacket.isFinished())
{
serial_packet_count++;
byte b = hostPacket.get_8(0);
// top bit high == bufferable command packet (eg. #128-255)
if (b & 0x80)
{
//do we have room?
if (commandBuffer.remainingCapacity() >= hostPacket.getLength())
{
//okay, throw it in the buffer.
for (byte i=0; i<hostPacket.getLength(); i++)
commandBuffer.append(hostPacket.get_8(i));
}
else
{
hostPacket.overflow();
}
}
// top bit low == reply needed query packet (eg. #0-127)
else
handle_query(b);
}
//okay, send our response
hostPacket.sendReply();
}
//this is for handling query commands that need a response.
void handle_query(byte cmd)
{
//which one did we get?
switch (cmd)
{
//WORKS
case HOST_CMD_VERSION:
//get our host version
host_version = hostPacket.get_16(1);
//send our version back.
hostPacket.add_16(FIRMWARE_VERSION);
break;
//WORKS
case HOST_CMD_INIT:
//just initialize
initialize();
break;
case HOST_CMD_GET_BUFFER_SIZE:
//send our remaining buffer size.
hostPacket.add_16(commandBuffer.remainingCapacity());
break;
case HOST_CMD_CLEAR_BUFFER:
//just clear it.
commandBuffer.clear();
break;
case HOST_CMD_GET_POSITION:
//send our position
hostPacket.add_32(current_steps.x);
hostPacket.add_32(current_steps.y);
hostPacket.add_32(current_steps.z);
hostPacket.add_8(get_endstop_states());
break;
case HOST_CMD_GET_RANGE:
//send our range
hostPacket.add_32(range_steps.x);
hostPacket.add_32(range_steps.y);
hostPacket.add_32(range_steps.z);
break;
case HOST_CMD_SET_RANGE:
//set our range to what the host tells us
range_steps.x = (long)hostPacket.get_32(1);
range_steps.y = (long)hostPacket.get_32(5);
range_steps.z = (long)hostPacket.get_32(9);
//write it back to eeprom
write_range_to_eeprom();
break;
case HOST_CMD_ABORT:
//support a microcontrollers right to choice.
abort_print();
break;
case HOST_CMD_PAUSE:
if (is_machine_paused)
{
//unpause our machine.
is_machine_paused = false;
//unpause our tools
set_tool_pause_state(false);
//resume stepping.
enable_needed_steppers();
enableTimer1Interrupt();
}
else
{
//pause our activity.
is_machine_paused = true;
//pause our tools
set_tool_pause_state(true);
//pause stepping
disableTimer1Interrupt();
disable_steppers();
}
break;
case HOST_CMD_PROBE:
//we dont support this yet.
hostPacket.unsupported();
break;
//WORKS
case HOST_CMD_TOOL_QUERY:
send_tool_query();
break;
case HOST_CMD_IS_FINISHED:
{
bool finished = is_point_buffer_empty() && at_target() && commandBuffer.size() == 0;
hostPacket.add_8(finished?1:0);
break;
}
case HOST_CMD_READ_EEPROM:
{
const uint16_t offset = hostPacket.get_16(1);
const uint8_t count = hostPacket.get_8(3);
if (count > 16) {
hostPacket.overflow();
} else {
for (uint8_t i = 0; i < count; i++) {
hostPacket.add_8(EEPROM.read(offset+i));
}
}
}
break;
case HOST_CMD_WRITE_EEPROM:
{
uint16_t offset = hostPacket.get_16(1);
uint8_t count = hostPacket.get_8(3);
if (count > 16) {
hostPacket.overflow();
} else {
for (uint8_t i = 0; i < count; i++) {
EEPROM.write(offset+i,hostPacket.get_8(4+i));
}
hostPacket.add_8(count);
}
}
break;
default:
hostPacket.unsupported();
}
}
//this is for handling buffered commands with no response
void handle_commands()
{
byte flags = 0;
long x;
long y;
long z;
unsigned long step_delay;
byte cmd;
//do we have any commands?
if (commandBuffer.size() > 0)
{
CircularBuffer::Cursor cursor = commandBuffer.newCursor();
uint16_t index = 0;
cmd = cursor.read_8();
// Do it later if it's a point queueing command and we don't have room yet.
if ((cmd == HOST_CMD_QUEUE_POINT_ABS /* || cmd == HOST_CMD_QUEUE_POINT_INC */) &&
pointBuffer.remainingCapacity() < POINT_SIZE) {
return;
}
switch(cmd)
{
case HOST_CMD_QUEUE_POINT_ABS:
x = (long)cursor.read_32();
y = (long)cursor.read_32();
z = (long)cursor.read_32();
step_delay = (unsigned long)cursor.read_32();
queue_absolute_point(x, y, z, step_delay);
break;
case HOST_CMD_SET_POSITION:
// Belay until we're at a good location.
if (!is_point_buffer_empty()) { return; }
cli();
target_steps.x = current_steps.x = (long)cursor.read_32();
target_steps.y = current_steps.y = (long)cursor.read_32();
target_steps.z = current_steps.z = (long)cursor.read_32();
sei();
break;
case HOST_CMD_FIND_AXES_MINIMUM:
// Belay until we're at a good location.
if (!is_point_buffer_empty()) { return; }
//no dda interrupts.
disableTimer1Interrupt();
//which ones are we going to?
flags = cursor.read_8();
//find them!
seek_minimums(
flags & 1,
flags & 2,
flags & 4,
cursor.read_32(),
cursor.read_16());
//turn on point seeking agian.
enableTimer1Interrupt();
break;
case HOST_CMD_FIND_AXES_MAXIMUM:
// Belay until we're at a good location.
if (!is_point_buffer_empty()) { return; }
//no dda interrupts.
disableTimer1Interrupt();
//find them!
seek_maximums(
flags & 1,
flags & 2,
flags & 4,
cursor.read_32(),
cursor.read_16());
//turn on point seeking agian.
enableTimer1Interrupt();
break;
case HOST_CMD_DELAY:
// Belay until we're at a good location.
if (!is_point_buffer_empty()) { return; }
//take it easy.
delay(cursor.read_32());
break;
case HOST_CMD_CHANGE_TOOL:
// Belay until we're at a good location.
if (!is_point_buffer_empty()) { return; }
//extruder, i choose you!
select_tool(cursor.read_8());
break;
case HOST_CMD_WAIT_FOR_TOOL:
// Belay until we're at a good location.
if (!is_point_buffer_empty()) { return; }
//get your temp in gear, you lazy bum.
//what tool / timeout /etc?
currentToolIndex = cursor.read_8();
toolPingDelay = (unsigned int)cursor.read_16();
toolTimeout = (unsigned int)cursor.read_16();
//check to see if its ready now
if (!is_tool_ready(currentToolIndex))
{
//how often to ping?
toolNextPing = millis() + toolPingDelay;
toolTimeoutEnd = millis() + (toolTimeout * 1000);
//okay, put us in ping-tool-until-ready mode
commandMode = COMMAND_MODE_WAIT_FOR_TOOL;
}
break;
case HOST_CMD_TOOL_COMMAND:
// Belay until we're at a good location.
if (!is_point_buffer_empty()) { return; }
send_tool_command(cursor);
break;
case HOST_CMD_ENABLE_AXES:
// Belay until we're at a good location.
if (!is_point_buffer_empty()) { return; }
{
unsigned char param = cursor.read_8();
bool x = (param & 0x01) != 0;
bool y = (param & 0x02) != 0;
bool z = (param & 0x04) != 0;
if ((param & 0x80) != 0) {
// enable axes
enable_steppers(x,y,z);
} else {
// disable axes
disable_steppers(x,y,z);
}
}
default:
hostPacket.unsupported();
}
cursor.commit();
}
}
|