Marlin_gebruikte_dual_ballspindle_elecnieuw.ino 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  1. /*
  2. Reprap firmware based on Sprinter and grbl.
  3. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. This firmware is a mashup between Sprinter and grbl.
  17. (https://github.com/kliment/Sprinter)
  18. (https://github.com/simen/grbl/tree)
  19. It has preliminary support for Matthew Roberts advance algorithm
  20. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  21. */
  22. #include "Marlin.h"
  23. #include "ultralcd.h"
  24. #include "planner.h"
  25. #include "stepper.h"
  26. #include "temperature.h"
  27. #include "motion_control.h"
  28. #include "cardreader.h"
  29. #include "watchdog.h"
  30. #include "EEPROMwrite.h"
  31. #include "language.h"
  32. #include "pins_arduino.h"
  33. #define VERSION_STRING "1.0.0 RC2"
  34. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  35. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  36. //Implemented Codes
  37. //-------------------
  38. // G0 -> G1
  39. // G1 - Coordinated Movement X Y Z E
  40. // G2 - CW ARC
  41. // G3 - CCW ARC
  42. // G4 - Dwell S<seconds> or P<milliseconds>
  43. // G10 - retract filament according to settings of M207
  44. // G11 - retract recover filament according to settings of M208
  45. // G28 - Home all Axis
  46. // G90 - Use Absolute Coordinates
  47. // G91 - Use Relative Coordinates
  48. // G92 - Set current position to cordinates given
  49. //RepRap M Codes
  50. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  51. // M1 - Same as M0
  52. // M104 - Set extruder target temp
  53. // M105 - Read current temp
  54. // M106 - Fan on
  55. // M107 - Fan off
  56. // M109 - Wait for extruder current temp to reach target temp.
  57. // M114 - Display current position
  58. //Custom M Codes
  59. // M17 - Enable/Power all stepper motors
  60. // M18 - Disable all stepper motors; same as M84
  61. // M20 - List SD card
  62. // M21 - Init SD card
  63. // M22 - Release SD card
  64. // M23 - Select SD file (M23 filename.g)
  65. // M24 - Start/resume SD print
  66. // M25 - Pause SD print
  67. // M26 - Set SD position in bytes (M26 S12345)
  68. // M27 - Report SD print status
  69. // M28 - Start SD write (M28 filename.g)
  70. // M29 - Stop SD write
  71. // M30 - Delete file from SD (M30 filename.g)
  72. // M31 - Output time since last M109 or SD card start to serial
  73. // M42 - Change pin status via gcode
  74. // M80 - Turn on Power Supply
  75. // M81 - Turn off Power Supply
  76. // M82 - Set E codes absolute (default)
  77. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  78. // M84 - Disable steppers until next move,
  79. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  80. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  81. // M92 - Set axis_steps_per_unit - same syntax as G92
  82. // M114 - Output current position to serial port
  83. // M115 - Capabilities string
  84. // M117 - display message
  85. // M119 - Output Endstop status to serial port
  86. // M140 - Set bed target temp
  87. // M190 - Wait for bed current temp to reach target temp.
  88. // M200 - Set filament diameter
  89. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  90. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  91. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  92. // M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) im mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer underruns and M20 minimum feedrate
  93. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk, E=maximum E jerk
  94. // M206 - set additional homeing offset
  95. // M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  96. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  97. // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  98. // M220 S<factor in percent>- set speed factor override percentage
  99. // M221 S<factor in percent>- set extrude factor override percentage
  100. // M240 - Trigger a camera to take a photograph
  101. // M301 - Set PID parameters P I and D
  102. // M302 - Allow cold extrudes
  103. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  104. // M400 - Finish all moves
  105. // M500 - stores paramters in EEPROM
  106. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  107. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  108. // M503 - print the current settings (from memory not from eeprom)
  109. // M999 - Restart after being stopped by error
  110. //Stepper Movement Variables
  111. //===========================================================================
  112. //=============================imported variables============================
  113. //===========================================================================
  114. //===========================================================================
  115. //=============================public variables=============================
  116. //===========================================================================
  117. #ifdef SDSUPPORT
  118. CardReader card;
  119. #endif
  120. float homing_feedrate[] = HOMING_FEEDRATE;
  121. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  122. volatile int feedmultiply=100; //100->1 200->2
  123. int saved_feedmultiply;
  124. volatile bool feedmultiplychanged=false;
  125. volatile int extrudemultiply=100; //100->1 200->2
  126. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  127. float add_homeing[3]={0,0,0};
  128. uint8_t active_extruder = 0;
  129. unsigned char FanSpeed=0;
  130. #ifdef FWRETRACT
  131. bool autoretract_enabled=true;
  132. bool retracted=false;
  133. float retract_length=3, retract_feedrate=17*60, retract_zlift=0.8;
  134. float retract_recover_length=0, retract_recover_feedrate=8*60;
  135. #endif
  136. //===========================================================================
  137. //=============================private variables=============================
  138. //===========================================================================
  139. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  140. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  141. static float offset[3] = {0.0, 0.0, 0.0};
  142. static bool home_all_axis = true;
  143. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  144. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  145. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  146. static bool relative_mode_e = false; //Determines Absolute or Relative E Codes while in Absolute Coordinates mode. E is always relative in Relative Coordinates mode.
  147. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  148. static bool fromsd[BUFSIZE];
  149. static int bufindr = 0;
  150. static int bufindw = 0;
  151. static int buflen = 0;
  152. //static int i = 0;
  153. static char serial_char;
  154. static int serial_count = 0;
  155. static boolean comment_mode = false;
  156. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  157. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  158. //static float tt = 0;
  159. //static float bt = 0;
  160. //Inactivity shutdown variables
  161. static unsigned long previous_millis_cmd = 0;
  162. static unsigned long max_inactive_time = 0;
  163. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  164. static unsigned long starttime=0;
  165. static unsigned long stoptime=0;
  166. static uint8_t tmp_extruder;
  167. bool Stopped=false;
  168. //===========================================================================
  169. //=============================ROUTINES=============================
  170. //===========================================================================
  171. void get_arc_coordinates();
  172. extern "C"{
  173. extern unsigned int __bss_end;
  174. extern unsigned int __heap_start;
  175. extern void *__brkval;
  176. int freeMemory() {
  177. int free_memory;
  178. if((int)__brkval == 0)
  179. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  180. else
  181. free_memory = ((int)&free_memory) - ((int)__brkval);
  182. return free_memory;
  183. }
  184. }
  185. //adds an command to the main command buffer
  186. //thats really done in a non-safe way.
  187. //needs overworking someday
  188. void enquecommand(const char *cmd)
  189. {
  190. if(buflen < BUFSIZE)
  191. {
  192. //this is dangerous if a mixing of serial and this happsens
  193. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  194. SERIAL_ECHO_START;
  195. SERIAL_ECHOPGM("enqueing \"");
  196. SERIAL_ECHO(cmdbuffer[bufindw]);
  197. SERIAL_ECHOLNPGM("\"");
  198. bufindw= (bufindw + 1)%BUFSIZE;
  199. buflen += 1;
  200. }
  201. }
  202. void setup_photpin()
  203. {
  204. #ifdef PHOTOGRAPH_PIN
  205. #if (PHOTOGRAPH_PIN > -1)
  206. SET_OUTPUT(PHOTOGRAPH_PIN);
  207. WRITE(PHOTOGRAPH_PIN, LOW);
  208. #endif
  209. #endif
  210. }
  211. void setup_powerhold()
  212. {
  213. #ifdef SUICIDE_PIN
  214. #if (SUICIDE_PIN> -1)
  215. SET_OUTPUT(SUICIDE_PIN);
  216. WRITE(SUICIDE_PIN, HIGH);
  217. #endif
  218. #endif
  219. }
  220. void suicide()
  221. {
  222. #ifdef SUICIDE_PIN
  223. #if (SUICIDE_PIN> -1)
  224. SET_OUTPUT(SUICIDE_PIN);
  225. WRITE(SUICIDE_PIN, LOW);
  226. #endif
  227. #endif
  228. }
  229. void setup()
  230. {
  231. setup_powerhold();
  232. MYSERIAL.begin(BAUDRATE);
  233. SERIAL_PROTOCOLLNPGM("start");
  234. SERIAL_ECHO_START;
  235. // Check startup - does nothing if bootloader sets MCUSR to 0
  236. byte mcu = MCUSR;
  237. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  238. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  239. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  240. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  241. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  242. MCUSR=0;
  243. SERIAL_ECHOPGM(MSG_MARLIN);
  244. SERIAL_ECHOLNPGM(VERSION_STRING);
  245. #ifdef STRING_VERSION_CONFIG_H
  246. #ifdef STRING_CONFIG_H_AUTHOR
  247. SERIAL_ECHO_START;
  248. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  249. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  250. SERIAL_ECHOPGM(MSG_AUTHOR);
  251. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  252. #endif
  253. #endif
  254. SERIAL_ECHO_START;
  255. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  256. SERIAL_ECHO(freeMemory());
  257. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  258. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  259. for(int8_t i = 0; i < BUFSIZE; i++)
  260. {
  261. fromsd[i] = false;
  262. }
  263. EEPROM_RetrieveSettings(); // loads data from EEPROM if available
  264. for(int8_t i=0; i < NUM_AXIS; i++)
  265. {
  266. axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
  267. }
  268. tp_init(); // Initialize temperature loop
  269. plan_init(); // Initialize planner;
  270. st_init(); // Initialize stepper;
  271. wd_init();
  272. setup_photpin();
  273. LCD_INIT;
  274. }
  275. void loop()
  276. {
  277. if(buflen < (BUFSIZE-1))
  278. get_command();
  279. #ifdef SDSUPPORT
  280. card.checkautostart(false);
  281. #endif
  282. if(buflen)
  283. {
  284. #ifdef SDSUPPORT
  285. if(card.saving)
  286. {
  287. if(strstr(cmdbuffer[bufindr],"M29") == NULL)
  288. {
  289. card.write_command(cmdbuffer[bufindr]);
  290. SERIAL_PROTOCOLLNPGM(MSG_OK);
  291. }
  292. else
  293. {
  294. card.closefile();
  295. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  296. }
  297. }
  298. else
  299. {
  300. process_commands();
  301. }
  302. #else
  303. process_commands();
  304. #endif //SDSUPPORT
  305. buflen = (buflen-1);
  306. bufindr = (bufindr + 1)%BUFSIZE;
  307. }
  308. //check heater every n milliseconds
  309. manage_heater();
  310. manage_inactivity(1);
  311. checkHitEndstops();
  312. LCD_STATUS;
  313. }
  314. void get_command()
  315. {
  316. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  317. serial_char = MYSERIAL.read();
  318. if(serial_char == '\n' ||
  319. serial_char == '\r' ||
  320. (serial_char == ':' && comment_mode == false) ||
  321. serial_count >= (MAX_CMD_SIZE - 1) )
  322. {
  323. if(!serial_count) { //if empty line
  324. comment_mode = false; //for new command
  325. return;
  326. }
  327. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  328. if(!comment_mode){
  329. comment_mode = false; //for new command
  330. fromsd[bufindw] = false;
  331. if(strstr(cmdbuffer[bufindw], "N") != NULL)
  332. {
  333. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  334. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  335. if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
  336. SERIAL_ERROR_START;
  337. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  338. SERIAL_ERRORLN(gcode_LastN);
  339. //Serial.println(gcode_N);
  340. FlushSerialRequestResend();
  341. serial_count = 0;
  342. return;
  343. }
  344. if(strstr(cmdbuffer[bufindw], "*") != NULL)
  345. {
  346. byte checksum = 0;
  347. byte count = 0;
  348. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  349. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  350. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  351. SERIAL_ERROR_START;
  352. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  353. SERIAL_ERRORLN(gcode_LastN);
  354. FlushSerialRequestResend();
  355. serial_count = 0;
  356. return;
  357. }
  358. //if no errors, continue parsing
  359. }
  360. else
  361. {
  362. SERIAL_ERROR_START;
  363. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  364. SERIAL_ERRORLN(gcode_LastN);
  365. FlushSerialRequestResend();
  366. serial_count = 0;
  367. return;
  368. }
  369. gcode_LastN = gcode_N;
  370. //if no errors, continue parsing
  371. }
  372. else // if we don't receive 'N' but still see '*'
  373. {
  374. if((strstr(cmdbuffer[bufindw], "*") != NULL))
  375. {
  376. SERIAL_ERROR_START;
  377. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  378. SERIAL_ERRORLN(gcode_LastN);
  379. serial_count = 0;
  380. return;
  381. }
  382. }
  383. if((strstr(cmdbuffer[bufindw], "G") != NULL)){
  384. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  385. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  386. case 0:
  387. case 1:
  388. case 2:
  389. case 3:
  390. if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
  391. #ifdef SDSUPPORT
  392. if(card.saving)
  393. break;
  394. #endif //SDSUPPORT
  395. SERIAL_PROTOCOLLNPGM(MSG_OK);
  396. }
  397. else {
  398. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  399. LCD_MESSAGEPGM(MSG_STOPPED);
  400. }
  401. break;
  402. default:
  403. break;
  404. }
  405. }
  406. bufindw = (bufindw + 1)%BUFSIZE;
  407. buflen += 1;
  408. }
  409. serial_count = 0; //clear buffer
  410. }
  411. else
  412. {
  413. if(serial_char == ';') comment_mode = true;
  414. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  415. }
  416. }
  417. #ifdef SDSUPPORT
  418. if(!card.sdprinting || serial_count!=0){
  419. return;
  420. }
  421. while( !card.eof() && buflen < BUFSIZE) {
  422. int16_t n=card.get();
  423. serial_char = (char)n;
  424. if(serial_char == '\n' ||
  425. serial_char == '\r' ||
  426. (serial_char == ':' && comment_mode == false) ||
  427. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  428. {
  429. if(card.eof()){
  430. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  431. stoptime=millis();
  432. char time[30];
  433. unsigned long t=(stoptime-starttime)/1000;
  434. int sec,min;
  435. min=t/60;
  436. sec=t%60;
  437. sprintf(time,"%i min, %i sec",min,sec);
  438. SERIAL_ECHO_START;
  439. SERIAL_ECHOLN(time);
  440. LCD_MESSAGE(time);
  441. card.printingHasFinished();
  442. card.checkautostart(true);
  443. }
  444. if(!serial_count)
  445. {
  446. comment_mode = false; //for new command
  447. return; //if empty line
  448. }
  449. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  450. // if(!comment_mode){
  451. fromsd[bufindw] = true;
  452. buflen += 1;
  453. bufindw = (bufindw + 1)%BUFSIZE;
  454. // }
  455. comment_mode = false; //for new command
  456. serial_count = 0; //clear buffer
  457. }
  458. else
  459. {
  460. if(serial_char == ';') comment_mode = true;
  461. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  462. }
  463. }
  464. #endif //SDSUPPORT
  465. }
  466. float code_value()
  467. {
  468. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  469. }
  470. long code_value_long()
  471. {
  472. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  473. }
  474. bool code_seen(char code_string[]) //Return True if the string was found
  475. {
  476. return (strstr(cmdbuffer[bufindr], code_string) != NULL);
  477. }
  478. bool code_seen(char code)
  479. {
  480. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  481. return (strchr_pointer != NULL); //Return True if a character was found
  482. }
  483. #define HOMEAXIS(LETTER) \
  484. if ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))\
  485. { \
  486. current_position[LETTER##_AXIS] = 0; \
  487. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); \
  488. destination[LETTER##_AXIS] = 1.5 * LETTER##_MAX_LENGTH * LETTER##_HOME_DIR; \
  489. feedrate = homing_feedrate[LETTER##_AXIS]; \
  490. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  491. st_synchronize();\
  492. \
  493. current_position[LETTER##_AXIS] = 0;\
  494. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  495. destination[LETTER##_AXIS] = -LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  496. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  497. st_synchronize();\
  498. \
  499. destination[LETTER##_AXIS] = 2*LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  500. feedrate = homing_feedrate[LETTER##_AXIS]/2 ; \
  501. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  502. st_synchronize();\
  503. \
  504. current_position[LETTER##_AXIS] = (LETTER##_HOME_DIR == -1) ? LETTER##_HOME_POS : LETTER##_MAX_LENGTH;\
  505. destination[LETTER##_AXIS] = current_position[LETTER##_AXIS];\
  506. feedrate = 0.0;\
  507. endstops_hit_on_purpose();\
  508. }
  509. void process_commands()
  510. {
  511. unsigned long codenum; //throw away variable
  512. char *starpos = NULL;
  513. if(code_seen('G'))
  514. {
  515. switch((int)code_value())
  516. {
  517. case 0: // G0 -> G1
  518. case 1: // G1
  519. if(Stopped == false) {
  520. get_coordinates(); // For X Y Z E F
  521. prepare_move();
  522. //ClearToSend();
  523. return;
  524. }
  525. //break;
  526. case 2: // G2 - CW ARC
  527. if(Stopped == false) {
  528. get_arc_coordinates();
  529. prepare_arc_move(true);
  530. return;
  531. }
  532. case 3: // G3 - CCW ARC
  533. if(Stopped == false) {
  534. get_arc_coordinates();
  535. prepare_arc_move(false);
  536. return;
  537. }
  538. case 4: // G4 dwell
  539. LCD_MESSAGEPGM(MSG_DWELL);
  540. codenum = 0;
  541. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  542. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  543. st_synchronize();
  544. codenum += millis(); // keep track of when we started waiting
  545. previous_millis_cmd = millis();
  546. while(millis() < codenum ){
  547. manage_heater();
  548. manage_inactivity(1);
  549. LCD_STATUS;
  550. }
  551. break;
  552. #ifdef FWRETRACT
  553. case 10: // G10 retract
  554. if(!retracted)
  555. {
  556. destination[X_AXIS]=current_position[X_AXIS];
  557. destination[Y_AXIS]=current_position[Y_AXIS];
  558. destination[Z_AXIS]=current_position[Z_AXIS];
  559. current_position[Z_AXIS]+=-retract_zlift;
  560. destination[E_AXIS]=current_position[E_AXIS]-retract_length;
  561. feedrate=retract_feedrate;
  562. retracted=true;
  563. prepare_move();
  564. }
  565. break;
  566. case 11: // G10 retract_recover
  567. if(!retracted)
  568. {
  569. destination[X_AXIS]=current_position[X_AXIS];
  570. destination[Y_AXIS]=current_position[Y_AXIS];
  571. destination[Z_AXIS]=current_position[Z_AXIS];
  572. current_position[Z_AXIS]+=retract_zlift;
  573. current_position[E_AXIS]+=-retract_recover_length;
  574. feedrate=retract_recover_feedrate;
  575. retracted=false;
  576. prepare_move();
  577. }
  578. break;
  579. #endif //FWRETRACT
  580. case 28: //G28 Home all Axis one at a time
  581. saved_feedrate = feedrate;
  582. saved_feedmultiply = feedmultiply;
  583. feedmultiply = 100;
  584. previous_millis_cmd = millis();
  585. enable_endstops(true);
  586. for(int8_t i=0; i < NUM_AXIS; i++) {
  587. destination[i] = current_position[i];
  588. }
  589. feedrate = 0.0;
  590. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  591. #ifdef QUICK_HOME
  592. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  593. {
  594. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  595. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  596. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  597. feedrate = homing_feedrate[X_AXIS];
  598. if(homing_feedrate[Y_AXIS]<feedrate)
  599. feedrate =homing_feedrate[Y_AXIS];
  600. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  601. st_synchronize();
  602. current_position[X_AXIS] = (X_HOME_DIR == -1) ? X_HOME_POS : X_MAX_LENGTH;
  603. current_position[Y_AXIS] = (Y_HOME_DIR == -1) ? Y_HOME_POS : Y_MAX_LENGTH;
  604. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  605. destination[X_AXIS] = current_position[X_AXIS];
  606. destination[Y_AXIS] = current_position[Y_AXIS];
  607. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  608. feedrate = 0.0;
  609. st_synchronize();
  610. endstops_hit_on_purpose();
  611. }
  612. #endif
  613. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  614. {
  615. HOMEAXIS(X);
  616. }
  617. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  618. HOMEAXIS(Y);
  619. }
  620. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  621. HOMEAXIS(Z);
  622. }
  623. if(code_seen(axis_codes[X_AXIS]))
  624. {
  625. if(code_value_long() != 0) {
  626. current_position[X_AXIS]=code_value()+add_homeing[0];
  627. }
  628. }
  629. if(code_seen(axis_codes[Y_AXIS])) {
  630. if(code_value_long() != 0) {
  631. current_position[Y_AXIS]=code_value()+add_homeing[1];
  632. }
  633. }
  634. if(code_seen(axis_codes[Z_AXIS])) {
  635. if(code_value_long() != 0) {
  636. current_position[Z_AXIS]=code_value()+add_homeing[2];
  637. }
  638. }
  639. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  640. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  641. enable_endstops(false);
  642. #endif
  643. feedrate = saved_feedrate;
  644. feedmultiply = saved_feedmultiply;
  645. previous_millis_cmd = millis();
  646. endstops_hit_on_purpose();
  647. break;
  648. case 90: // G90
  649. relative_mode = false;
  650. break;
  651. case 91: // G91
  652. relative_mode = true;
  653. break;
  654. case 92: // G92
  655. if(!code_seen(axis_codes[E_AXIS]))
  656. st_synchronize();
  657. for(int8_t i=0; i < NUM_AXIS; i++) {
  658. if(code_seen(axis_codes[i])) {
  659. if(i == E_AXIS) {
  660. current_position[i] = code_value();
  661. plan_set_e_position(current_position[E_AXIS]);
  662. }
  663. else {
  664. current_position[i] = code_value()+add_homeing[i];
  665. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  666. }
  667. }
  668. }
  669. break;
  670. }
  671. }
  672. else if(code_seen('M'))
  673. {
  674. switch( (int)code_value() )
  675. {
  676. #ifdef ULTRA_LCD
  677. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  678. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  679. {
  680. LCD_MESSAGEPGM(MSG_USERWAIT);
  681. codenum = 0;
  682. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  683. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  684. st_synchronize();
  685. previous_millis_cmd = millis();
  686. if (codenum > 0)
  687. {
  688. codenum += millis(); // keep track of when we started waiting
  689. while(millis() < codenum && !CLICKED){
  690. manage_heater();
  691. manage_inactivity(1);
  692. LCD_STATUS;
  693. }
  694. }else{
  695. while(!CLICKED) {
  696. manage_heater();
  697. manage_inactivity(1);
  698. LCD_STATUS;
  699. }
  700. }
  701. }
  702. break;
  703. #endif
  704. case 17:
  705. LCD_MESSAGEPGM(MSG_NO_MOVE);
  706. enable_x();
  707. enable_y();
  708. enable_z();
  709. enable_e0();
  710. enable_e1();
  711. enable_e2();
  712. break;
  713. #ifdef SDSUPPORT
  714. case 20: // M20 - list SD card
  715. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  716. card.ls();
  717. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  718. break;
  719. case 21: // M21 - init SD card
  720. card.initsd();
  721. break;
  722. case 22: //M22 - release SD card
  723. card.release();
  724. break;
  725. case 23: //M23 - Select file
  726. starpos = (strchr(strchr_pointer + 4,'*'));
  727. if(starpos!=NULL)
  728. *(starpos-1)='\0';
  729. card.openFile(strchr_pointer + 4,true);
  730. break;
  731. case 24: //M24 - Start SD print
  732. card.startFileprint();
  733. starttime=millis();
  734. break;
  735. case 25: //M25 - Pause SD print
  736. card.pauseSDPrint();
  737. break;
  738. case 26: //M26 - Set SD index
  739. if(card.cardOK && code_seen('S')) {
  740. card.setIndex(code_value_long());
  741. }
  742. break;
  743. case 27: //M27 - Get SD status
  744. card.getStatus();
  745. break;
  746. case 28: //M28 - Start SD write
  747. starpos = (strchr(strchr_pointer + 4,'*'));
  748. if(starpos != NULL){
  749. char* npos = strchr(cmdbuffer[bufindr], 'N');
  750. strchr_pointer = strchr(npos,' ') + 1;
  751. *(starpos-1) = '\0';
  752. }
  753. card.openFile(strchr_pointer+4,false);
  754. break;
  755. case 29: //M29 - Stop SD write
  756. //processed in write to file routine above
  757. //card,saving = false;
  758. break;
  759. case 30: //M30 <filename> Delete File
  760. if (card.cardOK){
  761. card.closefile();
  762. starpos = (strchr(strchr_pointer + 4,'*'));
  763. if(starpos != NULL){
  764. char* npos = strchr(cmdbuffer[bufindr], 'N');
  765. strchr_pointer = strchr(npos,' ') + 1;
  766. *(starpos-1) = '\0';
  767. }
  768. card.removeFile(strchr_pointer + 4);
  769. }
  770. break;
  771. #endif //SDSUPPORT
  772. case 31: //M31 take time since the start of the SD print or an M109 command
  773. {
  774. stoptime=millis();
  775. char time[30];
  776. unsigned long t=(stoptime-starttime)/1000;
  777. int sec,min;
  778. min=t/60;
  779. sec=t%60;
  780. sprintf(time,"%i min, %i sec",min,sec);
  781. SERIAL_ECHO_START;
  782. SERIAL_ECHOLN(time);
  783. LCD_MESSAGE(time);
  784. autotempShutdown();
  785. }
  786. break;
  787. case 42: //M42 -Change pin status via gcode
  788. if (code_seen('S'))
  789. {
  790. int pin_status = code_value();
  791. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  792. {
  793. int pin_number = code_value();
  794. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  795. {
  796. if (sensitive_pins[i] == pin_number)
  797. {
  798. pin_number = -1;
  799. break;
  800. }
  801. }
  802. if (pin_number > -1)
  803. {
  804. pinMode(pin_number, OUTPUT);
  805. digitalWrite(pin_number, pin_status);
  806. analogWrite(pin_number, pin_status);
  807. }
  808. }
  809. }
  810. break;
  811. case 104: // M104
  812. tmp_extruder = active_extruder;
  813. if(code_seen('T')) {
  814. tmp_extruder = code_value();
  815. if(tmp_extruder >= EXTRUDERS) {
  816. SERIAL_ECHO_START;
  817. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  818. SERIAL_ECHOLN(tmp_extruder);
  819. break;
  820. }
  821. }
  822. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  823. setWatch();
  824. break;
  825. case 140: // M140 set bed temp
  826. if (code_seen('S')) setTargetBed(code_value());
  827. break;
  828. case 105 : // M105
  829. tmp_extruder = active_extruder;
  830. if(code_seen('T')) {
  831. tmp_extruder = code_value();
  832. if(tmp_extruder >= EXTRUDERS) {
  833. SERIAL_ECHO_START;
  834. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  835. SERIAL_ECHOLN(tmp_extruder);
  836. break;
  837. }
  838. }
  839. #if (TEMP_0_PIN > -1)
  840. SERIAL_PROTOCOLPGM("ok T:");
  841. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  842. SERIAL_PROTOCOLPGM(" /");
  843. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  844. #if TEMP_BED_PIN > -1
  845. SERIAL_PROTOCOLPGM(" B:");
  846. SERIAL_PROTOCOL_F(degBed(),1);
  847. SERIAL_PROTOCOLPGM(" /");
  848. SERIAL_PROTOCOL_F(degTargetBed(),1);
  849. #endif //TEMP_BED_PIN
  850. #else
  851. SERIAL_ERROR_START;
  852. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  853. #endif
  854. #ifdef PIDTEMP
  855. SERIAL_PROTOCOLPGM(" @:");
  856. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  857. #endif
  858. SERIAL_PROTOCOLLN("");
  859. return;
  860. break;
  861. case 109:
  862. {// M109 - Wait for extruder heater to reach target.
  863. tmp_extruder = active_extruder;
  864. if(code_seen('T')) {
  865. tmp_extruder = code_value();
  866. if(tmp_extruder >= EXTRUDERS) {
  867. SERIAL_ECHO_START;
  868. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  869. SERIAL_ECHOLN(tmp_extruder);
  870. break;
  871. }
  872. }
  873. LCD_MESSAGEPGM(MSG_HEATING);
  874. #ifdef AUTOTEMP
  875. autotemp_enabled=false;
  876. #endif
  877. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  878. #ifdef AUTOTEMP
  879. if (code_seen('S')) autotemp_min=code_value();
  880. if (code_seen('B')) autotemp_max=code_value();
  881. if (code_seen('F'))
  882. {
  883. autotemp_factor=code_value();
  884. autotemp_enabled=true;
  885. }
  886. #endif
  887. setWatch();
  888. codenum = millis();
  889. /* See if we are heating up or cooling down */
  890. bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  891. #ifdef TEMP_RESIDENCY_TIME
  892. long residencyStart;
  893. residencyStart = -1;
  894. /* continue to loop until we have reached the target temp
  895. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  896. while((residencyStart == -1) ||
  897. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  898. #else
  899. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  900. #endif //TEMP_RESIDENCY_TIME
  901. if( (millis() - codenum) > 1000UL )
  902. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  903. SERIAL_PROTOCOLPGM("T:");
  904. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  905. SERIAL_PROTOCOLPGM(" E:");
  906. SERIAL_PROTOCOL((int)tmp_extruder);
  907. #ifdef TEMP_RESIDENCY_TIME
  908. SERIAL_PROTOCOLPGM(" W:");
  909. if(residencyStart > -1)
  910. {
  911. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  912. SERIAL_PROTOCOLLN( codenum );
  913. }
  914. else
  915. {
  916. SERIAL_PROTOCOLLN( "?" );
  917. }
  918. #else
  919. SERIAL_PROTOCOLLN("");
  920. #endif
  921. codenum = millis();
  922. }
  923. manage_heater();
  924. manage_inactivity(1);
  925. LCD_STATUS;
  926. #ifdef TEMP_RESIDENCY_TIME
  927. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  928. or when current temp falls outside the hysteresis after target temp was reached */
  929. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  930. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  931. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  932. {
  933. residencyStart = millis();
  934. }
  935. #endif //TEMP_RESIDENCY_TIME
  936. }
  937. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  938. starttime=millis();
  939. previous_millis_cmd = millis();
  940. }
  941. break;
  942. case 190: // M190 - Wait for bed heater to reach target.
  943. #if TEMP_BED_PIN > -1
  944. LCD_MESSAGEPGM(MSG_BED_HEATING);
  945. if (code_seen('S')) setTargetBed(code_value());
  946. codenum = millis();
  947. while(isHeatingBed())
  948. {
  949. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  950. {
  951. float tt=degHotend(active_extruder);
  952. SERIAL_PROTOCOLPGM("T:");
  953. SERIAL_PROTOCOL(tt);
  954. SERIAL_PROTOCOLPGM(" E:");
  955. SERIAL_PROTOCOL((int)active_extruder);
  956. SERIAL_PROTOCOLPGM(" B:");
  957. SERIAL_PROTOCOL_F(degBed(),1);
  958. SERIAL_PROTOCOLLN("");
  959. codenum = millis();
  960. }
  961. manage_heater();
  962. manage_inactivity(1);
  963. LCD_STATUS;
  964. }
  965. LCD_MESSAGEPGM(MSG_BED_DONE);
  966. previous_millis_cmd = millis();
  967. #endif
  968. break;
  969. #if FAN_PIN > -1
  970. case 106: //M106 Fan On
  971. if (code_seen('S')){
  972. FanSpeed=constrain(code_value(),0,255);
  973. }
  974. else {
  975. FanSpeed=255;
  976. }
  977. break;
  978. case 107: //M107 Fan Off
  979. FanSpeed = 0;
  980. break;
  981. #endif //FAN_PIN
  982. #if (PS_ON_PIN > -1)
  983. case 80: // M80 - ATX Power On
  984. SET_OUTPUT(PS_ON_PIN); //GND
  985. WRITE(PS_ON_PIN, LOW);
  986. break;
  987. #endif
  988. case 81: // M81 - ATX Power Off
  989. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  990. st_synchronize();
  991. suicide();
  992. #elif (PS_ON_PIN > -1)
  993. SET_INPUT(PS_ON_PIN); //Floating
  994. #endif
  995. break;
  996. case 82:
  997. axis_relative_modes[3] = false;
  998. break;
  999. case 83:
  1000. axis_relative_modes[3] = true;
  1001. break;
  1002. case 18: //compatibility
  1003. case 84: // M84
  1004. if(code_seen('S')){
  1005. stepper_inactive_time = code_value() * 1000;
  1006. }
  1007. else
  1008. {
  1009. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  1010. if(all_axis)
  1011. {
  1012. st_synchronize();
  1013. disable_e0();
  1014. disable_e1();
  1015. disable_e2();
  1016. finishAndDisableSteppers();
  1017. }
  1018. else
  1019. {
  1020. st_synchronize();
  1021. if(code_seen('X')) disable_x();
  1022. if(code_seen('Y')) disable_y();
  1023. if(code_seen('Z')) disable_z();
  1024. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  1025. if(code_seen('E')) {
  1026. disable_e0();
  1027. disable_e1();
  1028. disable_e2();
  1029. }
  1030. #endif
  1031. LCD_MESSAGEPGM(MSG_PART_RELEASE);
  1032. }
  1033. }
  1034. break;
  1035. case 85: // M85
  1036. code_seen('S');
  1037. max_inactive_time = code_value() * 1000;
  1038. break;
  1039. case 92: // M92
  1040. for(int8_t i=0; i < NUM_AXIS; i++)
  1041. {
  1042. if(code_seen(axis_codes[i]))
  1043. if(i == 3) { // E
  1044. float value = code_value();
  1045. if(value < 20.0) {
  1046. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  1047. max_e_jerk *= factor;
  1048. max_feedrate[i] *= factor;
  1049. axis_steps_per_sqr_second[i] *= factor;
  1050. }
  1051. axis_steps_per_unit[i] = value;
  1052. }
  1053. else {
  1054. axis_steps_per_unit[i] = code_value();
  1055. }
  1056. }
  1057. break;
  1058. case 115: // M115
  1059. SerialprintPGM(MSG_M115_REPORT);
  1060. break;
  1061. case 117: // M117 display message
  1062. LCD_MESSAGE(cmdbuffer[bufindr]+5);
  1063. break;
  1064. case 114: // M114
  1065. SERIAL_PROTOCOLPGM("X:");
  1066. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1067. SERIAL_PROTOCOLPGM("Y:");
  1068. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1069. SERIAL_PROTOCOLPGM("Z:");
  1070. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1071. SERIAL_PROTOCOLPGM("E:");
  1072. SERIAL_PROTOCOL(current_position[E_AXIS]);
  1073. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1074. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  1075. SERIAL_PROTOCOLPGM("Y:");
  1076. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  1077. SERIAL_PROTOCOLPGM("Z:");
  1078. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  1079. SERIAL_PROTOCOLLN("");
  1080. break;
  1081. case 120: // M120
  1082. enable_endstops(false) ;
  1083. break;
  1084. case 121: // M121
  1085. enable_endstops(true) ;
  1086. break;
  1087. case 119: // M119
  1088. #if (X_MIN_PIN > -1)
  1089. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  1090. SERIAL_PROTOCOL(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  1091. #endif
  1092. #if (X_MAX_PIN > -1)
  1093. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  1094. SERIAL_PROTOCOL(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  1095. #endif
  1096. #if (Y_MIN_PIN > -1)
  1097. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  1098. SERIAL_PROTOCOL(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  1099. #endif
  1100. #if (Y_MAX_PIN > -1)
  1101. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  1102. SERIAL_PROTOCOL(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  1103. #endif
  1104. #if (Z_MIN_PIN > -1)
  1105. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  1106. SERIAL_PROTOCOL(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  1107. #endif
  1108. #if (Z_MAX_PIN > -1)
  1109. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  1110. SERIAL_PROTOCOL(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  1111. #endif
  1112. SERIAL_PROTOCOLLN("");
  1113. break;
  1114. //TODO: update for all axis, use for loop
  1115. case 201: // M201
  1116. for(int8_t i=0; i < NUM_AXIS; i++)
  1117. {
  1118. if(code_seen(axis_codes[i]))
  1119. {
  1120. max_acceleration_units_per_sq_second[i] = code_value();
  1121. axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1122. }
  1123. }
  1124. break;
  1125. #if 0 // Not used for Sprinter/grbl gen6
  1126. case 202: // M202
  1127. for(int8_t i=0; i < NUM_AXIS; i++) {
  1128. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1129. }
  1130. break;
  1131. #endif
  1132. case 203: // M203 max feedrate mm/sec
  1133. for(int8_t i=0; i < NUM_AXIS; i++) {
  1134. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1135. }
  1136. break;
  1137. case 204: // M204 acclereration S normal moves T filmanent only moves
  1138. {
  1139. if(code_seen('S')) acceleration = code_value() ;
  1140. if(code_seen('T')) retract_acceleration = code_value() ;
  1141. }
  1142. break;
  1143. case 205: //M205 advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  1144. {
  1145. if(code_seen('S')) minimumfeedrate = code_value();
  1146. if(code_seen('T')) mintravelfeedrate = code_value();
  1147. if(code_seen('B')) minsegmenttime = code_value() ;
  1148. if(code_seen('X')) max_xy_jerk = code_value() ;
  1149. if(code_seen('Z')) max_z_jerk = code_value() ;
  1150. if(code_seen('E')) max_e_jerk = code_value() ;
  1151. }
  1152. break;
  1153. case 206: // M206 additional homeing offset
  1154. for(int8_t i=0; i < 3; i++)
  1155. {
  1156. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1157. }
  1158. break;
  1159. #ifdef FWRETRACT
  1160. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  1161. {
  1162. if(code_seen('S'))
  1163. {
  1164. retract_length = code_value() ;
  1165. }
  1166. if(code_seen('F'))
  1167. {
  1168. retract_feedrate = code_value() ;
  1169. }
  1170. if(code_seen('Z'))
  1171. {
  1172. retract_zlift = code_value() ;
  1173. }
  1174. }break;
  1175. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  1176. {
  1177. if(code_seen('S'))
  1178. {
  1179. retract_recover_length = code_value() ;
  1180. }
  1181. if(code_seen('F'))
  1182. {
  1183. retract_recover_feedrate = code_value() ;
  1184. }
  1185. }break;
  1186. case 209: // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  1187. {
  1188. if(code_seen('S'))
  1189. {
  1190. int t= code_value() ;
  1191. switch(t)
  1192. {
  1193. case 0: autoretract_enabled=false;retracted=false;break;
  1194. case 1: autoretract_enabled=true;retracted=false;break;
  1195. default:
  1196. SERIAL_ECHO_START;
  1197. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1198. SERIAL_ECHO(cmdbuffer[bufindr]);
  1199. SERIAL_ECHOLNPGM("\"");
  1200. }
  1201. }
  1202. }break;
  1203. #endif
  1204. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1205. {
  1206. if(code_seen('S'))
  1207. {
  1208. feedmultiply = code_value() ;
  1209. feedmultiplychanged=true;
  1210. }
  1211. }
  1212. break;
  1213. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1214. {
  1215. if(code_seen('S'))
  1216. {
  1217. extrudemultiply = code_value() ;
  1218. }
  1219. }
  1220. break;
  1221. #ifdef PIDTEMP
  1222. case 301: // M301
  1223. {
  1224. if(code_seen('P')) Kp = code_value();
  1225. if(code_seen('I')) Ki = code_value()*PID_dT;
  1226. if(code_seen('D')) Kd = code_value()/PID_dT;
  1227. #ifdef PID_ADD_EXTRUSION_RATE
  1228. if(code_seen('C')) Kc = code_value();
  1229. #endif
  1230. updatePID();
  1231. SERIAL_PROTOCOL(MSG_OK);
  1232. SERIAL_PROTOCOL(" p:");
  1233. SERIAL_PROTOCOL(Kp);
  1234. SERIAL_PROTOCOL(" i:");
  1235. SERIAL_PROTOCOL(Ki/PID_dT);
  1236. SERIAL_PROTOCOL(" d:");
  1237. SERIAL_PROTOCOL(Kd*PID_dT);
  1238. #ifdef PID_ADD_EXTRUSION_RATE
  1239. SERIAL_PROTOCOL(" c:");
  1240. SERIAL_PROTOCOL(Kc*PID_dT);
  1241. #endif
  1242. SERIAL_PROTOCOLLN("");
  1243. }
  1244. break;
  1245. #endif //PIDTEMP
  1246. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1247. {
  1248. #ifdef PHOTOGRAPH_PIN
  1249. #if (PHOTOGRAPH_PIN > -1)
  1250. const uint8_t NUM_PULSES=16;
  1251. const float PULSE_LENGTH=0.01524;
  1252. for(int i=0; i < NUM_PULSES; i++) {
  1253. WRITE(PHOTOGRAPH_PIN, HIGH);
  1254. _delay_ms(PULSE_LENGTH);
  1255. WRITE(PHOTOGRAPH_PIN, LOW);
  1256. _delay_ms(PULSE_LENGTH);
  1257. }
  1258. delay(7.33);
  1259. for(int i=0; i < NUM_PULSES; i++) {
  1260. WRITE(PHOTOGRAPH_PIN, HIGH);
  1261. _delay_ms(PULSE_LENGTH);
  1262. WRITE(PHOTOGRAPH_PIN, LOW);
  1263. _delay_ms(PULSE_LENGTH);
  1264. }
  1265. #endif
  1266. #endif
  1267. }
  1268. break;
  1269. case 302: // allow cold extrudes
  1270. {
  1271. allow_cold_extrudes(true);
  1272. }
  1273. break;
  1274. case 303: // M303 PID autotune
  1275. {
  1276. float temp = 150.0;
  1277. if (code_seen('S')) temp=code_value();
  1278. PID_autotune(temp);
  1279. }
  1280. break;
  1281. case 400: // M400 finish all moves
  1282. {
  1283. st_synchronize();
  1284. }
  1285. break;
  1286. case 500: // Store settings in EEPROM
  1287. {
  1288. EEPROM_StoreSettings();
  1289. }
  1290. break;
  1291. case 501: // Read settings from EEPROM
  1292. {
  1293. EEPROM_RetrieveSettings();
  1294. }
  1295. break;
  1296. case 502: // Revert to default settings
  1297. {
  1298. EEPROM_RetrieveSettings(true);
  1299. }
  1300. break;
  1301. case 503: // print settings currently in memory
  1302. {
  1303. EEPROM_printSettings();
  1304. }
  1305. break;
  1306. case 999: // Restart after being stopped
  1307. Stopped = false;
  1308. gcode_LastN = Stopped_gcode_LastN;
  1309. FlushSerialRequestResend();
  1310. break;
  1311. }
  1312. }
  1313. else if(code_seen('T'))
  1314. {
  1315. tmp_extruder = code_value();
  1316. if(tmp_extruder >= EXTRUDERS) {
  1317. SERIAL_ECHO_START;
  1318. SERIAL_ECHO("T");
  1319. SERIAL_ECHO(tmp_extruder);
  1320. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  1321. }
  1322. else {
  1323. active_extruder = tmp_extruder;
  1324. SERIAL_ECHO_START;
  1325. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  1326. SERIAL_PROTOCOLLN((int)active_extruder);
  1327. }
  1328. }
  1329. else
  1330. {
  1331. SERIAL_ECHO_START;
  1332. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1333. SERIAL_ECHO(cmdbuffer[bufindr]);
  1334. SERIAL_ECHOLNPGM("\"");
  1335. }
  1336. ClearToSend();
  1337. }
  1338. void FlushSerialRequestResend()
  1339. {
  1340. //char cmdbuffer[bufindr][100]="Resend:";
  1341. MYSERIAL.flush();
  1342. SERIAL_PROTOCOLPGM(MSG_RESEND);
  1343. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1344. ClearToSend();
  1345. }
  1346. void ClearToSend()
  1347. {
  1348. previous_millis_cmd = millis();
  1349. #ifdef SDSUPPORT
  1350. if(fromsd[bufindr])
  1351. return;
  1352. #endif //SDSUPPORT
  1353. SERIAL_PROTOCOLLNPGM(MSG_OK);
  1354. }
  1355. void get_coordinates()
  1356. {
  1357. bool seen[4]={false,false,false,false};
  1358. for(int8_t i=0; i < NUM_AXIS; i++) {
  1359. if(code_seen(axis_codes[i]))
  1360. {
  1361. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1362. if(active_extruder == 1){
  1363. if (i==0){
  1364. destination[i] += EXTR2_X_OFFSET;
  1365. }
  1366. else if (i==1){
  1367. destination[i] += EXTR2_Y_OFFSET;
  1368. }
  1369. }
  1370. seen[i]=true;
  1371. }
  1372. else destination[i] = current_position[i]; //Are these else lines really needed?
  1373. }
  1374. if(code_seen('F')) {
  1375. next_feedrate = code_value();
  1376. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1377. }
  1378. #ifdef FWRETRACT
  1379. if(autoretract_enabled)
  1380. if( !(seen[X_AXIS] || seen[Y_AXIS] || seen[Z_AXIS]) && seen[E_AXIS])
  1381. {
  1382. float echange=destination[E_AXIS]-current_position[E_AXIS];
  1383. if(echange<-MIN_RETRACT) //retract
  1384. {
  1385. if(!retracted)
  1386. {
  1387. destination[Z_AXIS]+=retract_zlift; //not sure why chaninging current_position negatively does not work.
  1388. //if slicer retracted by echange=-1mm and you want to retract 3mm, corrrectede=-2mm additionally
  1389. float correctede=-echange-retract_length;
  1390. //to generate the additional steps, not the destination is changed, but inversely the current position
  1391. current_position[E_AXIS]+=-correctede;
  1392. feedrate=retract_feedrate;
  1393. retracted=true;
  1394. }
  1395. }
  1396. else
  1397. if(echange>MIN_RETRACT) //retract_recover
  1398. {
  1399. if(retracted)
  1400. {
  1401. //current_position[Z_AXIS]+=-retract_zlift;
  1402. //if slicer retracted_recovered by echange=+1mm and you want to retract_recover 3mm, corrrectede=2mm additionally
  1403. float correctede=-echange+1*retract_length+retract_recover_length; //total unretract=retract_length+retract_recover_length[surplus]
  1404. current_position[E_AXIS]+=correctede; //to generate the additional steps, not the destination is changed, but inversely the current position
  1405. feedrate=retract_recover_feedrate;
  1406. retracted=false;
  1407. }
  1408. }
  1409. }
  1410. #endif //FWRETRACT
  1411. }
  1412. void get_arc_coordinates()
  1413. {
  1414. get_coordinates();
  1415. if(code_seen('I')) {
  1416. offset[0] = code_value();
  1417. }
  1418. else {
  1419. offset[0] = 0.0;
  1420. }
  1421. if(code_seen('J')) {
  1422. offset[1] = code_value();
  1423. }
  1424. else {
  1425. offset[1] = 0.0;
  1426. }
  1427. }
  1428. void prepare_move()
  1429. {
  1430. if (min_software_endstops) {
  1431. if (destination[X_AXIS] < X_HOME_POS) destination[X_AXIS] = X_HOME_POS;
  1432. if (destination[Y_AXIS] < Y_HOME_POS) destination[Y_AXIS] = Y_HOME_POS;
  1433. if (destination[Z_AXIS] < Z_HOME_POS) destination[Z_AXIS] = Z_HOME_POS;
  1434. }
  1435. if (max_software_endstops) {
  1436. if (destination[X_AXIS] > X_MAX_LENGTH) destination[X_AXIS] = X_MAX_LENGTH;
  1437. if (destination[Y_AXIS] > Y_MAX_LENGTH) destination[Y_AXIS] = Y_MAX_LENGTH;
  1438. if (destination[Z_AXIS] > Z_MAX_LENGTH) destination[Z_AXIS] = Z_MAX_LENGTH;
  1439. }
  1440. previous_millis_cmd = millis();
  1441. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1442. for(int8_t i=0; i < NUM_AXIS; i++) {
  1443. current_position[i] = destination[i];
  1444. }
  1445. }
  1446. void prepare_arc_move(char isclockwise) {
  1447. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1448. // Trace the arc
  1449. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1450. // As far as the parser is concerned, the position is now == target. In reality the
  1451. // motion control system might still be processing the action and the real tool position
  1452. // in any intermediate location.
  1453. for(int8_t i=0; i < NUM_AXIS; i++) {
  1454. current_position[i] = destination[i];
  1455. }
  1456. previous_millis_cmd = millis();
  1457. }
  1458. #ifdef CONTROLLERFAN_PIN
  1459. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  1460. unsigned long lastMotorCheck = 0;
  1461. void controllerFan()
  1462. {
  1463. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  1464. {
  1465. lastMotorCheck = millis();
  1466. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  1467. #if EXTRUDERS > 2
  1468. || !READ(E2_ENABLE_PIN)
  1469. #endif
  1470. #if EXTRUDER > 1
  1471. || !READ(E2_ENABLE_PIN)
  1472. #endif
  1473. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  1474. {
  1475. lastMotor = millis(); //... set time to NOW so the fan will turn on
  1476. }
  1477. if ((millis() - lastMotor) >= (CONTROLLERFAN_SEC*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  1478. {
  1479. WRITE(CONTROLLERFAN_PIN, LOW); //... turn the fan off
  1480. }
  1481. else
  1482. {
  1483. WRITE(CONTROLLERFAN_PIN, HIGH); //... turn the fan on
  1484. }
  1485. }
  1486. }
  1487. #endif
  1488. void manage_inactivity(byte debug)
  1489. {
  1490. if( (millis() - previous_millis_cmd) > max_inactive_time )
  1491. if(max_inactive_time)
  1492. kill();
  1493. if(stepper_inactive_time) {
  1494. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  1495. {
  1496. if(blocks_queued() == false) {
  1497. disable_x();
  1498. disable_y();
  1499. disable_z();
  1500. disable_e0();
  1501. disable_e1();
  1502. disable_e2();
  1503. }
  1504. }
  1505. }
  1506. #ifdef CONTROLLERFAN_PIN
  1507. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  1508. #endif
  1509. #ifdef EXTRUDER_RUNOUT_PREVENT
  1510. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1511. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1512. {
  1513. bool oldstatus=READ(E0_ENABLE_PIN);
  1514. enable_e0();
  1515. float oldepos=current_position[E_AXIS];
  1516. float oldedes=destination[E_AXIS];
  1517. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1518. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1519. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1520. current_position[E_AXIS]=oldepos;
  1521. destination[E_AXIS]=oldedes;
  1522. plan_set_e_position(oldepos);
  1523. previous_millis_cmd=millis();
  1524. st_synchronize();
  1525. WRITE(E0_ENABLE_PIN,oldstatus);
  1526. }
  1527. #endif
  1528. check_axes_activity();
  1529. }
  1530. void kill()
  1531. {
  1532. cli(); // Stop interrupts
  1533. disable_heater();
  1534. disable_x();
  1535. disable_y();
  1536. disable_z();
  1537. disable_e0();
  1538. disable_e1();
  1539. disable_e2();
  1540. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1541. SERIAL_ERROR_START;
  1542. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  1543. LCD_MESSAGEPGM(MSG_KILLED);
  1544. suicide();
  1545. while(1); // Wait for reset
  1546. }
  1547. void Stop()
  1548. {
  1549. disable_heater();
  1550. if(Stopped == false) {
  1551. Stopped = true;
  1552. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  1553. SERIAL_ERROR_START;
  1554. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  1555. LCD_MESSAGEPGM(MSG_STOPPED);
  1556. }
  1557. }
  1558. bool IsStopped() { return Stopped; };
  1559. #ifdef FAST_PWM_FAN
  1560. void setPwmFrequency(uint8_t pin, int val)
  1561. {
  1562. val &= 0x07;
  1563. switch(digitalPinToTimer(pin))
  1564. {
  1565. #if defined(TCCR0A)
  1566. case TIMER0A:
  1567. case TIMER0B:
  1568. // TCCR0B &= ~(CS00 | CS01 | CS02);
  1569. // TCCR0B |= val;
  1570. break;
  1571. #endif
  1572. #if defined(TCCR1A)
  1573. case TIMER1A:
  1574. case TIMER1B:
  1575. // TCCR1B &= ~(CS10 | CS11 | CS12);
  1576. // TCCR1B |= val;
  1577. break;
  1578. #endif
  1579. #if defined(TCCR2)
  1580. case TIMER2:
  1581. case TIMER2:
  1582. TCCR2 &= ~(CS10 | CS11 | CS12);
  1583. TCCR2 |= val;
  1584. break;
  1585. #endif
  1586. #if defined(TCCR2A)
  1587. case TIMER2A:
  1588. case TIMER2B:
  1589. TCCR2B &= ~(CS20 | CS21 | CS22);
  1590. TCCR2B |= val;
  1591. break;
  1592. #endif
  1593. #if defined(TCCR3A)
  1594. case TIMER3A:
  1595. case TIMER3B:
  1596. case TIMER3C:
  1597. TCCR3B &= ~(CS30 | CS31 | CS32);
  1598. TCCR3B |= val;
  1599. break;
  1600. #endif
  1601. #if defined(TCCR4A)
  1602. case TIMER4A:
  1603. case TIMER4B:
  1604. case TIMER4C:
  1605. TCCR4B &= ~(CS40 | CS41 | CS42);
  1606. TCCR4B |= val;
  1607. break;
  1608. #endif
  1609. #if defined(TCCR5A)
  1610. case TIMER5A:
  1611. case TIMER5B:
  1612. case TIMER5C:
  1613. TCCR5B &= ~(CS50 | CS51 | CS52);
  1614. TCCR5B |= val;
  1615. break;
  1616. #endif
  1617. }
  1618. }
  1619. #endif