This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathadvi3pp_handlers.cpp
3502 lines (2942 loc) · 101 KB
/
advi3pp_handlers.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Marlin 3D Printer Firmware For Wanhao Duplicator i3 Plus (ADVi3++)
*
* Copyright (C) 2017-2019 Sebastien Andrivet [https://github.com/andrivet/]
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "configuration_store.h"
#include "temperature.h"
#include "cardreader.h"
#include "planner.h"
#include "parser.h"
#include "duration_t.h"
#include "advi3pp.h"
#include "advi3pp_log.h"
#include "advi3pp_dgus.h"
#include "advi3pp_stack.h"
#include "advi3pp_versions.h"
#include "advi3pp_.h"
#include <HardwareSerial.h>
// --------------------------------------------------------------------
// From Marlin and Arduino
// --------------------------------------------------------------------
uint8_t progress_bar_percent;
int16_t lcd_contrast;
extern int freeMemory();
#ifdef ADVi3PP_PROBE
bool set_probe_deployed(bool);
float run_z_probe();
extern float zprobe_zoffset;
#endif
// --------------------------------------------------------------------
namespace
{
//! List of possible baudrates
constexpr uint32_t usb_baudrates[] = {9600, 19200, 38400, 57600, 115200, 230400, 250000};
//! Default preheat values
const advi3pp::Preset DEFAULT_PREHEAT_PRESET[advi3pp::Preheat::NB_PRESETS] = {
{180, 50, 0},
{200, 60, 0},
{220, 70, 0},
{180, 00, 0},
{200, 00, 0}
};
//! List of multipliers in Print Settings
const double PRINT_SETTINGS_MULTIPLIERS[] = {0.04, 0.08, 0.12};
#ifdef ADVi3PP_PROBE
//! List of multipliers in Z-height Tuning
const double SENSOR_Z_HEIGHT_MULTIPLIERS[] = {0.04, 0.12, 1.0};
//! Get the name of a sensor holder
//! @param index Index of the holder
//! @return The name (in Flash memory) of the holder
const FlashChar* get_sensor_name(size_t index)
{
// Note: F macro can be used only in a function, this is why this is coded like this
auto diicooler = F("Custom 1");
auto custom = F("Custom 2");
#if defined(ADVi3PP_MARK2)
auto mark2 = F("EZABL OEM");
static const FlashChar* names[advi3pp::SensorSettings::NB_SENSOR_POSITIONS] =
{mark2, diicooler, custom};
#elif defined(ADVi3PP_BLTOUCH)
auto baseggio = F("Indianagio Front");
static const FlashChar* names[advi3pp::SensorSettings::NB_SENSOR_POSITIONS] =
{baseggio, diicooler, custom};
#endif
assert(index < advi3pp::SensorSettings::NB_SENSOR_POSITIONS);
return names[index];
}
//! Default position of the sensor for the different holders
const advi3pp::SensorPosition DEFAULT_SENSOR_POSITION[advi3pp::SensorSettings::NB_SENSOR_POSITIONS] =
{
#if defined(ADVi3PP_MARK2)
{ -3500, -4000 }, // OEM
{ 0, 0 }, // Custom 1
{ 0, 0 } // Custom 2
#elif defined(ADVi3PP_BLTOUCH)
{ +150, -4270 }, // Baseggio/Indianagio Front
{ -2400, -3800 }, // Teaching Tech L. Side
{ 0, 0 } // Custom
#endif
};
#endif
//! List of digital pins for the Diagnosis page
const uint8_t diagnosis_digital_pins[] =
{
54, // PF0 / ADC0 - A0
24, // PA2 / AD2
23, // PA1 / AD1
6, // PH3 / OC4A
25, // PA3 / AD3
40, // PG1 / !RD
56, // PF2 / ADC2 - A2
36, // PC1 / A9
37, // PC0 / A8
34, // PC3 / A11
35, // PC2 / A10
32, // PC5 / A13
33, // PC4 / A12
};
//! List of analogic pins for the Diagnosis page
const uint8_t diagnosis_analog_pins[] = {55, 68, 54, 56}; // A1, A14, A0, A2
}
namespace advi3pp {
inline namespace singletons
{
extern Feature features;
extern Dimming dimming;
extern Graphs graphs;
Screens screens;
Wait wait;
Temperatures temperatures;
LoadUnload load_unload;
Preheat preheat;
Move move;
SdCard sd_card;
FactoryReset factory_reset;
ManualLeveling manual_leveling;
ExtruderTuning extruder_tuning;
PidTuning pid_tuning;
SensorSettings sensor_settings;
FirmwareSettings firmware_settings;
NoSensor no_sensor;
LcdSettings lcd_settings;
Statistics statistics;
Versions versions;
PrintSettings print_settings;
PidSettings pid_settings;
StepSettings steps_settings;
FeedrateSettings feedrates_settings;
AccelerationSettings accelerations_settings;
JerkSettings jerks_settings;
Copyrights copyrights;
AutomaticLeveling automatic_leveling;
LevelingGrid leveling_grid;
SensorTuning sensor_tuning;
SensorZHeight sensor_z_height;
ChangeFilament change_filament;
EepromMismatch eeprom_mismatch;
VersionsMismatch versions_mismatch;
Sponsors sponsors;
LinearAdvanceTuning linear_advance_tuning;
LinearAdvanceSettings linear_advance_settings;
Diagnosis diagnosis;
Print print;
AdvancedPause pause;
}
// --------------------------------------------------------------------
// Pages management
// --------------------------------------------------------------------
//! Show the given page on the LCD screen
//! @param [in] page The page to be displayed on the LCD screen
void Pages::show_page(Page page, ShowOptions options)
{
Log::log() << F("Show page ") << static_cast<uint8_t>(page) << Log::endl();
if(test_one_bit(options, ShowOptions::SaveBack))
{
auto current = get_current_page();
Log::log() << F("Save back page ") << static_cast<uint8_t>(current) << Log::endl();
back_pages_.push(current);
}
WriteRegisterDataRequest frame{Register::PictureID};
frame << 00_u8 << page;
frame.send(true);
current_page_ = page;
}
//! Retrieve the current page on the LCD screen
Page Pages::get_current_page()
{
// Boot page switches automatically (animation) to the Main page
return current_page_ == Page::Boot ? Page::Main : current_page_;
}
//! Set page to display after the completion of an operation.
void Pages::save_forward_page()
{
auto current = get_current_page();
Log::log() << F("Save forward page ") << static_cast<uint8_t>(current) << Log::endl();
forward_page_ = current;
}
//! Show the "Back" page on the LCD display.
void Pages::show_back_page()
{
forward_page_ = Page::None;
if(back_pages_.is_empty())
{
Log::log() << F("No back page, show Main") << Log::endl();
show_page(Page::Main, ShowOptions::None);
return;
}
auto back = back_pages_.pop();
Log::log() << F("Pop back page ") << static_cast<uint8_t>(back) << Log::endl();
show_page(back, ShowOptions::None);
}
//! Show the "Next" page on the LCD display.
void Pages::show_forward_page()
{
if(forward_page_ == Page::None)
{
show_back_page();
return;
}
if(!back_pages_.contains(forward_page_))
{
Log::error() << F("Back pages do not contain forward page ") << static_cast<uint8_t>(forward_page_) << Log::endl();
return;
}
while(!back_pages_.is_empty())
{
Page back_page = back_pages_.pop();
Log::log() << F("Pop back page ") << static_cast<uint8_t>(back_page) << Log::endl();
if(back_page == forward_page_)
{
Log::log() << F("Show forward page ") << static_cast<uint8_t>(forward_page_) << Log::endl();
show_page(forward_page_, ShowOptions::None);
forward_page_ = Page::None;
return;
}
}
}
// --------------------------------------------------------------------
// Screens
// --------------------------------------------------------------------
//! Dispatch a key value to the right handler
//! @param key_value The sub-action to handle
//! @return True if the action was handled
bool Screens::do_dispatch(KeyValue key_value)
{
// Do not call Parent::do_dispatch
switch(key_value)
{
case KeyValue::Temps: show_temps(); break;
case KeyValue::Print: show_print(); break;
case KeyValue::Controls: pages.show_page(Page::Controls); break;
case KeyValue::Tuning: pages.show_page(Page::Tuning); break;
case KeyValue::Settings: pages.show_page(Page::Settings); break;
case KeyValue::Infos: pages.show_page(Page::Infos); break;
case KeyValue::Motors: pages.show_page(Page::MotorsSettings); break;
case KeyValue::Leveling: pages.show_page(Page::Leveling); break;
case KeyValue::PrintSettings: show_print_settings(); break;
case KeyValue::Back: back_command(); break;
default: return false;
}
return true;
}
//! Show one of the temperature graph screens depending of the context: either the SD printing screen,
//! the printing screen or the temperature screen.
void Screens::show_temps()
{
if(!PrintCounter::isRunning() && !PrintCounter::isPaused())
{
temperatures.show();
return;
}
// If there is a print running (or paused), display the print screen.
pages.show_page(Page::Print);
}
//! Show Print Settings page (only if a print is running or paused)
void Screens::show_print_settings()
{
if(!PrintCounter::isRunning() && !PrintCounter::isPaused())
{
temperatures.show();
return;
}
// If there is a print running (or paused), display the print settings.
print_settings.show(ShowOptions::SaveBack);
}
//! Show one of the Printing screens depending of the context:
//! - If a print is running, display the Print screen
//! - Otherwise, try to access the SD card. Depending of the result, display the SD card Page or the Temperatures page
void Screens::show_print()
{
// If there is a print running (or paused), display the SD or USB print screen
if(PrintCounter::isRunning() || PrintCounter::isPaused())
{
pages.show_page(Page::Print);
return;
}
wait.show(F("Try to access the SD card..."));
task.set_background_task(BackgroundTask{this, &Screens::show_sd_or_temp_page});
}
//! Show the SD card page (if a SD card is inserted) or the Temperature page
void Screens::show_sd_or_temp_page()
{
task.clear_background_task();
card.initsd(); // Can take some time
advi3pp.reset_status();
if(!card.cardOK)
{
// SD card not accessible so fall back to Temperatures
temperatures.show(ShowOptions::None);
return;
}
sd_card.show(ShowOptions::None);
}
// --------------------------------------------------------------------
// Wait
// --------------------------------------------------------------------
//! Prepare the page before being displayed and return the right Page value
//! @return The index of the page to display
Page Wait::do_prepare_page()
{
return Page::Waiting;
}
//! Show a simple wait page with a message
//! @param message The message to display
//! @param options Options when displaying the page (i.e. save the current page or not)
void Wait::show(const FlashChar* message, ShowOptions options)
{
advi3pp.set_status(message);
back_ = nullptr;
continue_ = nullptr;
pages.show_page(Page::Waiting, options);
}
//! Show a simple wait page with a message
//! @param message The message to display
//! @param back Callback to be called when the back button is pressed
//! @param options Options when displaying the page (i.e. save the current page or not)
void Wait::show(const FlashChar* message, const WaitCallback& back, ShowOptions options)
{
advi3pp.set_status(message);
back_ = back;
continue_ = nullptr;
pages.show_page(Page::WaitBack, options);
}
//! Show a simple wait page with a message
//! @param message The message to display
//! @param back Callback to be called when the back button is pressed
//! @param cont Callback to be called when the continue button is pressed
//! @param options Options when displaying the page (i.e. save the current page or not)
void Wait::show(const FlashChar* message, const WaitCallback& back, const WaitCallback& cont, ShowOptions options)
{
advi3pp.set_status(message);
back_ = back;
continue_ = cont;
pages.show_page(Page::WaitBackContinue, options);
}
//! Show a simple wait page with a message
//! @param message The message to display
//! @param cont Callback to be called when the continue button is pressed
//! @param options Options when displaying the page (i.e. save the current page or not)
void Wait::show_continue(const FlashChar* message, const WaitCallback& cont, ShowOptions options)
{
advi3pp.set_status(message);
back_ = nullptr;
continue_ = cont;
pages.show_page(Page::WaitContinue, options);
}
//! Show a simple wait page with a message
//! @param message The message to display
//! @param options Options when displaying the page (i.e. save the current page or not)
void Wait::show_continue(const FlashChar* message, ShowOptions options)
{
advi3pp.set_status(message);
back_ = nullptr;
continue_ = WaitCallback{this, &Wait::on_continue};
pages.show_page(Page::WaitContinue, options);
}
//! Show a simple wait page without a message
//! @param message The message to display
//! @param options Options when displaying the page (i.e. save the current page or not)
void Wait::show_continue(ShowOptions options)
{
back_ = nullptr;
continue_ = WaitCallback{this, &Wait::on_continue};
advi3pp.buzz();
pages.show_page(Page::WaitContinue, options);
}
//! Default action when the continue button is pressed (inform Marlin)
bool Wait::on_continue()
{
::wait_for_user = false;
return false;
}
//! Handles the Back command
void Wait::do_back_command()
{
bool continue_processing = true;
if(!back_)
Log::error() << F("No Back action defined") << Log::endl();
else
{
continue_processing = back_();
back_ = nullptr;
}
if(continue_processing)
Parent::do_back_command();
}
//! Handles the Save (Continue) command
void Wait::do_save_command()
{
bool continue_processing = true;
if(!continue_)
Log::error() << F("No Continue action defined") << Log::endl();
else
{
continue_processing = continue_();
continue_ = nullptr;
}
if(continue_processing)
pages.show_forward_page();
}
// --------------------------------------------------------------------
// Temperatures Graph
// --------------------------------------------------------------------
//! Prepare the page before being displayed and return the right Page value
//! @return The index of the page to display
Page Temperatures::do_prepare_page()
{
return Page::Temperature;
}
//! Show the temperature page and record and action to be executed when the back button is pressed
//! @param back Action to be executed when the back button is pressed
void Temperatures::show(const WaitCallback& back)
{
back_ = back;
Parent::show(ShowOptions::SaveBack);
}
//! Show the temperature page
//! @param options Options when displaying the page (i.e. save the current page or not)
void Temperatures::show(ShowOptions options)
{
back_ = nullptr;
Parent::show(options);
}
//! Execute the Back command
void Temperatures::do_back_command()
{
if(back_)
{
back_();
back_ = nullptr;
}
Parent::do_back_command();
}
// --------------------------------------------------------------------
// Load and Unload Filament
// --------------------------------------------------------------------
//! Handle Load & Unload actions.
//! @param key_value The sub-action to handle
//! @return True if the action was handled
bool LoadUnload::do_dispatch(KeyValue key_value)
{
if(Parent::do_dispatch(key_value))
return true;
switch(key_value)
{
case KeyValue::Load: load_command(); break;
case KeyValue::Unload: unload_command(); break;
default: return false;
}
return true;
}
//! Prepare the page before being displayed and return the right Page value
//! @return The index of the page to display
Page LoadUnload::do_prepare_page()
{
WriteRamDataRequest frame{Variable::Value0};
frame << Uint16(advi3pp.get_last_used_temperature(TemperatureKind::Hotend));
frame.send();
return Page::LoadUnload;
}
//! Prepare Load or Unload step #1: set the target temperature, setup the next step and display a wait message
//! @param background Background task to detect if it is time for step #2
void LoadUnload::prepare(const BackgroundTask& background)
{
ReadRamData frame{Variable::Value0, 1};
if(!frame.send_and_receive())
{
Log::error() << F("Receiving Frame (Target Temperature)") << Log::endl();
return;
}
Uint16 hotend; frame >> hotend;
Temperature::setTargetHotend(hotend.word, 0);
enqueue_and_echo_commands_P(PSTR("M83")); // relative E mode
enqueue_and_echo_commands_P(PSTR("G92 E0")); // reset E axis
task.set_background_task(background);
wait.show(F("Wait until the target temp is reached..."), WaitCallback{this, &LoadUnload::stop});
}
//! Start Load action.
void LoadUnload::load_command()
{
prepare(BackgroundTask(this, &LoadUnload::load_start_task));
}
//! Start Unload action.
void LoadUnload::unload_command()
{
prepare(BackgroundTask(this, &LoadUnload::unload_start_task));
}
//! Handle back from the Load or Unload LCD screen: stop the process.
//! @return true to continue Back processing
bool LoadUnload::stop()
{
Log::log() << F("Load/Unload Stop") << Log::endl();
advi3pp.reset_status();
task.set_background_task(BackgroundTask(this, &LoadUnload::stop_task));
clear_command_queue();
Temperature::setTargetHotend(0, 0);
return true;
}
//! Check if the process is actually stopped and reset E axis
void LoadUnload::stop_task()
{
if(advi3pp.is_busy() || !task.has_background_task())
return;
task.clear_background_task();
// Do this asynchronously to avoid race conditions
enqueue_and_echo_commands_P(PSTR("M82")); // absolute E mode
enqueue_and_echo_commands_P(PSTR("G92 E0")); // reset E axis
}
//! Check if the target temperature is reached and in this case, do step #2: extrude or unextrude
//! @param command Command to extrude or unextrude
//! @param back_task Background task to detect if the target temperature is reached and in this case, do step #2
void LoadUnload::start_task(const char* command, const BackgroundTask& back_task)
{
if(Temperature::current_temperature[0] >= Temperature::target_temperature[0] - 10)
{
Log::log() << F("Load/Unload Filament") << Log::endl();
advi3pp.buzz(); // Inform the user that the extrusion starts
enqueue_and_echo_commands_P(command);
task.set_background_task(back_task);
advi3pp.set_status(F("Press Back when the filament comes out..."));
}
}
//! Load the filament if the temperature is high enough.
void LoadUnload::load_start_task()
{
start_task(PSTR("G1 E1 F120"), BackgroundTask(this, &LoadUnload::load_task));
}
//! Load the filament if the temperature is high enough.
void LoadUnload::load_task()
{
if(Temperature::current_temperature[0] >= Temperature::target_temperature[0] - 10)
enqueue_and_echo_commands_P(PSTR("G1 E1 F120"));
}
//! Unload the filament if the temperature is high enough.
void LoadUnload::unload_start_task()
{
start_task(PSTR("G1 E-1 F120"), BackgroundTask(this, &LoadUnload::unload_task));
}
//! Unload the filament if the temperature is high enough.
void LoadUnload::unload_task()
{
if(Temperature::current_temperature[0] >= Temperature::target_temperature[0] - 10)
enqueue_and_echo_commands_P(PSTR("G1 E-1 F120"));
}
// --------------------------------------------------------------------
// Preheat & Cooldown
// --------------------------------------------------------------------
//! Handle Preheat actions.
//! @param key_value Sub-action to handle
//! @return True if the action was handled
bool Preheat::do_dispatch(KeyValue key_value)
{
if(Parent::do_dispatch(key_value))
return true;
switch(key_value)
{
case KeyValue::PresetPrevious: previous_command(); break;
case KeyValue::PresetNext: next_command(); break;
case KeyValue::Cooldown: cooldown_command(); break;
default: return false;
}
return true;
}
//! Store presets in permanent memory (EEPROM)
//! @param eeprom EEPROM writer
void Preheat::do_write(EepromWrite& eeprom) const
{
for(auto& preset: presets_)
{
eeprom.write(preset.hotend);
eeprom.write(preset.bed);
}
}
//! Restore presets from permanent memory (EEPROM).
//! @param eeprom EEPROM reader
void Preheat::do_read(EepromRead& eeprom)
{
for(auto& preset: presets_)
{
eeprom.read(preset.hotend);
eeprom.read(preset.bed);
}
}
//! Reset presets.
void Preheat::do_reset()
{
for(size_t i = 0; i < NB_PRESETS; ++i)
{
presets_[i].hotend = DEFAULT_PREHEAT_PRESET[i].hotend;
presets_[i].bed = DEFAULT_PREHEAT_PRESET[i].bed;
presets_[i].fan = DEFAULT_PREHEAT_PRESET[i].fan;
}
}
//! Return the amount of data (in bytes) necessary to save settings in permanent memory (EEPROM).
//! @return Number of bytes
uint16_t Preheat::do_size_of() const
{
return NB_PRESETS * (sizeof(Preset::hotend) + sizeof(Preset::bed));
}
//! Send the presets t the LCD Panel
void Preheat::send_presets()
{
Log::log() << F("Preheat page") << Log::endl();
WriteRamDataRequest frame{Variable::Value0};
frame << Uint16(presets_[index_].hotend)
<< Uint16(presets_[index_].bed)
<< Uint16(presets_[index_].fan);
frame.send();
ADVString<8> preset;
preset << index_ + 1 << F(" / ") << NB_PRESETS;
frame.reset(Variable::ShortText0);
frame << preset;
frame.send();
}
//! Retrieve presets values from the LCD Panel
void Preheat::retrieve_presets()
{
ReadRamData frame{Variable::Value0, 3};
if(!frame.send_and_receive())
{
Log::error() << F("Error receiving presets") << Log::endl();
return;
}
Uint16 hotend, bed, fan;
frame >> hotend >> bed >> fan;
presets_[index_].hotend = hotend.word;
presets_[index_].bed = bed.word;
presets_[index_].fan = fan.word;
}
//! Prepare the page before being displayed and return the right Page value
//! @return The index of the page to display
Page Preheat::do_prepare_page()
{
send_presets();
return Page::Preheat;
}
//! Handle Previous command
void Preheat::previous_command()
{
if(index_ <= 0)
return;
retrieve_presets();
--index_;
send_presets();
}
//! Handle Next command
void Preheat::next_command()
{
if(index_ >= NB_PRESETS - 1)
return;
retrieve_presets();
++index_;
send_presets();
}
//! Handles the Save (Continue) command
void Preheat::do_save_command()
{
retrieve_presets();
const Preset& preset = presets_[index_];
ADVString<15> command;
command = F("M104 S"); command << preset.hotend;
enqueue_and_echo_command(command.get());
command = F("M140 S"); command << preset.bed;
enqueue_and_echo_command(command.get());
command = F("M106 S"); command << scale(preset.fan, 100, 255);
enqueue_and_echo_command(command.get());
advi3pp.save_settings();
temperatures.show(ShowOptions::None);
}
//! Cooldown the bed and the nozzle, turn off the fan
void Preheat::cooldown_command()
{
Log::log() << F("Cooldown") << Log::endl();
advi3pp.reset_status();
Temperature::disable_all_heaters();
enqueue_and_echo_commands_P(PSTR("M106 S0")); // Turn off fan
}
// --------------------------------------------------------------------
// Move & Home
// --------------------------------------------------------------------
//! Execute a move command
//! @param key_value The sub-action to handle
//! @return True if the action was handled
bool Move::do_dispatch(KeyValue key_value)
{
if(Parent::do_dispatch(key_value))
return true;
switch(key_value)
{
case KeyValue::MoveXHome: x_home_command(); break;
case KeyValue::MoveYHome: y_home_command(); break;
case KeyValue::MoveZHome: z_home_command(); break;
case KeyValue::MoveAllHome: all_home_command(); break;
case KeyValue::DisableMotors: disable_motors_command(); break;
default: return false;
}
return true;
}
//! Prepare the page before being displayed and return the right Page value
//! @return The index of the page to display
Page Move::do_prepare_page()
{
Planner::finish_and_disable(); // To circumvent homing problems
return Page::Move;
}
//! Move the nozzle. Check that the command is not send too early when multiple move commands are send in a short time
//! (i.e. when the user keep the button presses)
//! @params command Actual command to move the nozzle.
void Move::move(const char* command, millis_t delay)
{
if(!ELAPSED(millis(), last_move_time_ + delay))
return;
enqueue_and_echo_commands_P(PSTR("G91"));
enqueue_and_echo_commands_P(command);
enqueue_and_echo_commands_P(PSTR("G90"));
last_move_time_ = millis();
}
//! Move the nozzle (+X)
void Move::x_plus_command()
{
move(PSTR("G1 X4 F1000"), 150);
}
//! Move the nozzle (-X)
void Move::x_minus_command()
{
move(PSTR("G1 X-4 F1000"), 150);
}
//! Move the nozzle (+Y)
void Move::y_plus_command()
{
move(PSTR("G1 Y4 F1000"), 150);
}
//! Move the nozzle (-Y)
void Move::y_minus_command()
{
move(PSTR("G1 Y-4 F1000"), 150);
}
//! Move the nozzle (+Z)
void Move::z_plus_command()
{
move(PSTR("G1 Z0.5 F240"), 10);
}
//! Move the nozzle (-Z)
void Move::z_minus_command()
{
move(PSTR("G1 Z-0.5 F240"), 10);
}
//! Extrude some filament.
void Move::e_plus_command()
{
if(Temperature::degHotend(0) < 180)
return;
clear_command_queue();
enqueue_and_echo_commands_P(PSTR("G91"));
enqueue_and_echo_commands_P(PSTR("G1 E1 F120"));
enqueue_and_echo_commands_P(PSTR("G90"));
}
//! Unextrude some filament.
void Move::e_minus_command()
{
if(Temperature::degHotend(0) < 180)
return;
clear_command_queue();
enqueue_and_echo_commands_P(PSTR("G91"));
enqueue_and_echo_commands_P(PSTR("G1 E-1 F120"));
enqueue_and_echo_commands_P(PSTR("G90"));
}
//! Disable the motors.
void Move::disable_motors_command()
{
enqueue_and_echo_commands_P(PSTR("M84"));
axis_homed = 0;
axis_known_position = 0;
}
//! Go to home on the X axis.
void Move::x_home_command()
{
enqueue_and_echo_commands_P(PSTR("G28 X F6000"));
}
//! Go to home on the Y axis.
void Move::y_home_command()
{
enqueue_and_echo_commands_P(PSTR("G28 Y F6000"));
}
//! Go to home on the Z axis.
void Move::z_home_command()
{
enqueue_and_echo_commands_P(PSTR("G28 Z F6000"));
}
//! Go to home on all axis.
void Move::all_home_command()
{
enqueue_and_echo_commands_P(PSTR("G28 F6000"));
}
// --------------------------------------------------------------------
// Sensor Tuning
// --------------------------------------------------------------------
#ifdef ADVi3PP_PROBE
//! Execute a Sensor Tuning command
//! @param key_value The sub-action to handle
//! @return True if the action was handled
bool SensorTuning::do_dispatch(KeyValue key_value)
{
if(Parent::do_dispatch(key_value))
return true;
switch(key_value)
{
case KeyValue::SensorSelfTest: self_test_command(); break;
case KeyValue::SensorReset: reset_command(); break;
case KeyValue::SensorDeploy: deploy_command(); break;
case KeyValue::SensorStow: stow_command(); break;
default: return false;
}
return true;
}
//! Prepare the page before being displayed and return the right Page value
//! @return The index of the page to display
Page SensorTuning::do_prepare_page()
{
return Page::SensorTuning;
}
//! Execute the sensor Self-test command
void SensorTuning::self_test_command()
{
enqueue_and_echo_commands_P(PSTR("M280 P0 S120"));
}
//! Execute the sensor Reset command
void SensorTuning::reset_command()
{
enqueue_and_echo_commands_P(PSTR("M280 P0 S160"));
}
//! Execute the sensor Deploy command
void SensorTuning::deploy_command()
{
enqueue_and_echo_commands_P(PSTR("M280 P0 S10"));
}
//! Execute the sensor Stow command
void SensorTuning::stow_command()
{
enqueue_and_echo_commands_P(PSTR("M280 P0 S90"));
}