-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnano.cpp
477 lines (421 loc) · 10.4 KB
/
nano.cpp
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include "Marlin.h"
#include "cardreader.h"
#include "temperature.h"
#include "cardreader.h"
#include "stepper.h"
//#include "bsp_pin.h"
#include "mks_fastio.h"
#include "nano.h"
#include "mks_reprint.h"
#include "mks_cfg.h"
uint16_t blink_time = 0;
uint8_t print_key_flag = 0;
float z_height_stop = 0;
float temperature_protect_last = 0;
uint32_t protect_time = 0;
uint8_t print_pause = 0; //????????
uint32_t sys_time = 0;
uint8_t print_cancel_raise;
bool cancel_heatup;
extern bool volatile wait_for_heatup;
extern DATA_REPRINT_ITMES mksReprint;
void nano_sdcard_pause() {
if(mksReprint.mks_printer_state == MKS_REPRINTED) return;
card.pauseSDPrint();
print_job_timer.pause();
#if ENABLED(PARK_HEAD_ON_PAUSE)
enqueue_and_echo_commands_P(PSTR("M125"));
#endif
/*-----------pause begin---------------*/
//stepper.waitUntilEndOfAllBuffers();
mksReprint.mks_printer_state = MKS_PAUSING;
/*-----------pause end-----------------*/
}
void nano_sdcard_resume() {
mks_resumePrint();
#if ENABLED(PARK_HEAD_ON_PAUSE)
enqueue_and_echo_commands_P(PSTR("M24"));
#else
if(mksReprint.mks_printer_state != MKS_REPRINTING && mksReprint.mks_printer_state != MKS_REPRINTED)
{
card.startFileprint();
print_job_timer.start();
}
#endif
}
void nano_sdcard_stop() {
if(mksReprint.mks_printer_state == MKS_REPRINTED)
return;
mksReprint.mks_printer_state = MKS_IDLE;
epr_write_data(EPR_SAV_FLAG, (uint8_t *)&mksReprint.mks_printer_state,sizeof(mksReprint.mks_printer_state));
card.stopSDPrint();
clear_command_queue();
quickstop_stepper();
print_job_timer.stop();
thermalManager.disable_all_heaters();
#if FAN_COUNT > 0
for (uint8_t i = 0; i < FAN_COUNT; i++) fanSpeeds[i] = 0;
TIM1->CCR1 = 0 ;
#endif
wait_for_heatup = false;
// LCD_MESSAGEPGM(MSG_PRINT_ABORTED);
}
void NanoInit()
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4 , GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1|GPIO_PIN_2, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
}
//go home
void GoHomeCheck(void)
{
static int pin_time;
static int once=0;
if (card.sdprintflag == true||print_key_flag==2)
{
return;
}
if(!PRINT_HOME_PIN)
{
pin_time++;
}
else
{
once = 0;
pin_time = 0;
}
if(pin_time>32)
{
if(once==0)
{
once=1;
enqueue_and_echo_commands_P(PSTR("G28 X0 Y0 Z0"));
}
}
}
void BlinkLed(void)
{
static uint32_t blink_time_previous=0;
static uint32_t blink_time_start=0;
if(blink_time == 0) //OFF
{
PRINT_LED_PIN=1;//WRITE(PRINT_LED_PIN,1);
return;
}
if(blink_time > 3000) //ON
{
PRINT_LED_PIN=0;//WRITE(PRINT_LED_PIN,0);
return;
}
if(blink_time_previous!=blink_time)
{
blink_time_previous = blink_time;
blink_time_start = millis();
}
if(millis()<blink_time_start+blink_time)
{
PRINT_LED_PIN = 0;
//WRITE(PRINT_LED_PIN,0);
}
else if(millis()<blink_time_start+2*blink_time)
{
PRINT_LED_PIN = 1;
//WRITE(PRINT_LED_PIN,1);
}
else
{
blink_time_start = millis();
}
}
//Print key
void PrintOneKey(void)
{
static uint8_t key_status=0;
static uint32_t key_time = 0;
static uint8_t pause_flag = 0;
static uint8_t print_flag = 0;
if(key_status == 0)
{
if(!PRINT_PIN)//!READ(PRINT_PIN))
{
key_time = millis();
key_status = 1;
}
if(print_flag!=0 && card.sdprintflag == false)
{
BLINK_LED(LED_ON);
print_key_flag = 0;
print_flag = 0;
}
}
else if(key_status == 1)
{
if(key_time+30<millis())
{
if(!PRINT_PIN)//!READ(PRINT_PIN))
{
key_time = millis();
key_status = 2;
}
else
{
key_status = 0;
}
}
}
else if(key_status == 2)
{
if(PRINT_PIN)//READ(PRINT_PIN))
{
if(key_time + 1200 > millis()) //short press
{
if(print_key_flag == 0) //SD print
{
if(card.sdprinting == false)
{
print_flag = 1;
card.initsd();
if(card.cardOK==false)
{
BLINK_LED(LED_OFF);
key_status = 0;
key_time = 0;
return;
}
uint16_t filecnt = card.getnrfilenames();//card.getfilecount(card.path);
if(filecnt==0) return;
card.getfilename(filecnt); //card.getfilename(filecnt,card.path);
card.openFile(card.filename,true);
card.startFileprint();
BLINK_LED(LED_BLINK_2);
print_key_flag = 1;
}
}
else if(print_key_flag == 1) //pause
{
//MYSERIAL.print("pause");
BLINK_LED(LED_ON);
//card.pauseSDPrint();
nano_sdcard_pause();
print_pause = 1;
print_key_flag = 2;
}
else if(print_key_flag == 2) //back
{
//MYSERIAL.print("back");
if(temperature_protect_last > 60)
{
thermalManager.target_temperature[0]= temperature_protect_last;
temperature_protect_last = 0;
}
BLINK_LED(LED_BLINK_0);
//card.startFileprint();
nano_sdcard_resume();
print_pause = 0;
print_key_flag = 1;
}
else
{
print_key_flag = 0;
}
}
else
{
if(print_key_flag==0) //long press Z up 10mm
{
enqueue_and_echo_commands_P("G91");
enqueue_and_echo_commands_P("G0 Z+10 F600");
enqueue_and_echo_commands_P("G90");
}
else
{ if(wait_for_heatup)
{
wait_for_heatup=false;
}
//cancel_heatup = true; //disable heat
card.sdprinting = false;
card.sdprintflag = false;
//card.closefile();; // switch off all heaters.
nano_sdcard_stop();
//quickstop_stepper();//quickStop();
print_flag = 0;
BLINK_LED(LED_OFF);
}
//while(blocks_queued());
//disable_x();
//disable_y();
stepper.synchronize();
disable_X();
disable_Y();
print_key_flag = 0;
}
key_status = 0;
key_time = 0;
}
}
else
{
key_status = 0;
key_time = 0;
}
}
void feed_filament(void)
{
enqueue_and_echo_commands_P("G91");
enqueue_and_echo_commands_P("G0 E+100 F120");
enqueue_and_echo_commands_P("G90");
enqueue_and_echo_commands_P("M109 S210");
enqueue_and_echo_commands_P("M104 S0");
}
void retract_filament(void)
{
enqueue_and_echo_commands_P("G91");
enqueue_and_echo_commands_P("G0 E+25 F180");
enqueue_and_echo_commands_P("G0 E-120 F180");
enqueue_and_echo_commands_P("M109 S210");
enqueue_and_echo_commands_P("M104 S0");
}
void LoadFilament(void)
{
static uint32_t filament_time = 0;
static uint8_t filament_status=0;
static uint8_t filament_action=0;
if(card.sdprintflag==true)
{
return;
}
if(filament_status == 0) //
{
if(!RETRACT_PIN || !FEED_PIN )//(!READ(RETRACT_PIN))||(!READ(FEED_PIN)))
{
filament_status++;
filament_time = millis();
}
}
else if(filament_status == 1)
{
if(filament_time+20<millis())
{
if(!RETRACT_PIN || !FEED_PIN)//((!READ(RETRACT_PIN))||(!READ(FEED_PIN)))
{
//thermalManager.setTargetHotend0(195);
thermalManager.setTargetHotend(210, 0);
BLINK_LED(LED_BLINK_7);
protect_time = millis();
filament_status++;
}
else
{
filament_status = 0;
}
}
}
else if(filament_status == 2)
{
if(thermalManager.current_temperature[0] > 180)
{
filament_status++;
BLINK_LED(LED_BLINK_5);
}
if(RETRACT_PIN && FEED_PIN)//(READ(RETRACT_PIN))&&(READ(FEED_PIN)))
{
// cleaning_buffer_counter=2;
BLINK_LED(LED_ON);
filament_status = 0;
thermalManager.disable_all_heaters();
}
}
else if(filament_status == 3)
{
static uint8_t flag = 0;
if(!RETRACT_PIN)//!READ(RETRACT_PIN))
{
if(flag ==0)
{
retract_filament();
BLINK_LED(LED_BLINK_5);
flag = 1;
}
}
if(!FEED_PIN)//!READ(FEED_PIN))
{
if(flag ==0)
{
feed_filament();
BLINK_LED(LED_BLINK_5);
flag = 1;
}
}
if(RETRACT_PIN && FEED_PIN)//((READ(RETRACT_PIN))&&(READ(FEED_PIN)))
{
flag = 0;
filament_status = 0;
quickstop_stepper();
// cleaning_buffer_counter=2;
BLINK_LED(LED_ON);
thermalManager.disable_all_heaters();
}
}
else
{
filament_status = 0;
}
}
void heat_protect(void)
{
if(thermalManager.current_temperature[0] < 60)
{
return;
}
if((card.sdprintflag==false))
{
if(protect_time+60001 < millis())
{
protect_time = millis();
temperature_protect_last = thermalManager.target_temperature[0];
thermalManager.disable_all_heaters();
// disable_all_heaters();
// MYSERIAL.print("extruder heating off!");
}
}
else
{
if(protect_time != millis())
{
protect_time = millis();
}
}
}